@statewalker/db-sqlite-do 0.1.1 → 0.1.3
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/README.md +12 -3
- package/dist/fake.d.mts.map +1 -1
- package/dist/fake.mjs +8 -2
- package/dist/fake.mjs.map +1 -1
- package/package.json +12 -7
- package/src/fake-do-storage.ts +15 -2
- package/src/do-sqlite-db.test.ts +0 -79
package/README.md
CHANGED
|
@@ -61,11 +61,11 @@ const db = newDoSqliteDb(newFakeDoStorage());
|
|
|
61
61
|
`pnpm test:do` (from the repository root) builds the workspace, starts `celld dev` on
|
|
62
62
|
`packages/db-sqlite-do/test-do/` with a clean state on a free port, and runs, inside a real
|
|
63
63
|
Durable Object, the shared `Db` conformance suite (through a small HTTP bridge), every scenario
|
|
64
|
-
listed in `test-do/scenarios.ts` (the SQLite transaction scenarios a DO can meet,
|
|
65
|
-
vector and n-gram smoke scenarios of `@statewalker/db-sqlite-index`), and a platform probe whose expectations the `./fake` must also meet. It
|
|
64
|
+
listed in `test-do/scenarios.ts` (the SQLite transaction scenarios a DO can meet), and a platform probe whose expectations the `./fake` must also meet. It
|
|
66
65
|
needs celld 0.6.x on `PATH`; with another version it stops at once with
|
|
67
66
|
`test:do needs celld 0.6.x on PATH, found "<version>"`. A celld that prints no `ready` line within 60 seconds fails
|
|
68
|
-
the run with `celld dev was not ready within 60 s`. It stops only the celld process it started. It is not part of `pnpm test`: run it before a release.
|
|
67
|
+
the run with `celld dev was not ready within 60 s`. It stops only the celld process it started. It is not part of `pnpm test`: run it before a release. The same root `pnpm test:do` also runs
|
|
68
|
+
`@statewalker/db-sqlite-index`'s smoke scenarios in its own `test-do/`.
|
|
69
69
|
|
|
70
70
|
## Internals
|
|
71
71
|
|
|
@@ -88,6 +88,15 @@ the run with `celld dev was not ready within 60 s`. It stops only the celld proc
|
|
|
88
88
|
retried rather than failed, so keep each write step small.
|
|
89
89
|
- **BLOBs are converted both ways.** Typed-array parameters are bound as `ArrayBuffer`;
|
|
90
90
|
`ArrayBuffer` values read back become `Uint8Array`, as on every other SQLite driver.
|
|
91
|
+
- **Input handling comes from `@statewalker/db-sqlite`.** `query` rejects a second statement and
|
|
92
|
+
empty SQL before `sql.exec` sees it, and `true`/`false`, `undefined` and `Date` parameters bind
|
|
93
|
+
as `1`/`0`, `NULL` and ISO 8601 text, as on every SQLite driver. How DO SQLite itself binds a
|
|
94
|
+
`bigint` and reads an integer above 2^53 is not probed yet: the fake reads such an integer as a
|
|
95
|
+
`bigint` (as the other drivers do), and `test:do` skips that scenario
|
|
96
|
+
(`DO_UNPROBED_SCENARIOS` in `test-do/unsupported-scenarios.ts`). The real-DO run of
|
|
97
|
+
`runDbConformance` also skips the scenarios its harness cannot carry, since each call is a
|
|
98
|
+
separate HTTP request with a JSON body (call order, close during a call, BLOB and bigint
|
|
99
|
+
round trips: `DO_REMOTE_UNSUPPORTED_DB_SCENARIOS`); they run on the fake.
|
|
91
100
|
- **`close()` releases nothing.** The storage belongs to the DO. It only makes later calls on this
|
|
92
101
|
Db reject with `db-sqlite: the database is closed`.
|
|
93
102
|
- **Use one Db per DO.** Calls are queued inside one Db; direct `ctx.storage.sql` calls made
|
package/dist/fake.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fake.d.mts","names":[],"sources":["../src/fake-do-storage.ts"],"mappings":";;KAGY,gBAAgB;EAAiB;;;;;;;;
|
|
1
|
+
{"version":3,"file":"fake.d.mts","names":[],"sources":["../src/fake-do-storage.ts"],"mappings":";;KAGY,gBAAgB;EAAiB;;;;;;;;wBA2B7B,oBAAoB"}
|
package/dist/fake.mjs
CHANGED
|
@@ -5,6 +5,8 @@ import { DatabaseSync } from "node:sqlite";
|
|
|
5
5
|
* store, such as `new Uint8Array(new ArrayBuffer(0))`, as NULL; the platform stores an empty BLOB.
|
|
6
6
|
*/
|
|
7
7
|
const EMPTY_BLOB = (/* @__PURE__ */ new Uint8Array(1)).subarray(0, 0);
|
|
8
|
+
/** Integers read as numbers, as on the platform; one above 2^53 stays a bigint (unprobed there). */
|
|
9
|
+
const fromInteger = (value) => typeof value === "bigint" && value >= BigInt(Number.MIN_SAFE_INTEGER) && value <= BigInt(Number.MAX_SAFE_INTEGER) ? Number(value) : value;
|
|
8
10
|
/** Limits probed on celld's DO SQLite (see test-do/platform-probe.ts). */
|
|
9
11
|
const MAX_BOUND_PARAMETERS = 100;
|
|
10
12
|
const MAX_VALUE_BYTES = 2e6;
|
|
@@ -25,6 +27,7 @@ function newFakeDoStorage() {
|
|
|
25
27
|
if (value.byteLength > MAX_VALUE_BYTES) throw tooBig();
|
|
26
28
|
return value.byteLength === 0 ? EMPTY_BLOB : new Uint8Array(value);
|
|
27
29
|
}
|
|
30
|
+
if (typeof value === "number" && Number.isSafeInteger(value)) return BigInt(value);
|
|
28
31
|
return value;
|
|
29
32
|
};
|
|
30
33
|
return {
|
|
@@ -34,8 +37,11 @@ function newFakeDoStorage() {
|
|
|
34
37
|
const statement = db.prepare(query);
|
|
35
38
|
let rows = [];
|
|
36
39
|
if (bindings.length === 0 && statement.columns().length === 0) db.exec(query);
|
|
37
|
-
else
|
|
38
|
-
|
|
40
|
+
else {
|
|
41
|
+
statement.setReadBigInts(true);
|
|
42
|
+
rows = statement.all(...bindings.map(bind));
|
|
43
|
+
}
|
|
44
|
+
const out = rows.map((row) => Object.fromEntries(Object.entries(row).map(([key, value]) => [key, value instanceof Uint8Array ? value.slice().buffer : fromInteger(value)])));
|
|
39
45
|
return { toArray: () => out };
|
|
40
46
|
} },
|
|
41
47
|
async transaction(closure) {
|
package/dist/fake.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fake.mjs","names":[],"sources":["../src/fake-do-storage.ts"],"sourcesContent":["import { DatabaseSync, type SQLInputValue } from \"node:sqlite\";\nimport type { DoSqlStorage } from \"./do-sqlite-db.js\";\n\nexport type FakeDoStorage = DoSqlStorage & { close(): void };\n\n/**\n * An empty view over a real buffer. node:sqlite (Node 22) binds a view whose buffer has no backing\n * store, such as `new Uint8Array(new ArrayBuffer(0))`, as NULL; the platform stores an empty BLOB.\n */\nconst EMPTY_BLOB = new Uint8Array(1).subarray(0, 0);\n\n/** Limits probed on celld's DO SQLite (see test-do/platform-probe.ts). */\nconst MAX_BOUND_PARAMETERS = 100;\nconst MAX_VALUE_BYTES = 2_000_000;\nconst NOT_AUTHORIZED = /^\\s*(BEGIN|COMMIT|END|ROLLBACK|SAVEPOINT|RELEASE|PRAGMA)\\b/i;\n\n/**\n * An in-memory stand-in for a Durable Object's `ctx.storage` on Node.js's `node:sqlite`, with the\n * DO limits: no SQL-level transactions or PRAGMA, at most 100 bound parameters, BLOBs bound and\n * read as ArrayBuffer, and bound strings (UTF-8 bytes) and BLOBs at most ~2 MB. `transaction` runs the closure between BEGIN and\n * COMMIT/ROLLBACK and, like the platform, does not queue concurrent calls.\n */\nexport function newFakeDoStorage(): FakeDoStorage {\n const db = new DatabaseSync(\":memory:\");\n const bind = (value: unknown): SQLInputValue => {\n if (ArrayBuffer.isView(value)) {\n throw new TypeError(\"fake DO storage: bind a BLOB as an ArrayBuffer, not a typed array\");\n }\n const tooBig = () => new Error(\"SQL error: bind sync KV cursor: string or blob too big\");\n if (typeof value === \"string\" && Buffer.byteLength(value) > MAX_VALUE_BYTES) throw tooBig();\n if (value instanceof ArrayBuffer) {\n if (value.byteLength > MAX_VALUE_BYTES) throw tooBig();\n return value.byteLength === 0 ? EMPTY_BLOB : new Uint8Array(value);\n }\n return value as SQLInputValue;\n };\n return {\n sql: {\n exec(query: string, ...bindings: unknown[]) {\n if (NOT_AUTHORIZED.test(query))\n throw new Error(\"SQL error: prepare SQL cursor: not authorized\");\n if (bindings.length > MAX_BOUND_PARAMETERS) {\n throw new Error(\"SQL error: prepare SQL cursor: too many SQL variables\");\n }\n const statement = db.prepare(query);\n let rows: Record<string, unknown>[] = [];\n // prepare() compiles only the first statement: run row-less, parameter-less text with exec().\n if (bindings.length === 0 && statement.columns().length === 0) db.exec(query);\n else rows = statement.all(...bindings.map(bind)) as Record<string, unknown>[];\n const out = rows.map((row) =>\n Object.fromEntries(\n Object.entries(row).map(([key, value]) => [\n key,\n value instanceof Uint8Array ? value.slice().buffer : value,\n ]),\n ),\n );\n return { toArray: () => out };\n },\n },\n async transaction<T>(closure: () => Promise<T>): Promise<T> {\n db.exec(\"BEGIN\");\n try {\n const result = await closure();\n db.exec(\"COMMIT\");\n return result;\n } catch (error) {\n // Like the platform: when SQLite already ended the transaction (ON CONFLICT ROLLBACK), the\n // rollback fails and its error replaces the callback's.\n if (!db.isTransaction) {\n throw new Error(\n \"a transaction rollback failed: cannot rollback - no transaction is active\",\n );\n }\n db.exec(\"ROLLBACK\");\n throw error;\n }\n },\n close: () => db.close(),\n };\n}\n"],"mappings":";;;;;;AASA,MAAM,8BAAa,IAAI,WAAW,CAAC,EAAA,CAAE,SAAS,GAAG,CAAC;;AAGlD,MAAM,uBAAuB;AAC7B,MAAM,kBAAkB;AACxB,MAAM,iBAAiB;;;;;;;AAQvB,SAAgB,mBAAkC;CAChD,MAAM,KAAK,IAAI,aAAa,UAAU;CACtC,MAAM,QAAQ,UAAkC;EAC9C,IAAI,YAAY,OAAO,KAAK,GAC1B,MAAM,IAAI,UAAU,mEAAmE;EAEzF,MAAM,+BAAe,IAAI,MAAM,wDAAwD;EACvF,IAAI,OAAO,UAAU,YAAY,OAAO,WAAW,KAAK,IAAI,iBAAiB,MAAM,OAAO;EAC1F,IAAI,iBAAiB,aAAa;GAChC,IAAI,MAAM,aAAa,iBAAiB,MAAM,OAAO;GACrD,OAAO,MAAM,eAAe,IAAI,aAAa,IAAI,WAAW,KAAK;EACnE;
|
|
1
|
+
{"version":3,"file":"fake.mjs","names":[],"sources":["../src/fake-do-storage.ts"],"sourcesContent":["import { DatabaseSync, type SQLInputValue } from \"node:sqlite\";\nimport type { DoSqlStorage } from \"./do-sqlite-db.js\";\n\nexport type FakeDoStorage = DoSqlStorage & { close(): void };\n\n/**\n * An empty view over a real buffer. node:sqlite (Node 22) binds a view whose buffer has no backing\n * store, such as `new Uint8Array(new ArrayBuffer(0))`, as NULL; the platform stores an empty BLOB.\n */\nconst EMPTY_BLOB = new Uint8Array(1).subarray(0, 0);\n\n/** Integers read as numbers, as on the platform; one above 2^53 stays a bigint (unprobed there). */\nconst fromInteger = (value: unknown): unknown =>\n typeof value === \"bigint\" &&\n value >= BigInt(Number.MIN_SAFE_INTEGER) &&\n value <= BigInt(Number.MAX_SAFE_INTEGER)\n ? Number(value)\n : value;\n\n/** Limits probed on celld's DO SQLite (see test-do/platform-probe.ts). */\nconst MAX_BOUND_PARAMETERS = 100;\nconst MAX_VALUE_BYTES = 2_000_000;\nconst NOT_AUTHORIZED = /^\\s*(BEGIN|COMMIT|END|ROLLBACK|SAVEPOINT|RELEASE|PRAGMA)\\b/i;\n\n/**\n * An in-memory stand-in for a Durable Object's `ctx.storage` on Node.js's `node:sqlite`, with the\n * DO limits: no SQL-level transactions or PRAGMA, at most 100 bound parameters, BLOBs bound and\n * read as ArrayBuffer, and bound strings (UTF-8 bytes) and BLOBs at most ~2 MB. `transaction` runs the closure between BEGIN and\n * COMMIT/ROLLBACK and, like the platform, does not queue concurrent calls.\n */\nexport function newFakeDoStorage(): FakeDoStorage {\n const db = new DatabaseSync(\":memory:\");\n const bind = (value: unknown): SQLInputValue => {\n if (ArrayBuffer.isView(value)) {\n throw new TypeError(\"fake DO storage: bind a BLOB as an ArrayBuffer, not a typed array\");\n }\n const tooBig = () => new Error(\"SQL error: bind sync KV cursor: string or blob too big\");\n if (typeof value === \"string\" && Buffer.byteLength(value) > MAX_VALUE_BYTES) throw tooBig();\n if (value instanceof ArrayBuffer) {\n if (value.byteLength > MAX_VALUE_BYTES) throw tooBig();\n return value.byteLength === 0 ? EMPTY_BLOB : new Uint8Array(value);\n }\n // node:sqlite binds every number as REAL; bind integer-valued ones as INTEGER.\n if (typeof value === \"number\" && Number.isSafeInteger(value)) return BigInt(value);\n return value as SQLInputValue;\n };\n return {\n sql: {\n exec(query: string, ...bindings: unknown[]) {\n if (NOT_AUTHORIZED.test(query))\n throw new Error(\"SQL error: prepare SQL cursor: not authorized\");\n if (bindings.length > MAX_BOUND_PARAMETERS) {\n throw new Error(\"SQL error: prepare SQL cursor: too many SQL variables\");\n }\n const statement = db.prepare(query);\n let rows: Record<string, unknown>[] = [];\n // prepare() compiles only the first statement: run row-less, parameter-less text with exec().\n if (bindings.length === 0 && statement.columns().length === 0) db.exec(query);\n else {\n statement.setReadBigInts(true); // an integer above 2^53 reads back exactly, as a bigint\n rows = statement.all(...bindings.map(bind)) as Record<string, unknown>[];\n }\n const out = rows.map((row) =>\n Object.fromEntries(\n Object.entries(row).map(([key, value]) => [\n key,\n value instanceof Uint8Array ? value.slice().buffer : fromInteger(value),\n ]),\n ),\n );\n return { toArray: () => out };\n },\n },\n async transaction<T>(closure: () => Promise<T>): Promise<T> {\n db.exec(\"BEGIN\");\n try {\n const result = await closure();\n db.exec(\"COMMIT\");\n return result;\n } catch (error) {\n // Like the platform: when SQLite already ended the transaction (ON CONFLICT ROLLBACK), the\n // rollback fails and its error replaces the callback's.\n if (!db.isTransaction) {\n throw new Error(\n \"a transaction rollback failed: cannot rollback - no transaction is active\",\n );\n }\n db.exec(\"ROLLBACK\");\n throw error;\n }\n },\n close: () => db.close(),\n };\n}\n"],"mappings":";;;;;;AASA,MAAM,8BAAa,IAAI,WAAW,CAAC,EAAA,CAAE,SAAS,GAAG,CAAC;;AAGlD,MAAM,eAAe,UACnB,OAAO,UAAU,YACjB,SAAS,OAAO,OAAO,gBAAgB,KACvC,SAAS,OAAO,OAAO,gBAAgB,IACnC,OAAO,KAAK,IACZ;;AAGN,MAAM,uBAAuB;AAC7B,MAAM,kBAAkB;AACxB,MAAM,iBAAiB;;;;;;;AAQvB,SAAgB,mBAAkC;CAChD,MAAM,KAAK,IAAI,aAAa,UAAU;CACtC,MAAM,QAAQ,UAAkC;EAC9C,IAAI,YAAY,OAAO,KAAK,GAC1B,MAAM,IAAI,UAAU,mEAAmE;EAEzF,MAAM,+BAAe,IAAI,MAAM,wDAAwD;EACvF,IAAI,OAAO,UAAU,YAAY,OAAO,WAAW,KAAK,IAAI,iBAAiB,MAAM,OAAO;EAC1F,IAAI,iBAAiB,aAAa;GAChC,IAAI,MAAM,aAAa,iBAAiB,MAAM,OAAO;GACrD,OAAO,MAAM,eAAe,IAAI,aAAa,IAAI,WAAW,KAAK;EACnE;EAEA,IAAI,OAAO,UAAU,YAAY,OAAO,cAAc,KAAK,GAAG,OAAO,OAAO,KAAK;EACjF,OAAO;CACT;CACA,OAAO;EACL,KAAK,EACH,KAAK,OAAe,GAAG,UAAqB;GAC1C,IAAI,eAAe,KAAK,KAAK,GAC3B,MAAM,IAAI,MAAM,+CAA+C;GACjE,IAAI,SAAS,SAAS,sBACpB,MAAM,IAAI,MAAM,uDAAuD;GAEzE,MAAM,YAAY,GAAG,QAAQ,KAAK;GAClC,IAAI,OAAkC,CAAC;GAEvC,IAAI,SAAS,WAAW,KAAK,UAAU,QAAQ,CAAC,CAAC,WAAW,GAAG,GAAG,KAAK,KAAK;QACvE;IACH,UAAU,eAAe,IAAI;IAC7B,OAAO,UAAU,IAAI,GAAG,SAAS,IAAI,IAAI,CAAC;GAC5C;GACA,MAAM,MAAM,KAAK,KAAK,QACpB,OAAO,YACL,OAAO,QAAQ,GAAG,CAAC,CAAC,KAAK,CAAC,KAAK,WAAW,CACxC,KACA,iBAAiB,aAAa,MAAM,MAAM,CAAC,CAAC,SAAS,YAAY,KAAK,CACxE,CAAC,CACH,CACF;GACA,OAAO,EAAE,eAAe,IAAI;EAC9B,EACF;EACA,MAAM,YAAe,SAAuC;GAC1D,GAAG,KAAK,OAAO;GACf,IAAI;IACF,MAAM,SAAS,MAAM,QAAQ;IAC7B,GAAG,KAAK,QAAQ;IAChB,OAAO;GACT,SAAS,OAAO;IAGd,IAAI,CAAC,GAAG,eACN,MAAM,IAAI,MACR,2EACF;IAEF,GAAG,KAAK,UAAU;IAClB,MAAM;GACR;EACF;EACA,aAAa,GAAG,MAAM;CACxB;AACF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@statewalker/db-sqlite-do",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"private": false,
|
|
5
5
|
"type": "module",
|
|
6
6
|
"description": "Durable Object SQLite (ctx.storage) driver for @statewalker/db-api, with transactions through @statewalker/db-sqlite and a node:sqlite fake for tests.",
|
|
@@ -22,24 +22,28 @@
|
|
|
22
22
|
".": {
|
|
23
23
|
"source": "./src/index.ts",
|
|
24
24
|
"types": "./dist/index.d.mts",
|
|
25
|
-
"import": "./dist/index.mjs"
|
|
25
|
+
"import": "./dist/index.mjs",
|
|
26
|
+
"default": "./dist/index.mjs"
|
|
26
27
|
},
|
|
27
28
|
"./fake": {
|
|
28
29
|
"source": "./src/fake.ts",
|
|
29
30
|
"types": "./dist/fake.d.mts",
|
|
30
|
-
"import": "./dist/fake.mjs"
|
|
31
|
+
"import": "./dist/fake.mjs",
|
|
32
|
+
"default": "./dist/fake.mjs"
|
|
31
33
|
}
|
|
32
34
|
},
|
|
33
35
|
"files": [
|
|
34
36
|
"dist",
|
|
35
|
-
"src"
|
|
37
|
+
"src",
|
|
38
|
+
"!src/**/*.test.ts"
|
|
36
39
|
],
|
|
37
40
|
"dependencies": {
|
|
38
|
-
"@statewalker/db-
|
|
39
|
-
"@statewalker/db-
|
|
41
|
+
"@statewalker/db-sqlite": "^0.1.3",
|
|
42
|
+
"@statewalker/db-api": "^0.1.7"
|
|
40
43
|
},
|
|
41
44
|
"devDependencies": {
|
|
42
45
|
"@types/node": "^26.6.4",
|
|
46
|
+
"esbuild": "^0.28.2",
|
|
43
47
|
"rimraf": "^6.1.3",
|
|
44
48
|
"tsdown": "^0.23.0",
|
|
45
49
|
"typescript": "^7.0.2",
|
|
@@ -61,6 +65,7 @@
|
|
|
61
65
|
"typecheck": "tsc --noEmit",
|
|
62
66
|
"clean": "rimraf dist",
|
|
63
67
|
"lint": "biome check --write .",
|
|
64
|
-
"format": "biome format --write ."
|
|
68
|
+
"format": "biome format --write .",
|
|
69
|
+
"typecheck:tests": "tsc --noEmit -p tsconfig.test.json"
|
|
65
70
|
}
|
|
66
71
|
}
|
package/src/fake-do-storage.ts
CHANGED
|
@@ -9,6 +9,14 @@ export type FakeDoStorage = DoSqlStorage & { close(): void };
|
|
|
9
9
|
*/
|
|
10
10
|
const EMPTY_BLOB = new Uint8Array(1).subarray(0, 0);
|
|
11
11
|
|
|
12
|
+
/** Integers read as numbers, as on the platform; one above 2^53 stays a bigint (unprobed there). */
|
|
13
|
+
const fromInteger = (value: unknown): unknown =>
|
|
14
|
+
typeof value === "bigint" &&
|
|
15
|
+
value >= BigInt(Number.MIN_SAFE_INTEGER) &&
|
|
16
|
+
value <= BigInt(Number.MAX_SAFE_INTEGER)
|
|
17
|
+
? Number(value)
|
|
18
|
+
: value;
|
|
19
|
+
|
|
12
20
|
/** Limits probed on celld's DO SQLite (see test-do/platform-probe.ts). */
|
|
13
21
|
const MAX_BOUND_PARAMETERS = 100;
|
|
14
22
|
const MAX_VALUE_BYTES = 2_000_000;
|
|
@@ -32,6 +40,8 @@ export function newFakeDoStorage(): FakeDoStorage {
|
|
|
32
40
|
if (value.byteLength > MAX_VALUE_BYTES) throw tooBig();
|
|
33
41
|
return value.byteLength === 0 ? EMPTY_BLOB : new Uint8Array(value);
|
|
34
42
|
}
|
|
43
|
+
// node:sqlite binds every number as REAL; bind integer-valued ones as INTEGER.
|
|
44
|
+
if (typeof value === "number" && Number.isSafeInteger(value)) return BigInt(value);
|
|
35
45
|
return value as SQLInputValue;
|
|
36
46
|
};
|
|
37
47
|
return {
|
|
@@ -46,12 +56,15 @@ export function newFakeDoStorage(): FakeDoStorage {
|
|
|
46
56
|
let rows: Record<string, unknown>[] = [];
|
|
47
57
|
// prepare() compiles only the first statement: run row-less, parameter-less text with exec().
|
|
48
58
|
if (bindings.length === 0 && statement.columns().length === 0) db.exec(query);
|
|
49
|
-
else
|
|
59
|
+
else {
|
|
60
|
+
statement.setReadBigInts(true); // an integer above 2^53 reads back exactly, as a bigint
|
|
61
|
+
rows = statement.all(...bindings.map(bind)) as Record<string, unknown>[];
|
|
62
|
+
}
|
|
50
63
|
const out = rows.map((row) =>
|
|
51
64
|
Object.fromEntries(
|
|
52
65
|
Object.entries(row).map(([key, value]) => [
|
|
53
66
|
key,
|
|
54
|
-
value instanceof Uint8Array ? value.slice().buffer : value,
|
|
67
|
+
value instanceof Uint8Array ? value.slice().buffer : fromInteger(value),
|
|
55
68
|
]),
|
|
56
69
|
),
|
|
57
70
|
);
|
package/src/do-sqlite-db.test.ts
DELETED
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
import { inTransaction } from "@statewalker/db-sqlite";
|
|
2
|
-
import { runDbConformance, runSqliteTransactionConformance } from "@statewalker/db-tests";
|
|
3
|
-
import { describe, expect, it } from "vitest";
|
|
4
|
-
import { PROBE_EXPECTATIONS, probeDoSqlite } from "../test-do/platform-probe.js";
|
|
5
|
-
import { DO_UNSUPPORTED_SCENARIOS } from "../test-do/unsupported-scenarios.js";
|
|
6
|
-
import { newDoSqliteDb } from "./do-sqlite-db.js";
|
|
7
|
-
import { newFakeDoStorage } from "./fake-do-storage.js";
|
|
8
|
-
|
|
9
|
-
// Same suites as every other SQLite driver, on the node:sqlite fake of ctx.storage.
|
|
10
|
-
runDbConformance(async () => newDoSqliteDb(newFakeDoStorage()), {
|
|
11
|
-
placeholder: "?",
|
|
12
|
-
skipFilePersistence: true,
|
|
13
|
-
});
|
|
14
|
-
runSqliteTransactionConformance(async () => newDoSqliteDb(newFakeDoStorage()), {
|
|
15
|
-
skip: DO_UNSUPPORTED_SCENARIOS,
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
describe("newDoSqliteDb", () => {
|
|
19
|
-
it("fails the transaction with the platform's rollback error when SQLite already rolled back", async () => {
|
|
20
|
-
const db = newDoSqliteDb(newFakeDoStorage());
|
|
21
|
-
await db.exec("CREATE TABLE u (k TEXT UNIQUE ON CONFLICT ROLLBACK)");
|
|
22
|
-
await db.exec("INSERT INTO u VALUES ('a')");
|
|
23
|
-
await expect(
|
|
24
|
-
inTransaction(db, async (tx) => {
|
|
25
|
-
await tx.query("INSERT INTO u VALUES ('b')");
|
|
26
|
-
await tx.query("INSERT INTO u VALUES ('a')");
|
|
27
|
-
}),
|
|
28
|
-
).rejects.toThrow(/no transaction is active/);
|
|
29
|
-
expect(await db.query("SELECT k FROM u")).toEqual([{ k: "a" }]);
|
|
30
|
-
await db.close();
|
|
31
|
-
});
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
describe("newFakeDoStorage", () => {
|
|
35
|
-
// The same probe runs on real DO SQLite in `test:do`; both must meet the same expectations.
|
|
36
|
-
it("matches the probed Durable Object SQLite behaviour", () => {
|
|
37
|
-
const storage = newFakeDoStorage();
|
|
38
|
-
try {
|
|
39
|
-
const result = probeDoSqlite(storage);
|
|
40
|
-
for (const [name, want] of Object.entries(PROBE_EXPECTATIONS)) {
|
|
41
|
-
if (want === "ok") expect(result[name], name).toBe("ok");
|
|
42
|
-
else expect(result[name], name).toMatch(want);
|
|
43
|
-
}
|
|
44
|
-
} finally {
|
|
45
|
-
storage.close();
|
|
46
|
-
}
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
it("stores an empty BLOB as a BLOB through the driver (not NULL, on any Node)", async () => {
|
|
50
|
-
const db = newDoSqliteDb(newFakeDoStorage());
|
|
51
|
-
await db.exec("CREATE TABLE e (k INTEGER PRIMARY KEY, d BLOB)");
|
|
52
|
-
await db.query("INSERT INTO e VALUES (1, ?), (2, ?)", [
|
|
53
|
-
new Uint8Array(0),
|
|
54
|
-
new Uint8Array(new ArrayBuffer(0)),
|
|
55
|
-
]);
|
|
56
|
-
expect(await db.query("SELECT typeof(d) AS t, length(d) AS n FROM e ORDER BY k")).toEqual([
|
|
57
|
-
{ t: "blob", n: 0 },
|
|
58
|
-
{ t: "blob", n: 0 },
|
|
59
|
-
]);
|
|
60
|
-
const back = await db.query<{ d: unknown }>("SELECT d FROM e ORDER BY k");
|
|
61
|
-
for (const { d } of back) {
|
|
62
|
-
expect(d).toBeInstanceOf(Uint8Array);
|
|
63
|
-
expect((d as Uint8Array).byteLength).toBe(0);
|
|
64
|
-
}
|
|
65
|
-
await db.close();
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
it("rejects a typed array bound as a BLOB, as the driver must pass an ArrayBuffer", () => {
|
|
69
|
-
const storage = newFakeDoStorage();
|
|
70
|
-
try {
|
|
71
|
-
storage.sql.exec("CREATE TABLE b (d BLOB)");
|
|
72
|
-
expect(() => storage.sql.exec("INSERT INTO b VALUES (?)", new Uint8Array([1]))).toThrow(
|
|
73
|
-
/ArrayBuffer/,
|
|
74
|
-
);
|
|
75
|
-
} finally {
|
|
76
|
-
storage.close();
|
|
77
|
-
}
|
|
78
|
-
});
|
|
79
|
-
});
|