@reventlessdev/reventless-postgres 3.0.0-alpha.5 → 3.0.0-alpha.7

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.
Files changed (125) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/lib/bs/.compiler.log +2 -2
  3. package/lib/bs/ReventlessPostgres.cmi +0 -0
  4. package/lib/bs/ReventlessPostgres.cmt +0 -0
  5. package/lib/bs/ReventlessPostgres.mlmap +3 -0
  6. package/lib/bs/compiler-info.json +1 -1
  7. package/lib/bs/src/DcbEventLogStorage_Postgres-ReventlessPostgres.cmi +0 -0
  8. package/lib/bs/src/DcbEventLogStorage_Postgres-ReventlessPostgres.cmj +0 -0
  9. package/lib/bs/src/DcbEventLogStorage_Postgres-ReventlessPostgres.cmt +0 -0
  10. package/lib/bs/src/DcbEventLogStorage_Postgres.ast +0 -0
  11. package/lib/bs/src/DcbEventLogStorage_Postgres.res +25 -340
  12. package/lib/bs/src/DcbEventLogStorage_Postgres.res.mjs +32 -348
  13. package/lib/bs/src/DcbEventLogStorage_Postgres_Ops-ReventlessPostgres.cmi +0 -0
  14. package/lib/bs/src/DcbEventLogStorage_Postgres_Ops-ReventlessPostgres.cmj +0 -0
  15. package/lib/bs/src/DcbEventLogStorage_Postgres_Ops-ReventlessPostgres.cmt +0 -0
  16. package/lib/bs/src/DcbEventLogStorage_Postgres_Ops.ast +0 -0
  17. package/lib/bs/src/DcbEventLogStorage_Postgres_Ops.res +350 -0
  18. package/lib/bs/src/DcbEventLogStorage_Postgres_Ops.res.mjs +380 -0
  19. package/lib/bs/src/EventLogChangeFeed-ReventlessPostgres.cmi +0 -0
  20. package/lib/bs/src/EventLogChangeFeed-ReventlessPostgres.cmt +0 -0
  21. package/lib/bs/src/EventLogStorage_Postgres-ReventlessPostgres.cmi +0 -0
  22. package/lib/bs/src/EventLogStorage_Postgres-ReventlessPostgres.cmj +0 -0
  23. package/lib/bs/src/EventLogStorage_Postgres-ReventlessPostgres.cmt +0 -0
  24. package/lib/bs/src/EventLogStorage_Postgres.ast +0 -0
  25. package/lib/bs/src/EventLogStorage_Postgres.res +12 -170
  26. package/lib/bs/src/EventLogStorage_Postgres.res.mjs +20 -186
  27. package/lib/bs/src/EventLogStorage_Postgres_Ops-ReventlessPostgres.cmi +0 -0
  28. package/lib/bs/src/EventLogStorage_Postgres_Ops-ReventlessPostgres.cmj +0 -0
  29. package/lib/bs/src/EventLogStorage_Postgres_Ops-ReventlessPostgres.cmt +0 -0
  30. package/lib/bs/src/EventLogStorage_Postgres_Ops.ast +0 -0
  31. package/lib/bs/src/EventLogStorage_Postgres_Ops.res +180 -0
  32. package/lib/bs/src/EventLogStorage_Postgres_Ops.res.mjs +202 -0
  33. package/lib/bs/src/PgChangeFeed-ReventlessPostgres.cmi +0 -0
  34. package/lib/bs/src/PgChangeFeed-ReventlessPostgres.cmt +0 -0
  35. package/lib/bs/src/PgSchema-ReventlessPostgres.cmi +0 -0
  36. package/lib/bs/src/PgSchema-ReventlessPostgres.cmt +0 -0
  37. package/lib/bs/src/QueryDbStorage_Postgres-ReventlessPostgres.cmi +0 -0
  38. package/lib/bs/src/QueryDbStorage_Postgres-ReventlessPostgres.cmt +0 -0
  39. package/lib/bs/src/QueryDbStorage_Postgres.ast +0 -0
  40. package/lib/bs/src/QueryDbStorage_Postgres.res +9 -232
  41. package/lib/bs/src/QueryDbStorage_Postgres.res.mjs +25 -204
  42. package/lib/bs/src/QueryDbStorage_Postgres_Ops-ReventlessPostgres.cmi +0 -0
  43. package/lib/bs/src/QueryDbStorage_Postgres_Ops-ReventlessPostgres.cmj +0 -0
  44. package/lib/bs/src/QueryDbStorage_Postgres_Ops-ReventlessPostgres.cmt +0 -0
  45. package/lib/bs/src/QueryDbStorage_Postgres_Ops.ast +0 -0
  46. package/lib/bs/src/QueryDbStorage_Postgres_Ops.res +229 -0
  47. package/lib/bs/src/QueryDbStorage_Postgres_Ops.res.mjs +222 -0
  48. package/lib/bs/src/QueryEnginePostgres-ReventlessPostgres.cmi +0 -0
  49. package/lib/bs/src/QueryEnginePostgres-ReventlessPostgres.cmt +0 -0
  50. package/lib/bs/src/QueryEnginePostgres.ast +0 -0
  51. package/lib/bs/src/QueryEnginePostgres.res +84 -0
  52. package/lib/bs/src/QueryEnginePostgres.res.mjs +74 -0
  53. package/lib/bs/tests/PostgresIntegrationTest-ReventlessPostgres.cmi +0 -0
  54. package/lib/bs/tests/PostgresIntegrationTest-ReventlessPostgres.cmt +0 -0
  55. package/lib/bs/tests/QueryEnginePostgresListPageTest-ReventlessPostgres.cmi +0 -0
  56. package/lib/bs/tests/QueryEnginePostgresListPageTest-ReventlessPostgres.cmt +0 -0
  57. package/lib/bs/tests/QueryEnginePostgresListPageTest.ast +0 -0
  58. package/lib/bs/tests/QueryEnginePostgresListPageTest.res +76 -0
  59. package/lib/bs/tests/QueryEnginePostgresListPageTest.res.mjs +136 -0
  60. package/lib/ocaml/.compiler.log +2 -2
  61. package/lib/ocaml/DcbEventLogStorage_Postgres-ReventlessPostgres.cmi +0 -0
  62. package/lib/ocaml/DcbEventLogStorage_Postgres-ReventlessPostgres.cmj +0 -0
  63. package/lib/ocaml/DcbEventLogStorage_Postgres-ReventlessPostgres.cmt +0 -0
  64. package/lib/ocaml/DcbEventLogStorage_Postgres.ast +0 -0
  65. package/lib/ocaml/DcbEventLogStorage_Postgres.res +25 -340
  66. package/lib/ocaml/DcbEventLogStorage_Postgres_Ops-ReventlessPostgres.cmi +0 -0
  67. package/lib/ocaml/DcbEventLogStorage_Postgres_Ops-ReventlessPostgres.cmj +0 -0
  68. package/lib/ocaml/DcbEventLogStorage_Postgres_Ops-ReventlessPostgres.cmt +0 -0
  69. package/lib/ocaml/DcbEventLogStorage_Postgres_Ops.ast +0 -0
  70. package/lib/ocaml/DcbEventLogStorage_Postgres_Ops.res +350 -0
  71. package/lib/ocaml/EventLogChangeFeed-ReventlessPostgres.cmi +0 -0
  72. package/lib/ocaml/EventLogChangeFeed-ReventlessPostgres.cmt +0 -0
  73. package/lib/ocaml/EventLogStorage_Postgres-ReventlessPostgres.cmi +0 -0
  74. package/lib/ocaml/EventLogStorage_Postgres-ReventlessPostgres.cmj +0 -0
  75. package/lib/ocaml/EventLogStorage_Postgres-ReventlessPostgres.cmt +0 -0
  76. package/lib/ocaml/EventLogStorage_Postgres.ast +0 -0
  77. package/lib/ocaml/EventLogStorage_Postgres.res +12 -170
  78. package/lib/ocaml/EventLogStorage_Postgres_Ops-ReventlessPostgres.cmi +0 -0
  79. package/lib/ocaml/EventLogStorage_Postgres_Ops-ReventlessPostgres.cmj +0 -0
  80. package/lib/ocaml/EventLogStorage_Postgres_Ops-ReventlessPostgres.cmt +0 -0
  81. package/lib/ocaml/EventLogStorage_Postgres_Ops.ast +0 -0
  82. package/lib/ocaml/EventLogStorage_Postgres_Ops.res +180 -0
  83. package/lib/ocaml/PgChangeFeed-ReventlessPostgres.cmi +0 -0
  84. package/lib/ocaml/PgChangeFeed-ReventlessPostgres.cmt +0 -0
  85. package/lib/ocaml/PgSchema-ReventlessPostgres.cmi +0 -0
  86. package/lib/ocaml/PgSchema-ReventlessPostgres.cmt +0 -0
  87. package/lib/ocaml/PostgresIntegrationTest-ReventlessPostgres.cmi +0 -0
  88. package/lib/ocaml/PostgresIntegrationTest-ReventlessPostgres.cmt +0 -0
  89. package/lib/ocaml/QueryDbStorage_Postgres-ReventlessPostgres.cmi +0 -0
  90. package/lib/ocaml/QueryDbStorage_Postgres-ReventlessPostgres.cmt +0 -0
  91. package/lib/ocaml/QueryDbStorage_Postgres.ast +0 -0
  92. package/lib/ocaml/QueryDbStorage_Postgres.res +9 -232
  93. package/lib/ocaml/QueryDbStorage_Postgres_Ops-ReventlessPostgres.cmi +0 -0
  94. package/lib/ocaml/QueryDbStorage_Postgres_Ops-ReventlessPostgres.cmj +0 -0
  95. package/lib/ocaml/QueryDbStorage_Postgres_Ops-ReventlessPostgres.cmt +0 -0
  96. package/lib/ocaml/QueryDbStorage_Postgres_Ops.ast +0 -0
  97. package/lib/ocaml/QueryDbStorage_Postgres_Ops.res +229 -0
  98. package/lib/ocaml/QueryEnginePostgres-ReventlessPostgres.cmi +0 -0
  99. package/lib/ocaml/QueryEnginePostgres-ReventlessPostgres.cmt +0 -0
  100. package/lib/ocaml/QueryEnginePostgres.ast +0 -0
  101. package/lib/ocaml/QueryEnginePostgres.res +84 -0
  102. package/lib/ocaml/QueryEnginePostgresListPageTest-ReventlessPostgres.cmi +0 -0
  103. package/lib/ocaml/QueryEnginePostgresListPageTest-ReventlessPostgres.cmt +0 -0
  104. package/lib/ocaml/QueryEnginePostgresListPageTest.ast +0 -0
  105. package/lib/ocaml/QueryEnginePostgresListPageTest.res +76 -0
  106. package/lib/ocaml/ReventlessPostgres.cmi +0 -0
  107. package/lib/ocaml/ReventlessPostgres.cmt +0 -0
  108. package/lib/ocaml/ReventlessPostgres.mlmap +3 -0
  109. package/package.json +2 -2
  110. package/src/DcbEventLogStorage_Postgres.res +25 -340
  111. package/src/DcbEventLogStorage_Postgres.res.mjs +32 -348
  112. package/src/DcbEventLogStorage_Postgres_Ops.res +350 -0
  113. package/src/DcbEventLogStorage_Postgres_Ops.res.mjs +380 -0
  114. package/src/EventLogStorage_Postgres.res +12 -170
  115. package/src/EventLogStorage_Postgres.res.mjs +20 -186
  116. package/src/EventLogStorage_Postgres_Ops.res +180 -0
  117. package/src/EventLogStorage_Postgres_Ops.res.mjs +202 -0
  118. package/src/QueryDbStorage_Postgres.res +9 -232
  119. package/src/QueryDbStorage_Postgres.res.mjs +25 -204
  120. package/src/QueryDbStorage_Postgres_Ops.res +229 -0
  121. package/src/QueryDbStorage_Postgres_Ops.res.mjs +222 -0
  122. package/src/QueryEnginePostgres.res +84 -0
  123. package/src/QueryEnginePostgres.res.mjs +74 -0
  124. package/tests/QueryEnginePostgresListPageTest.res +76 -0
  125. package/tests/QueryEnginePostgresListPageTest.res.mjs +136 -0
