orange-orm 4.8.1 → 4.9.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.
@@ -12129,6 +12129,10 @@ function requirePatchTable () {
12129
12129
 
12130
12130
 
12131
12131
  function createRowInCache({ context, table, key }) {
12132
+ const tryGetFromCacheById = getOrCreateRow.tryGetFromCacheById || (getOrCreateRow.tryGetFromCacheById = requireTryGetFromCacheById());
12133
+ const cachedRow = tryGetFromCacheById(context, table, ...key);
12134
+ if (cachedRow)
12135
+ return cachedRow;
12132
12136
  const newRow = getOrCreateRow.cachedNewRow || (getOrCreateRow.cachedNewRow = requireNewRow());
12133
12137
  const pkDto = {};
12134
12138
  for (let i = 0; i < key.length && i < table._primaryColumns.length; i++) {
package/dist/index.mjs CHANGED
@@ -12130,6 +12130,10 @@ function requirePatchTable () {
12130
12130
 
12131
12131
 
12132
12132
  function createRowInCache({ context, table, key }) {
12133
+ const tryGetFromCacheById = getOrCreateRow.tryGetFromCacheById || (getOrCreateRow.tryGetFromCacheById = requireTryGetFromCacheById());
12134
+ const cachedRow = tryGetFromCacheById(context, table, ...key);
12135
+ if (cachedRow)
12136
+ return cachedRow;
12133
12137
  const newRow = getOrCreateRow.cachedNewRow || (getOrCreateRow.cachedNewRow = requireNewRow());
12134
12138
  const pkDto = {};
12135
12139
  for (let i = 0; i < key.length && i < table._primaryColumns.length; i++) {
@@ -18098,27 +18102,42 @@ var hasRequiredWrapQuery$4;
18098
18102
  function requireWrapQuery$4 () {
18099
18103
  if (hasRequiredWrapQuery$4) return wrapQuery_1$4;
18100
18104
  hasRequiredWrapQuery$4 = 1;
18101
- var log = requireLog();
18105
+ const log = requireLog();
18106
+ const connectionCache = new WeakMap();
18102
18107
 
18103
18108
  function wrapQuery(_context, connection) {
18104
- var runOriginalQuery = connection.all;
18109
+ let statementCache = connectionCache.get(connection);
18110
+ if (!statementCache) {
18111
+ statementCache = new Map();
18112
+ connectionCache.set(connection, statementCache);
18113
+ }
18114
+
18105
18115
  return runQuery;
18106
18116
 
18107
18117
  function runQuery(query, onCompleted) {
18108
- var params = query.parameters;
18109
- var sql = query.sql();
18110
- log.emitQuery({sql, parameters: params});
18118
+ try {
18119
+ var params = query.parameters;
18120
+ var sql = query.sql();
18121
+ log.emitQuery({ sql, parameters: params });
18111
18122
 
18112
- 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
+ }
18113
18128
 
18114
- function onInnerCompleted(err, rows) {
18115
- if (err)
18116
- onCompleted(err);
18117
- else
18129
+ if (statement.reader) {
18130
+ const rows = statement.all.apply(statement, params);
18118
18131
  onCompleted(null, rows);
18132
+ } else {
18133
+ statement.run.apply(statement, params);
18134
+ onCompleted(null, []);
18135
+ }
18136
+ }
18137
+ catch (e) {
18138
+ onCompleted(e);
18119
18139
  }
18120
18140
  }
18121
-
18122
18141
  }
18123
18142
 
18124
18143
  wrapQuery_1$4 = wrapQuery;
@@ -18132,9 +18151,14 @@ function requireWrapCommand$4 () {
18132
18151
  if (hasRequiredWrapCommand$4) return wrapCommand_1$4;
18133
18152
  hasRequiredWrapCommand$4 = 1;
18134
18153
  var log = requireLog();
18154
+ var connectionCache = new WeakMap();
18135
18155
 
18136
18156
  function wrapCommand(_context, connection) {
18137
- var runOriginalQuery = connection.run;
18157
+ var statementCache = connectionCache.get(connection);
18158
+ if (!statementCache) {
18159
+ statementCache = new Map();
18160
+ connectionCache.set(connection, statementCache);
18161
+ }
18138
18162
  return runQuery;
18139
18163
 
18140
18164
  function runQuery(query, onCompleted) {
@@ -18142,14 +18166,23 @@ function requireWrapCommand$4 () {
18142
18166
  var sql = query.sql();
18143
18167
  log.emitQuery({ sql, parameters: params });
18144
18168
 
18145
- runOriginalQuery.call(connection, sql, params, function onInnerCompleted(err) {
18146
- if (err) {
18147
- onCompleted(err);
18148
- } else {
18149
- var affectedRows = typeof this.changes === 'number' ? this.changes : 0;
18150
- 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);
18151
18174
  }
18152
- });
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
+ }
18153
18186
  }
18154
18187
  }
18155
18188
 
@@ -18316,20 +18349,20 @@ function requireNewGenericPool$4 () {
18316
18349
  create: async function(cb) {
18317
18350
  try {
18318
18351
  if (!sqlite)
18319
- sqlite = await import('sqlite3');
18352
+ sqlite = await import('better-sqlite3');
18320
18353
  sqlite = sqlite.default || sqlite;
18321
18354
  }
18322
18355
  catch (err) {
18323
18356
  return cb(err, null);
18324
18357
  }
18325
- var client = new sqlite.Database(connectionString, onConnected);
18326
-
18327
- function onConnected(err) {
18328
- if(err)
18329
- return cb(err, null);
18358
+ try {
18359
+ var client = new sqlite(connectionString);
18330
18360
  client.poolCount = 0;
18331
18361
  return cb(null, client);
18332
18362
  }
18363
+ catch (err) {
18364
+ return cb(err, null);
18365
+ }
18333
18366
  },
18334
18367
 
18335
18368
  destroy: function(client) {
package/docs/changelog.md CHANGED
@@ -1,4 +1,9 @@
1
1
  ## 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`
5
+ __4.8.2__
6
+ Slight performance gain on updates and removed accidental console.dir
2
7
  __4.8.1__
3
8
  Atomic updates/deletes with SQL-level concurrency checks (affectedRows) [#142](https://github.com/alfateam/orange-orm/issues/142)
4
9
  __4.8.0__
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "orange-orm",
3
- "version": "4.8.1",
3
+ "version": "4.9.0",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -93,7 +93,7 @@
93
93
  "oracledb": "^6.3.0",
94
94
  "pg": "^8.5.1",
95
95
  "pg-query-stream": "^3.3.2",
96
- "sqlite3": "^5.0.2",
96
+ "better-sqlite3": "^11.8.1",
97
97
  "tedious": "^15.1.2 || ^16.0.0 || ^18.1.0 || || ^19.0.0"
98
98
  },
99
99
  "peerDependenciesMeta": {
@@ -106,7 +106,7 @@
106
106
  "mysql2": {
107
107
  "optional": true
108
108
  },
109
- "sqlite3": {
109
+ "better-sqlite3": {
110
110
  "optional": true
111
111
  },
112
112
  "pg-native": {
@@ -144,7 +144,7 @@
144
144
  "pg": "^8.5.1",
145
145
  "pg-query-stream": "^3.3.2",
146
146
  "rollup": "^2.52.7",
147
- "sqlite3": "^5.0.2",
147
+ "better-sqlite3": "^11.8.1",
148
148
  "tedious": "^19.0.0",
149
149
  "typescript": "^5.4.5",
150
150
  "vitest": "^3.2.4"
package/src/patchTable.js CHANGED
@@ -345,6 +345,10 @@ async function patchTableCore(context, table, patches, { strategy = undefined, d
345
345
 
346
346
 
347
347
  function createRowInCache({ context, table, key }) {
348
+ const tryGetFromCacheById = getOrCreateRow.tryGetFromCacheById || (getOrCreateRow.tryGetFromCacheById = require('./table/tryGetFromCacheById'));
349
+ const cachedRow = tryGetFromCacheById(context, table, ...key);
350
+ if (cachedRow)
351
+ return cachedRow;
348
352
  const newRow = getOrCreateRow.cachedNewRow || (getOrCreateRow.cachedNewRow = require('./table/commands/newRow'));
349
353
  const pkDto = {};
350
354
  for (let i = 0; i < key.length && i < table._primaryColumns.length; i++) {
@@ -15,20 +15,20 @@ function newGenericPool(connectionString, poolOptions) {
15
15
  create: async function(cb) {
16
16
  try {
17
17
  if (!sqlite)
18
- sqlite = await import('sqlite3');
18
+ sqlite = await import('better-sqlite3');
19
19
  sqlite = sqlite.default || sqlite;
20
20
  }
21
21
  catch (err) {
22
22
  return cb(err, null);
23
23
  }
24
- var client = new sqlite.Database(connectionString, onConnected);
25
-
26
- function onConnected(err) {
27
- if(err)
28
- return cb(err, null);
24
+ try {
25
+ var client = new sqlite(connectionString);
29
26
  client.poolCount = 0;
30
27
  return cb(null, client);
31
28
  }
29
+ catch (err) {
30
+ return cb(err, null);
31
+ }
32
32
  },
33
33
 
34
34
  destroy: function(client) {
@@ -53,4 +53,4 @@ function newGenericPool(connectionString, poolOptions) {
53
53
  return pool;
54
54
  }
55
55
 
56
- module.exports = newGenericPool;
56
+ module.exports = newGenericPool;
@@ -1,7 +1,12 @@
1
1
  var log = require('../table/log');
2
+ var connectionCache = new WeakMap();
2
3
 
3
4
  function wrapCommand(_context, connection) {
4
- var runOriginalQuery = connection.run;
5
+ var statementCache = connectionCache.get(connection);
6
+ if (!statementCache) {
7
+ statementCache = new Map();
8
+ connectionCache.set(connection, statementCache);
9
+ }
5
10
  return runQuery;
6
11
 
7
12
  function runQuery(query, onCompleted) {
@@ -9,14 +14,23 @@ function wrapCommand(_context, connection) {
9
14
  var sql = query.sql();
10
15
  log.emitQuery({ sql, parameters: params });
11
16
 
12
- runOriginalQuery.call(connection, sql, params, function onInnerCompleted(err) {
13
- if (err) {
14
- onCompleted(err);
15
- } else {
16
- var affectedRows = typeof this.changes === 'number' ? this.changes : 0;
17
- onCompleted(null, { rowsAffected: affectedRows });
17
+ try {
18
+ var statement = statementCache.get(sql);
19
+ if (!statement) {
20
+ statement = connection.prepare(sql);
21
+ statementCache.set(sql, statement);
18
22
  }
19
- });
23
+ var info = statement.run.apply(statement, params);
24
+ var affected = info && typeof info.changes === 'number' ? info.changes : 0;
25
+ var insertId = info && typeof info.lastInsertRowid !== 'undefined' ? info.lastInsertRowid : undefined;
26
+ if (typeof insertId !== 'undefined')
27
+ onCompleted(null, { rowsAffected: affected, lastInsertRowid: insertId });
28
+ else
29
+ onCompleted(null, { rowsAffected: affected });
30
+ }
31
+ catch (e) {
32
+ onCompleted(e);
33
+ }
20
34
  }
21
35
  }
22
36
 
@@ -1,24 +1,39 @@
1
- var log = require('../table/log');
1
+ const log = require('../table/log');
2
+ const connectionCache = new WeakMap();
2
3
 
3
4
  function wrapQuery(_context, connection) {
4
- var runOriginalQuery = connection.all;
5
+ let statementCache = connectionCache.get(connection);
6
+ if (!statementCache) {
7
+ statementCache = new Map();
8
+ connectionCache.set(connection, statementCache);
9
+ }
10
+
5
11
  return runQuery;
6
12
 
7
13
  function runQuery(query, onCompleted) {
8
- var params = query.parameters;
9
- var sql = query.sql();
10
- log.emitQuery({sql, parameters: params});
14
+ try {
15
+ var params = query.parameters;
16
+ var sql = query.sql();
17
+ log.emitQuery({ sql, parameters: params });
11
18
 
12
- runOriginalQuery.call(connection, sql, params, onInnerCompleted);
19
+ let statement = statementCache.get(sql);
20
+ if (!statement) {
21
+ statement = connection.prepare(sql);
22
+ statementCache.set(sql, statement);
23
+ }
13
24
 
14
- function onInnerCompleted(err, rows) {
15
- if (err)
16
- onCompleted(err);
17
- else
25
+ if (statement.reader) {
26
+ const rows = statement.all.apply(statement, params);
18
27
  onCompleted(null, rows);
28
+ } else {
29
+ statement.run.apply(statement, params);
30
+ onCompleted(null, []);
31
+ }
32
+ }
33
+ catch (e) {
34
+ onCompleted(e);
19
35
  }
20
36
  }
21
-
22
37
  }
23
38
 
24
- module.exports = wrapQuery;
39
+ module.exports = wrapQuery;