Total refactoring performed
This commit is contained in:
24
server/db.ts
Normal file
24
server/db.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import Database from 'better-sqlite3';
|
||||
|
||||
const db = new Database('session_state.db');
|
||||
|
||||
// Initialize tables
|
||||
db.exec(`
|
||||
CREATE TABLE IF NOT EXISTS sessions (
|
||||
id TEXT PRIMARY KEY,
|
||||
state TEXT NOT NULL,
|
||||
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||
)
|
||||
`);
|
||||
|
||||
const getSessionStmt = db.prepare('SELECT state FROM sessions WHERE id = ?');
|
||||
const upsertSessionStmt = db.prepare('INSERT INTO sessions (id, state) VALUES (?, ?) ON CONFLICT(id) DO UPDATE SET state = excluded.state, updated_at = CURRENT_TIMESTAMP');
|
||||
|
||||
export function getPersistedSession(id: string) {
|
||||
const row = getSessionStmt.get(id) as { state: string } | undefined;
|
||||
return row ? JSON.parse(row.state) : null;
|
||||
}
|
||||
|
||||
export function persistSession(id: string, state: any) {
|
||||
upsertSessionStmt.run(id, JSON.stringify(state));
|
||||
}
|
||||
Reference in New Issue
Block a user