Skip to content

Build your personal knowledge base with Trilium Notes

fix(share): handle all DB resolution cases for share module

Elian Doran646180db1337
diff --git a/apps/server/src/share/sql.ts b/apps/server/src/share/sql.ts
index d141e7e28c..8d41210c76 100644
--- a/apps/server/src/share/sql.ts
+++ b/apps/server/src/share/sql.ts
@@ -1,32 +1,39 @@
"use strict";
import Database from "better-sqlite3";
+import { existsSync } from "fs";
import dataDir from "../services/data_dir.js";
import sql_init from "../services/sql_init.js";
let dbConnection!: Database.Database;
let dbConnectionReady = false;
-sql_init.dbReady.then(() => {
- if (process.env.TRILIUM_INTEGRATION_TEST === "memory") {
- // In-memory test mode loads the DB from a buffer, so there is no
- // on-disk file at DOCUMENT_PATH. Open the original fixture file
- // directly so share routes still work in tests.
+function resolveDbPath(): string | null {
+ // Prefer the on-disk DB at DOCUMENT_PATH (production + e2e tests).
+ if (existsSync(dataDir.DOCUMENT_PATH)) {
+ return dataDir.DOCUMENT_PATH;
+ }
+
+ // In unit tests the main connection is in-memory, so DOCUMENT_PATH
+ // doesn't exist. Fall back to the fixture file from source.
+ if (process.env.TRILIUM_INTEGRATION_TEST) {
try {
- const fixturePath = require.resolve("@triliumnext/core/src/test/fixtures/document.db");
- dbConnection = new Database(fixturePath, {
- readonly: true,
- nativeBinding: process.env.BETTERSQLITE3_NATIVE_PATH || undefined
- });
- dbConnectionReady = true;
+ return require.resolve("@triliumnext/core/src/test/fixtures/document.db");
} catch {
- // Fixture not available (e.g. bundled/standalone) — share
- // endpoints will return 503 via the isShareDbReady() guard.
+ // Not available (e.g. bundled build) — share will return 503.
}
+ }
+
+ return null;
+}
+
+sql_init.dbReady.then(() => {
+ const dbPath = resolveDbPath();
+ if (!dbPath) {
return;
}
- dbConnection = new Database(dataDir.DOCUMENT_PATH, {
+ dbConnection = new Database(dbPath, {
readonly: true,
nativeBinding: process.env.BETTERSQLITE3_NATIVE_PATH || undefined
});