Binary file
@@ -297,6 +297,90 @@ module Make = (P: {let pool: PgDriver.pool}) => {
297
297
  }
298
298
  }
299
299
 
300
+ // Sub-id connection push-down: the `{single}Items(id, filter, first/after/
301
+ // last/before)` query. Keyset over `sub_key` WITHIN one partition — a page
302
+ // reads only what it returns. Reproduces QueryDbResolvers_GraphQL's items
303
+ // resolver (SortKey_Filter + forward/backward keyset), entirely in SQL since
304
+ // sub_key IS the sort key. Cursor = base64 of the sub_key value; comparisons
305
+ // use COLLATE "C" (byte order) to match JS string `<`. Unlike the main list
306
+ // there is no declined-shape fallback — every items shape maps to SQL.
307
+ let itemsPage = async (
308
+ ~readModelName,
309
+ ~subIdField as _: string,
310
+ ~id: string,
311
+ ~argsDict: dict<JSON.t>,
312
+ ): JSON.t => {
313
+ let table = QueryDbStorage_Postgres.tableName(readModelName)
314
+ let filterDict =
315
+ argsDict->Dict.get("filter")->Option.flatMap(JSON.Decode.object)->Option.getOr(Dict.make())
316
+ let fstr = k => filterDict->Dict.get(k)->Option.flatMap(JSON.Decode.string)
317
+ let orderDesc = fstr("order")->Option.mapOr(false, o => o == "DESC")
318
+ let last = argsDict->Dict.get("last")->Option.flatMap(JSON.Decode.float)->Option.map(Float.toInt)
319
+ let first = argsDict->Dict.get("first")->Option.flatMap(JSON.Decode.float)->Option.map(Float.toInt)
320
+ let after = argsDict->Dict.get("after")->Option.flatMap(JSON.Decode.string)
321
+ let before = argsDict->Dict.get("before")->Option.flatMap(JSON.Decode.string)
322
+ let isBackward = last->Option.isSome
323
+
324
+ let sk = `sub_key COLLATE "C"`
325
+ let b = {params: []}
326
+ let where = [`partition_key = ${b->param(JSON.Encode.string(id))}`, notExpired]
327
+ fstr("prefix")->Option.forEach(p =>
328
+ where->Array.push(`starts_with(sub_key, ${b->param(JSON.Encode.string(p))})`)
329
+ )
330
+ fstr("from")->Option.forEach(f => where->Array.push(`${sk} >= ${b->param(JSON.Encode.string(f))}`))
331
+ fstr("to")->Option.forEach(t => where->Array.push(`${sk} <= ${b->param(JSON.Encode.string(t))}`))
332
+ fstr("eq")->Option.forEach(e => where->Array.push(`${sk} = ${b->param(JSON.Encode.string(e))}`))
333
+ // Keyset: forward excludes ≤ after; backward excludes ≥ before.
334
+ switch (isBackward, after, before) {
335
+ | (false, Some(c), _) =>
336
+ where->Array.push(`${sk} > ${b->param(JSON.Encode.string(ReventlessCore.QueryDbListQuery.decodeCursor(c)))}`)
337
+ | (true, _, Some(c)) =>
338
+ where->Array.push(`${sk} < ${b->param(JSON.Encode.string(ReventlessCore.QueryDbListQuery.decodeCursor(c)))}`)
339
+ | _ => ()
340
+ }
341
+ // Page-fetch direction is the LOGICAL order forward, and its OPPOSITE
342
+ // backward (take the N nearest `before`, then reverse to logical) — mirrors
343
+ // the resolver's `reverse = isBackward ? !orderDesc : orderDesc`.
344
+ let fetchDesc = isBackward ? !orderDesc : orderDesc
345
+ let pageSize = (isBackward ? last : first)->Option.getOr(ReventlessCore.QueryDbListQuery.defaultListPageSize)
346
+ let sql = `SELECT partition_key, sub_key, item FROM ${table} WHERE ${where->Array.join(
347
+ " AND ",
348
+ )} ORDER BY ${sk} ${fetchDesc ? "DESC" : "ASC"} LIMIT ${b->param(JSON.Encode.int(pageSize + 1))}`
349
+ let rows = await P.pool->PgDriver.query(sql, b.params)
350
+ let hasMore = rows->Array.length > pageSize
351
+ let taken = rows->Array.slice(~start=0, ~end=pageSize)
352
+ // Backward fetched in reverse-logical order → flip back to logical.
353
+ let logical = isBackward ? taken->Array.toReversed : taken
354
+ let pageItems = logical->Array.map(decodeRow(~withId=false, ...))
355
+ // Cursor = the row's sub_key (== the item's subIdField value).
356
+ let subKeyOf = (row: dict<JSON.t>) =>
357
+ switch row->Dict.get("sub_key") {
358
+ | Some(JSON.String(s)) => s
359
+ | _ => ""
360
+ }
361
+ let cursors = logical->Array.map(subKeyOf)
362
+ let edges =
363
+ pageItems->Array.mapWithIndex((item, i) =>
364
+ Obj.magic({
365
+ "node": item,
366
+ "cursor": ReventlessCore.QueryDbListQuery.encodeCursor(cursors->Array.getUnsafe(i)),
367
+ })
368
+ )
369
+ let startCursor =
370
+ cursors->Array.get(0)->Option.map(ReventlessCore.QueryDbListQuery.encodeCursor)
371
+ let endCursor =
372
+ cursors->Array.get(cursors->Array.length - 1)->Option.map(ReventlessCore.QueryDbListQuery.encodeCursor)
373
+ Obj.magic({
374
+ "edges": edges,
375
+ "pageInfo": {
376
+ "hasNextPage": !isBackward && hasMore,
377
+ "hasPreviousPage": isBackward && hasMore,
378
+ "startCursor": startCursor->Nullable.fromOption,
379
+ "endCursor": endCursor->Nullable.fromOption,
380
+ },
381
+ })
382
+ }
383
+
300
384
  let make: QueryDb_Adapter.queryEngineMaker = _allQueryDbs =>
