perf(observabilidade): synchronous=NORMAL e upsertMany transaccional
This commit is contained in:
@@ -15,6 +15,7 @@ export interface ListFilters {
|
||||
|
||||
export interface SessionsDb {
|
||||
upsertSession(meta: SessionMeta): void
|
||||
upsertMany(metas: SessionMeta[]): void
|
||||
listSessions(filters: ListFilters): SessionMeta[]
|
||||
countSessions(filters: ListFilters): number
|
||||
getSession(id: string): SessionMeta | null
|
||||
@@ -104,6 +105,7 @@ export function openSessionsDb(dbPath: string): SessionsDb {
|
||||
mkdirSync(dirname(dbPath), { recursive: true })
|
||||
const db = new Database(dbPath)
|
||||
db.pragma('journal_mode = WAL')
|
||||
db.pragma('synchronous = NORMAL')
|
||||
db.exec(SCHEMA)
|
||||
|
||||
const upsertStmt = db.prepare(`
|
||||
@@ -133,6 +135,16 @@ export function openSessionsDb(dbPath: string): SessionsDb {
|
||||
indexed_at = excluded.indexed_at
|
||||
`)
|
||||
|
||||
const upsertManyTxn = db.transaction((metas: SessionMeta[]) => {
|
||||
for (const meta of metas) {
|
||||
upsertStmt.run({
|
||||
...meta,
|
||||
tools_used: JSON.stringify(meta.tools_used),
|
||||
skills_invoked: JSON.stringify(meta.skills_invoked),
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
return {
|
||||
upsertSession(meta) {
|
||||
upsertStmt.run({
|
||||
@@ -141,6 +153,9 @@ export function openSessionsDb(dbPath: string): SessionsDb {
|
||||
skills_invoked: JSON.stringify(meta.skills_invoked),
|
||||
})
|
||||
},
|
||||
upsertMany(metas) {
|
||||
upsertManyTxn(metas)
|
||||
},
|
||||
listSessions(filters) {
|
||||
const { sql, params } = buildWhere(filters)
|
||||
const limit = filters.limit ?? 50
|
||||
|
||||
Reference in New Issue
Block a user