---
title: "defineCollection"
description: "Define a flat directory of entries that share one schema."
canonical: "https://www.qino.works/docs/api/collection"
---

# defineCollection

> Define a flat directory of entries that share one schema.

Defines a collection: a flat directory of files with one extension and one schema. Returns getters for listing, reading, and enumerating slugs.

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

import qino from "../";
import { authorCollection } from "./authors";

export const postCollection = qino.defineCollection({
  directory: "posts",
  extension: ".md",
  schema: z.object({
    title: z.string(),
    author: z.string(),
    draft: z.boolean(),
  }),
  relations: { author: authorCollection },
  views: (view) => ({
    default: view({ filter: (post) => !post.draft }),
    detail: view({ resolveDepth: { author: 2 } }),
  }),
});
```

## Signature

```ts title="@qino/cms"
qino.defineCollection(params: {
  directory: `/${string}`;
  extension: ".md" | ".mdx" | ".markdown" | ".json";
  schema: StandardSchemaV1<unknown, Record<string, unknown>>;
  relations?: Record<RelationPath, Primitive | (() => Primitive)>;
  media?: Record<StringPath, MediaRule>;
  markdown?: {
    stats?: boolean;
    toc?: boolean | { upTo?: "h1" | "h2" | "h3" | "h4" | "h5" | "h6"; slug?: (text: string) => string };
    media?: MediaRule | Array<MediaRule>;
  };
  views?: (view: ViewHelper) => Record<string, View> & { default: View };
  pages?: { index: `/${string}`; entry: `/${string}{slug}${string}`; title: string; llmsFull?: boolean; markdownComponents?: Record<string, PageMarkdownComponent<Entry>> };
  cloud?: { displayAs?: string; displayEntryAs?: `${string}{${RequiredField}}${string}`; saveEntryAs?: `${string}{${RequiredField}}${string}`; searchableBy?: Array<Field>; sortableBy?: Array<ScalarField>; hiddenFields?: Array<OptionalField>; media?: { saveFileAs?: string } };
}): Collection;