301
385
  Pulumi.Output.make({QueryEngine.scan, query})
302
386
  }
@@ -233,4 +233,80 @@ switch processEnv->Dict.get("PG_URL") {
233
233
  expect(items->Array.length)->toBe(0)
234
234
  })
235
235
  })
236
+
237
+ // ── itemsPage (sub-id connection) ─────────────────────────────────────────
238
+ // A sub-id read model: partition "order-1" holds 5 lines keyed by `seq`.
239
+ let itemsName = "itemsparity"
240
+ let mkLine = seq => {
241
+ let o = Dict.make()
242
+ o->Dict.set("id", JSON.Encode.string("order-1"))
243
+ o->Dict.set("seq", JSON.Encode.string(seq))
244
+ JSON.Encode.object(o)
245
+ }
246
+ let seqs = ["a", "b", "c", "d", "e"]
247
+ beforeAllAsync(async () => {
248
+ let ops = QueryDbStorage_Postgres.makeOperations(
249
+ ~pool,
250
+ ~name=itemsName,
251
+ ~indexes=[],
252
+ ~subIdField=Some("seq"),
253
+ )
254
+ for i in 0 to seqs->Array.length - 1 {
255
+ let _ = await ops.save("order-1", mkLine(seqs->Array.getUnsafe(i)), QueryDb.Any, None)
256
+ }
257
+ })
258
+
259
+ // Extract node `seq` values in edge order.
260
+ let itemSeqs = (conn: JSON.t) =>
261
+ conn
262
+ ->field("edges")
263
+ ->Option.flatMap(JSON.Decode.array)
264
+ ->Option.getOr([])
265
+ ->Array.filterMap(e => e->field("node")->Option.flatMap(n => n->str("seq")))
266
+
267
+ let pageInfoBool = (conn, key) =>
268
+ conn->field("pageInfo")->Option.getOr(JSON.Encode.null)->field(key)->Option.flatMap(JSON.Decode.bool)->Option.getOr(false)
269
+
270
+ let itemsPage = args =>
271
+ Eng.itemsPage(~readModelName=itemsName, ~subIdField="seq", ~id="order-1", ~argsDict=argsOf(args))
272
+
273
+ describe("QueryEnginePostgres itemsPage (sub-id keyset)", () => {
274
+ testPromise("bare → all lines ASC, no next page", async () => {
275
+ let r = await itemsPage([])
276
+ expect(r->itemSeqs)->toEqual(["a", "b", "c", "d", "e"])
277
+ expect(pageInfoBool(r, "hasNextPage"))->toBe(false)
278
+ })
279
+ testPromise("first:2 → [a,b], hasNextPage", async () => {
280
+ let r = await itemsPage([("first", JSON.Encode.int(2))])
281
+ expect(r->itemSeqs)->toEqual(["a", "b"])
282
+ expect(pageInfoBool(r, "hasNextPage"))->toBe(true)
283
+ })
284
+ testPromise("first:2 after b → [c,d]", async () => {
285
+ let r = await itemsPage([("first", JSON.Encode.int(2)), ("after", JSON.Encode.string(cur("b")))])
286
+ expect(r->itemSeqs)->toEqual(["c", "d"])
287
+ })
288
+ testPromise("order DESC → [e..a]", async () => {
289
+ let r = await itemsPage([("filter", filterOf([("order", JSON.Encode.string("DESC"))]))])
290
+ expect(r->itemSeqs)->toEqual(["e", "d", "c", "b", "a"])
291
+ })
292
+ testPromise("filter eq c → [c]", async () => {
293
+ let r = await itemsPage([("filter", filterOf([("eq", JSON.Encode.string("c"))]))])
294
+ expect(r->itemSeqs)->toEqual(["c"])
295
+ })
296
+ testPromise("filter from b to d → [b,c,d]", async () => {
297
+ let r = await itemsPage([
298
+ ("filter", filterOf([("from", JSON.Encode.string("b")), ("to", JSON.Encode.string("d"))])),
299
+ ])
300
+ expect(r->itemSeqs)->toEqual(["b", "c", "d"])
301
+ })
302
+ testPromise("prefix c → [c]", async () => {
303
+ let r = await itemsPage([("filter", filterOf([("prefix", JSON.Encode.string("c"))]))])
304
+ expect(r->itemSeqs)->toEqual(["c"])
305
+ })
306
+ testPromise("backward last:2 before d → [b,c] (logical ASC)", async () => {
307
+ let r = await itemsPage([("last", JSON.Encode.int(2)), ("before", JSON.Encode.string(cur("d")))])
308
+ expect(r->itemSeqs)->toEqual(["b", "c"])
309
+ expect(pageInfoBool(r, "hasPreviousPage"))->toBe(true)
310
+ })
311
+ })
236
312
  }
