Interface: Collection

View as Markdown

Defined in: query.ts:39

One collection's documents. Everything is built ahead of time, so reads are synchronous.

Example

import { posts } from "tomekit/content";

posts.documents().filter((post) => post.metadata.tags.includes("vite"));
posts.get("hello-world").metadata.title;
posts.slugs(); // ["hello-world", "setup"]

Methods

documents()

documents(this: void): readonly TDocument[];

Defined in: query.ts:45

Every document, in the order its loader returned them, eg file name order for directory. Sort, filter and slice it like any array.


get()

get(this: void, slug: TKnownSlug): TDocument;

Defined in: query.ts:58

The document with this slug. A slug that exists returns its document.

posts.get("hello-world").metadata.title;
get<TKey extends string>(this: void, slug: LookupKey<TKey, TKnownSlug>): TDocument | undefined;

Defined in: query.ts:69

The document with this slug, or undefined if there is none, eg for a route param.

const post = posts.get(params.slug);
if (!post) throw notFound();

has()

has(this: void, slug: string): slug is TSlug;

Defined in: query.ts:83

Whether a document has this slug. Narrows a route param to the collection's slugs, so get then returns the document.

if (posts.has(params.slug)) posts.get(params.slug).metadata.title;

slugs()

slugs(this: void): readonly TSlug[];

Defined in: query.ts:85

Every slug, in the same order as documents().