---
title: "Collections"
description: "A flat folder of similarly shaped entries with one schema."
canonical: "https://www.qino.works/docs/concepts/collections"
---

# Collections

> A flat folder of similarly shaped entries with one schema.

A collection is a flat directory of files that share one schema, such as blog posts or authors. It is the primitive for "many of the same thing".

```ts title="qino/collections/posts.ts"
import { z } from "zod";

import qino from "../";

export const postCollection = qino.defineCollection({
  directory: "posts",
  extension: ".md",
  schema: z.object({
    title: z.string(),
    draft: z.boolean(),
  }),
});
```

## Entries and slugs

Every file in `directory` with the configured `extension` is an entry. The slug is the filename minus that extension, so `posts/hello-world.md` has the slug `hello-world`. Subfolders, hidden files, and other extensions are ignored. See [Content folder](https://www.qino.works/docs/guide/content-folder).

Each entry is the validated schema output plus `_meta`:

```ts title="src/app/posts/[slug]/page.tsx"
const post = await postCollection.getEntry("hello-world");

post._meta.slug; // "hello-world"
post._meta.fileName; // "hello-world.md"
post._meta.filePath; // absolute path
```

## Getters

| Getter                               | Reads content                                                 | Applies view | Returns                   |
| ------------------------------------ | ------------------------------------------------------------- | ------------ | ------------------------- |
| `getEntries(options?)`               | all files                                                     | yes          | `Array<Entry>`            |
| `getPaginatedEntries(page, options)` | the page's files, or all files when the view filters or sorts | yes          | `PaginatedEntries<Entry>` |
| `getEntry(slug, options?)`           | one file                                                      | yes          | `Entry`                   |
| `getAllSlugs()`                      | no                                                            | no           | `Array<string>` sorted    |

`getEntries` returns entries in slug order unless the selected view sorts them. `getPaginatedEntries` returns one page of that order, with its totals. See [getPaginatedEntries](https://www.qino.works/docs/api/collection/get-paginated-entries). `getAllSlugs` is the cheap way to build routes, since it never parses or validates anything.

Each collection keeps its parsed files between calls and re-reads a file only once it changes. Entries can share nested objects between calls, so treat them as read-only. See [Entry pipeline](https://www.qino.works/docs/concepts/pipeline) for what counts as a change.

## Filtering and sorting

Collections are the only primitive with `filter` and `sort`. Both live inside a view, never on the getter call:

```ts title="qino/collections/posts.ts"
views: (view) => ({
  default: view({
    filter: (post) => !post.draft,
    sort: (a, b) => a.title.localeCompare(b.title),
  }),
  all: view({}),
}),
```

Filtering runs after relation resolution and augmentation. Sorting runs after filtering, for `getEntries` and `getPaginatedEntries` only. `getEntry` throws when the selected view's filter excludes the entry. See [Views](https://www.qino.works/docs/concepts/views).

Because `getAllSlugs` ignores views, it can list slugs that a filtering default view excludes. When the default view filters, derive route slugs from the viewed set instead:

```ts title="src/app/posts/[slug]/page.tsx"
const slugs = (await postCollection.getEntries()).map(
  (post) => post._meta.slug,
);
```

## When to reach for something else

- One well-known file, such as a home page: use an [item](https://www.qino.works/docs/concepts/items).
- Nested, ordered content, such as docs: use a [tree](https://www.qino.works/docs/concepts/trees).
