Views
Named shapes of the same content, with resolution, augmentation, filtering, and sorting.
A view is a named configuration for how a getter shapes entries. Every primitive can declare views, and every getter that reads content accepts { view: "name" }.
export const postCollection = qino.defineCollection({
directory: "posts",
extension: ".md",
schema: PostSchema,
relations: { author: authorCollection },
views: (view) => ({
default: view({}),
detail: view({
resolveDepth: { author: 2 },
augment: (post) => ({ companyName: post.author.company.name }),
}),
}),
});
const listing = await postCollection.getEntries(); // default view
const post = await postCollection.getEntry("hello", { view: "detail" });
The factory and the helper
views is a function that receives a view helper and synchronously returns an object of named views. Every value must be created with view({ ... }), and the object must contain default. The factory runs once when the primitive is defined. Object-form views and unwrapped definitions are rejected.
Without views, getters return validated entries with each relation resolved to its own resolveDepth, and nothing else.
Options
Tree and item helpers reject filter and sort in TypeScript and at runtime.
Pipeline
For each getter call: read and validate, resolve relations to the view's depths, run augment, then for collections filter, then for getEntries sort. Augment receives resolved relations when the view resolves them. Filter and sort receive augmented entries. See Entry pipeline for every step, from parsing to sorting.
augment must return an object. It cannot overwrite an existing field, and cannot add markdown to a Markdown entry.
Views are independent
A custom view inherits nothing from default. Reuse a base by spreading it:
// With markdown: { stats: true } on the collection.
views: (view) => {
const base = view({
augment: (post) => ({ readingMinutes: post._stats.wordCount / 220 }),
sort: (a, b) => b.readingMinutes - a.readingMinutes,
});
return {
default: base,
highlight: view({ ...base, filter: (post) => post.highlight }),
};
},
Spreading copies settings; it does not compose callbacks. Set filter: undefined or sort: undefined to clear a copied callback.
Selecting
- Omit
view, or pass{ view: "default" }, to use the default. getPaginatedEntries(page, { view, perPage })takesviewthe same way, next to its page size.- Unknown names fail in TypeScript and at runtime.
- Passing
resolveDepth,filter, orsortto a getter throws. Configure them in a view. - An explicit
undefinedin getter options is treated as omitted.
Inferring the output
import type { Infer } from "@qino/cms";
type PostTypes = Infer<typeof postCollection>;
type Post = PostTypes["output"]; // default view
type DetailPost = PostTypes["views"]["detail"];
See Types.