Skip to content

Build your personal knowledge base with Trilium Notes

fix(server): tests failing due to SQL initialization order

Elian Dorand35b55f7d31d
diff --git a/apps/server/src/services/sql.ts b/apps/server/src/services/sql.ts
index ceff5a8917..6b913ea3ff 100644
--- a/apps/server/src/services/sql.ts
+++ b/apps/server/src/services/sql.ts
@@ -1,3 +1,17 @@
import { getSql } from "@triliumnext/core";
-export default getSql();
+// Lazy proxy: defers getSql() until first property access. Without this,
+// any static import chain that reaches this file crashes if initSql() has
+// not been called yet. The server avoids that today only because main.ts
+// is careful to use dynamic imports for everything that transitively
+// touches sql — an unenforced invariant. Tests hit the trap because
+// vitest's beforeAll runs after the test file's static imports resolve.
+const sqlProxy = new Proxy({} as ReturnType<typeof getSql>, {
+ get(_target, prop, receiver) {
+ const sql = getSql();
+ const value = Reflect.get(sql, prop, receiver);
+ return typeof value === "function" ? value.bind(sql) : value;
+ }
+});
+
+export default sqlProxy;