From d6e0cd8e0dd8fafafece732d15d3845f1a5b8396 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Fri, 20 Feb 2026 09:49:53 +0000 Subject: [PATCH] Extract document persistence queries into Effect repository Co-authored-by: Julius Marminge --- apps/server/src/persistence/documentsRepo.ts | 89 ++++++++++++++ apps/server/src/persistenceService.ts | 123 ++++++++++--------- 2 files changed, 157 insertions(+), 55 deletions(-) create mode 100644 apps/server/src/persistence/documentsRepo.ts diff --git a/apps/server/src/persistence/documentsRepo.ts b/apps/server/src/persistence/documentsRepo.ts new file mode 100644 index 0000000000..2cd29258c1 --- /dev/null +++ b/apps/server/src/persistence/documentsRepo.ts @@ -0,0 +1,89 @@ +import * as Effect from "effect/Effect"; +import * as SqlClient from "effect/unstable/sql/SqlClient"; + +export interface DocumentRow { + id: string; + kind: string; + project_id: string | null; + thread_id: string | null; + sort_key: number | null; + created_at: string; + updated_at: string; + data_json: string; +} + +export const getDocumentRowById = ( + id: string, +): Effect.Effect => + Effect.gen(function*() { + const sql = yield* SqlClient.SqlClient; + const rows = (yield* sql + .unsafe( + "SELECT id, kind, project_id, thread_id, sort_key, created_at, updated_at, data_json FROM documents WHERE id = ? LIMIT 1;", + [id], + ) + .unprepared) as DocumentRow[]; + return rows[0] ?? null; + }); + +export const upsertDocument = (input: { + id: string; + kind: "project" | "thread" | "message" | "turn_summary"; + projectId: string | null; + threadId: string | null; + sortKey: number | null; + createdAt: string; + updatedAt: string; + dataJson: string; +}): Effect.Effect => + Effect.gen(function*() { + const sql = yield* SqlClient.SqlClient; + yield* sql + .unsafe( + `INSERT INTO documents ( + id, + kind, + project_id, + thread_id, + sort_key, + schema_version, + created_at, + updated_at, + data_json + ) VALUES (?, ?, ?, ?, ?, 1, ?, ?, ?) + ON CONFLICT(id) DO UPDATE SET + kind = excluded.kind, + project_id = excluded.project_id, + thread_id = excluded.thread_id, + sort_key = excluded.sort_key, + schema_version = excluded.schema_version, + updated_at = excluded.updated_at, + data_json = excluded.data_json;`, + [ + input.id, + input.kind, + input.projectId, + input.threadId, + input.sortKey, + input.createdAt, + input.updatedAt, + input.dataJson, + ], + ) + .raw; + }).pipe(Effect.asVoid); + +export const readNextSortKey = ( + kind: "message" | "turn_summary", + threadId: string, +): Effect.Effect => + Effect.gen(function*() { + const sql = yield* SqlClient.SqlClient; + const rows = (yield* sql + .unsafe<{ next_sort_key?: number }>( + "SELECT COALESCE(MAX(sort_key), 0) + 1 AS next_sort_key FROM documents WHERE kind = ? AND thread_id = ?;", + [kind, threadId], + ) + .unprepared) as Array<{ next_sort_key?: number }>; + return rows[0]?.next_sort_key ?? 1; + }); diff --git a/apps/server/src/persistenceService.ts b/apps/server/src/persistenceService.ts index de6f2abbb2..0c2e69559c 100644 --- a/apps/server/src/persistenceService.ts +++ b/apps/server/src/persistenceService.ts @@ -67,6 +67,12 @@ import { } from "@t3tools/contracts"; import { StateDb } from "./stateDb"; +import { + getDocumentRowById as getDocumentRowByIdEffect, + readNextSortKey as readNextSortKeyEffect, + upsertDocument as upsertDocumentEffect, + type DocumentRow, +} from "./persistence/documentsRepo"; import { appendStateEvent as appendStateEventEffect, listStateEventsAfterSeq as listStateEventsAfterSeqEffect, @@ -84,17 +90,6 @@ const MAX_TERMINAL_COUNT = 4; const DEFAULT_TERMINAL_ID = "default"; const DEFAULT_TERMINAL_HEIGHT = 280; -interface DocumentRow { - id: string; - kind: string; - project_id: string | null; - thread_id: string | null; - sort_key: number | null; - created_at: string; - updated_at: string; - data_json: string; -} - interface StateEventRow { seq: number; event_type: string; @@ -1680,56 +1675,74 @@ export class PersistenceService extends EventEmitter { } private upsertDocument(input: UpsertDocumentInput): void { - this.db - .prepare( - `INSERT INTO documents ( - id, - kind, - project_id, - thread_id, - sort_key, - schema_version, - created_at, - updated_at, - data_json - ) VALUES (?, ?, ?, ?, ?, 1, ?, ?, ?) - ON CONFLICT(id) DO UPDATE SET - kind = excluded.kind, - project_id = excluded.project_id, - thread_id = excluded.thread_id, - sort_key = excluded.sort_key, - schema_version = excluded.schema_version, - updated_at = excluded.updated_at, - data_json = excluded.data_json;`, - ) - .run( - input.id, - input.kind, - input.projectId, - input.threadId, - input.sortKey, - input.createdAt, - input.updatedAt, - JSON.stringify(input.data), - ); + this.runWithEffectSql( + upsertDocumentEffect({ + id: input.id, + kind: input.kind, + projectId: input.projectId, + threadId: input.threadId, + sortKey: input.sortKey, + createdAt: input.createdAt, + updatedAt: input.updatedAt, + dataJson: JSON.stringify(input.data), + }), + () => { + this.db + .prepare( + `INSERT INTO documents ( + id, + kind, + project_id, + thread_id, + sort_key, + schema_version, + created_at, + updated_at, + data_json + ) VALUES (?, ?, ?, ?, ?, 1, ?, ?, ?) + ON CONFLICT(id) DO UPDATE SET + kind = excluded.kind, + project_id = excluded.project_id, + thread_id = excluded.thread_id, + sort_key = excluded.sort_key, + schema_version = excluded.schema_version, + updated_at = excluded.updated_at, + data_json = excluded.data_json;`, + ) + .run( + input.id, + input.kind, + input.projectId, + input.threadId, + input.sortKey, + input.createdAt, + input.updatedAt, + JSON.stringify(input.data), + ); + }, + ); } private getDocumentRowById(id: string): DocumentRow | null { - const row = this.db - .prepare( - "SELECT id, kind, project_id, thread_id, sort_key, created_at, updated_at, data_json FROM documents WHERE id = ? LIMIT 1;", - ) - .get(id) as DocumentRow | undefined; - return row ?? null; + return this.runWithEffectSql(getDocumentRowByIdEffect(id), () => { + const row = this.db + .prepare( + "SELECT id, kind, project_id, thread_id, sort_key, created_at, updated_at, data_json FROM documents WHERE id = ? LIMIT 1;", + ) + .get(id) as DocumentRow | undefined; + return row ?? null; + }); } private readNextSortKey(kind: "message" | "turn_summary", threadId: string): number { - const row = this.db - .prepare( - "SELECT COALESCE(MAX(sort_key), 0) + 1 AS next_sort_key FROM documents WHERE kind = ? AND thread_id = ?;", - ) - .get(kind, threadId) as { next_sort_key: number } | undefined; - return row?.next_sort_key ?? 1; + return this.runWithEffectSql(readNextSortKeyEffect(kind, threadId), () => { + const row = this.db + .prepare( + "SELECT COALESCE(MAX(sort_key), 0) + 1 AS next_sort_key FROM documents WHERE kind = ? AND thread_id = ?;", + ) + .get(kind, threadId) as { next_sort_key: number } | undefined; + return row?.next_sort_key ?? 1; + }); } private resolveThreadIdForEvent(event: ProviderEvent, runtimeThreadId: string | null): string | null {