|
1 | | -import type { Database } from "@db/sqlite"; |
2 | | -import type * as zen from "@dldc/zendb"; |
3 | | - |
4 | | -export interface TDbDatabase { |
5 | | - exec<Op extends zen.TOperation>(op: Op): zen.TOperationResult<Op>; |
6 | | - execMany<Op extends zen.TOperation>(ops: Op[]): zen.TOperationResult<Op>[]; |
7 | | - readonly sqlDb: Database; |
8 | | -} |
9 | | - |
10 | | -export function DbDatabase(sqlDb: Database): TDbDatabase { |
11 | | - return { |
12 | | - exec, |
13 | | - execMany, |
14 | | - sqlDb, |
15 | | - }; |
16 | | - |
17 | | - function exec<Op extends zen.TOperation>(op: Op): zen.TOperationResult<Op> { |
18 | | - if (op.kind === "CreateTable") { |
19 | | - sqlDb.exec(op.sql); |
20 | | - return opResult<zen.TCreateTableOperation>(null); |
21 | | - } |
22 | | - if (op.kind === "DropTable") { |
23 | | - sqlDb.exec(op.sql); |
24 | | - return opResult<zen.TDropTableOperation>(null); |
25 | | - } |
26 | | - if (op.kind === "Insert") { |
27 | | - sqlDb.prepare(op.sql).run(op.params); |
28 | | - return opResult<zen.TInsertOperation<any>>(op.parse()); |
29 | | - } |
30 | | - if (op.kind === "InsertMany") { |
31 | | - sqlDb.prepare(op.sql).run(op.params); |
32 | | - return opResult<zen.TInsertOperation<any>>(op.parse()); |
33 | | - } |
34 | | - if (op.kind === "Delete") { |
35 | | - const stmt = sqlDb.prepare(op.sql); |
36 | | - const res = op.params ? stmt.run(op.params) : stmt.run(); |
37 | | - return opResult<zen.TDeleteOperation>(op.parse({ deleted: res })); |
38 | | - } |
39 | | - if (op.kind === "Update") { |
40 | | - const stmt = sqlDb.prepare(op.sql); |
41 | | - const res = op.params ? stmt.run(op.params) : stmt.run(); |
42 | | - return opResult<zen.TUpdateOperation>(op.parse({ updated: res })); |
43 | | - } |
44 | | - if (op.kind === "Query") { |
45 | | - const stmt = sqlDb.prepare(op.sql); |
46 | | - const res = op.params ? stmt.all(op.params) : stmt.all(); |
47 | | - return opResult<zen.TQueryOperation<any>>( |
48 | | - op.parse(res as Record<string, any>[]), |
49 | | - ); |
50 | | - } |
51 | | - if (op.kind === "ListTables") { |
52 | | - const res = sqlDb.prepare(op.sql).all(); |
53 | | - return opResult<zen.TListTablesOperation>( |
54 | | - op.parse(res as Record<string, any>[]), |
55 | | - ); |
56 | | - } |
57 | | - if (op.kind === "Pragma") { |
58 | | - const res = sqlDb.prepare(op.sql).all(); |
59 | | - return opResult<zen.TPragmaOperation<any>>( |
60 | | - op.parse(res as Record<string, any>[]), |
61 | | - ); |
62 | | - } |
63 | | - if (op.kind === "PragmaSet") { |
64 | | - sqlDb.prepare(op.sql).run(); |
65 | | - return opResult<zen.TPragmaSetOperation>(null); |
66 | | - } |
67 | | - return expectNever(op); |
68 | | - } |
69 | | - |
70 | | - function opResult<Op extends zen.TOperation>( |
71 | | - res: zen.TOperationResult<Op>, |
72 | | - ): zen.TOperationResult<zen.TOperation> { |
73 | | - return res; |
74 | | - } |
75 | | - |
76 | | - function execMany<Op extends zen.TOperation>( |
77 | | - ops: Op[], |
78 | | - ): zen.TOperationResult<Op>[] { |
79 | | - return ops.map((op) => exec(op)); |
80 | | - } |
81 | | -} |
82 | | - |
83 | | -function expectNever(val: never): never { |
84 | | - throw new Error(`Unexpected value: ${val as any}`); |
85 | | -} |
| 1 | +import { Database } from "@db/sqlite"; |
| 2 | +import { Driver } from "@dldc/zendb"; |
| 3 | + |
| 4 | +export const DbSqliteDriver = Driver.createDriverFromPrepare<Database>({ |
| 5 | + exec: (db, sql) => db.exec(sql), |
| 6 | + prepare: (db, sql) => db.prepare(sql), |
| 7 | + createDatabase: () => new Database(":memory:"), |
| 8 | +}); |
0 commit comments