All files / src/server/platform db.server.ts

100% Statements 2/2
100% Branches 0/0
100% Functions 2/2
100% Lines 2/2

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24            50x                             2551x    
import { drizzle } from 'drizzle-orm/d1'
import type { BatchItem } from 'drizzle-orm/batch'
import * as schema from '#/server/db/schema'
 
/** Creates the Drizzle client for one request. Cheap: no connection pool. */
export function createDb(d1: D1Database) {
  return drizzle(d1, { schema })
}
 
/** The database the app receives through the per-request context. */
export type Db = ReturnType<typeof createDb>
 
/**
 * Runs statements atomically: all of them or none. D1 has no interactive
 * transactions (`db.transaction()` with reads between writes), only batches,
 * so build every statement first and pass them here. This is the only place
 * that calls D1's batch, so another database can swap in a transaction.
 */
export function atomically<
  T extends readonly [BatchItem<'sqlite'>, ...BatchItem<'sqlite'>[]],
>(db: Db, statements: T) {
  return db.batch(statements)
}