type MediaRule = { accepted: MimeType; maxSize: Size; parse?: (value: string, ctx: MediaParseContext) => string | undefined };
```

## Parameters

| Name        | Type                                                                                               | Required | Default | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
| ----------- | -------------------------------------------------------------------------------------------------- | -------- | ------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `directory` | `` `/${string}` ``                                                                                 | yes      |         | Directory under `contentFolder`. Must start with `/`. Flat: nested files are ignored                                                                                                                                                                                                                                                                                                                                                                                                |
| `extension` | `".md" \| ".mdx" \| ".markdown" \| ".json"`                                                        | yes      |         | Only files with this extension are entries                                                                                                                                                                                                                                                                                                                                                                                                                                          |
| `schema`    | Standard Schema object validator                                                                   | yes      |         | Synchronous. Receives frontmatter, or the JSON object. May not declare `_meta`, nor `markdown`/`_stats`/`_toc` for Markdown                                                                                                                                                                                                                                                                                                                                                         |
| `relations` | `Record<path, primitive \| () => primitive>`                                                       | no       | `{}`    | JSON paths into the schema output mapped to target primitives. `[*]` marks an array hop                                                                                                                                                                                                                                                                                                                                                                                             |
| `media`     | `Record<path, MediaRule>`                                                                          | no       | `{}`    | Media fields, keyed by path to a string field, with the one MIME type each accepts and its max size. Image fields are read as `QinoImage`, and `parse` reads a value first. See [Media](https://www.qino.works/docs/concepts/media)                                                                                                                                                                                                                                                                       |
| `markdown`  | `{ stats?; toc?; media? }`                                                                         | no       | `{}`    | Configures the body. Markdown and MDX only. `stats` adds `_stats`, its word and character counts. See [MarkdownStats](https://www.qino.works/docs/api/types/markdown-stats). `toc` adds `_toc`, its headings with their anchor ids: `true` lists every level, `upTo` stops at one, and `slug` sets how ids are made. See [MarkdownToc](https://www.qino.works/docs/api/types/markdown-toc). `media` lists the rules body images must match, each read with its own `parse`. See [Body images](https://www.qino.works/docs/concepts/media#body-images) |
| `views`     | `(view) => { default: View; [name]: View }`                                                        | no       |         | Factory run once at definition. Must return a `default` view made with the helper                                                                                                                                                                                                                                                                                                                                                                                                   |
| `pages`     | `{ index; entry; title; llmsFull?; markdownComponents? }`                                          | no       |         | Where the index page and entry pages live, for page Markdown, Markdown URLs and `llms.txt`, and how MDX components read in the pages' Markdown. Requires `siteUrl` on `initQino`. See [Pages](#pages)                                                                                                                                                                                                                                                                               |
| `cloud`     | `{ displayAs?; displayEntryAs?; saveEntryAs?; searchableBy?; sortableBy?; hiddenFields?; media? }` | no       |         | Cloud UI names, entry labels and naming, search, sort, hidden fields, and where uploads are saved. Requires `cloud` on `initQino`. See [Cloud](https://www.qino.works/docs/guide/cloud#primitives)                                                                                                                                                                                                                                                                                                        |

`resolveDepth`, `augment`, `filter`, and `sort` are rejected at the root. Put them inside a view. See [view](https://www.qino.works/docs/api/view).

## Pages

`pages` tells qino where the collection is shown, so it can link to it. qino never routes to these pages: your framework does.

```ts title="qino/collections/posts.ts"
pages: { index: "/blog", entry: "/blog/{slug}", title: "Blog" },
```

| Field                | Type                                    | Required | Default | Description                                                                                                                                                                        |
| -------------------- | --------------------------------------- | -------- | ------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `index`              | `` `/${string}` ``                      | yes      |         | The index page's path. Takes no token                                                                                                                                              |
| `entry`              | `` `/${string}{slug}${string}` ``       | yes      |         | The entry page's path, with `{slug}` once where the slug goes                                                                                                                      |
| `title`              | `string`                                | yes      |         | The index page's title, such as `"Blog"`. Heads its page Markdown and the `llms.txt` section                                                                                       |
| `llmsFull`           | `boolean`                               | no       | `true`  | Whether `llms-full.txt` holds the entry pages. They stay in `llms.txt` either way                                                                                                  |
| `markdownComponents` | `Record<string, PageMarkdownComponent>` | no       |         | Functions, by component name, whose output replaces an MDX component in page Markdown. Markdown and MDX only. See [PageMarkdownComponent](https://www.qino.works/docs/api/types/page-markdown-component) |

Both paths start with `/`, stay on the site, and hold no `?`, `#`, `\`, tab, line break, or `.` or `..` segment. A trailing `/` is allowed. A bad path or a blank title throws when the collection is defined.

## Returns

A `Collection` with six getters.

### Getters

- [`getEntries(options?)`](https://www.qino.works/docs/api/collection/get-entries)
- [`getPaginatedEntries(page, options)`](https://www.qino.works/docs/api/collection/get-paginated-entries)
- [`getEntry(slug, options?)`](https://www.qino.works/docs/api/collection/get-entry)
- [`getAllSlugs()`](https://www.qino.works/docs/api/collection/get-all-slugs)
- [`getPageMarkdown(slug?, options?)`](https://www.qino.works/docs/api/collection/get-page-markdown)
- [`getMarkdownUrl(slug?)`](https://www.qino.works/docs/api/collection/get-markdown-url)

Use `Infer<typeof postCollection>` to name the entry types. See [Infer](https://www.qino.works/docs/api/types/infer).

## Throws

At definition time:

- `Configure "<key>" inside views.default or a custom view, not at the root.`
- `Configure views with views: (view) => ({ default: view({ ... }) }). Object-form views are no longer supported.`
- `The views factory must synchronously return an object of named views.`
- `The views factory must return a "default" view created with view({ ... }).`
- `View "<name>" must be created with view({ ... }).`
- ``"<id>" sets `cloud`, but initQino doesn't. Add `cloud` to initQino.``
- `Collection "<directory>" declares pages, so initQino needs a siteUrl, such as "https://example.com".`
- `Collection "<directory>": pages.<index | entry> "<path>" <what's wrong>.`, such as `must start with "/".` See [Errors](https://www.qino.works/docs/api/errors#pages).
- `Collection "<directory>": pages.title must name the index page, such as "Blog".`

## See also

- [Collections](https://www.qino.works/docs/concepts/collections)
- [Agent-ready content](https://www.qino.works/docs/guide/agent-ready-content)
- [Content folder](https://www.qino.works/docs/guide/content-folder)
