@fabrika/db 0.9.0-dev.5 → 0.9.0-dev.6
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/package.json +4 -4
- package/src/codegen-sqlite.ts +3 -11
- package/src/sqlite-bun.ts +41 -14
- package/types/src/sqlite-bun.d.ts +11 -3
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fabrika/db",
|
|
3
|
-
"version": "0.9.0-dev.
|
|
3
|
+
"version": "0.9.0-dev.6",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -92,9 +92,9 @@
|
|
|
92
92
|
"test": "bun test"
|
|
93
93
|
},
|
|
94
94
|
"dependencies": {
|
|
95
|
-
"@fabrika/auth-core": "0.9.0-dev.
|
|
96
|
-
"@fabrika/platform": "0.9.0-dev.
|
|
97
|
-
"@fabrika/platform-node": "0.9.0-dev.
|
|
95
|
+
"@fabrika/auth-core": "0.9.0-dev.6",
|
|
96
|
+
"@fabrika/platform": "0.9.0-dev.6",
|
|
97
|
+
"@fabrika/platform-node": "0.9.0-dev.6",
|
|
98
98
|
"kysely": "0.29.6",
|
|
99
99
|
"kysely-postgres-js": "5.0.1",
|
|
100
100
|
"zod": "^3.25.76"
|
package/src/codegen-sqlite.ts
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
import { Database } from 'bun:sqlite'
|
|
6
6
|
import { z } from 'zod/v4'
|
|
7
7
|
import { CodegenError, type DescribedColumn, type ForeignKeyColumns, type NamedColumn, renderTables, type ScalarType } from './codegen'
|
|
8
|
+
import { execStatements } from './sqlite-bun'
|
|
8
9
|
import { migrateSqlite, type SqliteMigrationBundle, type SqliteMigrationTarget } from './sqlite-migrate'
|
|
9
10
|
|
|
10
11
|
const STRING: ScalarType = { select: 'string' }
|
|
@@ -55,17 +56,8 @@ const foreignKeySchema = z.object({ id: z.number(), seq: z.number(), table: z.st
|
|
|
55
56
|
function bunTarget(db: Database): SqliteMigrationTarget {
|
|
56
57
|
return {
|
|
57
58
|
exec(query, ...bindings) {
|
|
58
|
-
const
|
|
59
|
-
|
|
60
|
-
if (bindings.length > 0 || statement.columnNames.length > 0) {
|
|
61
|
-
const rows = statement.all(...bindings)
|
|
62
|
-
return { toArray: () => rows }
|
|
63
|
-
}
|
|
64
|
-
} finally {
|
|
65
|
-
statement.finalize()
|
|
66
|
-
}
|
|
67
|
-
db.run(query)
|
|
68
|
-
return { toArray: () => [] }
|
|
59
|
+
const rows = execStatements(db, query, bindings)
|
|
60
|
+
return { toArray: () => rows }
|
|
69
61
|
},
|
|
70
62
|
transactionSync: (fn) => db.transaction(fn)(),
|
|
71
63
|
}
|
package/src/sqlite-bun.ts
CHANGED
|
@@ -13,6 +13,44 @@ export interface BunSqliteStorage extends SqliteStorage {
|
|
|
13
13
|
/** workerd's authorizer refuses these in `sql.exec`: a transaction runs through `transaction` or `transactionSync`. */
|
|
14
14
|
const TRANSACTION_CONTROL = /(?:^|;)(?:\s|--[^\n]*(?:\n|$)|\/\*[\s\S]*?\*\/)*(?:begin|commit|end|rollback|savepoint|release)\b/i
|
|
15
15
|
|
|
16
|
+
/** workerd's `SQLITE_LIMIT_COLUMN` and `SQLITE_LIMIT_VARIABLE_NUMBER`, both 100. */
|
|
17
|
+
const WORKERD_MAX_COLUMNS = 100
|
|
18
|
+
|
|
19
|
+
/** What may follow a text's last statement: whitespace, comments and semicolons. */
|
|
20
|
+
const NO_STATEMENT = /^(?:\s|;|--[^\n]*(?:\n|$)|\/\*[\s\S]*?\*\/)*$/
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Runs each statement of `query` in turn and answers the rows of the last one, as a Durable Object's `sql.exec` does.
|
|
24
|
+
* bun:sqlite's `prepare` compiles only the first statement of a text, and its column count cannot tell a migration from a
|
|
25
|
+
* read (`ALTER TABLE … ADD COLUMN … CHECK (…)` reports one column), so the text is split at the end of each compiled
|
|
26
|
+
* statement. `bindings` belong to a text of one statement. Refuses what workerd's SQLite refuses: more than 100 result
|
|
27
|
+
* columns or parameters in one statement.
|
|
28
|
+
*/
|
|
29
|
+
export function execStatements(database: Database, query: string, bindings: readonly SQLQueryBindings[] = []): Record<string, unknown>[] {
|
|
30
|
+
let text = query
|
|
31
|
+
for (;;) {
|
|
32
|
+
const statement = database.prepare<Record<string, unknown>, SQLQueryBindings[]>(text)
|
|
33
|
+
try {
|
|
34
|
+
if (statement.columnNames.length > WORKERD_MAX_COLUMNS) {
|
|
35
|
+
throw new Error('too many columns in result set: SQLITE_ERROR')
|
|
36
|
+
}
|
|
37
|
+
if (statement.paramsCount > WORKERD_MAX_COLUMNS) {
|
|
38
|
+
throw new Error('too many SQL variables: SQLITE_ERROR')
|
|
39
|
+
}
|
|
40
|
+
// The compiled statement's own text, with no parameter to expand: the rest of `text` follows it verbatim.
|
|
41
|
+
const compiled = statement.toString()
|
|
42
|
+
const rest = bindings.length === 0 && text.startsWith(compiled) ? text.slice(compiled.length) : ''
|
|
43
|
+
const rows = statement.all(...bindings)
|
|
44
|
+
if (NO_STATEMENT.test(rest)) {
|
|
45
|
+
return rows
|
|
46
|
+
}
|
|
47
|
+
text = rest
|
|
48
|
+
} finally {
|
|
49
|
+
statement.finalize()
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
16
54
|
function bunBinding(value: SqliteValue): SQLQueryBindings {
|
|
17
55
|
return value instanceof ArrayBuffer ? new Uint8Array(value) : value
|
|
18
56
|
}
|
|
@@ -41,8 +79,8 @@ interface OpenTransaction {
|
|
|
41
79
|
/**
|
|
42
80
|
* `database` (a fresh `:memory:` one by default) with foreign keys enforced, as a Durable Object enforces them. One
|
|
43
81
|
* `transaction` runs at a time; one started inside a callback nests in a savepoint, as workerd nests it, and its
|
|
44
|
-
* siblings wait for each other. A statement outside one runs at once. A text of several statements runs
|
|
45
|
-
*
|
|
82
|
+
* siblings wait for each other. A statement outside one runs at once. A text of several statements runs each of them and
|
|
83
|
+
* answers the rows of the last (`execStatements`).
|
|
46
84
|
*/
|
|
47
85
|
export function bunSqliteStorage(database: Database = new Database(':memory:')): BunSqliteStorage {
|
|
48
86
|
database.run('PRAGMA foreign_keys = ON')
|
|
@@ -54,19 +92,8 @@ export function bunSqliteStorage(database: Database = new Database(':memory:')):
|
|
|
54
92
|
if (TRANSACTION_CONTROL.test(query)) {
|
|
55
93
|
throw new Error('sql.exec runs no transaction statement: use storage.transaction() or storage.transactionSync()')
|
|
56
94
|
}
|
|
57
|
-
const values = bindings.map(bunBinding)
|
|
58
95
|
const before = totalChanges()
|
|
59
|
-
const
|
|
60
|
-
let rows: Record<string, SqliteValue>[] = []
|
|
61
|
-
try {
|
|
62
|
-
if (statement.columnNames.length > 0) {
|
|
63
|
-
rows = statement.all(...values).map(storageRow)
|
|
64
|
-
} else {
|
|
65
|
-
database.run(query, values)
|
|
66
|
-
}
|
|
67
|
-
} finally {
|
|
68
|
-
statement.finalize()
|
|
69
|
-
}
|
|
96
|
+
const rows = execStatements(database, query, bindings.map(bunBinding)).map(storageRow)
|
|
70
97
|
const rowsWritten = totalChanges() - before
|
|
71
98
|
return { toArray: () => rows, rowsWritten }
|
|
72
99
|
}
|
|
@@ -1,13 +1,21 @@
|
|
|
1
|
-
import { Database } from 'bun:sqlite';
|
|
1
|
+
import { Database, type SQLQueryBindings } from 'bun:sqlite';
|
|
2
2
|
import type { SqliteStorage } from './sqlite';
|
|
3
3
|
export interface BunSqliteStorage extends SqliteStorage {
|
|
4
4
|
/** Runs `fn` in one transaction of its own, as a Durable Object's `storage.transactionSync` does. */
|
|
5
5
|
transactionSync<T>(fn: () => T): T;
|
|
6
6
|
}
|
|
7
|
+
/**
|
|
8
|
+
* Runs each statement of `query` in turn and answers the rows of the last one, as a Durable Object's `sql.exec` does.
|
|
9
|
+
* bun:sqlite's `prepare` compiles only the first statement of a text, and its column count cannot tell a migration from a
|
|
10
|
+
* read (`ALTER TABLE … ADD COLUMN … CHECK (…)` reports one column), so the text is split at the end of each compiled
|
|
11
|
+
* statement. `bindings` belong to a text of one statement. Refuses what workerd's SQLite refuses: more than 100 result
|
|
12
|
+
* columns or parameters in one statement.
|
|
13
|
+
*/
|
|
14
|
+
export declare function execStatements(database: Database, query: string, bindings?: readonly SQLQueryBindings[]): Record<string, unknown>[];
|
|
7
15
|
/**
|
|
8
16
|
* `database` (a fresh `:memory:` one by default) with foreign keys enforced, as a Durable Object enforces them. One
|
|
9
17
|
* `transaction` runs at a time; one started inside a callback nests in a savepoint, as workerd nests it, and its
|
|
10
|
-
* siblings wait for each other. A statement outside one runs at once. A text of several statements runs
|
|
11
|
-
*
|
|
18
|
+
* siblings wait for each other. A statement outside one runs at once. A text of several statements runs each of them and
|
|
19
|
+
* answers the rows of the last (`execStatements`).
|
|
12
20
|
*/
|
|
13
21
|
export declare function bunSqliteStorage(database?: Database): BunSqliteStorage;
|