@kerf-js/db 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/mongo.ts ADDED
@@ -0,0 +1,277 @@
1
+ // A list source over a MongoDB collection, and writes from anywhere as tag pokes. Every filter is `$eq`/`$in` on a
2
+ // checked scalar and a search is an escaped `$regex`, so a request has no way to name an operator. Takes
3
+ // whatever has the driver's collection methods, so `mongodb` is the app's dependency.
4
+
5
+ import {
6
+ pageOf,
7
+ plan,
8
+ rowOf,
9
+ storedId,
10
+ written,
11
+ type DbAdapter,
12
+ type ListParams,
13
+ type ListSource,
14
+ type SourceSpec,
15
+ } from '@kerf-js/start/list'
16
+ import { hub } from '@kerf-js/start/hub'
17
+ import type { Source } from '@kerf-js/start/changes'
18
+
19
+ /** What a change stream hands over, as far as anything here reads it. */
20
+ export interface MongoEvent extends MongoChange {
21
+ ns?: { coll?: string }
22
+ fullDocument?: { tags?: string[] }
23
+ }
24
+
25
+ /** The driver's change stream. */
26
+ export type MongoStream = AsyncIterable<MongoEvent> & { close?(): Promise<unknown> }
27
+
28
+ /** The methods of the driver's `Collection` a source calls. */
29
+ export interface MongoCollection {
30
+ collectionName?: string
31
+ find(filter: object, options?: object): { toArray(): Promise<object[]> }
32
+ countDocuments(filter: object): Promise<number>
33
+ findOne(filter: object, options?: object): Promise<object | null>
34
+ insertOne(doc: object): Promise<unknown>
35
+ findOneAndUpdate(filter: object, update: object, options: object): Promise<object | null>
36
+ deleteOne(filter: object): Promise<{ deletedCount?: number }>
37
+ watch(pipeline?: object[], options?: object): MongoStream
38
+ }
39
+
40
+ const regex = (text: string) => ({
41
+ $regex: text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'),
42
+ $options: 'i',
43
+ })
44
+
45
+ /** A `ListSource` over one collection. */
46
+ export function mongoSource<Row = Record<string, unknown>>(
47
+ collection: MongoCollection,
48
+ spec: SourceSpec,
49
+ ): ListSource<Row> {
50
+ const stored = Object.values(spec.columns)
51
+ const idColumn = storedId(spec)
52
+ const projection = Object.fromEntries([
53
+ ...stored.map((c) => [c, 1]),
54
+ ...(stored.includes('_id') ? [] : [['_id', 0]]),
55
+ ])
56
+ const reading = rowOf<Row>(spec)
57
+ return {
58
+ async list(p: ListParams) {
59
+ const q = plan(spec, p)
60
+ const and: object[] = q.where.map((w) => ({
61
+ [w.column]: w.values.length === 1 ? { $eq: w.values[0] } : { $in: w.values },
62
+ }))
63
+ if (q.search) and.push({ $or: q.search.columns.map((c) => ({ [c]: regex(q.search!.text) })) })
64
+ const filter = and.length ? { $and: and } : {}
65
+ const dir = q.sort?.dir === 'desc' ? -1 : 1,
66
+ cmp = dir < 0 ? '$lt' : '$gt'
67
+ const keyset =
68
+ p.after &&
69
+ (q.sort
70
+ ? {
71
+ $or: [
72
+ { [q.sort.column]: { [cmp]: p.after[0] } },
73
+ { [q.sort.column]: { $eq: p.after[0] }, [idColumn]: { [cmp]: p.after[1] } },
74
+ ],
75
+ }
76
+ : { [idColumn]: { $gt: p.after[1] } })
77
+ const sort = q.sort ? { [q.sort.column]: dir, [idColumn]: dir } : { [idColumn]: 1 }
78
+ const cursor = p.mode === 'cursor'
79
+ const rows = (
80
+ await collection
81
+ .find(keyset ? { $and: [...and, keyset] } : filter, {
82
+ projection,
83
+ sort,
84
+ skip: p.offset,
85
+ limit: cursor ? p.limit + 1 : p.limit,
86
+ })
87
+ .toArray()
88
+ ).map(reading)
89
+ return pageOf(
90
+ rows,
91
+ p,
92
+ { sort: q.sort?.field, id: q.id.field },
93
+ cursor ? undefined : await collection.countDocuments(filter),
94
+ )
95
+ },
96
+ get: async (id) => {
97
+ const r = await collection.findOne({ [idColumn]: { $eq: id } }, { projection })
98
+ return r ? reading(r) : null
99
+ },
100
+ async create(data) {
101
+ const doc = written(spec, data, true)
102
+ await collection.insertOne({ ...doc })
103
+ return reading(doc)
104
+ },
105
+ async update(id, patch) {
106
+ const r = await collection.findOneAndUpdate(
107
+ { [idColumn]: { $eq: id } },
108
+ { $set: written(spec, patch) },
109
+ { projection, returnDocument: 'after' },
110
+ )
111
+ return r ? reading(r) : null
112
+ },
113
+ remove: async (id) =>
114
+ ((await collection.deleteOne({ [idColumn]: { $eq: id } })).deletedCount ?? 0) > 0,
115
+ }
116
+ }
117
+
118
+ /** `db.adapter@1` over a Mongo collection. */
119
+ export const adapter: DbAdapter<MongoCollection> = {
120
+ name: 'mongo',
121
+ source: (t, spec) => mongoSource(t, spec),
122
+ }
123
+
124
+ /** The methods of the driver's `Db` the change feed calls. */
125
+ export interface MongoDb {
126
+ command(doc: object): Promise<{ setName?: string; msg?: string }>
127
+ watch(pipeline?: object[], options?: object): MongoStream
128
+ collection(name: string): { insertOne(doc: object): Promise<unknown> }
129
+ createCollection?(name: string, options?: object): Promise<unknown>
130
+ }
131
+
132
+ const BARRIER = '__kerf_barrier'
133
+ const said = (e: unknown) => (e as { message?: string } | null)?.message ?? String(e)
134
+
135
+ /**
136
+ * `realtime({ changes: mongoChanges(db, ['todos']) })`: one change stream, ids only, read by every instance. A mutation's
137
+ * barrier is a capped collection's insert, so the oplog puts it after the writes it covers. After a lost stream every
138
+ * collection is poked: the resume token is not kept, because a poke is cheaper than a stale page.
139
+ */
140
+ export function mongoChanges(
141
+ db: MongoDb,
142
+ collections: string[],
143
+ options: { retry?: number; external?: boolean } = {},
144
+ ): Source {
145
+ const retry = options.retry ?? 1000
146
+ return {
147
+ name: 'mongodb',
148
+ tables: collections,
149
+ hold: 120, // a stream hands over a batch at a time
150
+ external: options.external,
151
+ async probe() {
152
+ try {
153
+ const hello = await db.command({ hello: 1 })
154
+ if (hello.setName || hello.msg === 'isdbgrid')
155
+ return { mode: 'feed', via: 'mongo-change-stream' }
156
+ return {
157
+ mode: 'off',
158
+ via: 'none',
159
+ why: 'a standalone mongod has no oplog, so no change streams',
160
+ fix: 'run it as a replica set: a single member is enough (`mongod --replSet rs0`, then `rs.initiate()`)',
161
+ }
162
+ } catch (e) {
163
+ return {
164
+ mode: 'off',
165
+ via: 'none',
166
+ why: `MongoDB didn't answer: ${said(e)}`,
167
+ fix: 'check the connection string',
168
+ }
169
+ }
170
+ },
171
+ async start(sink, how) {
172
+ if (how.mode === 'off') return () => {}
173
+ await db.createCollection?.(BARRIER, { capped: true, size: 1 << 20 }).catch(() => {})
174
+ let stream: MongoStream | undefined,
175
+ stopped = false,
176
+ timer: ReturnType<typeof setTimeout> | undefined,
177
+ again = false
178
+ const pipeline = [
179
+ { $match: { 'ns.coll': { $in: [...collections, BARRIER] } } },
180
+ { $project: { operationType: 1, documentKey: 1, 'ns.coll': 1, 'fullDocument.tags': 1 } },
181
+ ]
182
+ const run = async () => {
183
+ if (stopped) return
184
+ try {
185
+ stream = db.watch(pipeline)
186
+ if (again) sink.lost()
187
+ sink.up()
188
+ again = true
189
+ for await (const c of stream) {
190
+ if (c.ns?.coll === BARRIER) {
191
+ if (c.fullDocument?.tags) sink.settled(c.fullDocument.tags)
192
+ continue
193
+ }
194
+ if (!['insert', 'update', 'replace', 'delete'].includes(c.operationType) || !c.ns?.coll)
195
+ continue
196
+ sink.changes([
197
+ {
198
+ table: c.ns.coll,
199
+ ...(c.documentKey?._id !== undefined && { id: String(c.documentKey._id) }),
200
+ },
201
+ ])
202
+ }
203
+ } catch (e) {
204
+ if (!stopped) sink.down(`change stream: ${said(e)}`)
205
+ }
206
+ if (!stopped) timer = setTimeout(() => void run(), retry)
207
+ }
208
+ void run()
209
+ return async () => {
210
+ stopped = true
211
+ clearTimeout(timer)
212
+ await stream?.close?.()
213
+ }
214
+ },
215
+ settle: async (tags) => void (await db.collection(BARRIER).insertOne({ tags })),
216
+ }
217
+ }
218
+
219
+ /** The fields of a change-stream event that say what happened to which row. */
220
+ export interface MongoChange {
221
+ operationType: string
222
+ documentKey?: { _id?: string | number | { toString(): string } }
223
+ }
224
+
225
+ /** The tags of one change: the collection's, and the row's. Never the row itself. */
226
+ export function changeTags(change: MongoChange, tag: string): string[] {
227
+ if (!['insert', 'update', 'replace', 'delete'].includes(change.operationType)) return []
228
+ const id = change.documentKey?._id
229
+ return id === undefined ? [tag] : [tag, `${tag}:${String(id)}`]
230
+ }
231
+
232
+ /** How `watchMongo` turns a stream into pokes. */
233
+ export interface WatchOptions {
234
+ /** Default the collection's name. */
235
+ tag?: string
236
+ /** Where the tags go. Default the realtime hub, as `invalidate` would. */
237
+ poke?: (tags: string[]) => unknown
238
+ /** Ms between attempts after the stream fails. Default 1000. */
239
+ retry?: number
240
+ }
241
+
242
+ /**
243
+ * Writes from cron, a shell or another service reach `.live` queries, as tags. Needs a replica set. After a lost stream
244
+ * the whole collection's tag is poked, because what happened meanwhile is unknown. Returns the way to stop.
245
+ */
246
+ export function watchMongo(
247
+ collection: MongoCollection,
248
+ options: WatchOptions = {},
249
+ ): () => Promise<void> {
250
+ const tag = options.tag ?? collection.collectionName
251
+ if (!tag) throw new Error('`watchMongo` needs a `tag`')
252
+ const poke = options.poke ?? ((tags: string[]) => hub().poke(tags))
253
+ const retry = options.retry ?? 1000
254
+ let stream: MongoStream | undefined,
255
+ stopped = false,
256
+ timer: ReturnType<typeof setTimeout> | undefined
257
+ const run = async (again: boolean) => {
258
+ if (stopped) return
259
+ try {
260
+ stream = collection.watch([{ $project: { operationType: 1, documentKey: 1 } }])
261
+ if (again) await poke([tag])
262
+ for await (const change of stream) {
263
+ const tags = changeTags(change, tag)
264
+ if (tags.length) await poke(tags)
265
+ }
266
+ } catch {
267
+ // A closed stream ends here too; the loop below decides.
268
+ }
269
+ if (!stopped) timer = setTimeout(() => void run(true), retry)
270
+ }
271
+ void run(false)
272
+ return async () => {
273
+ stopped = true
274
+ clearTimeout(timer)
275
+ await stream?.close?.()
276
+ }
277
+ }
@@ -0,0 +1,73 @@
1
+ // Better Auth uses a Kysely dialect over Native's connection, without a second driver.
2
+
3
+ import type { Dialect } from 'kysely'
4
+ import { driver } from '../driver'
5
+ import type { NativeDb } from './index'
6
+
7
+ const {
8
+ MysqlAdapter,
9
+ MysqlIntrospector,
10
+ MysqlQueryCompiler,
11
+ PostgresAdapter,
12
+ PostgresIntrospector,
13
+ PostgresQueryCompiler,
14
+ SqliteAdapter,
15
+ SqliteIntrospector,
16
+ SqliteQueryCompiler,
17
+ } = await driver('@kerf-js/db/plugin-native/auth', 'kysely', () => import('kysely'))
18
+
19
+ const PARTS = {
20
+ sqlite: {
21
+ adapter: SqliteAdapter,
22
+ compiler: SqliteQueryCompiler,
23
+ introspector: SqliteIntrospector,
24
+ type: 'sqlite',
25
+ },
26
+ postgresql: {
27
+ adapter: PostgresAdapter,
28
+ compiler: PostgresQueryCompiler,
29
+ introspector: PostgresIntrospector,
30
+ type: 'postgres',
31
+ },
32
+ mysql: {
33
+ adapter: MysqlAdapter,
34
+ compiler: MysqlQueryCompiler,
35
+ introspector: MysqlIntrospector,
36
+ type: 'mysql',
37
+ },
38
+ } as const
39
+
40
+ /** Better Auth's `database` option on a Native client. It runs without transactions, which Better Auth treats as optional. */
41
+ export function nativeAuth(db: NativeDb) {
42
+ const { adapter, compiler, introspector, type } = PARTS[db.$kerf.dialect]
43
+ const connection = {
44
+ async executeQuery({ sql, parameters }: { sql: string; parameters: readonly unknown[] }) {
45
+ const out = (await db.query(sql, [...parameters])) as unknown
46
+ // mysql2 answers a write with a header, not rows.
47
+ if (Array.isArray(out)) return { rows: out }
48
+ return {
49
+ rows: [],
50
+ numAffectedRows: BigInt((out as { affectedRows?: number }).affectedRows ?? 0),
51
+ }
52
+ },
53
+ streamQuery(): never {
54
+ throw new Error('Native has no streaming queries')
55
+ },
56
+ }
57
+ const dialect = {
58
+ createAdapter: () => new adapter(),
59
+ createDriver: () => ({
60
+ init: async () => {},
61
+ destroy: async () => {},
62
+ acquireConnection: () => Promise.resolve(connection),
63
+ releaseConnection: async () => {},
64
+ beginTransaction: async () => {},
65
+ commitTransaction: async () => {},
66
+ rollbackTransaction: async () => {},
67
+ }),
68
+ createIntrospector: (kysely: ConstructorParameters<typeof SqliteIntrospector>[0]) =>
69
+ new introspector(kysely),
70
+ createQueryCompiler: () => new compiler(),
71
+ } as unknown as Dialect
72
+ return { dialect, type, transaction: false }
73
+ }
@@ -0,0 +1,9 @@
1
+ // Bun's built-in driver has no types on Node, where this package is checked: only what `open` calls.
2
+ declare module 'bun:sqlite' {
3
+ /** One open database file. */
4
+ export class Database {
5
+ constructor(path: string, options: { create: boolean })
6
+ prepare(text: string): { all(...params: unknown[]): Record<string, unknown>[] }
7
+ run(text: string): unknown
8
+ }
9
+ }
@@ -0,0 +1,84 @@
1
+ // SQL template values always go to the driver as parameters.
2
+
3
+ import { gate, register, touch, type DbHandle, type Migration } from '@kerf-js/start/db/core'
4
+
5
+ /** One row, by column name. */
6
+ export type Row = Record<string, unknown>
7
+
8
+ /** What an engine supplies: statements, files of them, a transaction and a lock. */
9
+ export interface Driver<D = unknown> {
10
+ dialect: DbHandle['dialect']
11
+ raw: D
12
+ /** Run one statement with bound values, and return its rows as objects. */
13
+ query(text: string, params: unknown[]): Promise<Row[]>
14
+ exec(text: string): Promise<void>
15
+ /** Run `fn` on one connection, committed when it resolves. */
16
+ transaction<T>(fn: (driver: Pick<Driver, 'query'>) => Promise<T>): Promise<T>
17
+ /** Hold off other instances while migrations run. */
18
+ locked<T>(fn: () => Promise<T>): Promise<T>
19
+ }
20
+
21
+ /** The template tag: its values always go to the driver as parameters. */
22
+ export type Sql = (strings: TemplateStringsArray, ...values: unknown[]) => Promise<Row[]>
23
+
24
+ /** The client an app holds, as `ctx.db`. */
25
+ export interface NativeDb<D = unknown> {
26
+ sql: Sql
27
+ /** One statement you built, with its values bound. For a tool that writes SQL; in app code use `sql`. */
28
+ query: Driver['query']
29
+ driver: D
30
+ transaction<T>(fn: (tx: { sql: Sql }) => Promise<T>): Promise<T>
31
+ $kerf: DbHandle
32
+ }
33
+
34
+ const placeholder = (dialect: DbHandle['dialect'], i: number) =>
35
+ dialect === 'postgresql' ? `$${i + 1}` : '?'
36
+
37
+ const tag =
38
+ (handle: DbHandle, query: Driver['query']): Sql =>
39
+ (strings, ...values) => {
40
+ const text = strings.reduce((out, part, i) => out + placeholder(handle.dialect, i - 1) + part)
41
+ touch(handle, text)
42
+ return query(text, values)
43
+ }
44
+
45
+ /** A client over any `Driver`, with its migrations behind the same gate as every statement. */
46
+ export function createNative<D>(
47
+ driver: Driver<D>,
48
+ { migrate = true }: { migrate?: boolean | undefined } = {},
49
+ ): NativeDb<D> {
50
+ const handle: DbHandle = {
51
+ dialect: driver.dialect,
52
+ exempt: new Set(),
53
+ migrate: (list: Migration[]) =>
54
+ driver.locked(async () => {
55
+ await driver.exec(
56
+ 'create table if not exists __kerf_migrations (tag varchar(255) primary key)',
57
+ )
58
+ const done = new Set(
59
+ (await driver.query('select tag from __kerf_migrations', [])).map((r) => r.tag),
60
+ )
61
+ for (const m of list) {
62
+ if (done.has(m.tag)) continue
63
+ // A file with only comments is a migration still being written; some engines refuse an empty query.
64
+ if (m.sql.replace(/--.*$/gm, '').trim()) await driver.exec(m.sql)
65
+ await driver.query(
66
+ `insert into __kerf_migrations (tag) values (${placeholder(driver.dialect, 0)})`,
67
+ [m.tag],
68
+ )
69
+ }
70
+ }),
71
+ }
72
+ const ready = gate(handle, migrate)
73
+ const query: Driver['query'] = async (text, params) => (await ready(), driver.query(text, params))
74
+ const db = {
75
+ sql: tag(handle, query),
76
+ query,
77
+ driver: driver.raw,
78
+ transaction: async <T>(fn: (tx: { sql: Sql }) => Promise<T>) => (
79
+ await ready(),
80
+ driver.transaction((tx) => fn({ sql: tag(handle, tx.query) }))
81
+ ),
82
+ }
83
+ return register(db, handle)
84
+ }
@@ -0,0 +1,74 @@
1
+ // MySQL with no ORM: mysql2's pool, which connects on the first statement, so a build needs no database. Files run on
2
+ // one connection with multiple statements on; everything else has it off.
3
+
4
+ import { driver } from '../driver'
5
+ import { createNative, type Driver, type Row } from './index'
6
+
7
+ const { createConnection, createPool } = await driver(
8
+ '@kerf-js/db/plugin-native/mysql',
9
+ 'mysql2',
10
+ () => import('mysql2/promise'),
11
+ )
12
+
13
+ const LOCK = 'kerf_migrations'
14
+ type ExecuteValues = Parameters<Awaited<ReturnType<typeof createConnection>>['execute']>[1]
15
+
16
+ /** A client on a mysql2 pool. */
17
+ export function createDb({
18
+ url,
19
+ migrate,
20
+ max = 10,
21
+ }: {
22
+ url: string
23
+ migrate?: boolean
24
+ max?: number
25
+ }) {
26
+ const uri = url || 'mysql://localhost/build'
27
+ // No `maxIdle` below the limit: that starts mysql2's reaper, a timer that keeps a finished `kerf build` alive forever.
28
+ const pool = createPool({ uri, connectionLimit: max })
29
+ const rows = async (run: Promise<[unknown, unknown]>) => (await run)[0] as Row[]
30
+ const engine: Driver<typeof pool> = {
31
+ dialect: 'mysql',
32
+ raw: pool,
33
+ query: (text, params) => rows(pool.execute(text, params as ExecuteValues)),
34
+ async exec(text) {
35
+ const c = await createConnection({ uri, multipleStatements: true })
36
+ try {
37
+ await c.query(text)
38
+ } finally {
39
+ await c.end()
40
+ }
41
+ },
42
+ async transaction(fn) {
43
+ const c = await pool.getConnection()
44
+ try {
45
+ await c.beginTransaction()
46
+ const out = await fn({
47
+ query: (text, params) => rows(c.execute(text, params as ExecuteValues)),
48
+ })
49
+ await c.commit()
50
+ return out
51
+ } catch (e) {
52
+ await c.rollback()
53
+ throw e
54
+ } finally {
55
+ c.release()
56
+ }
57
+ },
58
+ async locked(fn) {
59
+ const c = await pool.getConnection()
60
+ try {
61
+ const [got] = (await c.query('select get_lock(?, 60) as got', [LOCK])) as unknown as [
62
+ { got: unknown }[],
63
+ ]
64
+ if (Number(got[0]?.got) !== 1)
65
+ throw new Error('another instance held the migration lock for 60s')
66
+ return await fn()
67
+ } finally {
68
+ await c.query('select release_lock(?)', [LOCK]).catch(() => {})
69
+ c.release()
70
+ }
71
+ },
72
+ }
73
+ return createNative(engine, { migrate })
74
+ }
@@ -0,0 +1,44 @@
1
+ // Postgres with no ORM: postgres.js, which connects on the first statement, so a build needs no database.
2
+
3
+ import { driver } from '../driver'
4
+ import { createNative, type Driver, type Row } from './index'
5
+
6
+ const { default: postgres } = await driver(
7
+ '@kerf-js/db/plugin-native/postgres',
8
+ 'postgres',
9
+ () => import('postgres'),
10
+ )
11
+
12
+ const LOCK = 7_301_987
13
+
14
+ /** A client on a postgres.js connection pool. */
15
+ export function createDb({
16
+ url,
17
+ migrate,
18
+ max = 10,
19
+ }: {
20
+ url: string
21
+ migrate?: boolean
22
+ max?: number
23
+ }) {
24
+ const client = postgres(url || 'postgres://localhost/build', {
25
+ max,
26
+ idle_timeout: 20,
27
+ onnotice: () => {},
28
+ })
29
+ const on = (c: { unsafe: typeof client.unsafe }) => (text: string, params: unknown[]) =>
30
+ c.unsafe(text, params as Parameters<typeof client.unsafe>[1]) as unknown as Promise<Row[]>
31
+ const engine: Driver<typeof client> = {
32
+ dialect: 'postgresql',
33
+ raw: client,
34
+ query: on(client),
35
+ exec: async (text) => void (await client.unsafe(text)),
36
+ transaction: (fn) => client.begin((tx) => fn({ query: on(tx) })),
37
+ locked: (fn) =>
38
+ client.begin(async (tx) => {
39
+ await tx.unsafe(`select pg_advisory_xact_lock(${LOCK})`)
40
+ return fn()
41
+ }),
42
+ }
43
+ return createNative(engine, { migrate })
44
+ }
@@ -0,0 +1,88 @@
1
+ // SQLite with no ORM: Node's and Deno's `node:sqlite`, or Bun's `bun:sqlite`. One connection, opened on the first
2
+ // statement. A transaction's body is async, so it holds a gate: statements outside it wait.
3
+
4
+ import { AsyncLocalStorage } from 'node:async_hooks'
5
+ import { mkdirSync } from 'node:fs'
6
+ import { dirname } from 'node:path'
7
+ import type { SQLInputValue } from 'node:sqlite'
8
+ import { createNative, type Driver, type Row } from './index'
9
+
10
+ interface Conn {
11
+ all(text: string, params: unknown[]): Row[]
12
+ exec(text: string): void
13
+ }
14
+
15
+ async function open(path: string): Promise<Conn> {
16
+ if (path !== ':memory:') mkdirSync(dirname(path), { recursive: true })
17
+ let conn: Conn
18
+ if ('Bun' in globalThis) {
19
+ const { Database } = await import('bun:sqlite')
20
+ const db = new Database(path, { create: true })
21
+ conn = {
22
+ all: (text, params) => db.prepare(text).all(...params),
23
+ exec: (text) => void db.run(text),
24
+ }
25
+ } else {
26
+ const { DatabaseSync } = await import('node:sqlite')
27
+ const db = new DatabaseSync(path)
28
+ // `node:sqlite` on Deno refuses a boolean, which SQLite stores as 0 or 1 anyway.
29
+ conn = {
30
+ all: (text, params) =>
31
+ db
32
+ .prepare(text)
33
+ .all(...(params.map((v) => (typeof v === 'boolean' ? Number(v) : v)) as SQLInputValue[])),
34
+ exec: (text) => db.exec(text),
35
+ }
36
+ }
37
+ if (path !== ':memory:') conn.exec('pragma journal_mode = wal')
38
+ conn.exec('pragma foreign_keys = on; pragma busy_timeout = 5000')
39
+ return conn
40
+ }
41
+
42
+ /** A client on one SQLite file, or `:memory:`. */
43
+ export function createDb({
44
+ url = 'file:.data/app.db',
45
+ migrate,
46
+ }: { url?: string; migrate?: boolean } = {}) {
47
+ const path = url.replace(/^file:/, '')
48
+ let conn: Promise<Conn> | undefined
49
+ const connect = () => (conn ??= open(path))
50
+ const inside = new AsyncLocalStorage<boolean>()
51
+ let gate: Promise<unknown> = Promise.resolve()
52
+ const query = async (text: string, params: unknown[]) => {
53
+ const c = await connect()
54
+ if (!inside.getStore()) await gate
55
+ return c.all(text, params)
56
+ }
57
+ const driver: Driver<() => Promise<Conn>> = {
58
+ dialect: 'sqlite',
59
+ raw: connect,
60
+ query,
61
+ exec: async (text) => (await connect()).exec(text),
62
+ async transaction(fn) {
63
+ if (inside.getStore()) return fn({ query })
64
+ const c = await connect()
65
+ const before = gate
66
+ let release!: () => void
67
+ gate = new Promise<void>((ok) => {
68
+ release = ok
69
+ })
70
+ await before
71
+ try {
72
+ c.exec('begin')
73
+ try {
74
+ const out = await inside.run(true, () => fn({ query }))
75
+ c.exec('commit')
76
+ return out
77
+ } catch (e) {
78
+ c.exec('rollback')
79
+ throw e
80
+ }
81
+ } finally {
82
+ release()
83
+ }
84
+ },
85
+ locked: (fn) => fn(),
86
+ }
87
+ return createNative(driver, { migrate })
88
+ }