fix: load persisted docs before module onInit to prevent re-seeding
Module onInit functions (rvote, rtasks, rcal, etc.) call seedDemoIfEmpty which checks the sync server for existing docs. Previously onInit ran as an IIFE before loadAllDocs completed, so it always found empty docs and re-seeded demo data — overwriting user deletions/changes. Now onInit runs inside the loadAllDocs .then() chain, ensuring persisted data is loaded before any seed checks run. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
df8901c975
commit
7f98dfcdb1
|
|
@ -3992,33 +3992,35 @@ const server = Bun.serve<WSData>({
|
||||||
|
|
||||||
// ── Startup ──
|
// ── Startup ──
|
||||||
|
|
||||||
// Call onInit for each module that defines it (schema registration, DB init, etc.)
|
|
||||||
(async () => {
|
|
||||||
for (const mod of getAllModules()) {
|
|
||||||
if (mod.onInit) {
|
|
||||||
try {
|
|
||||||
await mod.onInit({ syncServer });
|
|
||||||
console.log(`[Init] ${mod.name} initialized`);
|
|
||||||
} catch (e) {
|
|
||||||
console.error(`[Init] ${mod.name} failed:`, e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Pass syncServer to OAuth handlers
|
|
||||||
setNotionOAuthSyncServer(syncServer);
|
|
||||||
setGoogleOAuthSyncServer(syncServer);
|
|
||||||
setClickUpOAuthSyncServer(syncServer);
|
|
||||||
setOAuthStatusSyncServer(syncServer);
|
|
||||||
})();
|
|
||||||
|
|
||||||
// Ensure generated files directory exists
|
// Ensure generated files directory exists
|
||||||
import { mkdirSync } from "node:fs";
|
import { mkdirSync } from "node:fs";
|
||||||
try { mkdirSync(resolve(process.env.FILES_DIR || "./data/files", "generated"), { recursive: true }); } catch {}
|
try { mkdirSync(resolve(process.env.FILES_DIR || "./data/files", "generated"), { recursive: true }); } catch {}
|
||||||
|
|
||||||
ensureDemoCommunity().then(() => console.log("[Demo] Demo community ready")).catch((e) => console.error("[Demo] Failed:", e));
|
|
||||||
import { initTokenService, seedCUSDC } from "./token-service";
|
import { initTokenService, seedCUSDC } from "./token-service";
|
||||||
|
|
||||||
|
// IMPORTANT: Load persisted docs FIRST, then run module onInit + seeding.
|
||||||
|
// Previously onInit ran before loadAllDocs, causing seed functions to see
|
||||||
|
// empty docs and re-create demo data that users had already deleted.
|
||||||
|
ensureDemoCommunity().then(() => console.log("[Demo] Demo community ready")).catch((e) => console.error("[Demo] Failed:", e));
|
||||||
loadAllDocs(syncServer)
|
loadAllDocs(syncServer)
|
||||||
.then(async () => {
|
.then(async () => {
|
||||||
|
// Now that persisted docs are loaded, init modules (which may seed)
|
||||||
|
for (const mod of getAllModules()) {
|
||||||
|
if (mod.onInit) {
|
||||||
|
try {
|
||||||
|
await mod.onInit({ syncServer });
|
||||||
|
console.log(`[Init] ${mod.name} initialized`);
|
||||||
|
} catch (e) {
|
||||||
|
console.error(`[Init] ${mod.name} failed:`, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Pass syncServer to OAuth handlers
|
||||||
|
setNotionOAuthSyncServer(syncServer);
|
||||||
|
setGoogleOAuthSyncServer(syncServer);
|
||||||
|
setClickUpOAuthSyncServer(syncServer);
|
||||||
|
setOAuthStatusSyncServer(syncServer);
|
||||||
|
|
||||||
// Seed all modules' demo data so /demo routes always have content
|
// Seed all modules' demo data so /demo routes always have content
|
||||||
for (const mod of getAllModules()) {
|
for (const mod of getAllModules()) {
|
||||||
if (mod.seedTemplate) {
|
if (mod.seedTemplate) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue