Skip to content

Commit b400e87

Browse files
committed
replace sqlite3 with better-sqlite3 closes #143
1 parent 6d68d4b commit b400e87

File tree

7 files changed

+254
-1166
lines changed

7 files changed

+254
-1166
lines changed

dist/index.mjs

Lines changed: 54 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18102,27 +18102,42 @@ var hasRequiredWrapQuery$4;
1810218102
function requireWrapQuery$4 () {
1810318103
if (hasRequiredWrapQuery$4) return wrapQuery_1$4;
1810418104
hasRequiredWrapQuery$4 = 1;
18105-
var log = requireLog();
18105+
const log = requireLog();
18106+
const connectionCache = new WeakMap();
1810618107

1810718108
function wrapQuery(_context, connection) {
18108-
var runOriginalQuery = connection.all;
18109+
let statementCache = connectionCache.get(connection);
18110+
if (!statementCache) {
18111+
statementCache = new Map();
18112+
connectionCache.set(connection, statementCache);
18113+
}
18114+
1810918115
return runQuery;
1811018116

1811118117
function runQuery(query, onCompleted) {
18112-
var params = query.parameters;
18113-
var sql = query.sql();
18114-
log.emitQuery({sql, parameters: params});
18118+
try {
18119+
var params = query.parameters;
18120+
var sql = query.sql();
18121+
log.emitQuery({ sql, parameters: params });
1811518122

18116-
runOriginalQuery.call(connection, sql, params, onInnerCompleted);
18123+
let statement = statementCache.get(sql);
18124+
if (!statement) {
18125+
statement = connection.prepare(sql);
18126+
statementCache.set(sql, statement);
18127+
}
1811718128

18118-
function onInnerCompleted(err, rows) {
18119-
if (err)
18120-
onCompleted(err);
18121-
else
18129+
if (statement.reader) {
18130+
const rows = statement.all.apply(statement, params);
1812218131
onCompleted(null, rows);
18132+
} else {
18133+
statement.run.apply(statement, params);
18134+
onCompleted(null, []);
18135+
}
18136+
}
18137+
catch (e) {
18138+
onCompleted(e);
1812318139
}
1812418140
}
18125-
1812618141
}
1812718142

1812818143
wrapQuery_1$4 = wrapQuery;
@@ -18136,24 +18151,38 @@ function requireWrapCommand$4 () {
1813618151
if (hasRequiredWrapCommand$4) return wrapCommand_1$4;
1813718152
hasRequiredWrapCommand$4 = 1;
1813818153
var log = requireLog();
18154+
var connectionCache = new WeakMap();
1813918155

1814018156
function wrapCommand(_context, connection) {
18141-
var runOriginalQuery = connection.run;
18157+
var statementCache = connectionCache.get(connection);
18158+
if (!statementCache) {
18159+
statementCache = new Map();
18160+
connectionCache.set(connection, statementCache);
18161+
}
1814218162
return runQuery;
1814318163

1814418164
function runQuery(query, onCompleted) {
1814518165
var params = query.parameters;
1814618166
var sql = query.sql();
1814718167
log.emitQuery({ sql, parameters: params });
1814818168

18149-
runOriginalQuery.call(connection, sql, params, function onInnerCompleted(err) {
18150-
if (err) {
18151-
onCompleted(err);
18152-
} else {
18153-
var affectedRows = typeof this.changes === 'number' ? this.changes : 0;
18154-
onCompleted(null, { rowsAffected: affectedRows });
18169+
try {
18170+
var statement = statementCache.get(sql);
18171+
if (!statement) {
18172+
statement = connection.prepare(sql);
18173+
statementCache.set(sql, statement);
1815518174
}
18156-
});
18175+
var info = statement.run.apply(statement, params);
18176+
var affected = info && typeof info.changes === 'number' ? info.changes : 0;
18177+
var insertId = info && typeof info.lastInsertRowid !== 'undefined' ? info.lastInsertRowid : undefined;
18178+
if (typeof insertId !== 'undefined')
18179+
onCompleted(null, { rowsAffected: affected, lastInsertRowid: insertId });
18180+
else
18181+
onCompleted(null, { rowsAffected: affected });
18182+
}
18183+
catch (e) {
18184+
onCompleted(e);
18185+
}
1815718186
}
1815818187
}
1815918188

@@ -18320,20 +18349,20 @@ function requireNewGenericPool$4 () {
1832018349
create: async function(cb) {
1832118350
try {
1832218351
if (!sqlite)
18323-
sqlite = await import('sqlite3');
18352+
sqlite = await import('better-sqlite3');
1832418353
sqlite = sqlite.default || sqlite;
1832518354
}
1832618355
catch (err) {
1832718356
return cb(err, null);
1832818357
}
18329-
var client = new sqlite.Database(connectionString, onConnected);
18330-
18331-
function onConnected(err) {
18332-
if(err)
18333-
return cb(err, null);
18358+
try {
18359+
var client = new sqlite(connectionString);
1833418360
client.poolCount = 0;
1833518361
return cb(null, client);
1833618362
}
18363+
catch (err) {
18364+
return cb(err, null);
18365+
}
1833718366
},
1833818367

1833918368
destroy: function(client) {

docs/changelog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
## Changelog
2+
__4.9.0__
3+
Node.js 22.5+: Continues using built-in `node:sqlite` (no action needed)
4+
Node.js 18-22.4: Now requires `better-sqlite3` instead of `sqlite3`
25
__4.8.2__
36
Slight performance gain on updates and removed accidental console.dir
47
__4.8.1__

0 commit comments

Comments
 (0)