Skip to content

Commit

Permalink
feat: migrate better-sqlite3 to node-sqlite3-wasm (#1294)
Browse files Browse the repository at this point in the history
  • Loading branch information
dziudek committed Nov 19, 2023
1 parent fc8d3d3 commit 713d1cb
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 66 deletions.
5 changes: 3 additions & 2 deletions app/back-end/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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
Expand Down Expand Up @@ -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});
Expand Down
2 changes: 1 addition & 1 deletion app/back-end/builddata.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"version": "0.44.1",
"build": 16521
"build": 16523
}
7 changes: 4 additions & 3 deletions app/back-end/events/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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],
Expand Down
66 changes: 66 additions & 0 deletions app/back-end/helpers/db.utils.js
Original file line number Diff line number Diff line change
@@ -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;
5 changes: 3 additions & 2 deletions app/back-end/migrators/site-config.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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 = {
Expand Down
5 changes: 3 additions & 2 deletions app/back-end/modules/import/import.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
/**
Expand Down Expand Up @@ -39,7 +40,7 @@ class Import {
this.appInstance.db.close();
}

this.appInstance.db = new sqlite(dbPath);
this.appInstance.db = new DBUtils(new Database(dbPath));
}

/**
Expand Down
6 changes: 3 additions & 3 deletions app/back-end/modules/render-html/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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));
}

/*
Expand Down
9 changes: 5 additions & 4 deletions app/back-end/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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) {
Expand All @@ -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,
Expand Down Expand Up @@ -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') {
Expand Down
57 changes: 11 additions & 46 deletions app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down

0 comments on commit 713d1cb

Please sign in to comment.