Extract document persistence queries into Effect repository
Co-authored-by: Julius Marminge <[email protected]>
This commit is contained in:
1 parent
5d75a58a25
commit
d6e0cd8e0d
2 files changed
+157
-55
No files matched your search
@@ -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<DocumentRow | null, unknown, SqlClient.SqlClient> =>
|
||||
Effect.gen(function*() {
|
||||
const sql = yield* SqlClient.SqlClient;
|
||||
const rows = (yield* sql
|
||||
.unsafe<DocumentRow>(
|
||||
"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<void, unknown, SqlClient.SqlClient> =>
|
||||
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<number, unknown, SqlClient.SqlClient> =>
|
||||
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;
|
||||
});
|
||||
@@ -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<PersistenceServiceEvents> {
|
||||
}
|
||||
|
||||
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 {
|
||||
|
||||
Reference in new issue
Block a user