Skip to Content
DocsConceptsLocal API

Local API

The local API runs in your server process. Route handlers, server components, server actions and scripts all use it.

import { getQuickben } from "@quickben/core"; import config from "./quickben.config"; const qb = await getQuickben(config); const user = { id: "u1" }; const post = await qb.create("posts", { title: "Hello" }, { user }); const page = await qb.find("posts", { where: { status: { eq: "draft" } }, limit: 10 }, { user }); const same = await qb.findById("posts", post.id, { user, depth: 1 }); const total = await qb.count("posts", { where: { status: { eq: "published" } } }, { user }); await qb.update("posts", post.id, { status: "published" }, { user }); await qb.delete("posts", post.id, { user });

Methods

MethodReturnsNotes
find(slug, query, ctx)FindResult<Doc>See Query model.
findById(slug, id, options)Docoptions is ctx plus select and depth. Throws NotFoundError.
count(slug, { where }, ctx)numberApplies read access.
create(slug, data, ctx)DocValidation and hooks in one transaction.
update(slug, id, data, ctx)DocPartial update.
delete(slug, id, ctx)DocReturns the deleted document.
transaction(fn)result of fnfn gets a qb bound to the transaction.
collections[slug]SanitizedCollectionCollection metadata.
db.rawadapter’s native clientFor custom queries.

Request context

Every call takes a context. It is required, so a call without a user is explicit.

KeyPurpose
userQuickbenUser or null for anonymous calls.
reqThe incoming Request, passed to access rules and hooks.
overrideAccesstrue skips access rules. Use only in trusted server code.

Instances

FunctionBehavior
getQuickben(config)One cached instance per config object. A failed start is dropped, so the next call retries.
createQuickben(config)A new instance every call. Use it in tests.

Both call db.init(schema) and then each plugin’s onInit.

Transactions

await qb.transaction(async (tx) => { const tag = await tx.create("tags", { name: "release" }, { user }); await tx.create("posts", { title: "v0.1", tags: [tag.id] }, { user }); });

If fn throws, both writes are rolled back. Calling transaction inside fn joins the same transaction.

Result types

OptionEffect on the type
select: ["title"]Documents narrow to { id, title }.
depth: 1Relations become string | RelatedDoc. Missing or unreadable ones stay ids.
Last updated on