Database

Blanq is designed to be used with NuxtHub and Cloudflare. The database is a Cloudflare D1 database and locally it uses sqlite. The database is queried and managed with Drizzle.

Schemas

The database schemas live in server/database/schemas. They are then exported in server/database/schema.ts and used in the server/utils/drizzle.ts file. When defining a new table, remember to export it.

Making database changes

The flow to make changes in the database is as follows:

  1. Make a change to a schema file (or add a new one)
  2. Run pnpm db:generate to generate the SQL migration file
  3. Run pnpm dev to apply the migration

Migrations are applied automatically when the server starts and when it is built in production on NuxtHub.

Drizzle helpers

You can use the Drizzle instance in any event handler as follows and it is automatically imported:

export default defineEventHandler(async (event) => {
    const db = useDrizzle()

    // e.g.
    const user = await db.select().from(tables.users).where(eq(tables.users.email, 'some@email.com')).limit(1)
})

Some of the drizzle filters are exported for convenience in the server/utils/drizzle.ts file.

export { and, asc, desc, eq, not, or, sql } from 'drizzle-orm'
export const tables = schema

The tables object is also automatically imported into your event handlers.