@statewalker/db-sqlite-node 0.3.1 → 0.3.2
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 +9 -4
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +12 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +7 -5
- package/src/node-sqlite-db.ts +16 -2
- package/src/node-sqlite-db.test.ts +0 -87
package/README.md
CHANGED
|
@@ -47,11 +47,16 @@ await db.close();
|
|
|
47
47
|
- **Calls are queued.** The Db runs one call at a time in call order, and opens transactions
|
|
48
48
|
with `BEGIN IMMEDIATE` (see `@statewalker/db-sqlite`). `node:sqlite` is synchronous, so a long
|
|
49
49
|
statement blocks the event loop while it runs.
|
|
50
|
-
- **`query` runs one statement.**
|
|
51
|
-
|
|
50
|
+
- **`query` runs one statement.** SQL with a second statement rejects before anything runs, and
|
|
51
|
+
empty SQL rejects; `true`/`false`, `undefined` and `Date` parameters bind as `1`/`0`, `NULL` and
|
|
52
|
+
ISO 8601 text. All of it comes from `@statewalker/db-sqlite`, the same on every SQLite driver.
|
|
53
|
+
Use `exec` for multi-statement scripts.
|
|
52
54
|
- **Rows are plain objects with a `null` prototype**, BLOBs are `Uint8Array`, integers are
|
|
53
|
-
JavaScript numbers
|
|
54
|
-
|
|
55
|
+
JavaScript numbers, except one above 2^53 (or below -2^53), which comes back as an exact
|
|
56
|
+
`bigint`. Statements read integers with `setReadBigInts(true)`.
|
|
57
|
+
- **Integer-valued numbers bind as INTEGER.** node:sqlite binds every JS number as a REAL, so
|
|
58
|
+
the driver passes a safe integer as a `bigint`: `typeof(?)` of `5` is `'integer'`, and `5` in a
|
|
59
|
+
TEXT column reads back as `'5'`, as on the other SQLite drivers. Other numbers bind as REAL.
|
|
55
60
|
- **Errors are `node:sqlite`'s.** A path in a missing directory rejects with
|
|
56
61
|
`unable to open database file`; bad SQL rejects with `near "...": syntax error`; a call after
|
|
57
62
|
`close()` rejects with `db-sqlite: the database is closed`.
|
package/dist/index.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.mts","names":["DbOptions","Db"],"sources":["../src/node-sqlite-db.ts"],"mappings":";;;;;;;
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":["DbOptions","Db"],"sources":["../src/node-sqlite-db.ts"],"mappings":";;;;;;;wBAmBsB,gBAAgB,UAAUA,cAAY,QAAQC"}
|
package/dist/index.mjs
CHANGED
|
@@ -2,6 +2,13 @@ import { DatabaseSync } from "node:sqlite";
|
|
|
2
2
|
import { newSerialisedSqliteDb } from "@statewalker/db-sqlite";
|
|
3
3
|
//#region src/node-sqlite-db.ts
|
|
4
4
|
/**
|
|
5
|
+
* node:sqlite binds every JS number as a REAL, so `5` would be stored as `5.0` in a TEXT column.
|
|
6
|
+
* Bind integer-valued numbers as bigint, which it binds as INTEGER, like the other drivers do.
|
|
7
|
+
*/
|
|
8
|
+
function toNodeParam(value) {
|
|
9
|
+
return typeof value === "number" && Number.isSafeInteger(value) ? BigInt(value) : value;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
5
12
|
* Create a SQLite {@link Db} on Node.js's built-in `node:sqlite`.
|
|
6
13
|
*
|
|
7
14
|
* @param options.path Database file. Omit for an in-memory database.
|
|
@@ -9,7 +16,11 @@ import { newSerialisedSqliteDb } from "@statewalker/db-sqlite";
|
|
|
9
16
|
async function newNodeSqliteDb(options) {
|
|
10
17
|
const db = new DatabaseSync(options?.path ?? ":memory:");
|
|
11
18
|
return newSerialisedSqliteDb({
|
|
12
|
-
all: (sql, params) =>
|
|
19
|
+
all: (sql, params) => {
|
|
20
|
+
const statement = db.prepare(sql);
|
|
21
|
+
statement.setReadBigInts(true);
|
|
22
|
+
return statement.all(...params.map(toNodeParam));
|
|
23
|
+
},
|
|
13
24
|
exec: (sql) => db.exec(sql),
|
|
14
25
|
close: () => db.close()
|
|
15
26
|
});
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":[],"sources":["../src/node-sqlite-db.ts"],"sourcesContent":["import { DatabaseSync, type SQLInputValue } from \"node:sqlite\";\nimport type { Db, DbOptions } from \"@statewalker/db-api\";\nimport { newSerialisedSqliteDb } from \"@statewalker/db-sqlite\";\n\n/**\n * Create a SQLite {@link Db} on Node.js's built-in `node:sqlite`.\n *\n * @param options.path Database file. Omit for an in-memory database.\n */\nexport async function newNodeSqliteDb(options?: DbOptions): Promise<Db> {\n const db = new DatabaseSync(options?.path ?? \":memory:\");\n return newSerialisedSqliteDb({\n all: (sql, params)
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../src/node-sqlite-db.ts"],"sourcesContent":["import { DatabaseSync, type SQLInputValue } from \"node:sqlite\";\nimport type { Db, DbOptions } from \"@statewalker/db-api\";\nimport { newSerialisedSqliteDb } from \"@statewalker/db-sqlite\";\n\n/**\n * node:sqlite binds every JS number as a REAL, so `5` would be stored as `5.0` in a TEXT column.\n * Bind integer-valued numbers as bigint, which it binds as INTEGER, like the other drivers do.\n */\nfunction toNodeParam(value: unknown): SQLInputValue {\n return typeof value === \"number\" && Number.isSafeInteger(value)\n ? BigInt(value)\n : (value as SQLInputValue);\n}\n\n/**\n * Create a SQLite {@link Db} on Node.js's built-in `node:sqlite`.\n *\n * @param options.path Database file. Omit for an in-memory database.\n */\nexport async function newNodeSqliteDb(options?: DbOptions): Promise<Db> {\n const db = new DatabaseSync(options?.path ?? \":memory:\");\n return newSerialisedSqliteDb({\n all: (sql, params) => {\n // Integers as bigint, so one above 2^53 is read exactly; db-sqlite turns safe ones into numbers.\n const statement = db.prepare(sql);\n statement.setReadBigInts(true);\n return statement.all(...params.map(toNodeParam)) as Record<string, unknown>[];\n },\n exec: (sql) => db.exec(sql),\n close: () => db.close(),\n });\n}\n"],"mappings":";;;;;;;AAQA,SAAS,YAAY,OAA+B;CAClD,OAAO,OAAO,UAAU,YAAY,OAAO,cAAc,KAAK,IAC1D,OAAO,KAAK,IACX;AACP;;;;;;AAOA,eAAsB,gBAAgB,SAAkC;CACtE,MAAM,KAAK,IAAI,aAAa,SAAS,QAAQ,UAAU;CACvD,OAAO,sBAAsB;EAC3B,MAAM,KAAK,WAAW;GAEpB,MAAM,YAAY,GAAG,QAAQ,GAAG;GAChC,UAAU,eAAe,IAAI;GAC7B,OAAO,UAAU,IAAI,GAAG,OAAO,IAAI,WAAW,CAAC;EACjD;EACA,OAAO,QAAQ,GAAG,KAAK,GAAG;EAC1B,aAAa,GAAG,MAAM;CACxB,CAAC;AACH"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@statewalker/db-sqlite-node",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.2",
|
|
4
4
|
"private": false,
|
|
5
5
|
"type": "module",
|
|
6
6
|
"description": "SQLite driver for @statewalker/db-api on Node.js's built-in node:sqlite, with transactions through @statewalker/db-sqlite.",
|
|
@@ -22,16 +22,18 @@
|
|
|
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
|
},
|
|
28
29
|
"files": [
|
|
29
30
|
"dist",
|
|
30
|
-
"src"
|
|
31
|
+
"src",
|
|
32
|
+
"!src/**/*.test.ts"
|
|
31
33
|
],
|
|
32
34
|
"dependencies": {
|
|
33
|
-
"@statewalker/db-sqlite": "^0.1.
|
|
34
|
-
"@statewalker/db-api": "^0.1.
|
|
35
|
+
"@statewalker/db-sqlite": "^0.1.2",
|
|
36
|
+
"@statewalker/db-api": "^0.1.6"
|
|
35
37
|
},
|
|
36
38
|
"devDependencies": {
|
|
37
39
|
"@types/node": "^26.6.4",
|
package/src/node-sqlite-db.ts
CHANGED
|
@@ -2,6 +2,16 @@ import { DatabaseSync, type SQLInputValue } from "node:sqlite";
|
|
|
2
2
|
import type { Db, DbOptions } from "@statewalker/db-api";
|
|
3
3
|
import { newSerialisedSqliteDb } from "@statewalker/db-sqlite";
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* node:sqlite binds every JS number as a REAL, so `5` would be stored as `5.0` in a TEXT column.
|
|
7
|
+
* Bind integer-valued numbers as bigint, which it binds as INTEGER, like the other drivers do.
|
|
8
|
+
*/
|
|
9
|
+
function toNodeParam(value: unknown): SQLInputValue {
|
|
10
|
+
return typeof value === "number" && Number.isSafeInteger(value)
|
|
11
|
+
? BigInt(value)
|
|
12
|
+
: (value as SQLInputValue);
|
|
13
|
+
}
|
|
14
|
+
|
|
5
15
|
/**
|
|
6
16
|
* Create a SQLite {@link Db} on Node.js's built-in `node:sqlite`.
|
|
7
17
|
*
|
|
@@ -10,8 +20,12 @@ import { newSerialisedSqliteDb } from "@statewalker/db-sqlite";
|
|
|
10
20
|
export async function newNodeSqliteDb(options?: DbOptions): Promise<Db> {
|
|
11
21
|
const db = new DatabaseSync(options?.path ?? ":memory:");
|
|
12
22
|
return newSerialisedSqliteDb({
|
|
13
|
-
all: (sql, params) =>
|
|
14
|
-
|
|
23
|
+
all: (sql, params) => {
|
|
24
|
+
// Integers as bigint, so one above 2^53 is read exactly; db-sqlite turns safe ones into numbers.
|
|
25
|
+
const statement = db.prepare(sql);
|
|
26
|
+
statement.setReadBigInts(true);
|
|
27
|
+
return statement.all(...params.map(toNodeParam)) as Record<string, unknown>[];
|
|
28
|
+
},
|
|
15
29
|
exec: (sql) => db.exec(sql),
|
|
16
30
|
close: () => db.close(),
|
|
17
31
|
});
|
|
@@ -1,87 +0,0 @@
|
|
|
1
|
-
import type { Db } from "@statewalker/db-api";
|
|
2
|
-
import { runDbConformance, runSqliteTransactionConformance } from "@statewalker/db-tests";
|
|
3
|
-
import { afterEach, describe, expect, it } from "vitest";
|
|
4
|
-
import { newNodeSqliteDb } from "./node-sqlite-db.js";
|
|
5
|
-
|
|
6
|
-
// Common contract and SQLite transactions come from the shared suites; FTS5 stays here.
|
|
7
|
-
runDbConformance(newNodeSqliteDb, { placeholder: "?" });
|
|
8
|
-
runSqliteTransactionConformance(() => newNodeSqliteDb());
|
|
9
|
-
|
|
10
|
-
// Engine-specific behaviour kept per driver: FTS5 with the default, porter and trigram tokenizers.
|
|
11
|
-
describe("newNodeSqliteDb", () => {
|
|
12
|
-
let db: Db;
|
|
13
|
-
|
|
14
|
-
afterEach(async () => {
|
|
15
|
-
if (db) {
|
|
16
|
-
await db.close();
|
|
17
|
-
}
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
describe("FTS5", () => {
|
|
21
|
-
it("has the trigram tokenizer (substring search)", async () => {
|
|
22
|
-
db = await newNodeSqliteDb();
|
|
23
|
-
await db.exec("CREATE VIRTUAL TABLE g USING fts5(c, tokenize='trigram')");
|
|
24
|
-
await db.exec("INSERT INTO g VALUES ('idx_lefthand')");
|
|
25
|
-
const rows = await db.query<{ c: string }>("SELECT c FROM g WHERE g MATCH 'x_le'");
|
|
26
|
-
expect(rows.map((r) => r.c)).toEqual(["idx_lefthand"]);
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
it("returns empty results for unmatched search terms", async () => {
|
|
30
|
-
db = await newNodeSqliteDb();
|
|
31
|
-
await db.exec("CREATE VIRTUAL TABLE docs2_fts USING fts5(content)");
|
|
32
|
-
await db.exec(
|
|
33
|
-
"INSERT INTO docs2_fts (rowid, content) VALUES (1, 'hello world'), (2, 'goodbye world')",
|
|
34
|
-
);
|
|
35
|
-
|
|
36
|
-
const rows = await db.query<{ content: string }>(
|
|
37
|
-
"SELECT content FROM docs2_fts WHERE docs2_fts MATCH 'nonexistent'",
|
|
38
|
-
);
|
|
39
|
-
expect(rows).toHaveLength(0);
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
it("searches across multiple text columns", async () => {
|
|
43
|
-
db = await newNodeSqliteDb();
|
|
44
|
-
await db.exec("CREATE VIRTUAL TABLE articles_fts USING fts5(title, body, tokenize='porter')");
|
|
45
|
-
await db.exec(`
|
|
46
|
-
INSERT INTO articles_fts (rowid, title, body) VALUES
|
|
47
|
-
(1, 'Travel Guide', 'Visit the beautiful lakes of Switzerland'),
|
|
48
|
-
(2, 'Cooking Tips', 'How to make a perfect lake trout'),
|
|
49
|
-
(3, 'Programming', 'Learn about SQL databases')
|
|
50
|
-
`);
|
|
51
|
-
|
|
52
|
-
const rows = await db.query<{ rowid: number; title: string; score: number }>(`
|
|
53
|
-
SELECT rowid, title, bm25(articles_fts) AS score
|
|
54
|
-
FROM articles_fts
|
|
55
|
-
WHERE articles_fts MATCH 'lake'
|
|
56
|
-
ORDER BY score
|
|
57
|
-
`);
|
|
58
|
-
expect(rows).toHaveLength(2);
|
|
59
|
-
const titles = rows.map((r) => r.title);
|
|
60
|
-
expect(titles).toContain("Travel Guide");
|
|
61
|
-
expect(titles).toContain("Cooking Tips");
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
it("creates an FTS5 index and performs full-text search", async () => {
|
|
65
|
-
db = await newNodeSqliteDb();
|
|
66
|
-
await db.exec("CREATE VIRTUAL TABLE docs_fts USING fts5(content)");
|
|
67
|
-
await db.exec(`
|
|
68
|
-
INSERT INTO docs_fts (rowid, content) VALUES
|
|
69
|
-
(1, 'the quick brown fox jumps over the lazy dog'),
|
|
70
|
-
(2, 'a lazy cat sleeps on the mat'),
|
|
71
|
-
(3, 'the fox and the hound are friends')
|
|
72
|
-
`);
|
|
73
|
-
|
|
74
|
-
const rows = await db.query<{ rowid: number; content: string; score: number }>(`
|
|
75
|
-
SELECT rowid, content, bm25(docs_fts) AS score
|
|
76
|
-
FROM docs_fts
|
|
77
|
-
WHERE docs_fts MATCH 'fox'
|
|
78
|
-
ORDER BY score
|
|
79
|
-
`);
|
|
80
|
-
|
|
81
|
-
expect(rows.length).toBeGreaterThanOrEqual(1);
|
|
82
|
-
const rowids = rows.map((r) => r.rowid);
|
|
83
|
-
expect(rowids).toContain(1);
|
|
84
|
-
expect(rowids).toContain(3);
|
|
85
|
-
});
|
|
86
|
-
});
|
|
87
|
-
});
|