fix deployed server database access crashes

- Create database proxy for deployed servers to handle null database gracefully
- Pass database connection (or proxy) to deployed server init function
- Deployed servers now get meaningful error messages instead of crashing
- Ensures app stability when database connection is unavailable

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
rizky 2025-09-10 09:37:56 +07:00
parent 74a70e99ca
commit 4af0fc3e52
1 changed files with 25 additions and 2 deletions

View File

@ -14,6 +14,27 @@ import { g } from "./global";
import { gunzipAsync } from "./gzip"; import { gunzipAsync } from "./gzip";
const decoder = new TextDecoder(); const decoder = new TextDecoder();
const createDbProxy = () => {
return new Proxy({}, {
get(target, prop) {
if (!g.db) {
if (typeof prop === 'string') {
return new Proxy({}, {
get(target, method) {
return async () => {
throw new Error(`Database connection not available. Cannot execute ${prop}.${String(method)}`);
};
}
});
}
return undefined;
}
return g.db[prop];
}
});
};
export const deploy = { export const deploy = {
async init(load_from?: string) { async init(load_from?: string) {
await dirAsync(dir(`app/web/deploy`)); await dirAsync(dir(`app/web/deploy`));
@ -112,13 +133,15 @@ export const deploy = {
} }
} }
const dbProxy = createDbProxy();
if (g.server) { if (g.server) {
await g.deploy.server?.init?.({ port: g.server.port }); await g.deploy.server?.init?.({ port: g.server.port, db: dbProxy });
} else { } else {
const inv = setInterval(async () => { const inv = setInterval(async () => {
if (g.server) { if (g.server) {
clearInterval(inv); clearInterval(inv);
await g.deploy.server?.init?.({ port: g.server.port }); await g.deploy.server?.init?.({ port: g.server.port, db: dbProxy });
} }
}, 1000); }, 1000);
} }