From 2f17a0a731de9e7a4af93b47ff222186b2dfe2ea Mon Sep 17 00:00:00 2001 From: holzmaster Date: Thu, 16 Oct 2025 03:32:08 +0200 Subject: [PATCH 1/3] Use native node:sqlite insteda of better-sqlite3 to not have issues on node updates --- package-lock.json | 408 +------------------------------ package.json | 2 +- src/storage/db/db.ts | 17 +- src/storage/db/sqlite-dialect.ts | 37 +++ 4 files changed, 58 insertions(+), 406 deletions(-) create mode 100644 src/storage/db/sqlite-dialect.ts diff --git a/package-lock.json b/package-lock.json index 8fc72609..05074f2f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -16,7 +16,6 @@ "@sentry/node": "^10.20.0", "@snazzah/davey": "^0.1.7", "@spotify/web-api-ts-sdk": "^1.2.0", - "better-sqlite3": "^12.4.1", "chrono-node": "^2.9.0", "comment-json": "^4.4.1", "croner": "^9.1.0", @@ -25,6 +24,7 @@ "graphviz-wasm": "^3.0.2", "jsdom": "^27.0.0", "kysely": "^0.28.8", + "kysely-generic-sqlite": "^1.2.1", "libsodium-wrappers": "^0.7.15", "pino": "^10.0.0", "pino-pretty": "^13.1.2", @@ -2957,40 +2957,6 @@ "integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==", "license": "MIT" }, - "node_modules/base64-js": { - "version": "1.5.1", - "resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.5.1.tgz", - "integrity": "sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA==", - "funding": [ - { - "type": "github", - "url": "https://github.com/sponsors/feross" - }, - { - "type": "patreon", - "url": "https://www.patreon.com/feross" - }, - { - "type": "consulting", - "url": "https://feross.org/support" - } - ], - "license": "MIT" - }, - "node_modules/better-sqlite3": { - "version": "12.4.1", - "resolved": "https://registry.npmjs.org/better-sqlite3/-/better-sqlite3-12.4.1.tgz", - "integrity": "sha512-3yVdyZhklTiNrtg+4WqHpJpFDd+WHTg2oM7UcR80GqL05AOV0xEJzc6qNvFYoEtE+hRp1n9MpN6/+4yhlGkDXQ==", - "hasInstallScript": true, - "license": "MIT", - "dependencies": { - "bindings": "^1.5.0", - "prebuild-install": "^7.1.1" - }, - "engines": { - "node": "20.x || 22.x || 23.x || 24.x" - } - }, "node_modules/bidi-js": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/bidi-js/-/bidi-js-1.0.3.tgz", @@ -3167,26 +3133,6 @@ "url": "https://github.com/sponsors/sindresorhus" } }, - "node_modules/bindings": { - "version": "1.5.0", - "resolved": "https://registry.npmjs.org/bindings/-/bindings-1.5.0.tgz", - "integrity": "sha512-p2q/t/mhvuOj/UeLlV6566GD/guowlr0hHxClI0W9m7MWYkL1F0hLo+0Aexs9HSPCtR1SXQ0TD3MMKrXZajbiQ==", - "license": "MIT", - "dependencies": { - "file-uri-to-path": "1.0.0" - } - }, - "node_modules/bl": { - "version": "4.1.0", - "resolved": "https://registry.npmjs.org/bl/-/bl-4.1.0.tgz", - "integrity": "sha512-1W07cM9gS6DcLperZfFSj+bWLtaPGSOHWhPiGzXmvVJbRLdG82sH/Kn8EtW1VqWVA54AKf2h5k5BbnIbwF3h6w==", - "license": "MIT", - "dependencies": { - "buffer": "^5.5.0", - "inherits": "^2.0.4", - "readable-stream": "^3.4.0" - } - }, "node_modules/brace-expansion": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-2.0.2.tgz", @@ -3209,30 +3155,6 @@ "node": ">=8" } }, - "node_modules/buffer": { - "version": "5.7.1", - "resolved": "https://registry.npmjs.org/buffer/-/buffer-5.7.1.tgz", - "integrity": "sha512-EHcyIPBQ4BSGlvjB16k5KgAJ27CIsHY/2JBmCRReo48y9rQ3MaUzWX3KVlBa4U7MyX02HdVj0K7C3WaB3ju7FQ==", - "funding": [ - { - "type": "github", - "url": "https://github.com/sponsors/feross" - }, - { - "type": "patreon", - "url": "https://www.patreon.com/feross" - }, - { - "type": "consulting", - "url": "https://feross.org/support" - } - ], - "license": "MIT", - "dependencies": { - "base64-js": "^1.3.1", - "ieee754": "^1.1.13" - } - }, "node_modules/chalk": { "version": "4.1.2", "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz", @@ -3250,12 +3172,6 @@ "url": "https://github.com/chalk/chalk?sponsor=1" } }, - "node_modules/chownr": { - "version": "1.1.4", - "resolved": "https://registry.npmjs.org/chownr/-/chownr-1.1.4.tgz", - "integrity": "sha512-jJ0bqzaylmJtVnNgzTeSOs8DPavpbYgEr/b0YL8/2GO3xJEhInFmhKMUnEJQjZumK7KXGFhUy89PrsJWlakBVg==", - "license": "ISC" - }, "node_modules/chrono-node": { "version": "2.9.0", "resolved": "https://registry.npmjs.org/chrono-node/-/chrono-node-2.9.0.tgz", @@ -3472,39 +3388,6 @@ "node-buffer-encoding": "^1.0.3" } }, - "node_modules/decompress-response": { - "version": "6.0.0", - "resolved": "https://registry.npmjs.org/decompress-response/-/decompress-response-6.0.0.tgz", - "integrity": "sha512-aW35yZM6Bb/4oJlZncMH2LCoZtJXTRxES17vE3hoRiowU2kWHaJKFkSBDnDR+cm9J+9QhXmREyIfv0pji9ejCQ==", - "license": "MIT", - "dependencies": { - "mimic-response": "^3.1.0" - }, - "engines": { - "node": ">=10" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, - "node_modules/deep-extend": { - "version": "0.6.0", - "resolved": "https://registry.npmjs.org/deep-extend/-/deep-extend-0.6.0.tgz", - "integrity": "sha512-LOHxIOaPYdHlJRtCQfDIVZtfw/ufM8+rVj649RIHzcm/vGwQRXFt6OPqIFWsm2XEMrNIEtWR64sY1LEKD2vAOA==", - "license": "MIT", - "engines": { - "node": ">=4.0.0" - } - }, - "node_modules/detect-libc": { - "version": "2.1.1", - "resolved": "https://registry.npmjs.org/detect-libc/-/detect-libc-2.1.1.tgz", - "integrity": "sha512-ecqj/sy1jcK1uWrwpR67UhYrIFQ+5WlGxth34WquCbamhFA6hkkwiu37o6J5xCHdo1oixJRfVRw+ywV+Hq/0Aw==", - "license": "Apache-2.0", - "engines": { - "node": ">=8" - } - }, "node_modules/discord-api-types": { "version": "0.38.29", "resolved": "https://registry.npmjs.org/discord-api-types/-/discord-api-types-0.38.29.tgz", @@ -3649,15 +3532,6 @@ "url": "https://github.com/sindresorhus/execa?sponsor=1" } }, - "node_modules/expand-template": { - "version": "2.0.3", - "resolved": "https://registry.npmjs.org/expand-template/-/expand-template-2.0.3.tgz", - "integrity": "sha512-XYfuKMvj4O35f/pOXLObndIRvyQ+/+6AhODh+OKWj9S9498pHHn/IMszH+gt0fBCRWMNfk1ZSp5x3AifmnI2vg==", - "license": "(MIT OR WTFPL)", - "engines": { - "node": ">=6" - } - }, "node_modules/expect": { "version": "30.2.0", "resolved": "https://registry.npmjs.org/expect/-/expect-30.2.0.tgz", @@ -3694,12 +3568,6 @@ "integrity": "sha512-W+KJc2dmILlPplD/H4K9l9LcAHAfPtP6BY84uVLXQ6Evcz9Lcg33Y2z1IVblT6xdY54PXYVHEv+0Wpq8Io6zkA==", "license": "MIT" }, - "node_modules/file-uri-to-path": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/file-uri-to-path/-/file-uri-to-path-1.0.0.tgz", - "integrity": "sha512-0Zt+s3L7Vf1biwWZ29aARiVYLx7iMGnEUl9x33fbB/j3jR81u/O2LbqK+Bm1CDSNDKVtJ/YjwY7TUd5SkeLQLw==", - "license": "MIT" - }, "node_modules/fill-range": { "version": "7.1.1", "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.1.1.tgz", @@ -3735,12 +3603,6 @@ "integrity": "sha512-alTFZZQDKMporBH77856pXgzhEzaUVmLCDk+egLgIgHst3Tpndzz8MnKe+GzRJRfvVdn69HhpW7cmXzvtLvJAw==", "license": "MIT" }, - "node_modules/fs-constants": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/fs-constants/-/fs-constants-1.0.0.tgz", - "integrity": "sha512-y6OAwoSIf7FyjMIv94u+b5rdheZEjzR63GTyZJm5qh4Bi+2YgwLCcI/fPFZkL5PSixOt6ZNKm+w+Hfp/Bciwow==", - "license": "MIT" - }, "node_modules/fsevents": { "version": "2.3.3", "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz", @@ -3816,12 +3678,6 @@ "url": "https://github.com/privatenumber/get-tsconfig?sponsor=1" } }, - "node_modules/github-from-package": { - "version": "0.0.0", - "resolved": "https://registry.npmjs.org/github-from-package/-/github-from-package-0.0.0.tgz", - "integrity": "sha512-SyHy3T1v2NUXn29OsWdxmK6RwHD+vkj3v8en8AOBZ1wBQ/hCAQ5bAQTD02kW4W9tUp/3Qh6J8r9EvntiyCmOOw==", - "license": "MIT" - }, "node_modules/graceful-fs": { "version": "4.2.11", "resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.2.11.tgz", @@ -3925,26 +3781,6 @@ "node": ">=0.10.0" } }, - "node_modules/ieee754": { - "version": "1.2.1", - "resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.2.1.tgz", - "integrity": "sha512-dcyqhDvX1C46lXZcVqCpK+FtMRQVdIMN6/Df5js2zouUsqG7I6sFxitIC+7KYK29KdXOLHdu9zL4sFnoVQnqaA==", - "funding": [ - { - "type": "github", - "url": "https://github.com/sponsors/feross" - }, - { - "type": "patreon", - "url": "https://www.patreon.com/feross" - }, - { - "type": "consulting", - "url": "https://feross.org/support" - } - ], - "license": "BSD-3-Clause" - }, "node_modules/import-in-the-middle": { "version": "1.14.4", "resolved": "https://registry.npmjs.org/import-in-the-middle/-/import-in-the-middle-1.14.4.tgz", @@ -3957,18 +3793,6 @@ "module-details-from-path": "^1.0.3" } }, - "node_modules/inherits": { - "version": "2.0.4", - "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", - "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==", - "license": "ISC" - }, - "node_modules/ini": { - "version": "1.3.8", - "resolved": "https://registry.npmjs.org/ini/-/ini-1.3.8.tgz", - "integrity": "sha512-JV/yugV2uzW5iMRSiZAyDtQd+nxtUnjeLt0acNdw98kKLrvuRVyB80tsREOE7yvGVgalhZ6RNXCmEHkUKBKxew==", - "license": "ISC" - }, "node_modules/is-core-module": { "version": "2.16.1", "resolved": "https://registry.npmjs.org/is-core-module/-/is-core-module-2.16.1.tgz", @@ -4189,10 +4013,20 @@ "resolved": "https://registry.npmjs.org/kysely/-/kysely-0.28.8.tgz", "integrity": "sha512-QUOgl5ZrS9IRuhq5FvOKFSsD/3+IA6MLE81/bOOTRA/YQpKDza2sFdN5g6JCB9BOpqMJDGefLCQ9F12hRS13TA==", "license": "MIT", + "peer": true, "engines": { "node": ">=20.0.0" } }, + "node_modules/kysely-generic-sqlite": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/kysely-generic-sqlite/-/kysely-generic-sqlite-1.2.1.tgz", + "integrity": "sha512-/Bs3/Uktn04nQ9g/4oSphLMEtSHkS5+j5hbKjK5gMqXQfqr/v3V3FKtoN4pLTmo2W35hNdrIpQnBukGL1zZc6g==", + "license": "MIT", + "peerDependencies": { + "kysely": ">=0.26" + } + }, "node_modules/lefthook": { "version": "1.13.6", "resolved": "https://registry.npmjs.org/lefthook/-/lefthook-1.13.6.tgz", @@ -4446,18 +4280,6 @@ "node": ">=6" } }, - "node_modules/mimic-response": { - "version": "3.1.0", - "resolved": "https://registry.npmjs.org/mimic-response/-/mimic-response-3.1.0.tgz", - "integrity": "sha512-z0yWI+4FDrrweS8Zmt4Ej5HdJmky15+L2e6Wgn3+iK5fWzb6T3fhNFq2+MeTRb064c6Wr4N/wv0DzQTjNzHNGQ==", - "license": "MIT", - "engines": { - "node": ">=10" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, "node_modules/minimatch": { "version": "9.0.5", "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-9.0.5.tgz", @@ -4482,12 +4304,6 @@ "url": "https://github.com/sponsors/ljharb" } }, - "node_modules/mkdirp-classic": { - "version": "0.5.3", - "resolved": "https://registry.npmjs.org/mkdirp-classic/-/mkdirp-classic-0.5.3.tgz", - "integrity": "sha512-gKLcREMhtuZRwRAfqP3RFW+TK4JqApVBtOIftVgjuABpAtpxhPGaDcfvbhNvD0B8iD1oUr/txX35NjcaY6Ns/A==", - "license": "MIT" - }, "node_modules/module-details-from-path": { "version": "1.0.4", "resolved": "https://registry.npmjs.org/module-details-from-path/-/module-details-from-path-1.0.4.tgz", @@ -4518,24 +4334,6 @@ "node": "^10 || ^12 || ^13.7 || ^14 || >=15.0.1" } }, - "node_modules/napi-build-utils": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/napi-build-utils/-/napi-build-utils-2.0.0.tgz", - "integrity": "sha512-GEbrYkbfF7MoNaoh2iGG84Mnf/WZfB0GdGEsM8wz7Expx/LlWf5U8t9nvJKXSp3qr5IsEbK04cBGhol/KwOsWA==", - "license": "MIT" - }, - "node_modules/node-abi": { - "version": "3.77.0", - "resolved": "https://registry.npmjs.org/node-abi/-/node-abi-3.77.0.tgz", - "integrity": "sha512-DSmt0OEcLoK4i3NuscSbGjOf3bqiDEutejqENSplMSFA/gmB8mkED9G4pKWnPl7MDU4rSHebKPHeitpDfyH0cQ==", - "license": "MIT", - "dependencies": { - "semver": "^7.3.5" - }, - "engines": { - "node": ">=10" - } - }, "node_modules/node-buffer-encoding": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/node-buffer-encoding/-/node-buffer-encoding-1.0.3.tgz", @@ -4811,32 +4609,6 @@ "node": ">=0.10.0" } }, - "node_modules/prebuild-install": { - "version": "7.1.3", - "resolved": "https://registry.npmjs.org/prebuild-install/-/prebuild-install-7.1.3.tgz", - "integrity": "sha512-8Mf2cbV7x1cXPUILADGI3wuhfqWvtiLA1iclTDbFRZkgRQS0NqsPZphna9V+HyTEadheuPmjaJMsbzKQFOzLug==", - "license": "MIT", - "dependencies": { - "detect-libc": "^2.0.0", - "expand-template": "^2.0.3", - "github-from-package": "0.0.0", - "minimist": "^1.2.3", - "mkdirp-classic": "^0.5.3", - "napi-build-utils": "^2.0.0", - "node-abi": "^3.3.0", - "pump": "^3.0.0", - "rc": "^1.2.7", - "simple-get": "^4.0.0", - "tar-fs": "^2.0.0", - "tunnel-agent": "^0.6.0" - }, - "bin": { - "prebuild-install": "bin.js" - }, - "engines": { - "node": ">=10" - } - }, "node_modules/pretty-format": { "version": "30.2.0", "resolved": "https://registry.npmjs.org/pretty-format/-/pretty-format-30.2.0.tgz", @@ -4947,30 +4719,6 @@ "integrity": "sha512-tYC1Q1hgyRuHgloV/YXs2w15unPVh8qfu/qCTfhTYamaw7fyhumKa2yGpdSo87vY32rIclj+4fWYQXUMs9EHvg==", "license": "MIT" }, - "node_modules/rc": { - "version": "1.2.8", - "resolved": "https://registry.npmjs.org/rc/-/rc-1.2.8.tgz", - "integrity": "sha512-y3bGgqKj3QBdxLbLkomlohkvsA8gdAiUQlSBJnBhfn+BPxg4bc62d8TcBW15wavDfgexCgccckhcZvywyQYPOw==", - "license": "(BSD-2-Clause OR MIT OR Apache-2.0)", - "dependencies": { - "deep-extend": "^0.6.0", - "ini": "~1.3.0", - "minimist": "^1.2.0", - "strip-json-comments": "~2.0.1" - }, - "bin": { - "rc": "cli.js" - } - }, - "node_modules/rc/node_modules/strip-json-comments": { - "version": "2.0.1", - "resolved": "https://registry.npmjs.org/strip-json-comments/-/strip-json-comments-2.0.1.tgz", - "integrity": "sha512-4gB8na07fecVVkOI6Rs4e7T6NOTki5EmL7TUduTs6bu3EdnSycntVJ4re8kgZA+wx9IueI2Y11bfbgwtzuE0KQ==", - "license": "MIT", - "engines": { - "node": ">=0.10.0" - } - }, "node_modules/react-is": { "version": "18.3.1", "resolved": "https://registry.npmjs.org/react-is/-/react-is-18.3.1.tgz", @@ -4978,20 +4726,6 @@ "dev": true, "license": "MIT" }, - "node_modules/readable-stream": { - "version": "3.6.2", - "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-3.6.2.tgz", - "integrity": "sha512-9u/sniCrY3D5WdsERHzHE4G2YCXqoG5FTHUiCC4SIbr6XcLZBY05ya9EKjYek9O5xOAwjGq+1JdGBAS7Q9ScoA==", - "license": "MIT", - "dependencies": { - "inherits": "^2.0.3", - "string_decoder": "^1.1.1", - "util-deprecate": "^1.0.1" - }, - "engines": { - "node": ">= 6" - } - }, "node_modules/real-require": { "version": "0.2.0", "resolved": "https://registry.npmjs.org/real-require/-/real-require-0.2.0.tgz", @@ -5059,26 +4793,6 @@ "integrity": "sha512-guoltQEx+9aMf2gDZ0s62EcV8lsXR+0w8915TC3ITdn2YueuNjdAYh/levpU9nFaoChh9RUS5ZdQMrKfVEN9tw==", "license": "MIT" }, - "node_modules/safe-buffer": { - "version": "5.2.1", - "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz", - "integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==", - "funding": [ - { - "type": "github", - "url": "https://github.com/sponsors/feross" - }, - { - "type": "patreon", - "url": "https://www.patreon.com/feross" - }, - { - "type": "consulting", - "url": "https://feross.org/support" - } - ], - "license": "MIT" - }, "node_modules/safe-stable-stringify": { "version": "2.5.0", "resolved": "https://registry.npmjs.org/safe-stable-stringify/-/safe-stable-stringify-2.5.0.tgz", @@ -5194,51 +4908,6 @@ "integrity": "sha512-wnD2ZE+l+SPC/uoS0vXeE9L1+0wuaMqKlfz9AMUo38JsyLSBWSFcHR1Rri62LZc12vLr1gb3jl7iwQhgwpAbGQ==", "license": "ISC" }, - "node_modules/simple-concat": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/simple-concat/-/simple-concat-1.0.1.tgz", - "integrity": "sha512-cSFtAPtRhljv69IK0hTVZQ+OfE9nePi/rtJmw5UjHeVyVroEqJXP1sFztKUy1qU+xvz3u/sfYJLa947b7nAN2Q==", - "funding": [ - { - "type": "github", - "url": "https://github.com/sponsors/feross" - }, - { - "type": "patreon", - "url": "https://www.patreon.com/feross" - }, - { - "type": "consulting", - "url": "https://feross.org/support" - } - ], - "license": "MIT" - }, - "node_modules/simple-get": { - "version": "4.0.1", - "resolved": "https://registry.npmjs.org/simple-get/-/simple-get-4.0.1.tgz", - "integrity": "sha512-brv7p5WgH0jmQJr1ZDDfKDOSeWWg+OVypG99A/5vYGPqJ6pxiaHLy8nxtFjBA7oMa01ebA9gfh1uMCFqOuXxvA==", - "funding": [ - { - "type": "github", - "url": "https://github.com/sponsors/feross" - }, - { - "type": "patreon", - "url": "https://www.patreon.com/feross" - }, - { - "type": "consulting", - "url": "https://feross.org/support" - } - ], - "license": "MIT", - "dependencies": { - "decompress-response": "^6.0.0", - "once": "^1.3.1", - "simple-concat": "^1.0.0" - } - }, "node_modules/slash": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/slash/-/slash-3.0.0.tgz", @@ -5301,15 +4970,6 @@ "node": ">=10" } }, - "node_modules/string_decoder": { - "version": "1.3.0", - "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.3.0.tgz", - "integrity": "sha512-hkRX8U1WjJFd8LsDJ2yQ/wWWxaopEsABU1XfkM8A+j0+85JAGppt16cr1Whg6KIbb4okU6Mql6BOj+uup/wKeA==", - "license": "MIT", - "dependencies": { - "safe-buffer": "~5.2.0" - } - }, "node_modules/strip-final-newline": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/strip-final-newline/-/strip-final-newline-2.0.0.tgz", @@ -5378,34 +5038,6 @@ "integrity": "sha512-9QNk5KwDF+Bvz+PyObkmSYjI5ksVUYtjW7AU22r2NKcfLJcXp96hkDWU3+XndOsUb+AQ9QhfzfCT2O+CNWT5Tw==", "license": "MIT" }, - "node_modules/tar-fs": { - "version": "2.1.4", - "resolved": "https://registry.npmjs.org/tar-fs/-/tar-fs-2.1.4.tgz", - "integrity": "sha512-mDAjwmZdh7LTT6pNleZ05Yt65HC3E+NiQzl672vQG38jIrehtJk/J3mNwIg+vShQPcLF/LV7CMnDW6vjj6sfYQ==", - "license": "MIT", - "dependencies": { - "chownr": "^1.1.1", - "mkdirp-classic": "^0.5.2", - "pump": "^3.0.0", - "tar-stream": "^2.1.4" - } - }, - "node_modules/tar-stream": { - "version": "2.2.0", - "resolved": "https://registry.npmjs.org/tar-stream/-/tar-stream-2.2.0.tgz", - "integrity": "sha512-ujeqbceABgwMZxEJnk2HDY2DlnUZ+9oEcb1KzTVfYHio0UE6dG71n60d8D2I4qNvleWrrXpmjpt7vZeF1LnMZQ==", - "license": "MIT", - "dependencies": { - "bl": "^4.0.3", - "end-of-stream": "^1.4.1", - "fs-constants": "^1.0.0", - "inherits": "^2.0.3", - "readable-stream": "^3.1.1" - }, - "engines": { - "node": ">=6" - } - }, "node_modules/thread-stream": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/thread-stream/-/thread-stream-3.1.0.tgz", @@ -5525,18 +5157,6 @@ "fsevents": "~2.3.3" } }, - "node_modules/tunnel-agent": { - "version": "0.6.0", - "resolved": "https://registry.npmjs.org/tunnel-agent/-/tunnel-agent-0.6.0.tgz", - "integrity": "sha512-McnNiV1l8RYeY8tBgEpuodCC1mLUdbSN+CYBL7kJsJNInOP8UjDDEwdk6Mw60vdLLrr5NHKZhMAOSrR2NZuQ+w==", - "license": "Apache-2.0", - "dependencies": { - "safe-buffer": "^5.0.1" - }, - "engines": { - "node": "*" - } - }, "node_modules/undici": { "version": "6.21.3", "resolved": "https://registry.npmjs.org/undici/-/undici-6.21.3.tgz", @@ -5552,12 +5172,6 @@ "integrity": "sha512-QQiYxHuyZ9gQUIrmPo3IA+hUl4KYk8uSA7cHrcKd/l3p1OTpZcM0Tbp9x7FAtXdAYhlasd60ncPpgu6ihG6TOA==", "license": "MIT" }, - "node_modules/util-deprecate": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", - "integrity": "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==", - "license": "MIT" - }, "node_modules/w3c-xmlserializer": { "version": "5.0.0", "resolved": "https://registry.npmjs.org/w3c-xmlserializer/-/w3c-xmlserializer-5.0.0.tgz", diff --git a/package.json b/package.json index 27af37de..34e69495 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,6 @@ "@sentry/node": "^10.20.0", "@snazzah/davey": "^0.1.7", "@spotify/web-api-ts-sdk": "^1.2.0", - "better-sqlite3": "^12.4.1", "chrono-node": "^2.9.0", "comment-json": "^4.4.1", "croner": "^9.1.0", @@ -41,6 +40,7 @@ "graphviz-wasm": "^3.0.2", "jsdom": "^27.0.0", "kysely": "^0.28.8", + "kysely-generic-sqlite": "^1.2.1", "libsodium-wrappers": "^0.7.15", "pino": "^10.0.0", "pino-pretty": "^13.1.2", diff --git a/src/storage/db/db.ts b/src/storage/db/db.ts index 17c9018b..0ed0fae2 100644 --- a/src/storage/db/db.ts +++ b/src/storage/db/db.ts @@ -1,13 +1,15 @@ import { fileURLToPath, pathToFileURL } from "node:url"; import * as fs from "node:fs/promises"; import * as path from "node:path"; +import { DatabaseSync } from "node:sqlite"; -import { FileMigrationProvider, SqliteDialect, Kysely, Migrator } from "kysely"; -import SQLite from "better-sqlite3"; +import { FileMigrationProvider, Kysely, Migrator } from "kysely"; +import { GenericSqliteDialect } from "kysely-generic-sqlite"; import { captureException, startInactiveSpan } from "@sentry/node"; import type { Database } from "./model.js"; import datePlugin from "./date-plugin.js"; +import { createSqliteExecutor } from "./sqlite-dialect.js"; import { SqliteBooleanPlugin } from "./boolean-plugin.js"; import assertNever from "@/utils/assertNever.js"; import log from "@log"; @@ -21,12 +23,13 @@ export async function connectToDb(databasePath: string) { return; } - const nativeDb = new SQLite(databasePath); + const nativeDb = new DatabaseSync(databasePath, { + enableForeignKeyConstraints: true, + }); + const db = new Kysely({ plugins: [datePlugin, new SqliteBooleanPlugin()], - dialect: new SqliteDialect({ - database: nativeDb, - }), + dialect: new GenericSqliteDialect(() => createSqliteExecutor(nativeDb)), log: e => { const info = { sql: e.query.sql, @@ -66,8 +69,6 @@ export async function connectToDb(databasePath: string) { log.info("Connected to database."); - nativeDb.pragma("foreign_keys = ON"); - await runMigrationsIfNeeded(db); kysely = db; diff --git a/src/storage/db/sqlite-dialect.ts b/src/storage/db/sqlite-dialect.ts new file mode 100644 index 00000000..92b11ff7 --- /dev/null +++ b/src/storage/db/sqlite-dialect.ts @@ -0,0 +1,37 @@ +import { DatabaseSync } from "node:sqlite"; +import { Kysely } from "kysely"; +import { buildQueryFn, GenericSqliteDialect, parseBigInt } from "kysely-generic-sqlite"; +import type { IGenericSqlite } from "kysely-generic-sqlite"; + +const db = new Kysely({ + dialect: new GenericSqliteDialect(() => createSqliteExecutor(new DatabaseSync(":memory:"))), +}); + +export function createSqliteExecutor(db: DatabaseSync): IGenericSqlite { + const getStmt = (sql: string) => { + const stmt = db.prepare(sql); + stmt.setReadBigInts(false); + return stmt; + }; + + return { + db, + query: buildQueryFn({ + all: (sql, parameters = []) => getStmt(sql).all(...parameters), + run: (sql, parameters = []) => { + const { changes, lastInsertRowid } = getStmt(sql).run(...parameters); + return { + insertId: parseBigInt(lastInsertRowid), + numAffectedRows: parseBigInt(changes), + }; + }, + }), + close: () => db.close(), + iterator: (isSelect, sql, parameters = []) => { + if (!isSelect) { + throw new Error("Only support select in stream()"); + } + return getStmt(sql).iterate(...parameters) as any; + }, + }; +} From a06ef80eefd63661753efd612cab3f16aeff5d38 Mon Sep 17 00:00:00 2001 From: holzmaster Date: Thu, 16 Oct 2025 03:34:31 +0200 Subject: [PATCH 2/3] Add docs --- src/storage/db/sqlite-dialect.ts | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/storage/db/sqlite-dialect.ts b/src/storage/db/sqlite-dialect.ts index 92b11ff7..4ede1101 100644 --- a/src/storage/db/sqlite-dialect.ts +++ b/src/storage/db/sqlite-dialect.ts @@ -1,11 +1,9 @@ -import { DatabaseSync } from "node:sqlite"; -import { Kysely } from "kysely"; -import { buildQueryFn, GenericSqliteDialect, parseBigInt } from "kysely-generic-sqlite"; +import type { DatabaseSync } from "node:sqlite"; +import { buildQueryFn, parseBigInt } from "kysely-generic-sqlite"; import type { IGenericSqlite } from "kysely-generic-sqlite"; -const db = new Kysely({ - dialect: new GenericSqliteDialect(() => createSqliteExecutor(new DatabaseSync(":memory:"))), -}); +// Taken from: +// https://github.com/kysely-org/kysely/issues/1292#issuecomment-2670341588 export function createSqliteExecutor(db: DatabaseSync): IGenericSqlite { const getStmt = (sql: string) => { From 2c4ba89bbabad88e2dcbdd5b79ef86ad856538d0 Mon Sep 17 00:00:00 2001 From: holzmaster Date: Sun, 19 Oct 2025 22:08:51 +0200 Subject: [PATCH 3/3] Add new dialect --- package-lock.json | 10 - package.json | 1 - src/storage/db/db.ts | 5 +- src/storage/db/dialect/README.md | 1 + .../db/dialect/sqlite-dialect-config.ts | 25 +++ src/storage/db/dialect/sqlite-dialect.ts | 62 +++++++ src/storage/db/dialect/sqlite-driver.ts | 175 ++++++++++++++++++ src/storage/db/sqlite-dialect.ts | 35 ---- 8 files changed, 265 insertions(+), 49 deletions(-) create mode 100644 src/storage/db/dialect/README.md create mode 100644 src/storage/db/dialect/sqlite-dialect-config.ts create mode 100644 src/storage/db/dialect/sqlite-dialect.ts create mode 100644 src/storage/db/dialect/sqlite-driver.ts delete mode 100644 src/storage/db/sqlite-dialect.ts diff --git a/package-lock.json b/package-lock.json index b7c096a4..aafbf8fd 100644 --- a/package-lock.json +++ b/package-lock.json @@ -24,7 +24,6 @@ "graphviz-wasm": "^3.0.2", "jsdom": "^27.0.1", "kysely": "^0.28.8", - "kysely-generic-sqlite": "^1.2.1", "libsodium-wrappers": "^0.7.15", "pino": "^10.1.0", "pino-pretty": "^13.1.2", @@ -4036,15 +4035,6 @@ "node": ">=20.0.0" } }, - "node_modules/kysely-generic-sqlite": { - "version": "1.2.1", - "resolved": "https://registry.npmjs.org/kysely-generic-sqlite/-/kysely-generic-sqlite-1.2.1.tgz", - "integrity": "sha512-/Bs3/Uktn04nQ9g/4oSphLMEtSHkS5+j5hbKjK5gMqXQfqr/v3V3FKtoN4pLTmo2W35hNdrIpQnBukGL1zZc6g==", - "license": "MIT", - "peerDependencies": { - "kysely": ">=0.26" - } - }, "node_modules/lefthook": { "version": "1.13.6", "resolved": "https://registry.npmjs.org/lefthook/-/lefthook-1.13.6.tgz", diff --git a/package.json b/package.json index 8146cad1..d3456e79 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,6 @@ "graphviz-wasm": "^3.0.2", "jsdom": "^27.0.1", "kysely": "^0.28.8", - "kysely-generic-sqlite": "^1.2.1", "libsodium-wrappers": "^0.7.15", "pino": "^10.1.0", "pino-pretty": "^13.1.2", diff --git a/src/storage/db/db.ts b/src/storage/db/db.ts index 0ed0fae2..014e8cab 100644 --- a/src/storage/db/db.ts +++ b/src/storage/db/db.ts @@ -4,15 +4,14 @@ import * as path from "node:path"; import { DatabaseSync } from "node:sqlite"; import { FileMigrationProvider, Kysely, Migrator } from "kysely"; -import { GenericSqliteDialect } from "kysely-generic-sqlite"; import { captureException, startInactiveSpan } from "@sentry/node"; import type { Database } from "./model.js"; import datePlugin from "./date-plugin.js"; -import { createSqliteExecutor } from "./sqlite-dialect.js"; import { SqliteBooleanPlugin } from "./boolean-plugin.js"; import assertNever from "@/utils/assertNever.js"; import log from "@log"; +import { SqliteDialect } from "./dialect/sqlite-dialect.js"; let kysely: Kysely; @@ -29,7 +28,7 @@ export async function connectToDb(databasePath: string) { const db = new Kysely({ plugins: [datePlugin, new SqliteBooleanPlugin()], - dialect: new GenericSqliteDialect(() => createSqliteExecutor(nativeDb)), + dialect: new SqliteDialect({ database: nativeDb }), log: e => { const info = { sql: e.query.sql, diff --git a/src/storage/db/dialect/README.md b/src/storage/db/dialect/README.md new file mode 100644 index 00000000..247ed73d --- /dev/null +++ b/src/storage/db/dialect/README.md @@ -0,0 +1 @@ +This dialect was copied from some random issue. It's only needed until kysely comes with native support for node:sqlite. diff --git a/src/storage/db/dialect/sqlite-dialect-config.ts b/src/storage/db/dialect/sqlite-dialect-config.ts new file mode 100644 index 00000000..0ab88c6d --- /dev/null +++ b/src/storage/db/dialect/sqlite-dialect-config.ts @@ -0,0 +1,25 @@ +import type { DatabaseSync } from "node:sqlite"; +import type { DatabaseConnection } from "kysely"; + +/** + * Config for the SQLite dialect. + */ +export interface SqliteDialectConfig { + /** + * A node:sqlite DatabaseSync instance or a function that returns one. + * + * If a function is provided, it's called once when the first query is executed. + * + * https://nodejs.org/api/sqlite.html#class-databasesync + */ + database: + | InstanceType + | (() => Promise>); + + /** + * Called once when the first query is executed. + * + * This is a Kysely specific feature and does not come from the `node:sqlite` module. + */ + onCreateConnection?: (connection: DatabaseConnection) => Promise; +} diff --git a/src/storage/db/dialect/sqlite-dialect.ts b/src/storage/db/dialect/sqlite-dialect.ts new file mode 100644 index 00000000..af2ad201 --- /dev/null +++ b/src/storage/db/dialect/sqlite-dialect.ts @@ -0,0 +1,62 @@ +import { + type DatabaseIntrospector, + type Dialect, + type DialectAdapter, + type Kysely, + SqliteAdapter, + SqliteIntrospector, + SqliteQueryCompiler, + type Driver, + type QueryCompiler, +} from "kysely"; + +import type { SqliteDialectConfig } from "./sqlite-dialect-config.js"; +import { SqliteDriver } from "./sqlite-driver.js"; + +/** + * SQLite dialect that uses the [better-sqlite3](https://github.com/JoshuaWise/better-sqlite3) library. + * + * The constructor takes an instance of {@link SqliteDialectConfig}. + * + * ```ts + * import Database from 'better-sqlite3' + * + * new SqliteDialect({ + * database: new DatabaseSync('db.sqlite') + * }) + * ``` + * + * If you want the pool to only be created once it's first used, `database` + * can be a function: + * + * ```ts + * import Database from 'better-sqlite3' + * + * new SqliteDialect({ + * database: async () => new DatabaseSync('db.sqlite') + * }) + * ``` + */ +export class SqliteDialect implements Dialect { + readonly #config: SqliteDialectConfig; + + constructor(config: SqliteDialectConfig) { + this.#config = Object.freeze({ ...config }); + } + + createDriver(): Driver { + return new SqliteDriver(this.#config); + } + + createQueryCompiler(): QueryCompiler { + return new SqliteQueryCompiler(); + } + + createAdapter(): DialectAdapter { + return new SqliteAdapter(); + } + + createIntrospector(db: Kysely): DatabaseIntrospector { + return new SqliteIntrospector(db); + } +} diff --git a/src/storage/db/dialect/sqlite-driver.ts b/src/storage/db/dialect/sqlite-driver.ts new file mode 100644 index 00000000..82873138 --- /dev/null +++ b/src/storage/db/dialect/sqlite-driver.ts @@ -0,0 +1,175 @@ +import type { DatabaseSync, StatementSync } from "node:sqlite"; +import { + CompiledQuery, + createQueryId, + type QueryCompiler, + type QueryResult, + SelectQueryNode, + type DatabaseConnection, + type Driver, + RawNode, + IdentifierNode, +} from "kysely"; +import type { SqliteDialectConfig } from "./sqlite-dialect-config.js"; + +export class SqliteDriver implements Driver { + readonly #config: SqliteDialectConfig; + readonly #connectionMutex = new ConnectionMutex(); + + #db?: DatabaseSync; + #connection?: DatabaseConnection; + + constructor(config: SqliteDialectConfig) { + this.#config = Object.freeze({ ...config }); + } + + async init(): Promise { + this.#db = + typeof this.#config.database === "function" + ? await this.#config.database() + : this.#config.database; + + this.#connection = new SqliteConnection(this.#db); + + if (this.#config.onCreateConnection) { + await this.#config.onCreateConnection(this.#connection); + } + } + + async acquireConnection(): Promise { + // SQLite only has one single connection. We use a mutex here to wait + // until the single connection has been released. + await this.#connectionMutex.lock(); + // biome-ignore lint/style/noNonNullAssertion: :shrug: + return this.#connection!; + } + + async beginTransaction(connection: DatabaseConnection): Promise { + await connection.executeQuery(CompiledQuery.raw("begin")); + } + + async commitTransaction(connection: DatabaseConnection): Promise { + await connection.executeQuery(CompiledQuery.raw("commit")); + } + + async rollbackTransaction(connection: DatabaseConnection): Promise { + await connection.executeQuery(CompiledQuery.raw("rollback")); + } + + async savepoint( + connection: DatabaseConnection, + savepointName: string, + compileQuery: QueryCompiler["compileQuery"], + ): Promise { + await connection.executeQuery( + compileQuery(parseSavepointCommand("savepoint", savepointName), createQueryId()), + ); + } + + async rollbackToSavepoint( + connection: DatabaseConnection, + savepointName: string, + compileQuery: QueryCompiler["compileQuery"], + ): Promise { + await connection.executeQuery( + compileQuery(parseSavepointCommand("rollback to", savepointName), createQueryId()), + ); + } + + async releaseSavepoint( + connection: DatabaseConnection, + savepointName: string, + compileQuery: QueryCompiler["compileQuery"], + ): Promise { + await connection.executeQuery( + compileQuery(parseSavepointCommand("release", savepointName), createQueryId()), + ); + } + + async releaseConnection(): Promise { + this.#connectionMutex.unlock(); + } + + async destroy(): Promise { + this.#db?.close(); + } +} + +class SqliteConnection implements DatabaseConnection { + readonly #db: DatabaseSync; + + constructor(db: DatabaseSync) { + this.#db = db; + } + + executeQuery(compiledQuery: CompiledQuery): Promise> { + const { sql, parameters } = compiledQuery; + const stmt = this.#db.prepare(sql); + + const args = Array.isArray(parameters) ? parameters : []; + + if (stmt.columns().length > 0) { + const rows = stmt.all(...args) as O[]; + return Promise.resolve({ rows }); + } + + const result = stmt.run(...args); + + return Promise.resolve({ + numAffectedRows: BigInt(result.changes), + insertId: BigInt(result.lastInsertRowid), + rows: [], + }); + } + + async *streamQuery( + compiledQuery: CompiledQuery, + _chunkSize: number, + ): AsyncIterableIterator> { + const { sql, parameters, query } = compiledQuery; + const stmt: StatementSync = this.#db.prepare(sql); + const args = Array.isArray(parameters) ? parameters : []; + + if (!SelectQueryNode.is(query)) { + throw new Error("Sqlite driver only supports streaming of select queries"); + } + + const iter = stmt.iterate(...args) as IterableIterator; + for (const row of iter) { + yield { + rows: [row], + }; + } + } +} + +class ConnectionMutex { + #promise?: Promise; + #resolve?: () => void; + + async lock(): Promise { + while (this.#promise) { + await this.#promise; + } + + this.#promise = new Promise(resolve => { + this.#resolve = resolve; + }); + } + + unlock(): void { + const resolve = this.#resolve; + + this.#promise = undefined; + this.#resolve = undefined; + + resolve?.(); + } +} + +function parseSavepointCommand(command: string, savepointName: string): RawNode { + return RawNode.createWithChildren([ + RawNode.createWithSql(`${command} `), + IdentifierNode.create(savepointName), // ensures savepointName gets sanitized + ]); +} diff --git a/src/storage/db/sqlite-dialect.ts b/src/storage/db/sqlite-dialect.ts deleted file mode 100644 index 4ede1101..00000000 --- a/src/storage/db/sqlite-dialect.ts +++ /dev/null @@ -1,35 +0,0 @@ -import type { DatabaseSync } from "node:sqlite"; -import { buildQueryFn, parseBigInt } from "kysely-generic-sqlite"; -import type { IGenericSqlite } from "kysely-generic-sqlite"; - -// Taken from: -// https://github.com/kysely-org/kysely/issues/1292#issuecomment-2670341588 - -export function createSqliteExecutor(db: DatabaseSync): IGenericSqlite { - const getStmt = (sql: string) => { - const stmt = db.prepare(sql); - stmt.setReadBigInts(false); - return stmt; - }; - - return { - db, - query: buildQueryFn({ - all: (sql, parameters = []) => getStmt(sql).all(...parameters), - run: (sql, parameters = []) => { - const { changes, lastInsertRowid } = getStmt(sql).run(...parameters); - return { - insertId: parseBigInt(lastInsertRowid), - numAffectedRows: parseBigInt(changes), - }; - }, - }), - close: () => db.close(), - iterator: (isSelect, sql, parameters = []) => { - if (!isSelect) { - throw new Error("Only support select in stream()"); - } - return getStmt(sql).iterate(...parameters) as any; - }, - }; -}