API
useDb
Drizzle client for the app's database.
import { useDb } from '@wervt/nuxt/server'
const rows = await useDb().select().from(todos)
Returns a Drizzle ORM v1 client (postgres.js driver) for
DATABASE_URL. The client is created once per isolate with at most 2 connections, which are closed
after 20 seconds idle.
Relational queries
Pass your relations to get db.query:
// server/db/relations.ts
import { defineRelations } from 'drizzle-orm'
import * as schema from './schema'
export const relations = defineRelations(schema, (r) => ({
rooms: { players: r.many.players() },
players: { room: r.one.rooms({ from: r.players.roomId, to: r.rooms.id }) },
}))
import { useDb } from '@wervt/nuxt/server'
import { relations } from '../db/relations'
const room = await useDb({ relations }).query.rooms.findFirst({
where: { id },
with: { players: true },
})
undefined in a where throws, so a missing parameter can't silently drop a filter. For a filter
that is optional on purpose, use EmptyFilter from drizzle-orm:
where: { done: query.done ?? EmptyFilter }. To filter by whether a relation exists, use true or
false (where: { players: true }); an empty object {} filters nothing.
All clients share one connection pool.
Throws if DATABASE_URL is not set, which means the app has no drizzle/ folder and therefore no
database.