Use Effect SQL repository for state event persistence

Co-authored-by: Julius Marminge <[email protected]>
This commit is contained in:
Cursor AgentandJulius Marminge committed 2026-02-20 09:38:15 +00:00
1 parent 13dea398b0
commit a976a82311
2 files changed
+164 -35

No files matched your search

@@ -0,0 +1,90 @@
import * as Effect from "effect/Effect";
import * as SqlClient from "effect/unstable/sql/SqlClient";
import { stateEventSchema, type StateEvent } from "@t3tools/contracts";
interface StateEventRow {
seq: number;
event_type: string;
entity_id: string;
payload_json: string;
created_at: string;
}
function toSafeInteger(value: unknown, fallback = 0): number {
if (typeof value === "number" && Number.isFinite(value)) {
return Math.trunc(value);
}
if (typeof value === "bigint") {
return Number(value);
}
return fallback;
}
function tryParseJson(value: string): unknown {
try {
return JSON.parse(value) as unknown;
} catch {
return null;
}
}
export const appendStateEvent = (input: {
eventType: string;
entityId: string;
payload: unknown;
createdAt: string;
}): Effect.Effect<StateEvent, unknown, SqlClient.SqlClient> =>
Effect.gen(function*() {
const sql = yield* SqlClient.SqlClient;
const result = (yield* sql
.unsafe<{ changes?: number | bigint; lastInsertRowid?: number | bigint }>(
"INSERT INTO state_events (event_type, entity_id, payload_json, created_at) VALUES (?, ?, ?, ?);",
[input.eventType, input.entityId, JSON.stringify(input.payload), input.createdAt],
)
.raw) as {
changes?: number | bigint;
lastInsertRowid?: number | bigint;
};
return stateEventSchema.parse({
seq: toSafeInteger(result.lastInsertRowid, 0),
eventType: input.eventType,
entityId: input.entityId,
payload: input.payload,
createdAt: input.createdAt,
});
});
export const readLastStateSeq: Effect.Effect<number, unknown, SqlClient.SqlClient> = Effect.gen(
function*() {
const sql = yield* SqlClient.SqlClient;
const rows = (yield* sql
.unsafe<{ seq?: number | bigint }>(
"SELECT COALESCE(MAX(seq), 0) AS seq FROM state_events;",
)
.unprepared) as Array<{ seq?: number | bigint }>;
return toSafeInteger(rows[0]?.seq, 0);
},
);
export const listStateEventsAfterSeq = (
afterSeq: number,
): Effect.Effect<StateEvent[], unknown, SqlClient.SqlClient> =>
Effect.gen(function*() {
const sql = yield* SqlClient.SqlClient;
const rows = (yield* sql
.unsafe<StateEventRow>(
"SELECT seq, event_type, entity_id, payload_json, created_at FROM state_events WHERE seq > ? ORDER BY seq ASC;",
[afterSeq],
)
.unprepared) as StateEventRow[];
return rows.map((row) =>
stateEventSchema.parse({
seq: row.seq,
eventType: row.event_type,
entityId: row.entity_id,
payload: tryParseJson(row.payload_json),
createdAt: row.created_at,
}),
);
});
+74 -35
View File
@@ -2,6 +2,8 @@ import { randomUUID } from "node:crypto";
import { EventEmitter } from "node:events";
import fs from "node:fs";
import path from "node:path";
import * as Effect from "effect/Effect";
import * as SqlClient from "effect/unstable/sql/SqlClient";
import { parsePatchFiles } from "@pierre/diffs";
import {
@@ -65,6 +67,12 @@ import {
} from "@t3tools/contracts";
import { StateDb } from "./stateDb";
import {
appendStateEvent as appendStateEventEffect,
listStateEventsAfterSeq as listStateEventsAfterSeqEffect,
readLastStateSeq as readLastStateSeqEffect,
} from "./persistence/stateEventsRepo";
import type { EffectSqliteDatabaseAdapter } from "./sqliteAdapter";
const METADATA_KEY_PROJECTS_JSON_IMPORTED = "migration.projects_json_imported";
const METADATA_KEY_APP_SETTINGS = "app.settings.v1";
@@ -746,25 +754,31 @@ export class PersistenceService extends EventEmitter<PersistenceServiceEvents> {
catchUp(raw: StateCatchUpInput): StateCatchUpResult {
const input = stateCatchUpInputSchema.parse(raw);
const rows = this.db
.prepare(
"SELECT seq, event_type, entity_id, payload_json, created_at FROM state_events WHERE seq > ? ORDER BY seq ASC;",
)
.all(input.afterSeq) as unknown as StateEventRow[];
const events = this.runWithEffectSql(
listStateEventsAfterSeqEffect(input.afterSeq),
() => {
const rows = this.db
.prepare(
"SELECT seq, event_type, entity_id, payload_json, created_at FROM state_events WHERE seq > ? ORDER BY seq ASC;",
)
.all(input.afterSeq) as unknown as StateEventRow[];
const events: StateEvent[] = [];
for (const row of rows) {
const payload = this.tryParseJson(row.payload_json);
events.push(
stateEventSchema.parse({
seq: row.seq,
eventType: row.event_type,
entityId: row.entity_id,
payload,
createdAt: row.created_at,
}),
);
}
const fallbackEvents: StateEvent[] = [];
for (const row of rows) {
const payload = this.tryParseJson(row.payload_json);
fallbackEvents.push(
stateEventSchema.parse({
seq: row.seq,
eventType: row.event_type,
entityId: row.entity_id,
payload,
createdAt: row.created_at,
}),
);
}
return fallbackEvents;
},
);
return stateCatchUpResultSchema.parse({
events,
@@ -1354,10 +1368,12 @@ export class PersistenceService extends EventEmitter<PersistenceServiceEvents> {
}
private readLastStateSeq(): number {
const row = this.db
.prepare("SELECT COALESCE(MAX(seq), 0) AS seq FROM state_events;")
.get() as { seq: number } | undefined;
return row?.seq ?? 0;
return this.runWithEffectSql(readLastStateSeqEffect, () => {
const row = this.db
.prepare("SELECT COALESCE(MAX(seq), 0) AS seq FROM state_events;")
.get() as { seq: number } | undefined;
return row?.seq ?? 0;
});
}
private withTransaction<T>(fn: (pendingEvents: StateEvent[]) => T): T {
@@ -1375,28 +1391,37 @@ export class PersistenceService extends EventEmitter<PersistenceServiceEvents> {
private appendStateEvent(
pendingEvents: StateEvent[],
eventType: string,
eventType: StateEvent["eventType"],
entityId: string,
payload: unknown,
payload: StateEvent["payload"],
createdAt: string,
): void {
const result = this.db
.prepare(
"INSERT INTO state_events (event_type, entity_id, payload_json, created_at) VALUES (?, ?, ?, ?);",
)
.run(eventType, entityId, JSON.stringify(payload), createdAt) as {
lastInsertRowid?: number | bigint;
};
const seq = toSafeInteger(result.lastInsertRowid, 0);
pendingEvents.push(
stateEventSchema.parse({
seq,
const nextEvent = this.runWithEffectSql(
appendStateEventEffect({
eventType,
entityId,
payload,
createdAt,
}),
() => {
const result = this.db
.prepare(
"INSERT INTO state_events (event_type, entity_id, payload_json, created_at) VALUES (?, ?, ?, ?);",
)
.run(eventType, entityId, JSON.stringify(payload), createdAt) as {
lastInsertRowid?: number | bigint;
};
const seq = toSafeInteger(result.lastInsertRowid, 0);
return stateEventSchema.parse({
seq,
eventType,
entityId,
payload,
createdAt,
});
},
);
pendingEvents.push(nextEvent);
}
private upsertProjectDocument(project: StateProject): void {
@@ -1833,4 +1858,18 @@ export class PersistenceService extends EventEmitter<PersistenceServiceEvents> {
}
this.stateDb.transaction(write);
}
private runWithEffectSql<A>(
effect: Effect.Effect<A, unknown, SqlClient.SqlClient>,
fallback: () => A,
): A {
if (isEffectSqliteDatabase(this.db)) {
return this.db.runWithSqlClient(effect);
}
return fallback();
}
}
function isEffectSqliteDatabase(db: StateDb["db"]): db is EffectSqliteDatabaseAdapter {
return "runWithSqlClient" in db && typeof db.runWithSqlClient === "function";
}