11import * as Effect from "effect/Effect" ;
2+ import * as Schema from "effect/Schema" ;
23import * as SqlClient from "effect/unstable/sql/SqlClient" ;
4+ import {
5+ DataJsonRowSchema ,
6+ DocumentRowSchema ,
7+ TotalCountRowSchema ,
8+ } from "../schema" ;
39
410export interface DocumentRow {
511 id : string ;
@@ -34,6 +40,10 @@ function toSafeInteger(value: unknown, fallback = 0): number {
3440 return fallback ;
3541}
3642
43+ const decodeDocumentRows = Schema . decodeUnknownSync ( Schema . Array ( DocumentRowSchema ) ) ;
44+ const decodeDataJsonRows = Schema . decodeUnknownSync ( Schema . Array ( DataJsonRowSchema ) ) ;
45+ const decodeTotalCountRows = Schema . decodeUnknownSync ( Schema . Array ( TotalCountRowSchema ) ) ;
46+
3747export const getDocumentRowById = (
3848 id : string ,
3949) : Effect . Effect < DocumentRow | null , unknown , SqlClient . SqlClient > =>
@@ -45,7 +55,8 @@ export const getDocumentRowById = (
4555 [ id ] ,
4656 )
4757 . unprepared ) as DocumentRow [ ] ;
48- return rows [ 0 ] ?? null ;
58+ const parsedRows = decodeDocumentRows ( rows ) ;
59+ return parsedRows [ 0 ] ?? null ;
4960 } ) ;
5061
5162export const upsertDocument = ( input : {
@@ -118,7 +129,7 @@ export const listProjectPayloads = (): Effect.Effect<string[], unknown, SqlClien
118129 "SELECT data_json FROM documents WHERE kind = 'project' ORDER BY updated_at DESC, created_at DESC;" ,
119130 )
120131 . unprepared ) as DataJsonRow [ ] ;
121- return rows . map ( ( row ) => row . data_json ) ;
132+ return decodeDataJsonRows ( rows ) . map ( ( row ) => row . data_json ) ;
122133 } ) ;
123134
124135export const listThreadPayloads = ( ) : Effect . Effect < string [ ] , unknown , SqlClient . SqlClient > =>
@@ -129,7 +140,7 @@ export const listThreadPayloads = (): Effect.Effect<string[], unknown, SqlClient
129140 "SELECT data_json FROM documents WHERE kind = 'thread' ORDER BY updated_at DESC, created_at DESC;" ,
130141 )
131142 . unprepared ) as DataJsonRow [ ] ;
132- return rows . map ( ( row ) => row . data_json ) ;
143+ return decodeDataJsonRows ( rows ) . map ( ( row ) => row . data_json ) ;
133144 } ) ;
134145
135146export const listThreadPayloadsByProject = (
@@ -142,7 +153,7 @@ export const listThreadPayloadsByProject = (
142153 projectId ,
143154 ] )
144155 . unprepared ) as DataJsonRow [ ] ;
145- return rows . map ( ( row ) => row . data_json ) ;
156+ return decodeDataJsonRows ( rows ) . map ( ( row ) => row . data_json ) ;
146157 } ) ;
147158
148159export const findThreadPayloadByRuntimeThreadId = (
@@ -156,7 +167,8 @@ export const findThreadPayloadByRuntimeThreadId = (
156167 [ runtimeThreadId ] ,
157168 )
158169 . unprepared ) as DataJsonRow [ ] ;
159- return rows [ 0 ] ?. data_json ?? null ;
170+ const parsedRows = decodeDataJsonRows ( rows ) ;
171+ return parsedRows [ 0 ] ?. data_json ?? null ;
160172 } ) ;
161173
162174export const deleteDocumentsByProjectId = (
@@ -195,7 +207,7 @@ export const listMessagePayloadsForThread = (
195207 [ threadId ] ,
196208 )
197209 . unprepared ) as Array < { data_json : string } > ;
198- return rows . map ( ( row ) => row . data_json ) ;
210+ return decodeDataJsonRows ( rows ) . map ( ( row ) => row . data_json ) ;
199211 } ) ;
200212
201213export const listPaginatedMessagePayloadsForThread = ( input : {
@@ -211,7 +223,7 @@ export const listPaginatedMessagePayloadsForThread = (input: {
211223 [ input . threadId , input . limit , input . offset ] ,
212224 )
213225 . unprepared ) as PaginatedMessagePayloadsRow [ ] ;
214- return rows . map ( ( row ) => row . data_json ) ;
226+ return decodeDataJsonRows ( rows ) . map ( ( row ) => row . data_json ) ;
215227 } ) ;
216228
217229export const countMessagesForThread = (
@@ -224,7 +236,8 @@ export const countMessagesForThread = (
224236 threadId ,
225237 ] )
226238 . unprepared ) as TotalCountRow [ ] ;
227- return toSafeInteger ( rows [ 0 ] ?. total , 0 ) ;
239+ const parsedRows = decodeTotalCountRows ( rows ) ;
240+ return toSafeInteger ( parsedRows [ 0 ] ?. total , 0 ) ;
228241 } ) ;
229242
230243export const listMessagePayloadsForThreadDesc = (
@@ -241,7 +254,7 @@ export const listMessagePayloadsForThreadDesc = (
241254 [ threadId ] ,
242255 )
243256 . unprepared ) as Array < { data_json : string } > ;
244- return rows . map ( ( row ) => row . data_json ) ;
257+ return decodeDataJsonRows ( rows ) . map ( ( row ) => row . data_json ) ;
245258 } ) ;
246259
247260export const listTurnSummaryPayloadsForThread = (
@@ -255,5 +268,5 @@ export const listTurnSummaryPayloadsForThread = (
255268 [ threadId ] ,
256269 )
257270 . unprepared ) as Array < { data_json : string } > ;
258- return rows . map ( ( row ) => row . data_json ) ;
271+ return decodeDataJsonRows ( rows ) . map ( ( row ) => row . data_json ) ;
259272 } ) ;
0 commit comments