feat(state): replace in-memory Map with SQLite-backed stateService (#59)
- Create src/services/stateService.js with get/set/delete/has API - Create migration 004_user_states.js (chat_id PK, state_data JSON, updated_at) - TTL of 24 hours — expired states auto-deleted - Cleanup job runs every hour (setInterval) - Replace src/context/userStates.js Map with async stateService proxy - Add await to all 45 userStates.get/set/delete/has calls across 13 files - Add initStates() call in index.js startup sequence - All state survives bot restarts now 18 files changed, 172 insertions, 46 deletions
This commit is contained in:
10
src/migrations/004_user_states.js
Normal file
10
src/migrations/004_user_states.js
Normal file
@@ -0,0 +1,10 @@
|
||||
export default async function migration004(db) {
|
||||
await db.runAsync(`
|
||||
CREATE TABLE IF NOT EXISTS user_states (
|
||||
chat_id TEXT PRIMARY KEY,
|
||||
state_data TEXT NOT NULL,
|
||||
updated_at INTEGER NOT NULL
|
||||
)
|
||||
`);
|
||||
console.log('Migration 004: user_states table created');
|
||||
}
|
||||
@@ -38,6 +38,7 @@ export async function runMigrations() {
|
||||
(await import('./001_initial_schema.js')).default,
|
||||
(await import('./002_add_columns.js')).default,
|
||||
(await import('./003_add_indexes.js')).default,
|
||||
(await import('./004_user_states.js')).default,
|
||||
];
|
||||
|
||||
for (let i = currentVersion; i < migrations.length; i++) {
|
||||
|
||||
Reference in New Issue
Block a user