diff --git a/app/back-end/app.js b/app/back-end/app.js index 3381ac060..a6ae8b528 100644 --- a/app/back-end/app.js +++ b/app/back-end/app.js @@ -5,7 +5,7 @@ // Necessary packages const fs = require('fs-extra'); const path = require('path'); -const sqlite = require('better-sqlite3'); +const { Database } = require('node-sqlite3-wasm'); const compare = require('node-version-compare'); const normalizePath = require('normalize-path'); const url = require('url'); @@ -19,6 +19,7 @@ const Themes = require('./themes.js'); const Languages = require('./languages.js'); const Plugins = require('./plugins.js'); // Helper classes +const DBUtils = require('./helpers/db.utils.js'); const Site = require('./site.js'); const Utils = require('./helpers/utils.js'); // List of the Event classes @@ -215,7 +216,7 @@ class App { this.db.close(); } - this.db = new sqlite(dbPath); + this.db = new DBUtils(new Database(dbPath)); let tags = new Tags(this, {site}); let posts = new Posts(this, {site}); let authors = new Authors(this, {site}); diff --git a/app/back-end/builddata.json b/app/back-end/builddata.json index f42550deb..39ef323e1 100644 --- a/app/back-end/builddata.json +++ b/app/back-end/builddata.json @@ -1,4 +1,4 @@ { "version": "0.44.1", - "build": 16521 + "build": 16523 } \ No newline at end of file diff --git a/app/back-end/events/site.js b/app/back-end/events/site.js index 5eb76694c..1a7d193a9 100644 --- a/app/back-end/events/site.js +++ b/app/back-end/events/site.js @@ -5,7 +5,8 @@ const passwordSafeStorage = require('keytar'); const ipcMain = require('electron').ipcMain; const Site = require('../site.js'); const Themes = require('../themes.js'); -const sqlite = require('better-sqlite3'); +const { Database } = require('node-sqlite3-wasm'); +const DBUtils = require('../helpers/db.utils.js'); const UtilsHelper = require('../helpers/utils.js'); const normalizePath = require('normalize-path'); const URLHelper = require('../modules/render-html/helpers/url.js'); @@ -78,7 +79,7 @@ class SiteEvents { ); let dbPath = path.join(appInstance.sitesDir, config.settings.name, 'input', 'db.sqlite'); - appInstance.db = new sqlite(dbPath); + appInstance.db = new DBUtils(new Database(dbPath)); // Rename also the backups directory let backupsDir = appInstance.appConfig.backupsLocation; @@ -430,7 +431,7 @@ class SiteEvents { appInstance.db.close(); } - appInstance.db = new sqlite(dbPath); + appInstance.db = new DBUtils(new Database(dbPath)); result = { siteConfig: appInstance.sites[config.name], diff --git a/app/back-end/helpers/db.utils.js b/app/back-end/helpers/db.utils.js new file mode 100644 index 000000000..88b56e0d5 --- /dev/null +++ b/app/back-end/helpers/db.utils.js @@ -0,0 +1,66 @@ +/* + * Other helper functions + */ +class DBUtils { + constructor (dbInstance) { + this.DB = dbInstance; + this.statement = ''; + } + + prepare (sqlStatement) { + this.statement = sqlStatement; + return this; + } + + get (paramsObject = null) { + if (paramsObject) { + paramsObject = this.transformParams(paramsObject); + return this.DB.get(this.statement, paramsObject); + } + + return this.DB.get(this.statement); + } + + run (paramsObject = null) { + if (paramsObject) { + paramsObject = this.transformParams(paramsObject); + return this.DB.run(this.statement, paramsObject); + } + + return this.DB.run(this.statement); + } + + all (paramsObject = null) { + if (paramsObject) { + paramsObject = this.transformParams(paramsObject); + return this.DB.all(this.statement, paramsObject); + } + + return this.DB.all(this.statement); + } + + exec (sqlQueries) { + this.DB.exec(sqlQueries); + } + + close () { + this.DB.close(); + } + + /** + * Prefix all params in object with "@" + */ + transformParams (paramsObject) { + const newParamsObject = {}; + + for (const key in paramsObject) { + if (paramsObject.hasOwnProperty(key)) { + newParamsObject["@" + key] = paramsObject[key]; + } + } + + return newParamsObject; + } +} + +module.exports = DBUtils; diff --git a/app/back-end/migrators/site-config.js b/app/back-end/migrators/site-config.js index ab417eef6..72725fcf3 100644 --- a/app/back-end/migrators/site-config.js +++ b/app/back-end/migrators/site-config.js @@ -1,7 +1,8 @@ const fs = require('fs'); const path = require('path'); const slug = require('./../helpers/slug'); -const sqlite = require('better-sqlite3'); +const { Database } = require('node-sqlite3-wasm'); +const DBUtils = require('../helpers/db.utils.js'); class SiteConfigMigrator { static moveOldAuthorData(appInstance, siteConfig) { @@ -16,7 +17,7 @@ class SiteConfigMigrator { // If yes - save them in database as author with ID = 1 let siteDir = path.join(appInstance.sitesDir, siteConfig.name); let dbPath = path.join(siteDir, 'input', 'db.sqlite'); - let db = new sqlite(dbPath); + let db = new DBUtils(new Database(dbPath)); let newAuthorName = siteConfig.author.name; let newAuthorUsername = slug(newAuthorName); let newAuthorConfig = { diff --git a/app/back-end/modules/import/import.js b/app/back-end/modules/import/import.js index 0efd1d4c0..ad087f3e8 100644 --- a/app/back-end/modules/import/import.js +++ b/app/back-end/modules/import/import.js @@ -5,7 +5,8 @@ const fs = require('fs-extra'); const path = require('path'); const WxrParser = require('./wxr-parser'); -const sqlite = require('better-sqlite3'); +const { Database } = require('node-sqlite3-wasm'); +const DBUtils = require('../../helpers/db.utils.js'); class Import { /** @@ -39,7 +40,7 @@ class Import { this.appInstance.db.close(); } - this.appInstance.db = new sqlite(dbPath); + this.appInstance.db = new DBUtils(new Database(dbPath)); } /** diff --git a/app/back-end/modules/render-html/renderer.js b/app/back-end/modules/render-html/renderer.js index 285850292..e6889f1ef 100644 --- a/app/back-end/modules/render-html/renderer.js +++ b/app/back-end/modules/render-html/renderer.js @@ -7,8 +7,8 @@ const CleanCSS = require('clean-css'); const normalizePath = require('normalize-path'); // Internal packages -const slug = require('./../../helpers/slug'); -const sqlite = require('better-sqlite3'); +const DBUtils = require('./../../helpers/db.utils.js'); +const { Database } = require('node-sqlite3-wasm'); const URLHelper = require('./helpers/url.js'); const FilesHelper = require('./helpers/files.js'); const ViewSettingsHelper = require('./helpers/view-settings.js'); @@ -584,7 +584,7 @@ class Renderer { */ loadDataFromDB() { const dbPath = path.join(this.inputDir, 'db.sqlite'); - this.db = new sqlite(dbPath); + this.db = new DBUtils(new Database(dbPath)); } /* diff --git a/app/back-end/site.js b/app/back-end/site.js index 130acaf83..0b940124c 100644 --- a/app/back-end/site.js +++ b/app/back-end/site.js @@ -5,7 +5,8 @@ const fs = require('fs-extra'); const os = require('os'); const path = require('path'); -const sqlite = require('better-sqlite3'); +const { Database } = require('node-sqlite3-wasm'); +const DBUtils = require('./helpers/db.utils.js'); const Themes = require('./themes.js'); const Image = require('./image.js'); const UtilsHelper = require('./helpers/utils'); @@ -122,7 +123,7 @@ class Site { let dbPath = path.join(this.siteDir, 'input', 'db.sqlite'); try { - let db = new sqlite(dbPath); + let db = new DBUtils(new Database(dbPath)); db.exec(fs.readFileSync(this.application.basedir + '/back-end/sql/1.0.0.sql', 'utf8')); db.close(); } catch (error) { @@ -138,7 +139,7 @@ class Site { */ createAuthor(authorName) { let dbPath = path.join(this.siteDir, 'input', 'db.sqlite'); - let db = new sqlite(dbPath); + let db = new DBUtils(new Database(dbPath)); let sqlQuery = db.prepare(`INSERT INTO authors VALUES(1, @name, @slug, '', '{}', '{}')`); sqlQuery.run({ name: authorName, @@ -223,7 +224,7 @@ class Site { let themesHelper = new Themes(this.application, { site: this.name }); let themeName = themesHelper.currentTheme(); let dbPath = path.join(this.siteDir, 'input', 'db.sqlite'); - let db = new sqlite(dbPath); + let db = new DBUtils(new Database(dbPath)); // If there is no theme selected - abort if(themeName === 'not selected') { diff --git a/app/package-lock.json b/app/package-lock.json index 5ef56ba6f..4b2c723eb 100644 --- a/app/package-lock.json +++ b/app/package-lock.json @@ -14,7 +14,6 @@ "@google-cloud/storage": "6.11.0", "adm-zip": "0.5.10", "archiver": "5.3.1", - "better-sqlite3": "8.1.0", "clean-css": "5.3.2", "codemirror": "5.65.13", "codemirror-revisedsearch": "1.0.12", @@ -41,6 +40,7 @@ "marked": "5.1.1", "mime": "3.0.0", "moment": "2.29.4", + "node-sqlite3-wasm": "0.8.2", "node-version-compare": "1.0.3", "normalize-path": "3.0.0", "prismjs": "1.29.0", @@ -2409,16 +2409,6 @@ "tweetnacl": "^0.14.3" } }, - "node_modules/better-sqlite3": { - "version": "8.1.0", - "resolved": "https://registry.npmjs.org/better-sqlite3/-/better-sqlite3-8.1.0.tgz", - "integrity": "sha512-p1m09H+Oi8R9TPj810pdNswMFuVgRNgCJEWypp6jlkOgSwMIrNyuj3hW78xEuBRGok5RzeaUW8aBtTWF3l/TQA==", - "hasInstallScript": true, - "dependencies": { - "bindings": "^1.5.0", - "prebuild-install": "^7.1.0" - } - }, "node_modules/bignumber.js": { "version": "9.1.1", "resolved": "https://registry.npmjs.org/bignumber.js/-/bignumber.js-9.1.1.tgz", @@ -2427,14 +2417,6 @@ "node": "*" } }, - "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==", - "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", @@ -3453,11 +3435,6 @@ "url": "https://github.com/sindresorhus/file-type?sponsor=1" } }, - "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==" - }, "node_modules/filter-obj": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/filter-obj/-/filter-obj-1.1.0.tgz", @@ -4983,6 +4960,11 @@ "node": ">= 6.13.0" } }, + "node_modules/node-sqlite3-wasm": { + "version": "0.8.2", + "resolved": "https://registry.npmjs.org/node-sqlite3-wasm/-/node-sqlite3-wasm-0.8.2.tgz", + "integrity": "sha512-XRCG473M0LpxQo12X7YAP3Qr6Umr+KklhVZCoajPkpp9w6J6FOq33tfbfn8taWmegugV116dWvfE3MLbdyiHEg==" + }, "node_modules/node-version-compare": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/node-version-compare/-/node-version-compare-1.0.3.tgz", @@ -8735,28 +8717,11 @@ "tweetnacl": "^0.14.3" } }, - "better-sqlite3": { - "version": "8.1.0", - "resolved": "https://registry.npmjs.org/better-sqlite3/-/better-sqlite3-8.1.0.tgz", - "integrity": "sha512-p1m09H+Oi8R9TPj810pdNswMFuVgRNgCJEWypp6jlkOgSwMIrNyuj3hW78xEuBRGok5RzeaUW8aBtTWF3l/TQA==", - "requires": { - "bindings": "^1.5.0", - "prebuild-install": "^7.1.0" - } - }, "bignumber.js": { "version": "9.1.1", "resolved": "https://registry.npmjs.org/bignumber.js/-/bignumber.js-9.1.1.tgz", "integrity": "sha512-pHm4LsMJ6lzgNGVfZHjMoO8sdoRhOzOH4MLmY65Jg70bpxCKu5iOHNJyfF6OyvYw7t8Fpf35RuzUyqnQsj8Vig==" }, - "bindings": { - "version": "1.5.0", - "resolved": "https://registry.npmjs.org/bindings/-/bindings-1.5.0.tgz", - "integrity": "sha512-p2q/t/mhvuOj/UeLlV6566GD/guowlr0hHxClI0W9m7MWYkL1F0hLo+0Aexs9HSPCtR1SXQ0TD3MMKrXZajbiQ==", - "requires": { - "file-uri-to-path": "1.0.0" - } - }, "bl": { "version": "4.1.0", "resolved": "https://registry.npmjs.org/bl/-/bl-4.1.0.tgz", @@ -9545,11 +9510,6 @@ "token-types": "^4.1.1" } }, - "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==" - }, "filter-obj": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/filter-obj/-/filter-obj-1.1.0.tgz", @@ -10781,6 +10741,11 @@ "resolved": "https://registry.npmjs.org/node-forge/-/node-forge-1.3.1.tgz", "integrity": "sha512-dPEtOeMvF9VMcYV/1Wb8CPoVAXtp6MKMlcbAt4ddqmGqUJ6fQZFXkNZNkNlfevtNkGtaSoXf/vNNNSvgrdXwtA==" }, + "node-sqlite3-wasm": { + "version": "0.8.2", + "resolved": "https://registry.npmjs.org/node-sqlite3-wasm/-/node-sqlite3-wasm-0.8.2.tgz", + "integrity": "sha512-XRCG473M0LpxQo12X7YAP3Qr6Umr+KklhVZCoajPkpp9w6J6FOq33tfbfn8taWmegugV116dWvfE3MLbdyiHEg==" + }, "node-version-compare": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/node-version-compare/-/node-version-compare-1.0.3.tgz", diff --git a/app/package.json b/app/package.json index 7b6b7b417..efcae5648 100644 --- a/app/package.json +++ b/app/package.json @@ -30,7 +30,6 @@ "@google-cloud/storage": "6.11.0", "adm-zip": "0.5.10", "archiver": "5.3.1", - "better-sqlite3": "8.1.0", "clean-css": "5.3.2", "codemirror": "5.65.13", "codemirror-revisedsearch": "1.0.12", @@ -57,6 +56,7 @@ "marked": "5.1.1", "mime": "3.0.0", "moment": "2.29.4", + "node-sqlite3-wasm": "0.8.2", "node-version-compare": "1.0.3", "normalize-path": "3.0.0", "prismjs": "1.29.0", diff --git a/package.json b/package.json index c969faecf..2758b8cd0 100644 --- a/package.json +++ b/package.json @@ -70,8 +70,7 @@ "default-files/gdpr-assets/**/*", "default-files/vendor/**/*", "node_modules/sharp/**/*", - "node_modules/keytar/**/*", - "node_modules/better-sqlite3/**/*" + "node_modules/keytar/**/*" ], "afterPack": "./build/scripts/afterPack.js", "files": [