think-model-sqlite 2.1.0 → 3.0.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/README.md +18 -0
- package/lib/socket.js +16 -9
- package/package.json +2 -3
- package/test/index.js +64 -1
package/README.md
CHANGED
|
@@ -7,3 +7,21 @@
|
|
|
7
7
|
|
|
8
8
|
|
|
9
9
|
SQLite adapter for think model
|
|
10
|
+
|
|
11
|
+
## Node.js requirements
|
|
12
|
+
|
|
13
|
+
Version 3 uses Node.js's built-in `node:sqlite` and no longer installs
|
|
14
|
+
`better-sqlite3` or requires the `--experimental-sqlite` flag.
|
|
15
|
+
|
|
16
|
+
- Node.js 24: **24.15.0 or later** within the 24.x line.
|
|
17
|
+
- Node.js 25: **25.7.0 or later** within the 25.x line (compatibility only; Node.js 25 is end-of-life).
|
|
18
|
+
- Node.js 26 and later are also supported.
|
|
19
|
+
|
|
20
|
+
The minimum versions use the release-candidate API. See the Node.js
|
|
21
|
+
[24.15.0 release notes](https://nodejs.org/en/blog/release/v24.15.0) and
|
|
22
|
+
[25.7.0 release notes](https://nodejs.org/en/blog/release/v25.7.0).
|
|
23
|
+
|
|
24
|
+
On older Node versions, stay on `think-model-sqlite@2`. The CommonJS entry,
|
|
25
|
+
adapter configuration (including `path: true` / `':memory:'`) and on-disk
|
|
26
|
+
database path are unchanged. Existing SQLite files can be opened directly
|
|
27
|
+
without importing or migrating them.
|
package/lib/socket.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
const thinkInstance = require('think-instance');
|
|
2
|
-
const
|
|
2
|
+
const {DatabaseSync} = require('node:sqlite');
|
|
3
3
|
const assert = require('assert');
|
|
4
4
|
const helper = require('think-helper');
|
|
5
5
|
const path = require('path');
|
|
@@ -23,8 +23,18 @@ const TRANSACTION = {
|
|
|
23
23
|
end: 2
|
|
24
24
|
};
|
|
25
25
|
|
|
26
|
+
const DEFAULT_BUSY_TIMEOUT = 5000;
|
|
26
27
|
const SELECT_SQL_RE = /^\s*(SELECT|PRAGMA|WITH|EXPLAIN)\b/i;
|
|
27
28
|
|
|
29
|
+
function normalizeRow(row) {
|
|
30
|
+
// Keep ordinary objects, numeric INTEGERs and Buffer BLOBs as in v2.
|
|
31
|
+
return Object.fromEntries(Object.entries(row).map(([key, value]) => {
|
|
32
|
+
if (typeof value === 'bigint') value = Number(value);
|
|
33
|
+
else if (value instanceof Uint8Array) value = Buffer.from(value.buffer, value.byteOffset, value.byteLength);
|
|
34
|
+
return [key, value];
|
|
35
|
+
}));
|
|
36
|
+
}
|
|
37
|
+
|
|
28
38
|
class SQLiteSocket {
|
|
29
39
|
constructor(config = {}) {
|
|
30
40
|
// different socket may connect to different db,so use independent debounce instance
|
|
@@ -55,13 +65,8 @@ class SQLiteSocket {
|
|
|
55
65
|
const factory = {
|
|
56
66
|
create: () => {
|
|
57
67
|
return Promise.resolve().then(() => {
|
|
58
|
-
|
|
59
|
-
const
|
|
60
|
-
if (timeout) {
|
|
61
|
-
timeout = helper.ms(timeout);
|
|
62
|
-
options.timeout = timeout;
|
|
63
|
-
}
|
|
64
|
-
const db = new BetterSqlite3(this.savePath, options);
|
|
68
|
+
const timeout = this.config.timeout ? helper.ms(this.config.timeout) : DEFAULT_BUSY_TIMEOUT;
|
|
69
|
+
const db = new DatabaseSync(this.savePath, {timeout});
|
|
65
70
|
if (this.config.logConnect) {
|
|
66
71
|
this.config.logger(`sqlite:${this.savePath}`);
|
|
67
72
|
}
|
|
@@ -187,7 +192,9 @@ class SQLiteSocket {
|
|
|
187
192
|
}
|
|
188
193
|
|
|
189
194
|
if (SELECT_SQL_RE.test(sqlOptions.sql)) {
|
|
190
|
-
|
|
195
|
+
// node:sqlite otherwise throws when an INTEGER is outside the safe Number range.
|
|
196
|
+
stmt.setReadBigInts(true);
|
|
197
|
+
return stmt.all().map(normalizeRow);
|
|
191
198
|
}
|
|
192
199
|
|
|
193
200
|
stmt.run();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "think-model-sqlite",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"description": "SQLite adapter for ThinkJS 3.x",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"test": "pnpm run eslint && pnpm run test-cov",
|
|
@@ -27,7 +27,6 @@
|
|
|
27
27
|
},
|
|
28
28
|
"homepage": "https://github.com/thinkjs/think-model-sqlite#readme",
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"better-sqlite3": "^13.0.3",
|
|
31
30
|
"generic-pool": "^3.1.7",
|
|
32
31
|
"think-debounce": "^1.0.3",
|
|
33
32
|
"think-helper": "^1.1.3",
|
|
@@ -40,6 +39,6 @@
|
|
|
40
39
|
"think-model": "^1.5.4"
|
|
41
40
|
},
|
|
42
41
|
"engines": {
|
|
43
|
-
"node": ">=
|
|
42
|
+
"node": "^24.15.0 || ^25.7.0 || >=26.0.0"
|
|
44
43
|
}
|
|
45
44
|
}
|
package/test/index.js
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
const {test} = require('node:test');
|
|
2
|
+
const fs = require('node:fs/promises');
|
|
3
|
+
const os = require('node:os');
|
|
4
|
+
const path = require('node:path');
|
|
5
|
+
const {DatabaseSync} = require('node:sqlite');
|
|
2
6
|
const Model = require('think-model/lib/model');
|
|
3
7
|
const SQLite = require('../');
|
|
4
8
|
|
|
@@ -13,7 +17,7 @@ function createMemorySocket() {
|
|
|
13
17
|
});
|
|
14
18
|
}
|
|
15
19
|
|
|
16
|
-
test('socket execute & query with
|
|
20
|
+
test('socket execute & query with node:sqlite', async t => {
|
|
17
21
|
const socket = createMemorySocket();
|
|
18
22
|
|
|
19
23
|
try {
|
|
@@ -35,6 +39,65 @@ test('socket execute & query with better-sqlite3', async t => {
|
|
|
35
39
|
}
|
|
36
40
|
});
|
|
37
41
|
|
|
42
|
+
test('read results keep v2 row, BLOB and integer types', async t => {
|
|
43
|
+
const socket = createMemorySocket();
|
|
44
|
+
|
|
45
|
+
try {
|
|
46
|
+
const rows = await socket.query([
|
|
47
|
+
'SELECT 9223372036854775807 AS largeInteger, 1.5 AS fraction,',
|
|
48
|
+
"X'00FF' AS blob, NULL AS missing"
|
|
49
|
+
].join(' '));
|
|
50
|
+
|
|
51
|
+
t.assert.deepStrictEqual(rows, [{
|
|
52
|
+
largeInteger: Number(9223372036854775807n),
|
|
53
|
+
fraction: 1.5,
|
|
54
|
+
blob: Buffer.from([0, 255]),
|
|
55
|
+
missing: null
|
|
56
|
+
}]);
|
|
57
|
+
} finally {
|
|
58
|
+
await socket.close();
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
test('busy timeout keeps the v2 default and accepts a duration string', async t => {
|
|
63
|
+
for (const [timeout, expected] of [[undefined, 5000], ['2s', 2000]]) {
|
|
64
|
+
const socket = new Socket({path: true, timeout, logConnect: false});
|
|
65
|
+
try {
|
|
66
|
+
const rows = await socket.query('PRAGMA busy_timeout');
|
|
67
|
+
t.assert.deepStrictEqual(rows, [{timeout: expected}]);
|
|
68
|
+
} finally {
|
|
69
|
+
await socket.close();
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
test('opens an existing SQLite file and persists committed changes', async t => {
|
|
75
|
+
const dir = await fs.mkdtemp(path.join(os.tmpdir(), 'think-model-sqlite-'));
|
|
76
|
+
t.after(() => fs.rm(dir, {recursive: true, force: true}));
|
|
77
|
+
|
|
78
|
+
const db = new DatabaseSync(path.join(dir, 'legacy.sqlite'));
|
|
79
|
+
try {
|
|
80
|
+
db.exec("CREATE TABLE item (id INTEGER PRIMARY KEY, name TEXT); INSERT INTO item VALUES (1, 'existing')");
|
|
81
|
+
} finally {
|
|
82
|
+
db.close();
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const socket = new Socket({path: dir, database: 'legacy', logConnect: false});
|
|
86
|
+
try {
|
|
87
|
+
await socket.transaction(connection => socket.execute("INSERT INTO item VALUES (2, 'new')", connection));
|
|
88
|
+
} finally {
|
|
89
|
+
await socket.close();
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const reopened = new DatabaseSync(path.join(dir, 'legacy.sqlite'));
|
|
93
|
+
try {
|
|
94
|
+
const rows = reopened.prepare('SELECT name FROM item ORDER BY id').all();
|
|
95
|
+
t.assert.deepStrictEqual(rows.map(row => row.name), ['existing', 'new']);
|
|
96
|
+
} finally {
|
|
97
|
+
reopened.close();
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
|
|
38
101
|
test('transaction rollback should keep data unchanged', async t => {
|
|
39
102
|
const socket = createMemorySocket();
|
|
40
103
|
|