Binary file
Binary file
@@ -1,11 +1,14 @@
1
1
  randjbuildsystem
2
2
  DcbEventLogStorage_Postgres
3
+ DcbEventLogStorage_Postgres_Ops
3
4
  EventLogChangeFeed
4
5
  EventLogStorage_Postgres
6
+ EventLogStorage_Postgres_Ops
5
7
  PgChangeFeed
6
8
  PgDriver
7
9
  PgSchema
8
10
  PostgresIntegrationTest
9
11
  QueryDbStorage_Postgres
12
+ QueryDbStorage_Postgres_Ops
10
13
  QueryEnginePostgres
11
14
  QueryEnginePostgresListPageTest
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reventlessdev/reventless-postgres",
3
- "version": "3.0.0-alpha.5",
3
+ "version": "3.0.0-alpha.7",
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": {
@@ -23,8 +23,8 @@
23
23
  "pg": "^8.13.1",
24
24
  "sury": "11.0.0-alpha.4",
25
25
  "@reventlessdev/rescript-jest": "1.0.0-alpha.6",
26
- "@reventlessdev/rescript-effect": "0.1.0-alpha.25",
27
26
  "@reventlessdev/rescript-pulumi-pulumi": "2.3.0-alpha.14",
27
+ "@reventlessdev/rescript-effect": "0.1.0-alpha.25",
28
28
  "@reventlessdev/reventless-core": "3.0.0-alpha.143",
