mirror of
https://github.com/tiennm99/miti99bot.git
synced 2026-04-17 19:22:09 +00:00
feat: add D1 storage layer with per-module migration runner
- SqlStore interface + CF D1 wrapper + per-module factory (table prefix convention)
- init signature extended to ({ db, sql, env }); sql is null when DB binding absent
- custom migration runner walks src/modules/*/migrations/*.sql, tracks applied in _migrations table
- npm run db:migrate with --dry-run and --local flags; chained into deploy
- fake-d1 test helper with subset of SQL semantics for retention and history tests
This commit is contained in:
17
src/bot.js
17
src/bot.js
@@ -9,12 +9,29 @@
|
||||
|
||||
import { Bot } from "grammy";
|
||||
import { installDispatcher } from "./modules/dispatcher.js";
|
||||
import { getCurrentRegistry } from "./modules/registry.js";
|
||||
|
||||
/** @type {Bot | null} */
|
||||
let botInstance = null;
|
||||
/** @type {Promise<Bot> | null} */
|
||||
let botInitPromise = null;
|
||||
|
||||
/**
|
||||
* Returns the memoized registry, building it (and the bot) if needed.
|
||||
* Shares the same instance used by the fetch handler so scheduled() and
|
||||
* fetch() operate on identical registry state within a warm instance.
|
||||
*
|
||||
* @param {any} env
|
||||
* @returns {Promise<import("./modules/registry.js").Registry>}
|
||||
*/
|
||||
export async function getRegistry(env) {
|
||||
// If the bot is already initialised the registry was built as a side effect.
|
||||
if (botInstance) return getCurrentRegistry();
|
||||
// Otherwise bootstrap via getBot (which calls buildRegistry internally).
|
||||
await getBot(env);
|
||||
return getCurrentRegistry();
|
||||
}
|
||||
|
||||
/**
|
||||
* Fail fast if any required env var is missing — better a 500 on first webhook
|
||||
* than a confusing runtime error inside grammY.
|
||||
|
||||
Reference in New Issue
Block a user