Relations
Typed references between entries and how they resolve.
A relation links a field in one primitive to an entry of another. Any of the three primitives can point at any of the three. Relations are directional; reverse links are not created.
Declaring
relations maps a JSON path in the schema output to a target primitive:
export const postCollection = qino.defineCollection({
directory: "posts",
extension: ".md",
schema: z.object({
title: z.string(),
author: z.string(),
categories: z.array(z.string()),
}),
relations: {
author: authorCollection,
"categories[*]": categoryCollection,
},
});
Path grammar:
authortargets a top-level string field.categories[*]targets every element of an array.meta.editorandsections[*].authorreach into nested objects and arrays.
Only paths that lead to a string in the schema output are accepted. TypeScript narrows the keys accordingly.
For forward references and cycles, pass a function:
relations: { related: () => postCollection },
To set options, pass an object with the target under target:
relations: {
author: { target: authorCollection, resolveDepth: 2 },
related: { target: () => postCollection, resolveDepth: 0 },
},
All related primitives must come from the same initQino instance.
Authoring
Content stores the target's path relative to the content folder, with the extension:
author: "authors/camille-laurent.json"
categories:
- "categories/content-modeling.json"
The directory and extension must match the target. Item targets must equal the item's file. Bare slugs and leading slashes are rejected with a message that names the expected value. Root paths that go through the content folder, such as src/content/authors/camille-laurent.json, are accepted too. See Content paths.
Custom formats
When content can't store content paths, such as an Obsidian vault linking notes with "[[Jane Doe]]", parse turns each value into one. qino then checks and reads the path it returns as usual:
relations: {
author: {
target: authorCollection,
parse: (value, { target }) => {
const name = /^\[\[(.+)\]\]$/.exec(value)?.[1];
if (!name || target.is == "item") return undefined;
return `${target.directory}/${name}${target.extension}`; // "authors/Jane Doe.md"
},
},
},
Return undefined to read the value as a content path as is, so values that already follow the convention, like those the cloud UI writes, keep working.
ctx, the second argument, holds sourceFilePath, the file the value is written in, fieldPath, the relation's path as declared, and target, where the target's files live. Take a vault set up with initQino({ contentFolder: "vault", mediaFolder: "vault/attachments" }) and this post:
---
author: "[[Jane Doe]]"
categories: ["[[Design]]", "[[Tooling]]"]
site: "[[Settings]]"
---
Reading author takes four steps:
-
qino reads
vault/posts/Hello World.mdand findsauthor: "[[Jane Doe]]". -
It calls
parse("[[Jane Doe]]", ctx), withctx:{ sourceFilePath: "vault/posts/Hello World.md", // where the link is written fieldPath: "author", // the field that holds it target: { is: "collection", contentFolder: "vault", directory: "authors", extension: ".md", }, } -
parsereturns"authors/Jane Doe.md", built from the link andtarget. -
qino reads
vault/authors/Jane Doe.md, andpost.authoris that entry.
A wikilink only needs target. sourceFilePath is there for values relative to the file they're written in, such as "../authors/jane.md", and for error messages.
"categories[*]", a tree, calls parse once per element, with "[[Design]]" then "[[Tooling]]". fieldPath is the declared path, not the element's index:
{
sourceFilePath: "vault/posts/Hello World.md",
fieldPath: "categories[*]",
target: {
is: "tree",
contentFolder: "vault",
directory: "categories",
extension: ".md",
},
}
site, an item, gets "[[Settings]]". An item target has file instead of directory and extension:
{
sourceFilePath: "vault/posts/Hello World.md",
fieldPath: "site",
target: { is: "item", contentFolder: "vault", file: "settings.json" },
}
Paths are relative to the working directory, as passed to initQino. When the author's own relations resolve, sourceFilePath is the author's file, vault/authors/Jane Doe.md, since that's where their links are written.
parse runs when the relation resolves. A relation that stays a string keeps the raw value, such as "[[Jane Doe]]". Errors about the returned path name the raw value too. The RelationParse type types a callback declared on its own, and RelationParseContext its ctx.
The cloud UI can't run parse, so a relation with a parse callback is read-only there. See In config.json.
Resolving
Declared relations resolve by default. Each one resolves to its resolveDepth, which counts hops: at 1, the default, the post's author becomes the author entry, but relations declared on the author stay strings. At 2 they resolve too. At 0 the relation stays a string.
A nested relation never goes further than the hops left: if author has resolveDepth: 1, the author's own relations stay strings even when they set a higher resolveDepth.
A view can set the depth of some relations with resolveDepth, keyed by relation path:
views: (view) => ({
default: view({}), // each relation resolves to its own resolveDepth
card: view({ resolveDepth: { author: 0 } }), // author stays "authors/camille-laurent.json"
detail: view({ resolveDepth: { author: 2 } }), // author and the author's relations resolve
}),
Relations the view leaves out keep their own resolveDepth. A depth the view sets applies to the whole branch below that relation: with { author: 2 }, the author's relations resolve even when the author's primitive declares them with resolveDepth: 0. A path that isn't a relation, or a depth outside 0 to 6, is a type error and throws when the primitive is defined.
Keep depths low. Each hop reads the target files of every entry returned, and a self-relation such as related: () => postCollection can reach most of the collection within a few hops. Resolved entries are plain objects, so a deep graph also repeats the same entries in what a page sends to the client.
Resolved targets contain the target's validated fields plus _meta. They bypass the target's own views and augment callbacks. A resolved tree entry is content plus _meta, without children or navigation.
A nullable or optional relation field stays nullable after resolution. Within one getter call, repeated references to the same target are fetched once.
Where relations are and are not checked
getEntriesandgetEntry, unless the view or the relation turns resolution off: the reference is validated and the target is read. A bad reference throws.- Relations at depth
0, from their ownresolveDepthor the view's,getAllSlugs, andqino check: the reference is an ordinary string and is not followed.