add projectId parameter to session state storage

This commit is contained in:
Chris Scott committed 2026-02-25 15:42:26 -05:00
1 parent 567fa6f415
commit 8323da1151
2 files changed
+65 -6

No files matched your search

+32 -6
View File
@@ -5,6 +5,12 @@ import type { SessionState, PlanningState, PreCompactionSnapshot } from '../type
const DEFAULT_SESSION_TTL = 7 * 24 * 60 * 60 * 1000
const DEFAULT_SNAPSHOT_TTL = 24 * 60 * 60 * 1000
export interface PlanningStateResult {
sessionId: string
planningState: PlanningState
updatedAt: number
}
export class SessionStateService {
private queries: ReturnType<typeof createSessionStateQueries>
private cleanupInterval: ReturnType<typeof setInterval> | null = null
@@ -83,22 +89,42 @@ export class SessionStateService {
}
setPlanningState(sessionId: string, projectId: string, planningState: PlanningState): void {
const key = `session:${sessionId}`
const key = `session:${projectId}:${sessionId}`
this.set(key, projectId, planningState, DEFAULT_SESSION_TTL)
}
getPlanningState(sessionId: string): PlanningState | null {
const key = `session:${sessionId}`
getPlanningState(sessionId: string, projectId: string): PlanningState | null {
const key = `session:${projectId}:${sessionId}`
return this.get<PlanningState>(key)
}
listPlanningStates(projectId: string): PlanningStateResult[] {
const prefix = `session:${projectId}:`
const rows = this.queries.listByProjectAndPrefix(projectId, prefix)
return rows.map(row => ({
sessionId: row.key.slice(prefix.length),
planningState: JSON.parse(row.data) as PlanningState,
updatedAt: row.updated_at,
}))
}
searchPlanningStates(projectId: string, query: string): PlanningStateResult[] {
const prefix = `session:${projectId}:`
const rows = this.queries.searchByProjectAndPrefix(projectId, prefix, query)
return rows.map(row => ({
sessionId: row.key.slice(prefix.length),
planningState: JSON.parse(row.data) as PlanningState,
updatedAt: row.updated_at,
}))
}
setCompactionSnapshot(sessionId: string, projectId: string, snapshot: PreCompactionSnapshot): void {
const key = `compaction:snapshot:${sessionId}`
const key = `compaction:snapshot:${projectId}:${sessionId}`
this.set(key, projectId, snapshot, DEFAULT_SNAPSHOT_TTL)
}
getCompactionSnapshot(sessionId: string): PreCompactionSnapshot | null {
const key = `compaction:snapshot:${sessionId}`
getCompactionSnapshot(sessionId: string, projectId: string): PreCompactionSnapshot | null {
const key = `compaction:snapshot:${projectId}:${sessionId}`
return this.get<PreCompactionSnapshot>(key)
}
}
@@ -20,6 +20,10 @@ function mapRow(row: {
return row
}
function escapeLikeWildcards(value: string): string {
return value.replace(/\\/g, '\\\\').replace(/%/g, '\\%').replace(/_/g, '\\_')
}
type SessionStateRowRaw = Parameters<typeof mapRow>[0]
export function createSessionStateQueries(db: Database) {
@@ -55,6 +59,20 @@ export function createSessionStateQueries(db: Database) {
ORDER BY updated_at DESC`
)
const searchByProjectAndPrefixStmt = db.prepare(
`SELECT key, project_id, data, expires_at, created_at, updated_at
FROM session_state
WHERE project_id = ? AND key LIKE ? AND data LIKE ? ESCAPE '\\' AND (expires_at IS NULL OR expires_at > ?)
ORDER BY updated_at DESC`
)
const listByProjectAndPrefixStmt = db.prepare(
`SELECT key, project_id, data, expires_at, created_at, updated_at
FROM session_state
WHERE project_id = ? AND key LIKE ? AND (expires_at IS NULL OR expires_at > ?)
ORDER BY updated_at DESC`
)
return {
get(key: string): SessionStateRow | undefined {
const now = Date.now()
@@ -87,5 +105,20 @@ export function createSessionStateQueries(db: Database) {
const rows = listByProjectStmt.all(projectId, now) as SessionStateRowRaw[]
return rows.map(mapRow)
},
searchByProjectAndPrefix(projectId: string, keyPrefix: string, searchTerm: string): SessionStateRow[] {
const now = Date.now()
const keyPattern = `${escapeLikeWildcards(keyPrefix)}%`
const searchPattern = `%${escapeLikeWildcards(searchTerm)}%`
const rows = searchByProjectAndPrefixStmt.all(projectId, keyPattern, searchPattern, now) as SessionStateRowRaw[]
return rows.map(mapRow)
},
listByProjectAndPrefix(projectId: string, keyPrefix: string): SessionStateRow[] {
const now = Date.now()
const keyPattern = `${escapeLikeWildcards(keyPrefix)}%`
const rows = listByProjectAndPrefixStmt.all(projectId, keyPattern, now) as SessionStateRowRaw[]
return rows.map(mapRow)
},
}
}