Import content from contentgraph:server in Vite server modules, or from the prepared .contentgraph/server.js entry in other hosts.

Field selection

Row queries require explicit selection. These queries return the same fields:

const posts = await content.query("posts").select("id", "title").all();

const samePosts = await content.findMany("posts", {
  select: { id: true, title: true },
});

Neither query loads bodies or HTML. Use first() or findFirst() for one entry; they return undefined when nothing matches.

Filtering

import { eq } from "contentgraph/query";

const published = await content
  .query("posts")
  .where((fields) => eq(fields.draft, false))
  .select("id", "title")
  .all();

const samePublished = await content.findMany("posts", {
  where: { draft: false },
  select: { id: true, title: true },
});

Combine predicates with and, or, and not, or object AND, OR, and NOT. See the operator reference for comparisons, string matching, arrays, and presence checks.

Sorting, pagination, and counts

Use .orderBy("title").offset(0).limit(10) or object options orderBy: { title: "asc" }, offset: 0, limit: 10. IDs break ordering ties deterministically.

count() counts filtered entries before pagination and does not require selection. Query builders are immutable, so you can reuse a base query for a listing and its count.

For the author relationship, .with("author", (author) => author.select("name")) corresponds to:

select: {
  id: true,
  title: true,
  author: { select: { name: true } },
}

Previous and next entries

Use .selectAdjacent() or object select: { id: true, _adjacent: true } for neighbors in the ordered result. Detail pages can give adjacency its own sibling scope, independent of the current ID filter:

const post = await content
  .query("posts")
  .where((fields) => eq(fields.id, "hello"))
  .select("id", "title")
  .selectAdjacent((siblings) => siblings.orderBy("title").select("id", "title"))
  .first();

Neighbors are returned in _adjacent.previous and _adjacent.next, with null at each boundary.