Skip to content

Reusable Queries ​

To reuse a query in several places, build it from db and await it wherever you need its result:

ts
export const publishedForms = db.form.where({ published: true }).select("id", "name")

const forms = await publishedForms

Each await executes the query again.

ResultOf gives the type of each of its records, or of its values with pluck and get:

ts
import type { ResultOf } from "kern-orm"

type PublishedForm = ResultOf<typeof publishedForms>

Of a helper, ResultOf gives the type of the records that it selects.

For reusable conditions and other expressions, see defineExpression, and for reusable clauses, helpers.

Queries without a Db ​

To define a query before a Db is available, start it from the schema, such as schema.form. Execute it with $execute when you get the Db, such as through dependency injection:

ts
export const publishedForms = schema.form.where({ published: true }).select("id", "name")

const db = useDb()
const forms = await db.$execute(publishedForms)

Queries that change records work the same way, such as db.$execute(schema.form.find(formId).delete()). Any Db whose schema contains the tables of such a query and the relations it selects can execute it, such as the tenant and SaaS databases. Start other nested queries from the schema too, such as schema.field, and several queries at once with schema.$select.

Such a query is not bound to a database, so it can also be returned from async functions without executing.