29
29
  "@reventlessdev/reventless-infra": "3.0.0-alpha.89",
30
30
  "@reventlessdev/reventless-spec": "3.0.0-alpha.67"
@@ -1,352 +1,37 @@
1
- // Postgres-backed DCB event log storage — the exact-semantics backend.
1
+ // Deploy/local wrapper over the runtime-pure DcbEventLogStorage_Postgres_Ops.
2
2
  //
3
- // Unlike DynamoDB (which emulates the append condition with per-(tag,type) fence
4
- // sentinel rows), Postgres evaluates the actual DcbTag.query atomically inside
5
- // `dcb_append` (PgSchema.res): scoped advisory locks + exact check + insert, one
6
- // round trip, transaction-scoped. See docs/plans/postgres-storage-adapter.md §B.
7
- //
8
- // Reads apply the xmin read barrier so returned head positions are stable
9
- // forever (an event committed late — lower position, later commit can never be
10
- // skipped by a checkpointing reader). Positions are the opaque cursor
11
- // `<xid8>:<position>` (both zero-padded to 20 digits → string sort == numeric
12
- // sort); the append condition's `after` decodes to the same two-column
13
- // comparison, making read → decide → conditional-append exact end to end.
3
+ // The runtime ops, `lockStrategy`, `countAll`, and all query helpers live in
4
+ // DcbEventLogStorage_Postgres_Ops, which imports NO `@pulumi/pulumi` so the
5
+ // deployed Lambda import graph stays clean (the DCB command entry point pulls the
6
+ // ops in unconditionally at cold start — a deploy-time dep here would fail
7
+ // resolution on real Lambda). This module `include`s that pure module (so every
8
+ // existing `DcbEventLogStorage_Postgres.*` reference keeps working) and adds only
9
+ // the deploy-time `DcbEventLog_Adapter` shape, keeping the historic `makeStorage`
10
+ // 3-tuple `(name, ops, adapter)` API unchanged.
11
+ // See docs/plans/deployed-lambda-esm-self-containment.md (Rung-3 finding).
14
12
 
15
13
  open ReventlessCore
