new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

Drizzle · SQLite · all subjects

drivers/cloudflare-do

8 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

Drizzle ORM supports Cloudflare Durable Objects SQLite

Drizzle ORM fully supports the Cloudflare Durable Objects database and Cloudflare Workers environment. It mirrors most popular SQLite-like all, get, values and run query methods syntax.

Drizzle Durable Objects driver imports

To use Drizzle with Cloudflare Durable Objects SQLite, import from 'drizzle-orm/durable-sqlite'. The main database type is DrizzleSqliteDODatabase. The migrator is imported from 'drizzle-orm/durable-sqlite/migrator'.

Initialize Drizzle database in Cloudflare Durable Objects

Create a Drizzle database instance by passing DurableObjectStorage to the drizzle function: this.db = drizzle(this.storage, { logger: false });

Cloudflare Durable Objects SQLite wrangler.toml configuration

Configure Durable Objects in wrangler.toml with a [[durable_objects.bindings]] section containing name and class_name. Add migrations under [[migrations]] with tag and new_sqlite_classes array. Include [[rules]] with type 'Text' and globs pattern '**/*.sql' with fallthrough true to enable migration imports.

Run migrations in Cloudflare Durable Objects constructor

Use ctx.blockConcurrencyWhile() in the Durable Object constructor to ensure all migrations complete before accepting queries. Call migrate(this.db, migrations) inside an async function wrapped by blockConcurrencyWhile to prevent concurrent access during setup.

Cloudflare Durable Objects performance best practice

Bundle all database interactions within a single Durable Object call for maximum performance, since database access is fast within a DO. Avoid making separate round-trips to the Durable Object instance for individual queries unless necessary for debugging.

Cloudflare Durable Objects basic query example

Example showing how to use Drizzle with Cloudflare Durable Objects: ```typescript /// <reference types="@cloudflare/workers-types" /> import { drizzle, DrizzleSqliteDODatabase } from 'drizzle-orm/durable-sqlite'; import { DurableObject } from 'cloudflare:workers' import { migrate } from 'drizzle-orm/durable-sqlite/migrator'; import migrations from '../drizzle/migrations'; import { usersTable } from './db/schema'; export class MyDurableObject extends DurableObject { storage: DurableObjectStorage; db: DrizzleSqliteDODatabase<any>; constructor(ctx: DurableObjectState, env: Env) { super(ctx, env); this.storage = ctx.storage; this.db = drizzle(this.storage, { logger: false }); ctx.blockConcurrencyWhile(async () => { await this._migrate(); }); } async insertAndList(user: typeof usersTable.$inferInsert) { await this.insert(user); return this.select(); } async insert(user: typeof usersTable.$inferInsert) { await this.db.insert(usersTable).values(user); } async select() { return this.db.select().from(usersTable); } async _migrate() { migrate(this.db, migrations); } } export default { async fetch(request: Request, env: Env): Promise<Response> { const id: DurableObjectId = env.MY_DURABLE_OBJECT.idFromName('durable-object'); const stub = env.MY_DURABLE_OBJECT.get(id); const usersAll = await stub.insertAndList({ name: 'John', age: 30, email: 'john@example.com', }); console.log('New user created. Getting all users from the database: ', usersAll); return Response.json(usersAll); } } ``` This example demonstrates: creating a Durable Object class extending DurableObject, initializing Drizzle with DurableObjectStorage, running migrations with blockConcurrencyWhile, and defining async methods for insert and select queries that are called from a Worker fetch handler.

Drizzle Kit Cloudflare D1 dbCredentials configuration

For Cloudflare D1 HTTP driver, set dialect to "sqlite", driver to "d1-http", and provide dbCredentials with accountId, databaseId, and token fields.

Give your agent this brain