think-model-sqlite 2.0.1 → 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 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 BetterSqlite3 = require('better-sqlite3');
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
- let timeout = this.config.timeout;
59
- const options = {};
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
- return stmt.all();
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,12 +1,12 @@
1
1
  {
2
2
  "name": "think-model-sqlite",
3
- "version": "2.0.1",
3
+ "version": "3.0.0",
4
4
  "description": "SQLite adapter for ThinkJS 3.x",
5
5
  "scripts": {
6
- "test": "npm run eslint && npm run test-cov",
7
- "test-cov": "nyc ava test/ && nyc report --reporter=html",
6
+ "test": "pnpm run eslint && pnpm run test-cov",
7
+ "test-cov": "node --test --test-concurrency=1 --experimental-test-coverage test/index.js",
8
8
  "eslint": "eslint ./lib index.js",
9
- "prepublish": "npm test"
9
+ "prepublish": "pnpm test"
10
10
  },
11
11
  "repository": {
12
12
  "type": "git",
@@ -27,7 +27,6 @@
27
27
  },
28
28
  "homepage": "https://github.com/thinkjs/think-model-sqlite#readme",
29
29
  "dependencies": {
30
- "better-sqlite3": "^11.10.0",
31
30
  "generic-pool": "^3.1.7",
32
31
  "think-debounce": "^1.0.3",
33
32
  "think-helper": "^1.1.3",
@@ -35,11 +34,11 @@
35
34
  "think-model-abstract": "^1.6.5"
36
35
  },
37
36
  "devDependencies": {
38
- "ava": "^8.0.0",
39
- "eslint": "^4.2.0",
40
- "eslint-config-think": "^1.0.1",
37
+ "eslint": "^10.8.0",
41
38
  "muk": "^0.5.3",
42
- "nyc": "^10.3.0",
43
39
  "think-model": "^1.5.4"
40
+ },
41
+ "engines": {
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
- const {default: test} = require('ava');
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 better-sqlite3', async t => {
20
+ test('socket execute & query with node:sqlite', async t => {
17
21
  const socket = createMemorySocket();
18
22
 
19
23
  try {
@@ -23,10 +27,10 @@ test('socket execute & query with better-sqlite3', async t => {
23
27
  const insert2 = await socket.execute("INSERT INTO user(name) VALUES ('bob')");
24
28
  const rows = await socket.query('SELECT id, name FROM user ORDER BY id ASC');
25
29
 
26
- t.true(insert1.insertId > 0);
27
- t.is(insert1.affectedRows, 1);
28
- t.is(insert2.affectedRows, 1);
29
- t.deepEqual(rows, [
30
+ t.assert.strictEqual(insert1.insertId > 0, true);
31
+ t.assert.strictEqual(insert1.affectedRows, 1);
32
+ t.assert.strictEqual(insert2.affectedRows, 1);
33
+ t.assert.deepStrictEqual(rows, [
30
34
  {id: 1, name: 'alice'},
31
35
  {id: 2, name: 'bob'}
32
36
  ]);
@@ -35,22 +39,85 @@ 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
 
41
104
  try {
42
105
  await socket.execute('CREATE TABLE item (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT)');
43
106
 
44
- const err = await t.throwsAsync(async () => {
107
+ let err;
108
+ await t.assert.rejects(async () => {
45
109
  await socket.transaction(async connection => {
46
110
  await socket.execute("INSERT INTO item(name) VALUES ('rollback-me')", connection);
47
111
  throw new Error('mock failure');
48
112
  });
113
+ }, caughtError => {
114
+ err = caughtError;
115
+ return true;
49
116
  });
50
117
 
51
118
  const rows = await socket.query('SELECT COUNT(1) AS cnt FROM item');
52
- t.is(err.message, 'mock failure');
53
- t.is(rows[0].cnt, 0);
119
+ t.assert.strictEqual(err.message, 'mock failure');
120
+ t.assert.strictEqual(rows[0].cnt, 0);
54
121
  } finally {
55
122
  await socket.close();
56
123
  }
@@ -59,7 +126,7 @@ test('transaction rollback should keep data unchanged', async t => {
59
126
  test('think-model integration', async t => {
60
127
  const userModel = new Model('user', {
61
128
  handle: SQLite,
62
- path: '/tmp',
129
+ path: true,
63
130
  database: 'test',
64
131
  logConnect: false,
65
132
  debounce: false
@@ -71,9 +138,9 @@ test('think-model integration', async t => {
71
138
  const insert2 = await userModel.add({name: 'bob'});
72
139
  const rows = await userModel.select();
73
140
 
74
- t.is(insert1, 1);
75
- t.is(insert2, 2);
76
- t.deepEqual(rows, [
141
+ t.assert.strictEqual(insert1, 1);
142
+ t.assert.strictEqual(insert2, 2);
143
+ t.assert.deepStrictEqual(rows, [
77
144
  {id: 1, name: 'alice'},
78
145
  {id: 2, name: 'bob'}
79
146
  ]);
package/.eslintrc DELETED
@@ -1,3 +0,0 @@
1
- {
2
- "extends": "think"
3
- }
package/.travis.yml DELETED
@@ -1,8 +0,0 @@
1
- language: node_js
2
- node_js:
3
- - '6'
4
- sudo: false
5
- script:
6
- - "npm test"
7
- after_success:
8
- - 'npm install coveralls && ./node_modules/.bin/nyc report --reporter=text-lcov | ./node_modules/.bin/coveralls'