16
- open Reventless
17
-
18
- // Lock strategy for concurrent appends (B4). Advisory (default) is safe under
19
- // PgBouncer transaction mode; RowLocks avoids advisory-lock connection pinning
20
- // on RDS Proxy at the cost of a companion `dcb_scope` table.
21
- type lockStrategy = [#AdvisoryLocks | #RowLocks]
22
- let strategyParam = s =>
23
- switch s {
24
- | #AdvisoryLocks => "advisory"
25
- | #RowLocks => "rows"
26
- }
27
-
28
- // Injected by reventless-local to feed the projection-checkpoint low-watermark
29
- // (ProjectionPending, Dcb axis). Standalone/deploy use passes nothing.
30
- type onAppended = array<(string, int)> => unit
31
- let noTracking: onAppended = _ => ()
32
-
33
- // SELECT list shared by read and readStream. `data`/`meta` are jsonb (returned
34
- // already parsed); `tags` is text[] (a JS array); the cursor and recorded_at are
35
- // computed in SQL so the row maps straight to a rawSequencedEvent.
36
- let selectColumns = "
37
- lpad(transaction_id::text, 20, '0') || ':' || lpad(position::text, 20, '0') AS cursor,
38
- event_type,
39
- tags,
40
- data,
41
- meta,
42
- to_char(recorded_at AT TIME ZONE 'UTC', 'YYYY-MM-DD\"T\"HH24:MI:SS.MS\"Z\"') AS recorded_at
43
- "
44
-
45
- // Decode a 'key=value' tag string (split on the first '=') back to a DcbTag.tag.
46
- let decodeTag = (s: string): DcbTag.tag =>
47
- switch s->String.indexOf("=") {
48
- | -1 => {key: s, value: ""}
49
- | i => {
50
- key: s->String.slice(~start=0, ~end=i),
51
- value: s->String.slice(~start=i + 1, ~end=s->String.length),
52
- }
53
- }
54
-
55
- let decodeTags = (v: option<JSON.t>): array<DcbTag.tag> =>
56
- switch v {
57
- | Some(JSON.Array(arr)) =>
58
- arr->Array.filterMap(x =>
59
- switch x {
60
- | JSON.String(s) => Some(decodeTag(s))
61
- | _ => None
62
- }
63
- )
64
- | _ => []
65
- }
66
-
67
- let rowToEvent = (row: dict<JSON.t>): DcbEventLog_Adapter.rawSequencedEvent => {
68
- let str = key =>
69
- switch row->Dict.get(key) {
70
- | Some(JSON.String(s)) => s
71
- | _ => ""
72
- }
73
- let meta = switch row->Dict.get("meta") {
74
- | Some(metaJson) => metaJson->S.parseJsonOrThrow(Reventless.Message.metaSchema)
75
- | None => JsError.throwWithMessage("missing meta column in dcb_event row")
76
- }
77
- {
78
- DcbEventLog_Adapter.position: str("cursor"),
79
- eventType: str("event_type"),
80
- data: row->Dict.get("data")->Option.getOr(JSON.Encode.null),
81
- tags: decodeTags(row->Dict.get("tags")),
82
- meta,
83
- recordedAt: str("recorded_at"),
84
- }
85
- }
86
-
87
- // A tiny positional-parameter builder. Postgres uses `$1, $2, …` (not `?`), so we
88
- // accumulate params and mint tokens as we build the WHERE. A token may be reused
89
- // (same `$N`) to reference one param twice — used for the `after` cursor columns.
90
- type paramBuilder = {mutable params: array<JSON.t>}
91
- let mkBuilder = () => {params: []}
92
- let param = (b: paramBuilder, v: JSON.t): string => {
93
- b.params->Array.push(v)
94
- "$" ++ Int.toString(b.params->Array.length)
95
- }
96
-
97
- // Build the read WHERE + params for a query. `applyFence` gates the xmin read
98
- // barrier (true for consumer reads, so cursors are stable). `after` is the
99
- // decoded (tx, pos) cursor pair as numeric strings.
100
- let buildReadWhere = (
101
- b: paramBuilder,
102
- ~name: string,
103
- ~query: DcbTag.query,
104
- ~after: option<(string, string)>,
105
- ~applyFence: bool,
106
- ): string => {
107
- let parts = [`log_name = ${b->param(JSON.Encode.string(name))}`]
108
- if applyFence {
109
- parts->Array.push("transaction_id < pg_snapshot_xmin(pg_current_snapshot())")
110
- }
111
- switch after {
112
- | Some((tx, pos)) =>
113
- let txP = b->param(JSON.Encode.string(tx))
114
- let posP = b->param(JSON.Encode.string(pos))
115
- parts->Array.push(
116
- `(transaction_id > ${txP}::xid8 OR (transaction_id = ${txP}::xid8 AND position > ${posP}::bigint))`,
117
- )
118
- | None => ()
119
- }
120
-
121
- if query->Array.length > 0 {
122
- let clauseSqls = query->Array.map(clause => {
123
- let clauseParts = []
124
- switch clause.eventTypes {
125
- | Some(types) if types->Array.length > 0 =>
126
- let ph = types->Array.map(t => b->param(JSON.Encode.string(t)))->Array.join(",")
127
- clauseParts->Array.push(`event_type IN (${ph})`)
128
- | _ => ()
129
- }
130
- switch clause.tags {
131
- | Some(tags) if tags->Array.length > 0 =>
132
- let arr =
133
- tags
134
- ->Array.map(t => b->param(JSON.Encode.string(t.key ++ "=" ++ t.value)))
135
- ->Array.join(",")
136
- clauseParts->Array.push(`tags @> ARRAY[${arr}]::text[]`)
137
- | _ => ()
138
- }
139
- clauseParts->Array.length == 0 ? "TRUE" : "(" ++ clauseParts->Array.join(" AND ") ++ ")"
140
- })
141
- parts->Array.push("(" ++ clauseSqls->Array.join(" OR ") ++ ")")
142
- }
143
-
144
- parts->Array.join(" AND ")
145
- }
146
-
147
- // Strip the zero-padding a cursor carries for string-sortability, leaving a
148
- // plain decimal both `::xid8` and `::bigint` parse unambiguously ("0" if empty).
149
- let stripZeros = (s: string): string => {
150
- let t = s->String.replaceRegExp(%re("/^0+/"), "")
151
- t == "" ? "0" : t
152
- }
153
-
154
- // Split a '<xid8>:<position>' cursor into its two numeric-string columns.
155
- let decodeCursor = (pos: DcbTag.sequencePosition): option<(string, string)> =>
156
- switch pos->String.split(":") {
157
- | [tx, p] => Some((stripZeros(tx), stripZeros(p)))
158
- | _ => None
159
- }
160
-
161
- // Rows-per-page for readStream keyset pagination.
162
- let pageSize = 500
163
-
164
- let isConflict = (exn: exn): bool => {
165
- let msg = exn->JsExn.fromException->Option.flatMap(JsExn.message)->Option.getOr("")
166
- msg->String.includes("dcb_conflict") ||
167
- msg->String.includes("duplicate key") ||
168
- msg->String.includes("23505")
169
- }
170
-
171
- let countAll = async (pool: PgDriver.pool): int =>
172
- switch await pool->PgDriver.queryOne("SELECT COUNT(*)::bigint AS c FROM dcb_event", []) {
173
- | Some(row) =>
174
- switch row->Dict.get("c") {
175
- | Some(JSON.String(s)) => s->Int.fromString->Option.getOr(0)
176
- | Some(JSON.Number(n)) => Float.toInt(n)
177
- | _ => 0
178
- }
179
- | None => 0
180
- }
14
+ include DcbEventLogStorage_Postgres_Ops
181
15
 
182
16
  let makeStorage = (
183
17
  ~pool: PgDriver.pool,
184
18
  ~name: string,
185
- ~indexes as _,
186
- ~partitionTag as _,
187
- ~crossPartitionTagKeys as _=?,
188
- ~opts as _,
19
+ ~indexes,
20
+ ~partitionTag,
21
+ ~crossPartitionTagKeys=?,
22
+ ~opts,
189
23
  ~lockStrategy: lockStrategy=#AdvisoryLocks,
190
24
  ~onAppended=noTracking,
191
25
  ) => {
192
- // Fenced head cursor: the highest (transaction_id, position) visible under the
193
- // xmin barrier. Stable across reads, so it is a safe consistency marker.
194
- let currentHead = async (): option<string> =>
195
- switch await pool->PgDriver.queryOne(
196
- "SELECT lpad(transaction_id::text, 20, '0') || ':' || lpad(position::text, 20, '0') AS head
197
- FROM dcb_event
198
- WHERE log_name = $1 AND transaction_id < pg_snapshot_xmin(pg_current_snapshot())
199
- ORDER BY transaction_id DESC, position DESC
200
- LIMIT 1",
201
- [JSON.Encode.string(name)],
202
- ) {
203
- | Some(row) =>
204
- switch row->Dict.get("head") {
205
- | Some(JSON.String(s)) => Some(s)
206
- | _ => None
207
- }
208
- | None => None
209
- }
210
-
211
- let runQuery = async (~query, ~after): array<DcbEventLog_Adapter.rawSequencedEvent> => {
212
- let b = mkBuilder()
213
- let where = buildReadWhere(b, ~name, ~query, ~after, ~applyFence=true)
214
- let sql = `SELECT ${selectColumns} FROM dcb_event WHERE ${where} ORDER BY transaction_id ASC, position ASC`
215
- (await pool->PgDriver.query(sql, b.params))->Array.map(rowToEvent)
216
- }
217
-
218
- let read = async (~query: DcbTag.query, ~after=?): DcbEventLog_Adapter.rawReadResult => {
219
- let afterCols = after->Option.flatMap(decodeCursor)
220
- let events = await runQuery(~query, ~after=afterCols)
221
- switch await currentHead() {
222
- | Some(head) => {DcbEventLog_Adapter.events, headPosition: head}
223
- | None => ({DcbEventLog_Adapter.events: events}: DcbEventLog_Adapter.rawReadResult)
224
- }
225
- }
226
-
227
- let append = async (
228
- newEvents: array<DcbEventLog_Adapter.rawStoredEvent>,
229
- ~condition=?,
230
- ): result<DcbTag.sequencePosition, ReventlessInfra.DcbEventLog.appendError> => {
231
- let eventsJson = JSON.Array(
232
- newEvents->Array.map(ev =>
233
- JSON.Object(
234
- Dict.fromArray([
235
- ("event_type", JSON.Encode.string(ev.eventType)),
236
- (
237
- "tags",
238
- JSON.Array(ev.tags->Array.map(t => JSON.Encode.string(t.key ++ "=" ++ t.value))),
239
- ),
240
- ("data", ev.data),
241
- ("meta", ev.meta->S.reverseConvertToJsonOrThrow(Reventless.Message.metaSchema)),
242
- ]),
243
- )
244
- ),
245
- )
246
- let conditionParam = switch condition {
247
- | Some(cond: DcbTag.appendCondition) =>
248
- let queryJson = JSON.Array(
249
- cond.query->Array.map(clause =>
250
- JSON.Object(
251
- Dict.fromArray([
252
- (
253
- "eventTypes",
254
- switch clause.eventTypes {
255
- | Some(types) => JSON.Array(types->Array.map(JSON.Encode.string))
256
- | None => JSON.Encode.null
257
- },
258
- ),
259
- (
260
- "tags",
261
- switch clause.tags {
262
- | Some(tags) =>
263
- JSON.Array(tags->Array.map(t => JSON.Encode.string(t.key ++ "=" ++ t.value)))
264
- | None => JSON.Encode.null
265
- },
266
- ),
267
- ]),
268
- )
269
- ),
270
- )
271
- let fields = [("query", queryJson)]
272
- switch cond.after {
273
- | Some(a) => fields->Array.push(("after", JSON.Encode.string(a)))
274
- | None => ()
275
- }
276
- JSON.Encode.string(JSON.stringify(JSON.Object(Dict.fromArray(fields))))
277
- | None => JSON.Encode.null
278
- }
279
-
280
- try {
281
- let rows = await pool->PgDriver.query(
282
- "SELECT dcb_append($1, $2::jsonb, $3::jsonb, $4) AS pos",
283
- [
284
- JSON.Encode.string(name),
285
- JSON.Encode.string(JSON.stringify(eventsJson)),
286
- conditionParam,
287
- JSON.Encode.string(strategyParam(lockStrategy)),
288
- ],
289
- )
290
- switch rows->Array.get(0)->Option.flatMap(r => r->Dict.get("pos")) {
291
- | Some(JSON.String(cursor)) =>
292
- // Feed the projection checkpoint (Dcb axis): (msgId, numeric position).
293
- onAppended(
294
- newEvents->Array.filterMap(ev =>
295
- switch decodeCursor(cursor) {
296
- | Some((_, pos)) =>
297
- pos->Int.fromString->Option.map(p => (ev.meta.Reventless.Message.msgId, p))
298
- | None => None
299
- }
300
- ),
301
- )
302
- Ok(cursor)
303
- | _ => Error(ReventlessInfra.DcbEventLog.StorageFailure("dcb_append returned no position"))
304
- }
305
- } catch {
306
- | exn =>
307
- isConflict(exn)
308
- ? Error(ReventlessInfra.DcbEventLog.Conflict)
309
- : Error(
310
- ReventlessInfra.DcbEventLog.StorageFailure(
311
- exn->JsExn.fromException->Option.flatMap(JsExn.message)->Option.getOr("storage error"),
312
- ),
313
- )
314
- }
315
- }
316
-
317
- // ~strongConsistency is a genuine no-op here: every read applies the xmin
318
- // barrier, so it is always safely consistent (at the cost of a few ms lag).
319
- //
320
- // Keyset pagination on (transaction_id, position): one ordered SQL source, no
321
- // per-clause fan-out / k-way merge / dedup (the DynamoDB runtime's layers have
322
- // no Postgres counterpart). Each page reads strictly after the previous page's
323
- // last row; a short page ends the stream.
324
- let readStream = (~query: DcbTag.query, ~after=?, ~strongConsistency as _=?) =>
325
- Stream.paginateEffect(after->Option.flatMap(decodeCursor), cursor =>
326
- Effect.promise(() => {
327
- let b = mkBuilder()
328
- let where = buildReadWhere(b, ~name, ~query, ~after=cursor, ~applyFence=true)
329
- let sql = `SELECT ${selectColumns}, transaction_id::text AS tx_raw, position::text AS pos_raw FROM dcb_event WHERE ${where} ORDER BY transaction_id ASC, position ASC LIMIT ${Int.toString(pageSize)}`
330
- pool->PgDriver.query(sql, b.params)
331
- })->Effect.map(rows => {
332
- let events = rows->Array.map(rowToEvent)
333
- // Continue only on a full page; the next `after` is this page's last row.
334
- let next = if rows->Array.length < pageSize {
335
- None
336
- } else {
337
- switch rows->Array.get(rows->Array.length - 1) {
338
- | Some(lastRow) =>
339
- switch (lastRow->Dict.get("tx_raw"), lastRow->Dict.get("pos_raw")) {
340
- | (Some(JSON.String(tx)), Some(JSON.String(p))) => Some(Some((tx, p)))
341
- | _ => None
342
- }
343
- | None => None
344
- }
345
- }
346
- (events, next)
347
- })
348
- )
349
-
350
- let ops: DcbEventLog_Adapter.operations = {read, append, readStream}
26
+ let (name, ops) = makeOps(
27
+ ~pool,
28
+ ~name,
29
+ ~indexes,
30
+ ~partitionTag,
31
+ ~crossPartitionTagKeys?,
32
+ ~opts,
33
+ ~lockStrategy,
34
+ ~onAppended,
35
+ )
351
36
  (name, ops, {DcbEventLog_Adapter.resources: [], operations: Pulumi.Output.make(ops)})
352
37
  }