@reventlessdev/reventless-postgres 3.0.0-alpha.4 → 3.0.0-alpha.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +9 -0
- package/lib/bs/.compiler.log +2 -2
- package/lib/bs/ReventlessPostgres.cmi +0 -0
- package/lib/bs/ReventlessPostgres.cmt +0 -0
- package/lib/bs/ReventlessPostgres.mlmap +1 -0
- package/lib/bs/compiler-info.json +1 -1
- package/lib/bs/src/DcbEventLogStorage_Postgres-ReventlessPostgres.cmi +0 -0
- package/lib/bs/src/DcbEventLogStorage_Postgres-ReventlessPostgres.cmt +0 -0
- package/lib/bs/src/EventLogChangeFeed-ReventlessPostgres.cmi +0 -0
- package/lib/bs/src/EventLogChangeFeed-ReventlessPostgres.cmt +0 -0
- package/lib/bs/src/EventLogStorage_Postgres-ReventlessPostgres.cmi +0 -0
- package/lib/bs/src/EventLogStorage_Postgres-ReventlessPostgres.cmt +0 -0
- package/lib/bs/src/PgChangeFeed-ReventlessPostgres.cmi +0 -0
- package/lib/bs/src/PgChangeFeed-ReventlessPostgres.cmt +0 -0
- package/lib/bs/src/QueryDbStorage_Postgres-ReventlessPostgres.cmi +0 -0
- package/lib/bs/src/QueryDbStorage_Postgres-ReventlessPostgres.cmj +0 -0
- package/lib/bs/src/QueryDbStorage_Postgres-ReventlessPostgres.cmt +0 -0
- package/lib/bs/src/QueryDbStorage_Postgres.ast +0 -0
- package/lib/bs/src/QueryDbStorage_Postgres.res +35 -14
- package/lib/bs/src/QueryDbStorage_Postgres.res.mjs +23 -8
- package/lib/bs/src/QueryEnginePostgres-ReventlessPostgres.cmi +0 -0
- package/lib/bs/src/QueryEnginePostgres-ReventlessPostgres.cmt +0 -0
- package/lib/bs/src/QueryEnginePostgres.ast +0 -0
- package/lib/bs/src/QueryEnginePostgres.res +175 -0
- package/lib/bs/src/QueryEnginePostgres.res.mjs +91 -0
- package/lib/bs/tests/PostgresIntegrationTest-ReventlessPostgres.cmi +0 -0
- package/lib/bs/tests/PostgresIntegrationTest-ReventlessPostgres.cmt +0 -0
- package/lib/bs/tests/QueryEnginePostgresListPageTest-ReventlessPostgres.cmi +0 -0
- package/lib/bs/tests/QueryEnginePostgresListPageTest-ReventlessPostgres.cmj +0 -0
- package/lib/bs/tests/QueryEnginePostgresListPageTest-ReventlessPostgres.cmt +0 -0
- package/lib/bs/tests/QueryEnginePostgresListPageTest.ast +0 -0
- package/lib/bs/tests/QueryEnginePostgresListPageTest.res +236 -0
- package/lib/bs/tests/QueryEnginePostgresListPageTest.res.mjs +325 -0
- package/lib/ocaml/.compiler.log +2 -2
- package/lib/ocaml/DcbEventLogStorage_Postgres-ReventlessPostgres.cmi +0 -0
- package/lib/ocaml/DcbEventLogStorage_Postgres-ReventlessPostgres.cmt +0 -0
- package/lib/ocaml/EventLogChangeFeed-ReventlessPostgres.cmi +0 -0
- package/lib/ocaml/EventLogChangeFeed-ReventlessPostgres.cmt +0 -0
- package/lib/ocaml/EventLogStorage_Postgres-ReventlessPostgres.cmi +0 -0
- package/lib/ocaml/EventLogStorage_Postgres-ReventlessPostgres.cmt +0 -0
- package/lib/ocaml/PgChangeFeed-ReventlessPostgres.cmi +0 -0
- package/lib/ocaml/PgChangeFeed-ReventlessPostgres.cmt +0 -0
- package/lib/ocaml/PostgresIntegrationTest-ReventlessPostgres.cmi +0 -0
- package/lib/ocaml/PostgresIntegrationTest-ReventlessPostgres.cmt +0 -0
- package/lib/ocaml/QueryDbStorage_Postgres-ReventlessPostgres.cmi +0 -0
- package/lib/ocaml/QueryDbStorage_Postgres-ReventlessPostgres.cmj +0 -0
- package/lib/ocaml/QueryDbStorage_Postgres-ReventlessPostgres.cmt +0 -0
- package/lib/ocaml/QueryDbStorage_Postgres.ast +0 -0
- package/lib/ocaml/QueryDbStorage_Postgres.res +35 -14
- package/lib/ocaml/QueryEnginePostgres-ReventlessPostgres.cmi +0 -0
- package/lib/ocaml/QueryEnginePostgres-ReventlessPostgres.cmt +0 -0
- package/lib/ocaml/QueryEnginePostgres.ast +0 -0
- package/lib/ocaml/QueryEnginePostgres.res +175 -0
- package/lib/ocaml/QueryEnginePostgresListPageTest-ReventlessPostgres.cmi +0 -0
- package/lib/ocaml/QueryEnginePostgresListPageTest-ReventlessPostgres.cmj +0 -0
- package/lib/ocaml/QueryEnginePostgresListPageTest-ReventlessPostgres.cmt +0 -0
- package/lib/ocaml/QueryEnginePostgresListPageTest.ast +0 -0
- package/lib/ocaml/QueryEnginePostgresListPageTest.res +236 -0
- package/lib/ocaml/ReventlessPostgres.cmi +0 -0
- package/lib/ocaml/ReventlessPostgres.cmt +0 -0
- package/lib/ocaml/ReventlessPostgres.mlmap +1 -0
- package/package.json +7 -7
- package/src/QueryDbStorage_Postgres.res +35 -14
- package/src/QueryDbStorage_Postgres.res.mjs +23 -8
- package/src/QueryEnginePostgres.res +175 -0
- package/src/QueryEnginePostgres.res.mjs +91 -0
- package/tests/QueryEnginePostgresListPageTest.res +236 -0
- package/tests/QueryEnginePostgresListPageTest.res.mjs +325 -0
|
@@ -97,23 +97,36 @@ let withId = (item: JSON.t, partitionKey: string): JSON.t =>
|
|
|
97
97
|
| _ => item
|
|
98
98
|
}
|
|
99
99
|
|
|
100
|
-
|
|
100
|
+
// Plain operation set for a read model's qdb table. Exposed separately from
|
|
101
|
+
// `makeStorage` so the deployed-Lambda runtime (reventless-aws) can bind ops
|
|
102
|
+
// without touching the Pulumi-shaped `storage` record.
|
|
103
|
+
let makeOperations = (
|
|
101
104
|
~pool: PgDriver.pool,
|
|
102
105
|
~name: string,
|
|
103
106
|
~indexes: array<indexConfig>,
|
|
104
107
|
~subIdField: option<string>,
|
|
105
|
-
): QueryDb_Adapter.
|
|
108
|
+
): QueryDb_Adapter.operations => {
|
|
106
109
|
let table = tableName(name)
|
|
107
|
-
// Schema setup is async
|
|
108
|
-
//
|
|
109
|
-
//
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
110
|
+
// Schema setup is async and LAZY: kicked off by the first operation, not at
|
|
111
|
+
// construction. A deploy-time caller may build the ops on a machine that
|
|
112
|
+
// cannot reach the database (VPC-private RDS) — only the runtime, which
|
|
113
|
+
// actually calls an operation, may open a connection. Subsequent operations
|
|
114
|
+
// await the same promise (table + index creation runs exactly once).
|
|
115
|
+
let readyRef: ref<option<promise<unit>>> = ref(None)
|
|
116
|
+
let ready = () =>
|
|
117
|
+
switch readyRef.contents {
|
|
118
|
+
| Some(p) => p
|
|
119
|
+
| None =>
|
|
120
|
+
let p = {
|
|
121
|
+
open Promise
|
|
122
|
+
ensureTable(~pool, ~table)->then(() => ensureIndexes(~pool, ~table, ~indexes))
|
|
123
|
+
}
|
|
124
|
+
readyRef := Some(p)
|
|
125
|
+
p
|
|
126
|
+
}
|
|
114
127
|
|
|
115
128
|
let rowsFor = async (id: string): array<JSON.t> => {
|
|
116
|
-
await ready
|
|
129
|
+
await ready()
|
|
117
130
|
(await pool->PgDriver.query(
|
|
118
131
|
`SELECT item FROM ${table} WHERE partition_key = $1 AND ${notExpiredClause} ORDER BY sub_key ASC`,
|
|
119
132
|
[JSON.Encode.string(id)],
|
|
@@ -121,7 +134,7 @@ let makeStorage = (
|
|
|
121
134
|
}
|
|
122
135
|
|
|
123
136
|
let saveOne = async (id: string, state: JSON.t, ttl: option<int>) => {
|
|
124
|
-
await ready
|
|
137
|
+
await ready()
|
|
125
138
|
let subKey = computeSubKey(state, subIdField)
|
|
126
139
|
let expiresAt = switch ttl {
|
|
127
140
|
| Some(t) => JSON.Encode.int(t)
|
|
@@ -167,7 +180,7 @@ let makeStorage = (
|
|
|
167
180
|
// Mirror DynamoDB's `ADD #field :inc`: read the counter on the partition item
|
|
168
181
|
// (counters are single-state, sub_key=""), add inc, upsert, return NEW total.
|
|
169
182
|
let count: QueryDb.count<string> = async (id, fieldName, inc) => {
|
|
170
|
-
await ready
|
|
183
|
+
await ready()
|
|
171
184
|
let existing = (await rowsFor(id))->Array.get(0)->Option.flatMap(JSON.Decode.object)
|
|
172
185
|
let current =
|
|
173
186
|
existing
|
|
@@ -186,7 +199,7 @@ let makeStorage = (
|
|
|
186
199
|
}
|
|
187
200
|
|
|
188
201
|
let delete: QueryDb.delete<string> = async (id, subIdOpt) => {
|
|
189
|
-
await ready
|
|
202
|
+
await ready()
|
|
190
203
|
let _ = switch subIdOpt {
|
|
191
204
|
| None =>
|
|
192
205
|
await pool->PgDriver.query(
|
|
@@ -219,11 +232,19 @@ let makeStorage = (
|
|
|
219
232
|
delete,
|
|
220
233
|
deleteBatch,
|
|
221
234
|
}
|
|
235
|
+
ops
|
|
236
|
+
}
|
|
222
237
|
|
|
238
|
+
let makeStorage = (
|
|
239
|
+
~pool: PgDriver.pool,
|
|
240
|
+
~name: string,
|
|
241
|
+
~indexes: array<indexConfig>,
|
|
242
|
+
~subIdField: option<string>,
|
|
243
|
+
): QueryDb_Adapter.storage => {
|
|
223
244
|
{
|
|
224
245
|
QueryDb_Adapter.resources: [],
|
|
225
246
|
dataSourceName: ""->Pulumi.Output.make,
|
|
226
|
-
operations: Pulumi.Output.make(
|
|
247
|
+
operations: Pulumi.Output.make(makeOperations(~pool, ~name, ~indexes, ~subIdField)),
|
|
227
248
|
}
|
|
228
249
|
}
|
|
229
250
|
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -122,6 +122,181 @@ module Make = (P: {let pool: PgDriver.pool}) => {
|
|
|
122
122
|
(await P.pool->PgDriver.query(sql, b.params))->Array.map(decodeRow(~withId=true, ...))
|
|
123
123
|
}
|
|
124
124
|
|
|
125
|
+
// ---------------------------------------------------------------------------
|
|
126
|
+
// GraphQL resolver push-downs (B3.2). The Postgres resolver Lambda dispatches
|
|
127
|
+
// these; they parallel the SQLite backend's `lookupByField` / `listPage` (the
|
|
128
|
+
// ones registered on LocalBus). All return `id`-carrying items (COALESCE from
|
|
129
|
+
// partition_key), matching the shape `QueryDbListQuery.run` operates over so
|
|
130
|
+
// the connection helpers agree.
|
|
131
|
+
//
|
|
132
|
+
// Text comparisons use `COLLATE "C"` (byte order — memcmp), the Postgres
|
|
133
|
+
// analogue of SQLite's BINARY collation, so cursor/order comparisons match
|
|
134
|
+
// JS string `<` for the ASCII id/status/name fields used as sort/filter keys.
|
|
135
|
+
// ---------------------------------------------------------------------------
|
|
136
|
+
|
|
137
|
+
// Equality lookup on an indexed JSON field — rides the expression index
|
|
138
|
+
// (`ensureIndexes`) instead of scanning. String comparison mirrors the
|
|
139
|
+
// resolver's JS path (which only matched string-typed fields).
|
|
140
|
+
let indexLookup = async (~readModelName, field: string, value: string): array<JSON.t> => {
|
|
141
|
+
let table = QueryDbStorage_Postgres.tableName(readModelName)
|
|
142
|
+
let b = {params: []}
|
|
143
|
+
let sql = `SELECT partition_key, item FROM ${table} WHERE ${col(field)} = ${b->param(
|
|
144
|
+
JSON.Encode.string(value),
|
|
145
|
+
)} AND ${notExpired}`
|
|
146
|
+
(await P.pool->PgDriver.query(sql, b.params))->Array.map(decodeRow(~withId=true, ...))
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// Batched-by-ids lookup: BatchGetItem analogue over the projection's own
|
|
150
|
+
// table. Single-key projections only (partition_key = the id). Missing ids
|
|
151
|
+
// drop out of the response (no cardinality preservation), matching
|
|
152
|
+
// BatchGetItem semantics.
|
|
153
|
+
let byIds = async (~readModelName, ids: array<string>): array<JSON.t> => {
|
|
154
|
+
if ids->Array.length == 0 {
|
|
155
|
+
[]
|
|
156
|
+
} else {
|
|
157
|
+
let table = QueryDbStorage_Postgres.tableName(readModelName)
|
|
158
|
+
let b = {params: []}
|
|
159
|
+
let idsJson = JSON.Encode.array(ids->Array.map(JSON.Encode.string))
|
|
160
|
+
let sql = `SELECT partition_key, item FROM ${table} WHERE partition_key = ANY(${b->param(
|
|
161
|
+
idsJson,
|
|
162
|
+
)}::text[]) AND ${notExpired}`
|
|
163
|
+
(await P.pool->PgDriver.query(sql, b.params))->Array.map(decodeRow(~withId=true, ...))
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// COALESCE(item->>'id', partition_key) — the effective id, byte-ordered.
|
|
168
|
+
let idExprC = `COALESCE(item->>'id', partition_key) COLLATE "C"`
|
|
169
|
+
let jsonTextC = (field: string): string => `${col(field)} COLLATE "C"`
|
|
170
|
+
|
|
171
|
+
// Connection-list push-down. Builds `item->>'field'` predicates + keyset
|
|
172
|
+
// WHERE + `ORDER BY … LIMIT pageSize+1` so a page reads only what it returns
|
|
173
|
+
// rather than the whole read model. Reproduces `QueryDbListQuery` semantics
|
|
174
|
+
// exactly for the shapes it handles; returns `None` (→ the resolver Lambda
|
|
175
|
+
// falls back to `scan` + the shared spec) for shapes not bit-exact in SQL:
|
|
176
|
+
// case-insensitive search/searchPrefix, Relay-global-id `ids`, and backward
|
|
177
|
+
// pagination (last/before). Cursor values, edges, and pageInfo are built by
|
|
178
|
+
// the SAME `QueryDbListQuery` helpers the fallback uses, so only the
|
|
179
|
+
// WHERE/ORDER/LIMIT needs to match — the parity harness asserts it does
|
|
180
|
+
// (SQLite ≡ Postgres ≡ spec). Mirrors QueryDbStorage_Sqlite.listPage.
|
|
181
|
+
let listPage = async (
|
|
182
|
+
~readModelName,
|
|
183
|
+
~argsDict: dict<JSON.t>,
|
|
184
|
+
~capability: GraphQL_FragmentGenerator.serverCapability,
|
|
185
|
+
~labelField as _,
|
|
186
|
+
): option<JSON.t> => {
|
|
187
|
+
let table = QueryDbStorage_Postgres.tableName(readModelName)
|
|
188
|
+
let filterDict =
|
|
189
|
+
argsDict->Dict.get("filter")->Option.flatMap(JSON.Decode.object)->Option.getOr(Dict.make())
|
|
190
|
+
let strNonEmpty = k =>
|
|
191
|
+
filterDict
|
|
192
|
+
->Dict.get(k)
|
|
193
|
+
->Option.flatMap(JSON.Decode.string)
|
|
194
|
+
->Option.mapOr(false, s => s->String.length > 0)
|
|
195
|
+
let hasIds =
|
|
196
|
+
filterDict
|
|
197
|
+
->Dict.get("ids")
|
|
198
|
+
->Option.flatMap(JSON.Decode.array)
|
|
199
|
+
->Option.mapOr(false, a => a->Array.length > 0)
|
|
200
|
+
let last = argsDict->Dict.get("last")->Option.flatMap(JSON.Decode.float)
|
|
201
|
+
let before = argsDict->Dict.get("before")->Option.flatMap(JSON.Decode.string)
|
|
202
|
+
if (
|
|
203
|
+
strNonEmpty("search") ||
|
|
204
|
+
strNonEmpty("searchPrefix") ||
|
|
205
|
+
hasIds ||
|
|
206
|
+
last->Option.isSome ||
|
|
207
|
+
before->Option.isSome
|
|
208
|
+
) {
|
|
209
|
+
None
|
|
210
|
+
} else {
|
|
211
|
+
let b = {params: []}
|
|
212
|
+
let whereParts = [notExpired]
|
|
213
|
+
let valString = v =>
|
|
214
|
+
switch v->JSON.Decode.string {
|
|
215
|
+
| Some(s) => Some(s)
|
|
216
|
+
| None => v->JSON.Decode.float->Option.map(f => Float.toString(f))
|
|
217
|
+
}
|
|
218
|
+
capability.filterFields->Array.forEach(f => {
|
|
219
|
+
switch filterDict->Dict.get(f.name ++ "Eq") {
|
|
220
|
+
| Some(v) when v != JSON.Encode.null =>
|
|
221
|
+
whereParts->Array.push(
|
|
222
|
+
`${jsonTextC(f.name)} = ${b->param(JSON.Encode.string(valString(v)->Option.getOr("")))}`,
|
|
223
|
+
)
|
|
224
|
+
| _ => ()
|
|
225
|
+
}
|
|
226
|
+
if f.range {
|
|
227
|
+
switch filterDict->Dict.get(f.name ++ "From") {
|
|
228
|
+
| Some(v) when v != JSON.Encode.null =>
|
|
229
|
+
whereParts->Array.push(
|
|
230
|
+
`${jsonTextC(f.name)} >= ${b->param(
|
|
231
|
+
JSON.Encode.string(valString(v)->Option.getOr("")),
|
|
232
|
+
)}`,
|
|
233
|
+
)
|
|
234
|
+
| _ => ()
|
|
235
|
+
}
|
|
236
|
+
switch filterDict->Dict.get(f.name ++ "To") {
|
|
237
|
+
| Some(v) when v != JSON.Encode.null =>
|
|
238
|
+
whereParts->Array.push(
|
|
239
|
+
`${jsonTextC(f.name)} <= ${b->param(
|
|
240
|
+
JSON.Encode.string(valString(v)->Option.getOr("")),
|
|
241
|
+
)}`,
|
|
242
|
+
)
|
|
243
|
+
| _ => ()
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
})
|
|
247
|
+
let orderByDict = argsDict->Dict.get("orderBy")->Option.flatMap(JSON.Decode.object)
|
|
248
|
+
let orderByField =
|
|
249
|
+
orderByDict->Option.flatMap(ob => ob->Dict.get("field"))->Option.flatMap(JSON.Decode.string)
|
|
250
|
+
let isDesc =
|
|
251
|
+
orderByDict
|
|
252
|
+
->Option.flatMap(ob => ob->Dict.get("direction"))
|
|
253
|
+
->Option.flatMap(JSON.Decode.string)
|
|
254
|
+
->Option.getOr("ASC") == "DESC"
|
|
255
|
+
let cursorExpr = switch orderByField {
|
|
256
|
+
| Some(f) => jsonTextC(f)
|
|
257
|
+
| None => idExprC
|
|
258
|
+
}
|
|
259
|
+
switch argsDict->Dict.get("after")->Option.flatMap(JSON.Decode.string) {
|
|
260
|
+
| Some(c) =>
|
|
261
|
+
whereParts->Array.push(
|
|
262
|
+
`${cursorExpr} ${isDesc ? "<" : ">"} ${b->param(
|
|
263
|
+
JSON.Encode.string(ReventlessCore.QueryDbListQuery.decodeCursor(c)),
|
|
264
|
+
)}`,
|
|
265
|
+
)
|
|
266
|
+
| None => ()
|
|
267
|
+
}
|
|
268
|
+
let orderClause = switch orderByField {
|
|
269
|
+
| Some(f) => `${jsonTextC(f)} ${isDesc ? "DESC" : "ASC"}, ${idExprC} ASC`
|
|
270
|
+
| None => `${idExprC} ASC`
|
|
271
|
+
}
|
|
272
|
+
let pageSize =
|
|
273
|
+
argsDict
|
|
274
|
+
->Dict.get("first")
|
|
275
|
+
->Option.flatMap(JSON.Decode.float)
|
|
276
|
+
->Option.map(Float.toInt)
|
|
277
|
+
->Option.getOr(ReventlessCore.QueryDbListQuery.defaultListPageSize)
|
|
278
|
+
let sql = `SELECT partition_key, item FROM ${table} WHERE ${whereParts->Array.join(
|
|
279
|
+
" AND ",
|
|
280
|
+
)} ORDER BY ${orderClause} LIMIT ${b->param(JSON.Encode.int(pageSize + 1))}`
|
|
281
|
+
let rows = (await P.pool->PgDriver.query(sql, b.params))->Array.map(decodeRow(~withId=true, ...))
|
|
282
|
+
let hasMore = rows->Array.length > pageSize
|
|
283
|
+
let pageItems = rows->Array.slice(~start=0, ~end=pageSize)
|
|
284
|
+
let cursorField = orderByField->Option.getOr("id")
|
|
285
|
+
let cursorValueOf = item =>
|
|
286
|
+
ReventlessCore.QueryDbListQuery.getFieldString(item, cursorField)->Option.getOr(
|
|
287
|
+
ReventlessCore.QueryDbListQuery.getId(item),
|
|
288
|
+
)
|
|
289
|
+
Some(
|
|
290
|
+
ReventlessCore.QueryDbListQuery.buildConnection(
|
|
291
|
+
~pageItems,
|
|
292
|
+
~hasNextPage=hasMore,
|
|
293
|
+
~hasPreviousPage=false,
|
|
294
|
+
~cursorValueOf,
|
|
295
|
+
),
|
|
296
|
+
)
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
|
|
125
300
|
let make: QueryDb_Adapter.queryEngineMaker = _allQueryDbs =>
|
|
126
301
|
Pulumi.Output.make({QueryEngine.scan, query})
|
|
127
302
|
}
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
// Parity harness for the Postgres connection-list push-down (B3.2a-1).
|
|
2
|
+
//
|
|
3
|
+
// `QueryEnginePostgres.listPage` builds `item->>'field'` predicates + keyset
|
|
4
|
+
// WHERE + ORDER BY … LIMIT to serve a page without materialising the whole read
|
|
5
|
+
// model. Its result MUST equal `QueryDbListQuery.run` (the shared spec in
|
|
6
|
+
// reventless-core that the in-memory backend, the SQLite push-down, and the
|
|
7
|
+
// resolver fallback all use) for every shape it claims to handle. This is the
|
|
8
|
+
// Postgres twin of reventless-local's `QueryDbListPushdownParityTest` — same
|
|
9
|
+
// capability, same rows, same (data, args) matrix — so all three backends are
|
|
10
|
+
// held to one spec.
|
|
11
|
+
//
|
|
12
|
+
// Skipped unless PG_URL is set (like PostgresIntegrationTest), so the default
|
|
13
|
+
// `pnpm test` stays dependency-free. Run with e.g.:
|
|
14
|
+
// PG_URL=postgres://postgres:postgres@localhost:5432/postgres pnpm test
|
|
15
|
+
|
|
16
|
+
open JestGlobals
|
|
17
|
+
open ReventlessCore
|
|
18
|
+
|
|
19
|
+
@val external processEnv: dict<string> = "process.env"
|
|
20
|
+
|
|
21
|
+
let capability: GraphQL_FragmentGenerator.serverCapability = {
|
|
22
|
+
filterFields: [
|
|
23
|
+
{name: "status", gqlType: "String", range: false},
|
|
24
|
+
{name: "qty", gqlType: "Int", range: true},
|
|
25
|
+
],
|
|
26
|
+
sortFields: ["name", "qty", "status"],
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
let mk = (id, status, name, qty) => {
|
|
30
|
+
let o = Dict.make()
|
|
31
|
+
o->Dict.set("id", JSON.Encode.string(id))
|
|
32
|
+
o->Dict.set("status", JSON.Encode.string(status))
|
|
33
|
+
o->Dict.set("name", JSON.Encode.string(name))
|
|
34
|
+
o->Dict.set("qty", JSON.Encode.int(qty))
|
|
35
|
+
JSON.Encode.object(o)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Names differ from id-order; status has duplicates (tiebreak); qty exercises the
|
|
39
|
+
// numeric-field-as-string comparison the push-down must match.
|
|
40
|
+
let rows = [
|
|
41
|
+
("p-1", "active", "Charlie", 3),
|
|
42
|
+
("p-2", "active", "Alpha", 1),
|
|
43
|
+
("p-3", "inactive", "Echo", 5),
|
|
44
|
+
("p-4", "active", "Bravo", 2),
|
|
45
|
+
("p-5", "inactive", "Delta", 4),
|
|
46
|
+
]
|
|
47
|
+
|
|
48
|
+
// The exact items seeded, in memory — the spec runs over these (equivalent to a
|
|
49
|
+
// full scan of the table, since every save upserts one of these by id).
|
|
50
|
+
let seededItems = rows->Array.map(((id, status, name, qty)) => mk(id, status, name, qty))
|
|
51
|
+
|
|
52
|
+
// ── normalise a connection to a comparable value ────────────────────────────
|
|
53
|
+
type normEdge = {id: string, cursor: string}
|
|
54
|
+
type normConn = {
|
|
55
|
+
edges: array<normEdge>,
|
|
56
|
+
hasNext: bool,
|
|
57
|
+
hasPrev: bool,
|
|
58
|
+
startCursor: option<string>,
|
|
59
|
+
endCursor: option<string>,
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
let field = (j, k) => j->JSON.Decode.object->Option.flatMap(d => d->Dict.get(k))
|
|
63
|
+
let str = (j, k) => j->field(k)->Option.flatMap(JSON.Decode.string)
|
|
64
|
+
|
|
65
|
+
let norm = (conn: JSON.t): normConn => {
|
|
66
|
+
let edges =
|
|
67
|
+
conn
|
|
68
|
+
->field("edges")
|
|
69
|
+
->Option.flatMap(JSON.Decode.array)
|
|
70
|
+
->Option.getOr([])
|
|
71
|
+
->Array.map(e => {
|
|
72
|
+
id: e->field("node")->Option.flatMap(n => n->str("id"))->Option.getOr(""),
|
|
73
|
+
cursor: e->str("cursor")->Option.getOr(""),
|
|
74
|
+
})
|
|
75
|
+
let pi = conn->field("pageInfo")->Option.getOr(JSON.Encode.null)
|
|
76
|
+
{
|
|
77
|
+
edges,
|
|
78
|
+
hasNext: pi->field("hasNextPage")->Option.flatMap(JSON.Decode.bool)->Option.getOr(false),
|
|
79
|
+
hasPrev: pi->field("hasPreviousPage")->Option.flatMap(JSON.Decode.bool)->Option.getOr(false),
|
|
80
|
+
startCursor: pi->str("startCursor"),
|
|
81
|
+
endCursor: pi->str("endCursor"),
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
let argsOf = (entries: array<(string, JSON.t)>): dict<JSON.t> => Dict.fromArray(entries)
|
|
86
|
+
let filterOf = (entries: array<(string, JSON.t)>): JSON.t =>
|
|
87
|
+
JSON.Encode.object(Dict.fromArray(entries))
|
|
88
|
+
let orderBy = (f, dir) =>
|
|
89
|
+
JSON.Encode.object(
|
|
90
|
+
Dict.fromArray([("field", JSON.Encode.string(f)), ("direction", JSON.Encode.string(dir))]),
|
|
91
|
+
)
|
|
92
|
+
let cur = QueryDbListQuery.encodeCursor
|
|
93
|
+
|
|
94
|
+
let expectedFor = argsDict =>
|
|
95
|
+
QueryDbListQuery.run(
|
|
96
|
+
~items=seededItems,
|
|
97
|
+
~argsDict,
|
|
98
|
+
~capability,
|
|
99
|
+
~labelField="name",
|
|
100
|
+
~decodeLocalId=_ => None,
|
|
101
|
+
)->norm
|
|
102
|
+
|
|
103
|
+
switch processEnv->Dict.get("PG_URL") {
|
|
104
|
+
| None =>
|
|
105
|
+
testSync("Postgres list push-down parity (skipped — set PG_URL to run)", () =>
|
|
106
|
+
expect(true)->toBe(true)
|
|
107
|
+
)
|
|
108
|
+
| Some(url) =>
|
|
109
|
+
let pool = PgDriver.makePool({connectionString: url})
|
|
110
|
+
module Eng = QueryEnginePostgres.Make({
|
|
111
|
+
let pool = pool
|
|
112
|
+
})
|
|
113
|
+
let name = "listparity"
|
|
114
|
+
|
|
115
|
+
// Seed via the real storage ops (upsert by id → table converges to the 5 rows).
|
|
116
|
+
beforeAllAsync(async () => {
|
|
117
|
+
let ops = QueryDbStorage_Postgres.makeOperations(~pool, ~name, ~indexes=[], ~subIdField=None)
|
|
118
|
+
for i in 0 to rows->Array.length - 1 {
|
|
119
|
+
let (id, status, nm, qty) = rows->Array.getUnsafe(i)
|
|
120
|
+
let _ = await ops.save(id, mk(id, status, nm, qty), QueryDb.Any, None)
|
|
121
|
+
}
|
|
122
|
+
})
|
|
123
|
+
afterAll(() => {
|
|
124
|
+
let _ = pool->PgDriver.endPool
|
|
125
|
+
})
|
|
126
|
+
|
|
127
|
+
// Assert the push-down serves this shape AND matches the spec exactly.
|
|
128
|
+
let checkPushed = async (~label, args) => {
|
|
129
|
+
let argsDict = argsOf(args)
|
|
130
|
+
switch await Eng.listPage(~readModelName=name, ~argsDict, ~capability, ~labelField="name") {
|
|
131
|
+
| Some(actual) => expect(actual->norm)->toEqual(expectedFor(argsDict))
|
|
132
|
+
| None => expect("push-down for " ++ label)->toBe("returned None")
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// Assert the push-down declines this shape (resolver falls back to the spec).
|
|
137
|
+
let checkFallback = async args => {
|
|
138
|
+
let r = await Eng.listPage(~readModelName=name, ~argsDict=argsOf(args), ~capability, ~labelField="name")
|
|
139
|
+
expect(r->Option.isSome)->toBe(false)
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
describe("QueryDb list push-down parity (Postgres ≡ QueryDbListQuery spec)", () => {
|
|
143
|
+
testPromise("bare page", () => checkPushed(~label="bare", []))
|
|
144
|
+
testPromise("first:2", () => checkPushed(~label="first", [("first", JSON.Encode.int(2))]))
|
|
145
|
+
testPromise("first:2 + after", () =>
|
|
146
|
+
checkPushed(
|
|
147
|
+
~label="after",
|
|
148
|
+
[("first", JSON.Encode.int(2)), ("after", JSON.Encode.string(cur("p-2")))],
|
|
149
|
+
)
|
|
150
|
+
)
|
|
151
|
+
testPromise("statusEq active", () =>
|
|
152
|
+
checkPushed(~label="eq", [("filter", filterOf([("statusEq", JSON.Encode.string("active"))]))])
|
|
153
|
+
)
|
|
154
|
+
testPromise("statusEq active + first:2", () =>
|
|
155
|
+
checkPushed(
|
|
156
|
+
~label="eq+first",
|
|
157
|
+
[
|
|
158
|
+
("filter", filterOf([("statusEq", JSON.Encode.string("active"))])),
|
|
159
|
+
("first", JSON.Encode.int(2)),
|
|
160
|
+
],
|
|
161
|
+
)
|
|
162
|
+
)
|
|
163
|
+
testPromise("orderBy name ASC", () =>
|
|
164
|
+
checkPushed(~label="order-asc", [("orderBy", orderBy("name", "ASC"))])
|
|
165
|
+
)
|
|
166
|
+
testPromise("orderBy name DESC", () =>
|
|
167
|
+
checkPushed(~label="order-desc", [("orderBy", orderBy("name", "DESC"))])
|
|
168
|
+
)
|
|
169
|
+
testPromise("orderBy name DESC + first:2 + after", () =>
|
|
170
|
+
checkPushed(
|
|
171
|
+
~label="order-desc-after",
|
|
172
|
+
[
|
|
173
|
+
("orderBy", orderBy("name", "DESC")),
|
|
174
|
+
("first", JSON.Encode.int(2)),
|
|
175
|
+
("after", JSON.Encode.string(cur("Delta"))),
|
|
176
|
+
],
|
|
177
|
+
)
|
|
178
|
+
)
|
|
179
|
+
// status has duplicate values → exercises the id tiebreak in ORDER BY.
|
|
180
|
+
testPromise("orderBy status ASC (tiebreak by id)", () =>
|
|
181
|
+
checkPushed(~label="order-tiebreak", [("orderBy", orderBy("status", "ASC"))])
|
|
182
|
+
)
|
|
183
|
+
// qty is numeric → exercises `item->>'qty'` (text) vs Float.toString string parity.
|
|
184
|
+
testPromise("qty range From/To (numeric-as-string)", () =>
|
|
185
|
+
checkPushed(
|
|
186
|
+
~label="range",
|
|
187
|
+
[
|
|
188
|
+
(
|
|
189
|
+
"filter",
|
|
190
|
+
filterOf([("qtyFrom", JSON.Encode.string("2")), ("qtyTo", JSON.Encode.string("4"))]),
|
|
191
|
+
),
|
|
192
|
+
],
|
|
193
|
+
)
|
|
194
|
+
)
|
|
195
|
+
testPromise("orderBy qty ASC (numeric-as-string sort)", () =>
|
|
196
|
+
checkPushed(~label="order-qty", [("orderBy", orderBy("qty", "ASC"))])
|
|
197
|
+
)
|
|
198
|
+
|
|
199
|
+
// Shapes the push-down declines → resolver uses the JS fallback.
|
|
200
|
+
testPromise("search → fallback", () =>
|
|
201
|
+
checkFallback([("filter", filterOf([("search", JSON.Encode.string("alp"))]))])
|
|
202
|
+
)
|
|
203
|
+
testPromise("searchPrefix → fallback", () =>
|
|
204
|
+
checkFallback([("filter", filterOf([("searchPrefix", JSON.Encode.string("A"))]))])
|
|
205
|
+
)
|
|
206
|
+
testPromise("ids → fallback", () =>
|
|
207
|
+
checkFallback([("filter", filterOf([("ids", JSON.Encode.array([JSON.Encode.string("p-1")]))]))])
|
|
208
|
+
)
|
|
209
|
+
testPromise("backward (last/before) → fallback", () =>
|
|
210
|
+
checkFallback([("last", JSON.Encode.int(2)), ("before", JSON.Encode.string(cur("p-4")))])
|
|
211
|
+
)
|
|
212
|
+
})
|
|
213
|
+
|
|
214
|
+
// ── indexLookup / byIds push-downs ────────────────────────────────────────
|
|
215
|
+
let idsOf = (items: array<JSON.t>): array<string> =>
|
|
216
|
+
items->Array.filterMap(i => i->str("id"))->Array.toSorted(String.compare)
|
|
217
|
+
|
|
218
|
+
describe("QueryEnginePostgres index / batch push-downs", () => {
|
|
219
|
+
testPromise("indexLookup by status = active", async () => {
|
|
220
|
+
let items = await Eng.indexLookup(~readModelName=name, "status", "active")
|
|
221
|
+
expect(items->idsOf)->toEqual(["p-1", "p-2", "p-4"])
|
|
222
|
+
})
|
|
223
|
+
testPromise("indexLookup miss → empty", async () => {
|
|
224
|
+
let items = await Eng.indexLookup(~readModelName=name, "status", "nope")
|
|
225
|
+
expect(items->Array.length)->toBe(0)
|
|
226
|
+
})
|
|
227
|
+
testPromise("byIds returns matches, drops missing", async () => {
|
|
228
|
+
let items = await Eng.byIds(~readModelName=name, ["p-1", "p-3", "absent"])
|
|
229
|
+
expect(items->idsOf)->toEqual(["p-1", "p-3"])
|
|
230
|
+
})
|
|
231
|
+
testPromise("byIds [] → empty (no query)", async () => {
|
|
232
|
+
let items = await Eng.byIds(~readModelName=name, [])
|
|
233
|
+
expect(items->Array.length)->toBe(0)
|
|
234
|
+
})
|
|
235
|
+
})
|
|
236
|
+
}
|
|
Binary file
|
|
Binary file
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@reventlessdev/reventless-postgres",
|
|
3
|
-
"version": "3.0.0-alpha.
|
|
3
|
+
"version": "3.0.0-alpha.5",
|
|
4
4
|
"description": "Postgres storage backend for Reventless (classic + DCB event logs, query DB, change feed). Connection-string only — RDS/Aurora, any managed provider, or a local container.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"jest": {
|
|
@@ -22,12 +22,12 @@
|
|
|
22
22
|
"dependencies": {
|
|
23
23
|
"pg": "^8.13.1",
|
|
24
24
|
"sury": "11.0.0-alpha.4",
|
|
25
|
-
"@reventlessdev/rescript-
|
|
25
|
+
"@reventlessdev/rescript-jest": "1.0.0-alpha.6",
|
|
26
|
+
"@reventlessdev/rescript-effect": "0.1.0-alpha.25",
|
|
26
27
|
"@reventlessdev/rescript-pulumi-pulumi": "2.3.0-alpha.14",
|
|
27
|
-
"@reventlessdev/reventless-core": "3.0.0-alpha.
|
|
28
|
-
"@reventlessdev/
|
|
29
|
-
"@reventlessdev/reventless-
|
|
30
|
-
"@reventlessdev/reventless-spec": "3.0.0-alpha.66"
|
|
28
|
+
"@reventlessdev/reventless-core": "3.0.0-alpha.143",
|
|
29
|
+
"@reventlessdev/reventless-infra": "3.0.0-alpha.89",
|
|
30
|
+
"@reventlessdev/reventless-spec": "3.0.0-alpha.67"
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
33
33
|
"@types/pg": "^8.11.10",
|
|
@@ -50,7 +50,7 @@
|
|
|
50
50
|
"build": "rescript build",
|
|
51
51
|
"start": "rescript watch",
|
|
52
52
|
"clean": "rescript clean",
|
|
53
|
-
"rebuild": "pnpm run clean && pnpm run build
|
|
53
|
+
"rebuild": "pnpm run clean && pnpm run build",
|
|
54
54
|
"test": "NODE_OPTIONS='--experimental-vm-modules' npx jest"
|
|
55
55
|
}
|
|
56
56
|
}
|
|
@@ -97,23 +97,36 @@ let withId = (item: JSON.t, partitionKey: string): JSON.t =>
|
|
|
97
97
|
| _ => item
|
|
98
98
|
}
|
|
99
99
|
|
|
100
|
-
|
|
100
|
+
// Plain operation set for a read model's qdb table. Exposed separately from
|
|
101
|
+
// `makeStorage` so the deployed-Lambda runtime (reventless-aws) can bind ops
|
|
102
|
+
// without touching the Pulumi-shaped `storage` record.
|
|
103
|
+
let makeOperations = (
|
|
101
104
|
~pool: PgDriver.pool,
|
|
102
105
|
~name: string,
|
|
103
106
|
~indexes: array<indexConfig>,
|
|
104
107
|
~subIdField: option<string>,
|
|
105
|
-
): QueryDb_Adapter.
|
|
108
|
+
): QueryDb_Adapter.operations => {
|
|
106
109
|
let table = tableName(name)
|
|
107
|
-
// Schema setup is async
|
|
108
|
-
//
|
|
109
|
-
//
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
110
|
+
// Schema setup is async and LAZY: kicked off by the first operation, not at
|
|
111
|
+
// construction. A deploy-time caller may build the ops on a machine that
|
|
112
|
+
// cannot reach the database (VPC-private RDS) — only the runtime, which
|
|
113
|
+
// actually calls an operation, may open a connection. Subsequent operations
|
|
114
|
+
// await the same promise (table + index creation runs exactly once).
|
|
115
|
+
let readyRef: ref<option<promise<unit>>> = ref(None)
|
|
116
|
+
let ready = () =>
|
|
117
|
+
switch readyRef.contents {
|
|
118
|
+
| Some(p) => p
|
|
119
|
+
| None =>
|
|
120
|
+
let p = {
|
|
121
|
+
open Promise
|
|
122
|
+
ensureTable(~pool, ~table)->then(() => ensureIndexes(~pool, ~table, ~indexes))
|
|
123
|
+
}
|
|
124
|
+
readyRef := Some(p)
|
|
125
|
+
p
|
|
126
|
+
}
|
|
114
127
|
|
|
115
128
|
let rowsFor = async (id: string): array<JSON.t> => {
|
|
116
|
-
await ready
|
|
129
|
+
await ready()
|
|
117
130
|
(await pool->PgDriver.query(
|
|
118
131
|
`SELECT item FROM ${table} WHERE partition_key = $1 AND ${notExpiredClause} ORDER BY sub_key ASC`,
|
|
119
132
|
[JSON.Encode.string(id)],
|
|
@@ -121,7 +134,7 @@ let makeStorage = (
|
|
|
121
134
|
}
|
|
122
135
|
|
|
123
136
|
let saveOne = async (id: string, state: JSON.t, ttl: option<int>) => {
|
|
124
|
-
await ready
|
|
137
|
+
await ready()
|
|
125
138
|
let subKey = computeSubKey(state, subIdField)
|
|
126
139
|
let expiresAt = switch ttl {
|
|
127
140
|
| Some(t) => JSON.Encode.int(t)
|
|
@@ -167,7 +180,7 @@ let makeStorage = (
|
|
|
167
180
|
// Mirror DynamoDB's `ADD #field :inc`: read the counter on the partition item
|
|
168
181
|
// (counters are single-state, sub_key=""), add inc, upsert, return NEW total.
|
|
169
182
|
let count: QueryDb.count<string> = async (id, fieldName, inc) => {
|
|
170
|
-
await ready
|
|
183
|
+
await ready()
|
|
171
184
|
let existing = (await rowsFor(id))->Array.get(0)->Option.flatMap(JSON.Decode.object)
|
|
172
185
|
let current =
|
|
173
186
|
existing
|
|
@@ -186,7 +199,7 @@ let makeStorage = (
|
|
|
186
199
|
}
|
|
187
200
|
|
|
188
201
|
let delete: QueryDb.delete<string> = async (id, subIdOpt) => {
|
|
189
|
-
await ready
|
|
202
|
+
await ready()
|
|
190
203
|
let _ = switch subIdOpt {
|
|
191
204
|
| None =>
|
|
192
205
|
await pool->PgDriver.query(
|
|
@@ -219,11 +232,19 @@ let makeStorage = (
|
|
|
219
232
|
delete,
|
|
220
233
|
deleteBatch,
|
|
221
234
|
}
|
|
235
|
+
ops
|
|
236
|
+
}
|
|
222
237
|
|
|
238
|
+
let makeStorage = (
|
|
239
|
+
~pool: PgDriver.pool,
|
|
240
|
+
~name: string,
|
|
241
|
+
~indexes: array<indexConfig>,
|
|
242
|
+
~subIdField: option<string>,
|
|
243
|
+
): QueryDb_Adapter.storage => {
|
|
223
244
|
{
|
|
224
245
|
QueryDb_Adapter.resources: [],
|
|
225
246
|
dataSourceName: ""->Pulumi.Output.make,
|
|
226
|
-
operations: Pulumi.Output.make(
|
|
247
|
+
operations: Pulumi.Output.make(makeOperations(~pool, ~name, ~indexes, ~subIdField)),
|
|
227
248
|
}
|
|
228
249
|
}
|
|
229
250
|
|