Skip to content
qino

Since 0.1.0

defineCollection

Define a flat directory of entries that share one schema.

View Markdown
On this page

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

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

@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

resolveDepth, augment, filter, and sort are rejected at the root. Put them inside a view. See 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.

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

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

Use Infer<typeof postCollection> to name the entry types. See 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.
  • Collection "<directory>": pages.title must name the index page, such as "Blog".

See also