Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

server(themes): keep serving old theme db while a new one is being built

Abdullah Atta70cb39251973
diff --git a/servers/themes/src/api.ts b/servers/themes/src/api.ts
index 3fb03f8ea..2c96e41e0 100644
--- a/servers/themes/src/api.ts
+++ b/servers/themes/src/api.ts
@@ -45,7 +45,7 @@ export const ThemesAPI = router({
updateTotalInstalls(
theme,
await InstallsCounter.increment(theme.id, userId)
- );
+ ).catch((e) => console.error("Failed to update total installs:", e));
}
return theme;
}),
diff --git a/servers/themes/src/orama.ts b/servers/themes/src/orama.ts
index 96899e2a9..b1c338b77 100644
--- a/servers/themes/src/orama.ts
+++ b/servers/themes/src/orama.ts
@@ -22,7 +22,16 @@ import { ThemeQuerySchema } from "./schemas";
export let ThemesDatabase: Orama | null = null;
export async function initializeDatabase(): Promise<Orama> {
- ThemesDatabase = await create({
+ ThemesDatabase = await createDatabase();
+ return ThemesDatabase;
+}
+
+export function setDatabase(db: Orama) {
+ ThemesDatabase = db;
+}
+
+export function createDatabase(): Promise<Orama> {
+ return create({
schema: {
id: "string",
name: "string",
@@ -35,7 +44,6 @@ export async function initializeDatabase(): Promise<Orama> {
},
id: "notesnook-themes"
});
- return ThemesDatabase;
}
export async function findTheme(
diff --git a/servers/themes/src/server.ts b/servers/themes/src/server.ts
index 63895bfd5..282f8b058 100644
--- a/servers/themes/src/server.ts
+++ b/servers/themes/src/server.ts
@@ -31,10 +31,11 @@ const HOST = process.env.HOST || "localhost";
server.listen(PORT, HOST);
console.log(`Server started successfully on: http://${HOST}:${PORT}/`);
-syncThemes();
-setInterval(() => {
- syncThemes();
-}, 1000 * 60 * 60); // every hour
+function sync() {
+ syncThemes().catch((e) => console.error("Failed to sync themes:", e));
+}
+sync();
+setInterval(sync, 1000 * 60 * 60); // every hour
if (import.meta.hot) {
import.meta.hot.on("vite:beforeFullReload", () => {
diff --git a/servers/themes/src/sync.ts b/servers/themes/src/sync.ts
index 576ae215e..5c1e9e65c 100644
--- a/servers/themes/src/sync.ts
+++ b/servers/themes/src/sync.ts
@@ -26,7 +26,7 @@ import {
THEME_REPO_DIR_PATH
} from "./constants";
import { insertMultiple } from "@orama/orama";
-import { initializeDatabase } from "./orama";
+import { createDatabase, setDatabase } from "./orama";
import {
ThemeCompatibilityVersion,
ThemeDefinition,
@@ -66,7 +66,7 @@ export async function syncThemes() {
async function generateThemesMetadata() {
const themeDefinitions: CompiledThemeDefinition[] = [];
- const db = await initializeDatabase();
+ const db = await createDatabase();
const THEMES_PATH = path.join(THEME_REPO_DIR_PATH, "themes");
const themes = await readdir(THEMES_PATH);
@@ -107,5 +107,6 @@ async function generateThemesMetadata() {
theme.totalInstalls = installs[theme.id] || 0;
}
await insertMultiple(db, themeDefinitions);
+ setDatabase(db);
console.log("Metadata generated and cached.");
}