1
+ import { serialize } from "./pg-serializer"
2
+ import type { ExternalSubsetParamsRecord } from "@electric-sql/client"
1
3
import type { IR , OnLoadMoreOptions } from "@tanstack/db"
2
4
5
+ export type CompiledSqlRecord = Omit < ExternalSubsetParamsRecord , `params`> & {
6
+ params ?: Array < unknown >
7
+ }
8
+
3
9
export function compileSQL < T > (
4
10
options : OnLoadMoreOptions
5
11
) : ExternalSubsetParamsRecord {
6
12
const { where, orderBy, limit } = options
7
13
8
14
const params : Array < T > = [ ]
9
- const compiledSQL : ExternalSubsetParamsRecord = { params }
15
+ const compiledSQL : CompiledSqlRecord = { params }
10
16
11
17
if ( where ) {
12
18
// TODO: this only works when the where expression's PropRefs directly reference a column of the collection
@@ -22,7 +28,20 @@ export function compileSQL<T>(
22
28
compiledSQL . limit = limit
23
29
}
24
30
25
- return compiledSQL
31
+ // Serialize the values in the params array into PG formatted strings
32
+ // and transform the array into a Record<string, string>
33
+ const paramsRecord = params . reduce (
34
+ ( acc , param , index ) => {
35
+ acc [ `${ index + 1 } ` ] = serialize ( param )
36
+ return acc
37
+ } ,
38
+ { } as Record < string , string >
39
+ )
40
+
41
+ return {
42
+ ...compiledSQL ,
43
+ params : paramsRecord ,
44
+ }
26
45
}
27
46
28
47
/**
@@ -134,15 +153,12 @@ function getOpName(name: string): string {
134
153
concat : `CONCAT` ,
135
154
coalesce : `COALESCE` ,
136
155
}
137
- return opNames [ name as keyof typeof opNames ] || name
138
- }
139
156
140
- // TODO: remove this type once we rebase on top of Ilia's PR
141
- // that type will be exported by Ilia's PR
142
- export type ExternalSubsetParamsRecord = {
143
- where ?: string
144
- params ?: Record < string , any >
145
- limit ?: number
146
- offset ?: number
147
- orderBy ?: string
157
+ const opName = opNames [ name as keyof typeof opNames ]
158
+
159
+ if ( ! opName ) {
160
+ throw new Error ( `Unknown operator/function: ${ name } ` )
161
+ }
162
+
163
+ return opName
148
164
}
0 commit comments