Validate persistence repository rows with Effect Schema decoders
Co-authored-by: Julius Marminge <[email protected]>
This commit is contained in:
1 parent
72c33f7a0f
commit
40f9885dce
5 files changed
+95
-19
No files matched your search
@@ -1,5 +1,11 @@
|
||||
import * as Effect from "effect/Effect";
|
||||
import * as Schema from "effect/Schema";
|
||||
import * as SqlClient from "effect/unstable/sql/SqlClient";
|
||||
import {
|
||||
DataJsonRowSchema,
|
||||
DocumentRowSchema,
|
||||
TotalCountRowSchema,
|
||||
} from "../schema";
|
||||
|
||||
export interface DocumentRow {
|
||||
id: string;
|
||||
@@ -34,6 +40,10 @@ function toSafeInteger(value: unknown, fallback = 0): number {
|
||||
return fallback;
|
||||
}
|
||||
|
||||
const decodeDocumentRows = Schema.decodeUnknownSync(Schema.Array(DocumentRowSchema));
|
||||
const decodeDataJsonRows = Schema.decodeUnknownSync(Schema.Array(DataJsonRowSchema));
|
||||
const decodeTotalCountRows = Schema.decodeUnknownSync(Schema.Array(TotalCountRowSchema));
|
||||
|
||||
export const getDocumentRowById = (
|
||||
id: string,
|
||||
): Effect.Effect<DocumentRow | null, unknown, SqlClient.SqlClient> =>
|
||||
@@ -45,7 +55,8 @@ export const getDocumentRowById = (
|
||||
[id],
|
||||
)
|
||||
.unprepared) as DocumentRow[];
|
||||
return rows[0] ?? null;
|
||||
const parsedRows = decodeDocumentRows(rows);
|
||||
return parsedRows[0] ?? null;
|
||||
});
|
||||
|
||||
export const upsertDocument = (input: {
|
||||
@@ -118,7 +129,7 @@ export const listProjectPayloads = (): Effect.Effect<string[], unknown, SqlClien
|
||||
"SELECT data_json FROM documents WHERE kind = 'project' ORDER BY updated_at DESC, created_at DESC;",
|
||||
)
|
||||
.unprepared) as DataJsonRow[];
|
||||
return rows.map((row) => row.data_json);
|
||||
return decodeDataJsonRows(rows).map((row) => row.data_json);
|
||||
});
|
||||
|
||||
export const listThreadPayloads = (): Effect.Effect<string[], unknown, SqlClient.SqlClient> =>
|
||||
@@ -129,7 +140,7 @@ export const listThreadPayloads = (): Effect.Effect<string[], unknown, SqlClient
|
||||
"SELECT data_json FROM documents WHERE kind = 'thread' ORDER BY updated_at DESC, created_at DESC;",
|
||||
)
|
||||
.unprepared) as DataJsonRow[];
|
||||
return rows.map((row) => row.data_json);
|
||||
return decodeDataJsonRows(rows).map((row) => row.data_json);
|
||||
});
|
||||
|
||||
export const listThreadPayloadsByProject = (
|
||||
@@ -142,7 +153,7 @@ export const listThreadPayloadsByProject = (
|
||||
projectId,
|
||||
])
|
||||
.unprepared) as DataJsonRow[];
|
||||
return rows.map((row) => row.data_json);
|
||||
return decodeDataJsonRows(rows).map((row) => row.data_json);
|
||||
});
|
||||
|
||||
export const findThreadPayloadByRuntimeThreadId = (
|
||||
@@ -156,7 +167,8 @@ export const findThreadPayloadByRuntimeThreadId = (
|
||||
[runtimeThreadId],
|
||||
)
|
||||
.unprepared) as DataJsonRow[];
|
||||
return rows[0]?.data_json ?? null;
|
||||
const parsedRows = decodeDataJsonRows(rows);
|
||||
return parsedRows[0]?.data_json ?? null;
|
||||
});
|
||||
|
||||
export const deleteDocumentsByProjectId = (
|
||||
@@ -195,7 +207,7 @@ export const listMessagePayloadsForThread = (
|
||||
[threadId],
|
||||
)
|
||||
.unprepared) as Array<{ data_json: string }>;
|
||||
return rows.map((row) => row.data_json);
|
||||
return decodeDataJsonRows(rows).map((row) => row.data_json);
|
||||
});
|
||||
|
||||
export const listPaginatedMessagePayloadsForThread = (input: {
|
||||
@@ -211,7 +223,7 @@ export const listPaginatedMessagePayloadsForThread = (input: {
|
||||
[input.threadId, input.limit, input.offset],
|
||||
)
|
||||
.unprepared) as PaginatedMessagePayloadsRow[];
|
||||
return rows.map((row) => row.data_json);
|
||||
return decodeDataJsonRows(rows).map((row) => row.data_json);
|
||||
});
|
||||
|
||||
export const countMessagesForThread = (
|
||||
@@ -224,7 +236,8 @@ export const countMessagesForThread = (
|
||||
threadId,
|
||||
])
|
||||
.unprepared) as TotalCountRow[];
|
||||
return toSafeInteger(rows[0]?.total, 0);
|
||||
const parsedRows = decodeTotalCountRows(rows);
|
||||
return toSafeInteger(parsedRows[0]?.total, 0);
|
||||
});
|
||||
|
||||
export const listMessagePayloadsForThreadDesc = (
|
||||
@@ -241,7 +254,7 @@ export const listMessagePayloadsForThreadDesc = (
|
||||
[threadId],
|
||||
)
|
||||
.unprepared) as Array<{ data_json: string }>;
|
||||
return rows.map((row) => row.data_json);
|
||||
return decodeDataJsonRows(rows).map((row) => row.data_json);
|
||||
});
|
||||
|
||||
export const listTurnSummaryPayloadsForThread = (
|
||||
@@ -255,5 +268,5 @@ export const listTurnSummaryPayloadsForThread = (
|
||||
[threadId],
|
||||
)
|
||||
.unprepared) as Array<{ data_json: string }>;
|
||||
return rows.map((row) => row.data_json);
|
||||
return decodeDataJsonRows(rows).map((row) => row.data_json);
|
||||
});
|
||||
@@ -1,5 +1,7 @@
|
||||
import * as Effect from "effect/Effect";
|
||||
import * as Schema from "effect/Schema";
|
||||
import * as SqlClient from "effect/unstable/sql/SqlClient";
|
||||
import { MetadataRowSchema } from "../schema";
|
||||
|
||||
function tryParseJson(value: string): unknown {
|
||||
try {
|
||||
@@ -9,6 +11,8 @@ function tryParseJson(value: string): unknown {
|
||||
}
|
||||
}
|
||||
|
||||
const decodeMetadataRows = Schema.decodeUnknownSync(Schema.Array(MetadataRowSchema));
|
||||
|
||||
export const readMetadataValue = (
|
||||
key: string,
|
||||
): Effect.Effect<unknown | null, unknown, SqlClient.SqlClient> =>
|
||||
@@ -17,7 +21,7 @@ export const readMetadataValue = (
|
||||
const rows = (yield* sql
|
||||
.unsafe<{ value_json: string }>("SELECT value_json FROM metadata WHERE key = ? LIMIT 1;", [key])
|
||||
.unprepared) as Array<{ value_json: string }>;
|
||||
const row = rows[0];
|
||||
const row = decodeMetadataRows(rows)[0];
|
||||
if (!row) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
import * as Effect from "effect/Effect";
|
||||
import * as Schema from "effect/Schema";
|
||||
import * as SqlClient from "effect/unstable/sql/SqlClient";
|
||||
import {
|
||||
CompletedProviderItemRowSchema,
|
||||
ProviderEventInsertStatsSchema,
|
||||
} from "../schema";
|
||||
|
||||
function toSafeInteger(value: unknown, fallback = 0): number {
|
||||
if (typeof value === "number" && Number.isFinite(value)) {
|
||||
@@ -11,6 +16,11 @@ function toSafeInteger(value: unknown, fallback = 0): number {
|
||||
return fallback;
|
||||
}
|
||||
|
||||
const decodeProviderEventInsertStats = Schema.decodeUnknownSync(ProviderEventInsertStatsSchema);
|
||||
const decodeCompletedProviderItemRows = Schema.decodeUnknownSync(
|
||||
Schema.Array(CompletedProviderItemRowSchema),
|
||||
);
|
||||
|
||||
export interface CompletedProviderItemRow {
|
||||
item_id: string | null;
|
||||
payload_json: string | null;
|
||||
@@ -70,7 +80,8 @@ export const insertProviderEvent = (input: {
|
||||
],
|
||||
)
|
||||
.raw) as { changes?: number | bigint };
|
||||
return toSafeInteger(rawResult.changes, 0);
|
||||
const stats = decodeProviderEventInsertStats(rawResult);
|
||||
return toSafeInteger(stats.changes, 0);
|
||||
});
|
||||
|
||||
export const listCompletedItemEventsBySessionTurn = (input: {
|
||||
@@ -79,7 +90,7 @@ export const listCompletedItemEventsBySessionTurn = (input: {
|
||||
}): Effect.Effect<CompletedProviderItemRow[], unknown, SqlClient.SqlClient> =>
|
||||
Effect.gen(function*() {
|
||||
const sql = yield* SqlClient.SqlClient;
|
||||
return (yield* sql
|
||||
const rows = (yield* sql
|
||||
.unsafe<CompletedProviderItemRow>(
|
||||
`SELECT item_id, payload_json
|
||||
FROM provider_events
|
||||
@@ -88,6 +99,10 @@ export const listCompletedItemEventsBySessionTurn = (input: {
|
||||
[input.sessionId, input.turnId],
|
||||
)
|
||||
.unprepared) as CompletedProviderItemRow[];
|
||||
return decodeCompletedProviderItemRows(rows).map((row) => ({
|
||||
item_id: row.item_id,
|
||||
payload_json: row.payload_json,
|
||||
}));
|
||||
});
|
||||
|
||||
export const listCompletedItemEventsByThreadTurn = (input: {
|
||||
@@ -96,7 +111,7 @@ export const listCompletedItemEventsByThreadTurn = (input: {
|
||||
}): Effect.Effect<CompletedProviderItemRow[], unknown, SqlClient.SqlClient> =>
|
||||
Effect.gen(function*() {
|
||||
const sql = yield* SqlClient.SqlClient;
|
||||
return (yield* sql
|
||||
const rows = (yield* sql
|
||||
.unsafe<CompletedProviderItemRow>(
|
||||
`SELECT item_id, payload_json
|
||||
FROM provider_events
|
||||
@@ -105,6 +120,10 @@ export const listCompletedItemEventsByThreadTurn = (input: {
|
||||
[input.runtimeThreadId, input.turnId],
|
||||
)
|
||||
.unprepared) as CompletedProviderItemRow[];
|
||||
return decodeCompletedProviderItemRows(rows).map((row) => ({
|
||||
item_id: row.item_id,
|
||||
payload_json: row.payload_json,
|
||||
}));
|
||||
});
|
||||
|
||||
export const listCompletedItemEventsByTurn = (
|
||||
@@ -112,7 +131,7 @@ export const listCompletedItemEventsByTurn = (
|
||||
): Effect.Effect<CompletedProviderItemRow[], unknown, SqlClient.SqlClient> =>
|
||||
Effect.gen(function*() {
|
||||
const sql = yield* SqlClient.SqlClient;
|
||||
return (yield* sql
|
||||
const rows = (yield* sql
|
||||
.unsafe<CompletedProviderItemRow>(
|
||||
`SELECT item_id, payload_json
|
||||
FROM provider_events
|
||||
@@ -121,4 +140,8 @@ export const listCompletedItemEventsByTurn = (
|
||||
[turnId],
|
||||
)
|
||||
.unprepared) as CompletedProviderItemRow[];
|
||||
return decodeCompletedProviderItemRows(rows).map((row) => ({
|
||||
item_id: row.item_id,
|
||||
payload_json: row.payload_json,
|
||||
}));
|
||||
});
|
||||
@@ -1,6 +1,12 @@
|
||||
import * as Effect from "effect/Effect";
|
||||
import * as Schema from "effect/Schema";
|
||||
import * as SqlClient from "effect/unstable/sql/SqlClient";
|
||||
import { stateEventSchema, type StateEvent } from "@t3tools/contracts";
|
||||
import {
|
||||
StateEventRowSchema,
|
||||
StateSeqRowSchema,
|
||||
StateEventInsertStatsSchema,
|
||||
} from "../schema";
|
||||
|
||||
interface StateEventRow {
|
||||
seq: number;
|
||||
@@ -28,6 +34,10 @@ function tryParseJson(value: string): unknown {
|
||||
}
|
||||
}
|
||||
|
||||
const decodeStateEventRows = Schema.decodeUnknownSync(Schema.Array(StateEventRowSchema));
|
||||
const decodeStateSeqRows = Schema.decodeUnknownSync(Schema.Array(StateSeqRowSchema));
|
||||
const decodeStateEventInsertStats = Schema.decodeUnknownSync(StateEventInsertStatsSchema);
|
||||
|
||||
export const appendStateEvent = (input: {
|
||||
eventType: string;
|
||||
entityId: string;
|
||||
@@ -45,9 +55,10 @@ export const appendStateEvent = (input: {
|
||||
changes?: number | bigint;
|
||||
lastInsertRowid?: number | bigint;
|
||||
};
|
||||
const stats = decodeStateEventInsertStats(result);
|
||||
|
||||
return stateEventSchema.parse({
|
||||
seq: toSafeInteger(result.lastInsertRowid, 0),
|
||||
seq: toSafeInteger(stats.lastInsertRowid, 0),
|
||||
eventType: input.eventType,
|
||||
entityId: input.entityId,
|
||||
payload: input.payload,
|
||||
@@ -63,7 +74,8 @@ export const readLastStateSeq: Effect.Effect<number, unknown, SqlClient.SqlClien
|
||||
"SELECT COALESCE(MAX(seq), 0) AS seq FROM state_events;",
|
||||
)
|
||||
.unprepared) as Array<{ seq?: number | bigint }>;
|
||||
return toSafeInteger(rows[0]?.seq, 0);
|
||||
const parsedRows = decodeStateSeqRows(rows);
|
||||
return toSafeInteger(parsedRows[0]?.seq, 0);
|
||||
},
|
||||
);
|
||||
|
||||
@@ -78,7 +90,7 @@ export const listStateEventsAfterSeq = (
|
||||
[afterSeq],
|
||||
)
|
||||
.unprepared) as StateEventRow[];
|
||||
return rows.map((row) =>
|
||||
return decodeStateEventRows(rows).map((row) =>
|
||||
stateEventSchema.parse({
|
||||
seq: row.seq,
|
||||
eventType: row.event_type,
|
||||
|
||||
@@ -11,8 +11,24 @@ export const DocumentRowSchema = Schema.Struct({
|
||||
data_json: Schema.String,
|
||||
});
|
||||
|
||||
export const DataJsonRowSchema = Schema.Struct({
|
||||
data_json: Schema.String,
|
||||
});
|
||||
|
||||
const NumberOrBigIntSchema = Schema.Union([Schema.Number, Schema.BigInt]);
|
||||
|
||||
export const ProviderEventInsertStatsSchema = Schema.Struct({
|
||||
changes: Schema.NullOr(Schema.Number),
|
||||
changes: Schema.optional(Schema.NullOr(NumberOrBigIntSchema)),
|
||||
});
|
||||
|
||||
export const CompletedProviderItemRowSchema = Schema.Struct({
|
||||
item_id: Schema.NullOr(Schema.String),
|
||||
payload_json: Schema.NullOr(Schema.String),
|
||||
});
|
||||
|
||||
export const StateEventInsertStatsSchema = Schema.Struct({
|
||||
changes: Schema.optional(Schema.NullOr(NumberOrBigIntSchema)),
|
||||
lastInsertRowid: Schema.optional(Schema.NullOr(NumberOrBigIntSchema)),
|
||||
});
|
||||
|
||||
export const StateEventRowSchema = Schema.Struct({
|
||||
@@ -23,6 +39,14 @@ export const StateEventRowSchema = Schema.Struct({
|
||||
created_at: Schema.String,
|
||||
});
|
||||
|
||||
export const StateSeqRowSchema = Schema.Struct({
|
||||
seq: Schema.optional(Schema.NullOr(NumberOrBigIntSchema)),
|
||||
});
|
||||
|
||||
export const TotalCountRowSchema = Schema.Struct({
|
||||
total: Schema.optional(Schema.NullOr(NumberOrBigIntSchema)),
|
||||
});
|
||||
|
||||
export const MetadataRowSchema = Schema.Struct({
|
||||
value_json: Schema.String,
|
||||
});
|
||||
Reference in new issue
Block a user