feat(dx/desktop): perfect way to override bettersqlite native

This commit is contained in:
Elian Doran 2025-09-02 11:50:53 +03:00
parent 16beeb2e88
commit 7fdea613ff
No known key found for this signature in database
5 changed files with 19 additions and 10 deletions

View File

@ -9,7 +9,7 @@ const workspaceRoot = join(desktopProjectRoot, "../..");
function copyNativeDependencies() { function copyNativeDependencies() {
const destPath = join(desktopProjectRoot, "node_modules/better-sqlite3"); const destPath = join(desktopProjectRoot, "node_modules/better-sqlite3");
if (existsSync(destPath)) { if (existsSync(destPath)) {
rmSync(destPath, { recursive: true }); rmSync(destPath, { recursive: true });
} }
@ -30,10 +30,7 @@ function rebuildNativeDependencies() {
rebuild({ rebuild({
projectRootPath: desktopProjectRoot, projectRootPath: desktopProjectRoot,
buildPath: desktopProjectRoot, buildPath: desktopProjectRoot,
// on NixOS the prebuilt native fails with "Error: libstdc++.so.6: cannot open shared object file: No such file or directory" so we need to build from source. electronVersion
force: isNixOS(),
electronVersion: electronVersion,
buildFromSource: true
}); });
} }

View File

@ -17,6 +17,7 @@ execSync("electron ./src/main.ts", {
NODE_ENV: "development", NODE_ENV: "development",
TRILIUM_ENV: "dev", TRILIUM_ENV: "dev",
TRILIUM_DATA_DIR: "data", TRILIUM_DATA_DIR: "data",
TRILIUM_RESOURCE_DIR: "../server/src" TRILIUM_RESOURCE_DIR: "../server/src",
BETTERSQLITE3_NATIVE_PATH: join(projectRoot, "node_modules/better-sqlite3/build/Release/better_sqlite3.node")
} }
}); });

View File

@ -15,6 +15,10 @@ import becca_loader from "../becca/becca_loader.js";
import entity_changes from "./entity_changes.js"; import entity_changes from "./entity_changes.js";
import config from "./config.js"; import config from "./config.js";
const dbOpts: Database.Options = {
nativeBinding: process.env.BETTERSQLITE3_NATIVE_PATH || undefined
};
let dbConnection: DatabaseType = buildDatabase(); let dbConnection: DatabaseType = buildDatabase();
let statementCache: Record<string, Statement> = {}; let statementCache: Record<string, Statement> = {};
@ -23,15 +27,18 @@ function buildDatabase() {
if (process.env.TRILIUM_INTEGRATION_TEST === "memory") { if (process.env.TRILIUM_INTEGRATION_TEST === "memory") {
return buildIntegrationTestDatabase(); return buildIntegrationTestDatabase();
} else if (process.env.TRILIUM_INTEGRATION_TEST === "memory-no-store") { } else if (process.env.TRILIUM_INTEGRATION_TEST === "memory-no-store") {
return new Database(":memory:"); return new Database(":memory:", dbOpts);
} }
return new Database(dataDir.DOCUMENT_PATH, { readonly: config.General.readOnly }); return new Database(dataDir.DOCUMENT_PATH, {
...dbOpts,
readonly: config.General.readOnly
});
} }
function buildIntegrationTestDatabase(dbPath?: string) { function buildIntegrationTestDatabase(dbPath?: string) {
const dbBuffer = fs.readFileSync(dbPath ?? dataDir.DOCUMENT_PATH); const dbBuffer = fs.readFileSync(dbPath ?? dataDir.DOCUMENT_PATH);
return new Database(dbBuffer); return new Database(dbBuffer, dbOpts);
} }
function rebuildIntegrationTestDatabase(dbPath?: string) { function rebuildIntegrationTestDatabase(dbPath?: string) {

View File

@ -7,7 +7,10 @@ import sql_init from "../services/sql_init.js";
let dbConnection!: Database.Database; let dbConnection!: Database.Database;
sql_init.dbReady.then(() => { sql_init.dbReady.then(() => {
dbConnection = new Database(dataDir.DOCUMENT_PATH, { readonly: true }); dbConnection = new Database(dataDir.DOCUMENT_PATH, {
readonly: true,
nativeBinding: process.env.BETTERSQLITE3_NATIVE_PATH || undefined
});
[`exit`, `SIGINT`, `SIGUSR1`, `SIGUSR2`, `SIGTERM`].forEach((eventType) => { [`exit`, `SIGINT`, `SIGUSR1`, `SIGUSR2`, `SIGTERM`].forEach((eventType) => {
process.on(eventType, () => { process.on(eventType, () => {

View File

@ -14,6 +14,7 @@
"server:coverage": "nx test server --coverage", "server:coverage": "nx test server --coverage",
"server:start": "pnpm run --filter server dev", "server:start": "pnpm run --filter server dev",
"server:start-prod": "nx run server:start-prod", "server:start-prod": "nx run server:start-prod",
"desktop:start": "pnpm run --filter desktop dev",
"electron:build": "nx build desktop", "electron:build": "nx build desktop",
"chore:ci-update-nightly-version": "tsx ./scripts/update-nightly-version.ts", "chore:ci-update-nightly-version": "tsx ./scripts/update-nightly-version.ts",
"chore:generate-openapi": "tsx ./scripts/generate-openapi.ts", "chore:generate-openapi": "tsx ./scripts/generate-openapi.ts",