From 6b8ac8085fdc4d31a576d52087bb803c901c7f50 Mon Sep 17 00:00:00 2001 From: Justin Wind Date: Sat, 14 Jun 2025 12:15:10 -0700 Subject: [PATCH] sqlite updates --- package-lock.json | 76 ++++++++++++++++++------------------------ package.json | 6 ++-- src/db/sqlite/index.js | 22 +++++++----- 3 files changed, 48 insertions(+), 56 deletions(-) diff --git a/package-lock.json b/package-lock.json index 8b376f1..e47bc80 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,13 +12,13 @@ "@squeep/api-dingus": "^2", "@squeep/logger-json-console": "^3", "argon2": "^0.43.0", - "better-sqlite3": "^9.4.3", + "better-sqlite3": "^11", "pg-promise": "^11", "pm2": "^6", "uuid": "^11" }, "devDependencies": { - "@squeep/eslint-config": "^1.0.2", + "@squeep/eslint-config": "^1", "eslint": "^9", "husky": "^9.1.7", "mocha": "^11", @@ -403,6 +403,18 @@ "url": "https://opencollective.com/eslint" } }, + "node_modules/@eslint/eslintrc/node_modules/globals": { + "version": "14.0.0", + "resolved": "https://registry.npmjs.org/globals/-/globals-14.0.0.tgz", + "integrity": "sha512-oahGvuMGQlPw/ivIYBjVSrWAfWLBeku5tpPE2fOPLi+WHffIWbuh2tCjhyQhTBPMf5E9jDEH4FOmTYgYwbKwtQ==", + "dev": true, + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/@eslint/js": { "version": "9.29.0", "resolved": "https://registry.npmjs.org/@eslint/js/-/js-9.29.0.tgz", @@ -1030,30 +1042,6 @@ "eslint": ">= 9" } }, - "node_modules/@squeep/eslint-config/node_modules/eslint-plugin-sonarjs": { - "version": "1.0.4", - "resolved": "https://registry.npmjs.org/eslint-plugin-sonarjs/-/eslint-plugin-sonarjs-1.0.4.tgz", - "integrity": "sha512-jF0eGCUsq/HzMub4ExAyD8x1oEgjOyB9XVytYGyWgSFvdiJQJp6IuP7RmtauCf06o6N/kZErh+zW4b10y1WZ+Q==", - "dev": true, - "engines": { - "node": ">=16" - }, - "peerDependencies": { - "eslint": "^8.0.0 || ^9.0.0" - } - }, - "node_modules/@squeep/eslint-config/node_modules/globals": { - "version": "15.15.0", - "resolved": "https://registry.npmjs.org/globals/-/globals-15.15.0.tgz", - "integrity": "sha512-7ACyT3wmyp3I61S4fG682L0VA2RGD9otkqGJIwNUMF1SWUombIIk+af1unuDYgMm082aHYwD+mzJvv9Iu8dsgg==", - "dev": true, - "engines": { - "node": ">=18" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, "node_modules/@squeep/log-helper": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/@squeep/log-helper/-/log-helper-1.0.1.tgz", @@ -1480,9 +1468,9 @@ } }, "node_modules/better-sqlite3": { - "version": "9.6.0", - "resolved": "https://registry.npmjs.org/better-sqlite3/-/better-sqlite3-9.6.0.tgz", - "integrity": "sha512-yR5HATnqeYNVnkaUTf4bOP2dJSnyhP4puJN/QPRyx4YkBEEUxib422n2XzPqDEHjQQqazoYoADdAm5vE15+dAQ==", + "version": "11.10.0", + "resolved": "https://registry.npmjs.org/better-sqlite3/-/better-sqlite3-11.10.0.tgz", + "integrity": "sha512-EwhOpyXiOEL/lKzHz9AW1msWFNzGc/z+LzeB3/jnFJpxu+th2yqvzsSWas1v9jgs9+xiXJcD5A8CJxAG2TaghQ==", "hasInstallScript": true, "dependencies": { "bindings": "^1.5.0", @@ -2254,18 +2242,6 @@ "balanced-match": "^1.0.0" } }, - "node_modules/eslint-plugin-n/node_modules/globals": { - "version": "15.15.0", - "resolved": "https://registry.npmjs.org/globals/-/globals-15.15.0.tgz", - "integrity": "sha512-7ACyT3wmyp3I61S4fG682L0VA2RGD9otkqGJIwNUMF1SWUombIIk+af1unuDYgMm082aHYwD+mzJvv9Iu8dsgg==", - "dev": true, - "engines": { - "node": ">=18" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, "node_modules/eslint-plugin-n/node_modules/minimatch": { "version": "9.0.5", "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-9.0.5.tgz", @@ -2314,6 +2290,18 @@ "url": "https://opencollective.com/eslint" } }, + "node_modules/eslint-plugin-sonarjs": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/eslint-plugin-sonarjs/-/eslint-plugin-sonarjs-1.0.4.tgz", + "integrity": "sha512-jF0eGCUsq/HzMub4ExAyD8x1oEgjOyB9XVytYGyWgSFvdiJQJp6IuP7RmtauCf06o6N/kZErh+zW4b10y1WZ+Q==", + "dev": true, + "engines": { + "node": ">=16" + }, + "peerDependencies": { + "eslint": "^8.0.0 || ^9.0.0" + } + }, "node_modules/eslint-scope": { "version": "8.4.0", "resolved": "https://registry.npmjs.org/eslint-scope/-/eslint-scope-8.4.0.tgz", @@ -2797,9 +2785,9 @@ } }, "node_modules/globals": { - "version": "14.0.0", - "resolved": "https://registry.npmjs.org/globals/-/globals-14.0.0.tgz", - "integrity": "sha512-oahGvuMGQlPw/ivIYBjVSrWAfWLBeku5tpPE2fOPLi+WHffIWbuh2tCjhyQhTBPMf5E9jDEH4FOmTYgYwbKwtQ==", + "version": "15.15.0", + "resolved": "https://registry.npmjs.org/globals/-/globals-15.15.0.tgz", + "integrity": "sha512-7ACyT3wmyp3I61S4fG682L0VA2RGD9otkqGJIwNUMF1SWUombIIk+af1unuDYgMm082aHYwD+mzJvv9Iu8dsgg==", "dev": true, "engines": { "node": ">=18" diff --git a/package.json b/package.json index 9535b5d..bf6c526 100644 --- a/package.json +++ b/package.json @@ -24,15 +24,15 @@ "@squeep/api-dingus": "^2", "@squeep/logger-json-console": "^3", "argon2": "^0.43.0", - "better-sqlite3": "^9.4.3", + "better-sqlite3": "^11", "pg-promise": "^11", "pm2": "^6", "uuid": "^11" }, "devDependencies": { - "@squeep/eslint-config": "^1.0.2", + "@squeep/eslint-config": "^1", "eslint": "^9", - "husky": "^9.1.7", + "husky": "^9", "mocha": "^11", "nyc": "^17", "sinon": "^21" diff --git a/src/db/sqlite/index.js b/src/db/sqlite/index.js index 67c1f78..39b9b6b 100644 --- a/src/db/sqlite/index.js +++ b/src/db/sqlite/index.js @@ -1,7 +1,6 @@ 'use strict'; const SQLite = require('better-sqlite3'); -// XXX: const NodeCleanup = require('node-cleanup'); const BaseDatabase = require('../base'); const common = require('../../common'); const DBErrors = require('../errors'); @@ -33,13 +32,6 @@ class SQLiteDatabase extends BaseDatabase { this.changesSinceLastOptimize = BigInt(0); - /* - NodeCleanup(() => { - this._optimize(); - this._closeConnection(); - }); - */ - this.db.pragma('foreign_keys = on'); this.db.pragma('journal_mode = WAL'); this.db.defaultSafeIntegers(true); @@ -97,6 +89,9 @@ class SQLiteDatabase extends BaseDatabase { _initStatements() { this.statement = { + _beginImmediate: this.db.prepare('BEGIN IMMEDIATE'), + _commit: this.db.prepare('COMMIT'), + _rollback: this.db.prepare('ROLLBACK'), getAuthById: this.db.prepare('SELECT * FROM auth WHERE id = :id'), getLinkById: this.db.prepare('SELECT * FROM link WHERE id = :id'), getLinkByUrl: this.db.prepare('SELECT * FROM link WHERE url = :url'), @@ -149,7 +144,16 @@ class SQLiteDatabase extends BaseDatabase { async transaction(dbCtx, fn, ...rest) { dbCtx = dbCtx || this.db; - return await dbCtx.transaction(async (...args) => await fn(...args))(dbCtx, ...rest); + try { + this.statement._beginImmediate.run(); + const result = await fn(dbCtx, ...rest); + this.statement._commit.run(); + return result; + } finally { + if (this.db.inTransaction) { + this.statement._rollback.run(); + } + } } async getAuthById(dbCtx, id) { -- 2.49.0