Since 0.1.0
view
The helper passed to the views factory, and every option it accepts.
view is not imported. It is the argument the views factory receives, and every named view must be created with it.
views: (view) => ({
default: view({}),
detail: view({
resolveDepth: { author: 2 },
augment: (entry) => ({ companyName: entry.author.company.name }),
}),
}),
Signature
view(config: {
resolveDepth?: { [relationPath]?: 0 | 1 | 2 | 3 | 4 | 5 | 6 };
augment?: (entry: Entry) => Derived | Promise<Derived>;
filter?: (entry: Entry) => boolean; // collections only
sort?: (a: Entry, b: Entry) => number; // collections only
}): View;
Parameters
The helper for trees and items omits filter and sort from autocomplete and rejects them at runtime.
Resolving
resolveDepth keys are the primitive's relation paths, as declared in relations, such as author or "categories[*]". A depth set in the view applies to the whole branch: { author: 2 } also resolves the author's own relations, even those the author's primitive declares with resolveDepth: 0. 0 keeps the relation a string. See Relations.
Returns
A view definition. The factory must return an object with a default key and any number of named views, each created by the helper.
Rules
- The factory runs once at definition time and must return synchronously.
- Views are independent. Nothing is inherited from
default. - Reuse by spreading:
view({ ...base, filter: (entry) => entry.featured }). Spread replaces callbacks, it does not compose them. Clear a copied callback withfilter: undefined. augmentruns after relation resolution. Entries a relation resolves to never include their own augment fields.- Root-level
resolveDepth,augment,filter, orsorton the primitive are rejected.
Throws
view() requires a configuration object.view() no longer takes resolveRelations. Set depths by relation with resolveDepth, such as { author: 2 }.View "<name>": resolveDepth sets "<path>", which isn't a relation. Relations: <paths>.View "<name>": resolveDepth of "<path>" must be a whole number from 0 to 6, got <value>.tree views do not support filter or sort.anditem views do not support filter or sort.- At augment time:
<filePath>: augment failed: <message>,<filePath>: augment must return an object., and<filePath>: augment cannot add reserved or overwrite existing fields: <keys>.
Example
// 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 }),
plain: view({}),
};
},