@reventlessdev/reventless-postgres 3.0.0-alpha.3 → 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.
Files changed (95) hide show
  1. package/CHANGELOG.md +16 -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 +2 -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.cmt +0 -0
  9. package/lib/bs/src/EventLogChangeFeed-ReventlessPostgres.cmi +0 -0
  10. package/lib/bs/src/EventLogChangeFeed-ReventlessPostgres.cmj +0 -0
  11. package/lib/bs/src/EventLogChangeFeed-ReventlessPostgres.cmt +0 -0
  12. package/lib/bs/src/EventLogChangeFeed.ast +0 -0
  13. package/lib/bs/src/EventLogChangeFeed.res +184 -0
  14. package/lib/bs/src/EventLogChangeFeed.res.mjs +134 -0
  15. package/lib/bs/src/EventLogStorage_Postgres-ReventlessPostgres.cmi +0 -0
  16. package/lib/bs/src/EventLogStorage_Postgres-ReventlessPostgres.cmt +0 -0
  17. package/lib/bs/src/PgChangeFeed-ReventlessPostgres.cmi +0 -0
  18. package/lib/bs/src/PgChangeFeed-ReventlessPostgres.cmt +0 -0
  19. package/lib/bs/src/PgSchema-ReventlessPostgres.cmi +0 -0
  20. package/lib/bs/src/PgSchema-ReventlessPostgres.cmt +0 -0
  21. package/lib/bs/src/PgSchema.ast +0 -0
  22. package/lib/bs/src/PgSchema.res +36 -2
  23. package/lib/bs/src/PgSchema.res.mjs +9 -2
  24. package/lib/bs/src/QueryDbStorage_Postgres-ReventlessPostgres.cmi +0 -0
  25. package/lib/bs/src/QueryDbStorage_Postgres-ReventlessPostgres.cmj +0 -0
  26. package/lib/bs/src/QueryDbStorage_Postgres-ReventlessPostgres.cmt +0 -0
  27. package/lib/bs/src/QueryDbStorage_Postgres.ast +0 -0
  28. package/lib/bs/src/QueryDbStorage_Postgres.res +35 -14
  29. package/lib/bs/src/QueryDbStorage_Postgres.res.mjs +23 -8
  30. package/lib/bs/src/QueryEnginePostgres-ReventlessPostgres.cmi +0 -0
  31. package/lib/bs/src/QueryEnginePostgres-ReventlessPostgres.cmt +0 -0
  32. package/lib/bs/src/QueryEnginePostgres.ast +0 -0
  33. package/lib/bs/src/QueryEnginePostgres.res +175 -0
  34. package/lib/bs/src/QueryEnginePostgres.res.mjs +91 -0
  35. package/lib/bs/tests/PostgresIntegrationTest-ReventlessPostgres.cmi +0 -0
  36. package/lib/bs/tests/PostgresIntegrationTest-ReventlessPostgres.cmt +0 -0
  37. package/lib/bs/tests/PostgresIntegrationTest.ast +0 -0
  38. package/lib/bs/tests/PostgresIntegrationTest.res +68 -0
  39. package/lib/bs/tests/PostgresIntegrationTest.res.mjs +49 -0
  40. package/lib/bs/tests/QueryEnginePostgresListPageTest-ReventlessPostgres.cmi +0 -0
  41. package/lib/bs/tests/QueryEnginePostgresListPageTest-ReventlessPostgres.cmj +0 -0
  42. package/lib/bs/tests/QueryEnginePostgresListPageTest-ReventlessPostgres.cmt +0 -0
  43. package/lib/bs/tests/QueryEnginePostgresListPageTest.ast +0 -0
  44. package/lib/bs/tests/QueryEnginePostgresListPageTest.res +236 -0
  45. package/lib/bs/tests/QueryEnginePostgresListPageTest.res.mjs +325 -0
  46. package/lib/ocaml/.compiler.log +2 -2
  47. package/lib/ocaml/DcbEventLogStorage_Postgres-ReventlessPostgres.cmi +0 -0
  48. package/lib/ocaml/DcbEventLogStorage_Postgres-ReventlessPostgres.cmt +0 -0
  49. package/lib/ocaml/EventLogChangeFeed-ReventlessPostgres.cmi +0 -0
  50. package/lib/ocaml/EventLogChangeFeed-ReventlessPostgres.cmj +0 -0
  51. package/lib/ocaml/EventLogChangeFeed-ReventlessPostgres.cmt +0 -0
  52. package/lib/ocaml/EventLogChangeFeed.ast +0 -0
  53. package/lib/ocaml/EventLogChangeFeed.res +184 -0
  54. package/lib/ocaml/EventLogStorage_Postgres-ReventlessPostgres.cmi +0 -0
  55. package/lib/ocaml/EventLogStorage_Postgres-ReventlessPostgres.cmt +0 -0
  56. package/lib/ocaml/PgChangeFeed-ReventlessPostgres.cmi +0 -0
  57. package/lib/ocaml/PgChangeFeed-ReventlessPostgres.cmt +0 -0
  58. package/lib/ocaml/PgSchema-ReventlessPostgres.cmi +0 -0
  59. package/lib/ocaml/PgSchema-ReventlessPostgres.cmt +0 -0
  60. package/lib/ocaml/PgSchema.ast +0 -0
  61. package/lib/ocaml/PgSchema.res +36 -2
  62. package/lib/ocaml/PostgresIntegrationTest-ReventlessPostgres.cmi +0 -0
  63. package/lib/ocaml/PostgresIntegrationTest-ReventlessPostgres.cmt +0 -0
  64. package/lib/ocaml/PostgresIntegrationTest.ast +0 -0
  65. package/lib/ocaml/PostgresIntegrationTest.res +68 -0
  66. package/lib/ocaml/QueryDbStorage_Postgres-ReventlessPostgres.cmi +0 -0
  67. package/lib/ocaml/QueryDbStorage_Postgres-ReventlessPostgres.cmj +0 -0
  68. package/lib/ocaml/QueryDbStorage_Postgres-ReventlessPostgres.cmt +0 -0
  69. package/lib/ocaml/QueryDbStorage_Postgres.ast +0 -0
  70. package/lib/ocaml/QueryDbStorage_Postgres.res +35 -14
  71. package/lib/ocaml/QueryEnginePostgres-ReventlessPostgres.cmi +0 -0
  72. package/lib/ocaml/QueryEnginePostgres-ReventlessPostgres.cmt +0 -0
  73. package/lib/ocaml/QueryEnginePostgres.ast +0 -0
  74. package/lib/ocaml/QueryEnginePostgres.res +175 -0
  75. package/lib/ocaml/QueryEnginePostgresListPageTest-ReventlessPostgres.cmi +0 -0
  76. package/lib/ocaml/QueryEnginePostgresListPageTest-ReventlessPostgres.cmj +0 -0
  77. package/lib/ocaml/QueryEnginePostgresListPageTest-ReventlessPostgres.cmt +0 -0
  78. package/lib/ocaml/QueryEnginePostgresListPageTest.ast +0 -0
  79. package/lib/ocaml/QueryEnginePostgresListPageTest.res +236 -0
  80. package/lib/ocaml/ReventlessPostgres.cmi +0 -0
  81. package/lib/ocaml/ReventlessPostgres.cmt +0 -0
  82. package/lib/ocaml/ReventlessPostgres.mlmap +2 -0
  83. package/package.json +7 -7
  84. package/src/EventLogChangeFeed.res +184 -0
  85. package/src/EventLogChangeFeed.res.mjs +134 -0
  86. package/src/PgSchema.res +36 -2
  87. package/src/PgSchema.res.mjs +9 -2
  88. package/src/QueryDbStorage_Postgres.res +35 -14
  89. package/src/QueryDbStorage_Postgres.res.mjs +23 -8
  90. package/src/QueryEnginePostgres.res +175 -0
  91. package/src/QueryEnginePostgres.res.mjs +91 -0
  92. package/tests/PostgresIntegrationTest.res +68 -0
  93. package/tests/PostgresIntegrationTest.res.mjs +49 -0
  94. package/tests/QueryEnginePostgresListPageTest.res +236 -0
  95. package/tests/QueryEnginePostgresListPageTest.res.mjs +325 -0
@@ -0,0 +1,184 @@
1
+ // Classic `event_log` change feed (B2.5) — the aggregate-path analogue of
2
+ // `PgChangeFeed` (which serves the DCB `dcb_event` log).
3
+ //
4
+ // Like the DCB feed this is NOT logical decoding: classic events are already
5
+ // durably queryable in-table, so the feed is an xmin-fenced read over `event_log`
6
+ // plus an `event_log_subscription` checkpoint. A statement-level AFTER INSERT
7
+ // trigger (`event_log_notify`, see PgSchema) issues `pg_notify('evlog_<log>')`,
8
+ // turning polling into near-real-time wakeup; a low-frequency fallback tick in the
9
+ // consumer covers missed notifications.
10
+ //
11
+ // Cursor semantics mirror the DCB feed exactly: the opaque `<xid8>:<global_seq>`
12
+ // string (zero-padded so string order == numeric order). Reads apply the xmin
13
+ // barrier (`transaction_id < pg_snapshot_xmin(pg_current_snapshot())`), so a
14
+ // returned cursor is stable forever — an event committed late (lower global_seq,
15
+ // later commit) is never skipped by a checkpointing reader. `global_seq` alone,
16
+ // being an IDENTITY assigned at INSERT, could NOT provide that guarantee.
17
+
18
+ // Reuse the shared `<a>:<b>` cursor codec from the DCB storage — identical format.
19
+ module Dcb = DcbEventLogStorage_Postgres
20
+
21
+ // One classic event as the feed surfaces it. `payload` is the stored event JSON
22
+ // verbatim; a bus/topic relay transforms it into its target shape (e.g. the AWS
23
+ // EventCollector `{id, meta, event}` body) — that transform is not this package's
24
+ // concern.
25
+ type classicEvent = {
26
+ cursor: string,
27
+ aggregateId: string,
28
+ seqNr: int,
29
+ payload: JSON.t,
30
+ msgId: option<string>,
31
+ }
32
+
33
+ // One page of the feed. `events` are in (transaction_id, global_seq) order;
34
+ // `cursor` is the checkpoint to persist and to pass as the next `after` (None when
35
+ // the page was empty — nothing new).
36
+ type batch = {
37
+ events: array<classicEvent>,
38
+ cursor: option<string>,
39
+ }
40
+
41
+ let selectColumns = "
42
+ lpad(transaction_id::text, 20, '0') || ':' || lpad(global_seq::text, 20, '0') AS cursor,
43
+ aggregate_id,
44
+ seq_nr,
45
+ payload,
46
+ msg_id
47
+ "
48
+
49
+ let coerceInt = (v: JSON.t): int =>
50
+ switch v {
51
+ | JSON.Number(n) => Float.toInt(n)
52
+ | JSON.String(s) => s->Int.fromString->Option.getOr(0)
53
+ | _ => 0
54
+ }
55
+
56
+ let rowToEvent = (row: dict<JSON.t>): classicEvent => {
57
+ let str = key =>
58
+ switch row->Dict.get(key) {
59
+ | Some(JSON.String(s)) => s
60
+ | _ => ""
61
+ }
62
+ {
63
+ cursor: str("cursor"),
64
+ aggregateId: str("aggregate_id"),
65
+ seqNr: row->Dict.get("seq_nr")->Option.map(coerceInt)->Option.getOr(0),
66
+ payload: row->Dict.get("payload")->Option.getOr(JSON.Encode.null),
67
+ msgId: switch row->Dict.get("msg_id") {
68
+ | Some(JSON.String(s)) => Some(s)
69
+ | _ => None
70
+ },
71
+ }
72
+ }
73
+
74
+ // Read up to `limit` events after `after` (the xmin-fenced feed query — whole log).
75
+ // Returns the events and the cursor of the last one.
76
+ let readBatch = async (
77
+ pool: PgDriver.pool,
78
+ ~logName: string,
79
+ ~after: option<string>=?,
80
+ ~limit: int=500,
81
+ ): batch => {
82
+ let params = [JSON.Encode.string(logName)]
83
+ let where = ref("log_name = $1 AND transaction_id < pg_snapshot_xmin(pg_current_snapshot())")
84
+ switch after->Option.flatMap(Dcb.decodeCursor) {
85
+ | Some((tx, pos)) =>
86
+ // $2 = tx (referenced twice), $3 = global_seq.
87
+ params->Array.push(JSON.Encode.string(tx))
88
+ params->Array.push(JSON.Encode.string(pos))
89
+ where :=
90
+ where.contents ++
91
+ " AND (transaction_id > $2::xid8 OR (transaction_id = $2::xid8 AND global_seq > $3::bigint))"
92
+ | None => ()
93
+ }
94
+ let sql = `SELECT ${selectColumns} FROM event_log WHERE ${where.contents} ORDER BY transaction_id ASC, global_seq ASC LIMIT ${Int.toString(limit)}`
95
+ let rows = await pool->PgDriver.query(sql, params)
96
+ let events = rows->Array.map(rowToEvent)
97
+ let cursor = switch events->Array.get(events->Array.length - 1) {
98
+ | Some(last) => Some(last.cursor)
99
+ | None => None
100
+ }
101
+ {events, cursor}
102
+ }
103
+
104
+ // --- Checkpoint contract (event_log_subscription) ---
105
+
106
+ // The persisted checkpoint for `subscriber`, encoded as a cursor, or None if the
107
+ // subscriber has never committed (→ replay from the beginning).
108
+ let loadCheckpoint = async (pool: PgDriver.pool, ~subscriber: string): option<string> =>
109
+ switch await pool->PgDriver.queryOne(
110
+ "SELECT lpad(last_tx::text, 20, '0') || ':' || lpad(last_global_seq::text, 20, '0') AS cursor
111
+ FROM event_log_subscription WHERE subscriber = $1 AND last_global_seq > 0",
112
+ [JSON.Encode.string(subscriber)],
113
+ ) {
114
+ | Some(row) =>
115
+ switch row->Dict.get("cursor") {
116
+ | Some(JSON.String(s)) => Some(s)
117
+ | _ => None
118
+ }
119
+ | None => None
120
+ }
121
+
122
+ // Persist the checkpoint. Idempotent upsert; decodes the cursor to its two columns
123
+ // so a later `loadCheckpoint` round-trips exactly.
124
+ let saveCheckpoint = async (pool: PgDriver.pool, ~subscriber: string, ~cursor: string): unit =>
125
+ switch Dcb.decodeCursor(cursor) {
126
+ | Some((tx, pos)) =>
127
+ let _ = await pool->PgDriver.query(
128
+ "INSERT INTO event_log_subscription(subscriber, last_tx, last_global_seq)
129
+ VALUES ($1, $2::xid8, $3::bigint)
130
+ ON CONFLICT (subscriber)
131
+ DO UPDATE SET last_tx = EXCLUDED.last_tx, last_global_seq = EXCLUDED.last_global_seq",
132
+ [JSON.Encode.string(subscriber), JSON.Encode.string(tx), JSON.Encode.string(pos)],
133
+ )
134
+ | None => ()
135
+ }
136
+
137
+ // --- Wakeup ---
138
+
139
+ // LISTEN on `evlog_<logName>`; `onWake` fires on every notification. Returns the
140
+ // dedicated client (caller owns it — release via unlisten). Pair with a
141
+ // low-frequency fallback tick so a missed NOTIFY only adds latency, never a stall.
142
+ let listen = (pool: PgDriver.pool, ~logName: string, ~onWake: unit => unit): promise<
143
+ PgDriver.client,
144
+ > => pool->PgDriver.listen(~channel="evlog_" ++ logName, ~onNotify=_ => onWake())
145
+
146
+ let unlisten = (client: PgDriver.client, ~logName: string): promise<unit> =>
147
+ client->PgDriver.unlisten(~channel="evlog_" ++ logName)
148
+
149
+ // --- Reference consumer driver ---
150
+
151
+ // Drain the feed for `subscriber` from its checkpoint: repeatedly readBatch →
152
+ // `handle` the events → saveCheckpoint, until a short page. `handle` must be
153
+ // idempotent (a crash between handle and checkpoint replays the last batch).
154
+ // Returns the number of events processed.
155
+ let drain = async (
156
+ pool: PgDriver.pool,
157
+ ~logName: string,
158
+ ~subscriber: string,
159
+ ~limit: int=500,
160
+ ~handle: array<classicEvent> => promise<unit>,
161
+ ): int => {
162
+ let processed = ref(0)
163
+ let cursor = ref(await loadCheckpoint(pool, ~subscriber))
164
+ let continue = ref(true)
165
+ while continue.contents {
166
+ let {events, cursor: newCursor} = await readBatch(pool, ~logName, ~after=?cursor.contents, ~limit)
167
+ if events->Array.length == 0 {
168
+ continue := false
169
+ } else {
170
+ await handle(events)
171
+ switch newCursor {
172
+ | Some(c) =>
173
+ await saveCheckpoint(pool, ~subscriber, ~cursor=c)
174
+ cursor := Some(c)
175
+ | None => ()
176
+ }
177
+ processed := processed.contents + events->Array.length
178
+ if events->Array.length < limit {
179
+ continue := false
180
+ }
181
+ }
182
+ }
183
+ processed.contents
184
+ }
Binary file
@@ -36,6 +36,19 @@ let tableStatements = [
36
36
  schema_hash text NOT NULL,
37
37
  PRIMARY KEY (log_name, aggregate_id)
38
38
  )",
39
+ // --- Classic change feed (B2.5) ---
40
+ // Stamp every classic event with its committing xid8 so the feed can apply the
41
+ // same xmin read barrier the DCB feed uses — global_seq (an IDENTITY assigned at
42
+ // INSERT, before commit) is unsafe as a feed cursor on its own (a late-committing
43
+ // lower global_seq would be skipped). Idempotent; the volatile default auto-stamps
44
+ // every future insert with no change to EventLogStorage_Postgres.append.
45
+ "ALTER TABLE event_log ADD COLUMN IF NOT EXISTS transaction_id xid8 NOT NULL DEFAULT pg_current_xact_id()",
46
+ "CREATE INDEX IF NOT EXISTS event_log_tx_gseq ON event_log (log_name, transaction_id, global_seq)",
47
+ "CREATE TABLE IF NOT EXISTS event_log_subscription (
48
+ subscriber text PRIMARY KEY,
49
+ last_tx xid8 NOT NULL DEFAULT '0'::xid8,
50
+ last_global_seq bigint NOT NULL DEFAULT 0
51
+ )",
39
52
  // --- DCB event log (Phase B) ---
40
53
  "CREATE TABLE IF NOT EXISTS dcb_event (
41
54
  log_name text NOT NULL,
@@ -236,7 +249,28 @@ END;
236
249
  $fn$;
237
250
  "
238
251
 
239
- let functionStatements = [dcbConditionWhereFn, dcbAppendFn]
252
+ // Classic change-feed wakeup (B2.5). A statement-level AFTER INSERT trigger fires
253
+ // one pg_notify per distinct log_name touched by the statement, so the classic feed
254
+ // gets the same near-real-time wakeup the DCB append's inline pg_notify provides —
255
+ // without touching EventLogStorage_Postgres.append. No-op if nobody is LISTENing; a
256
+ // low-frequency fallback tick in the consumer covers missed notifications.
257
+ let eventLogNotifyFn = "
258
+ CREATE OR REPLACE FUNCTION event_log_notify() RETURNS trigger
259
+ LANGUAGE plpgsql AS $fn$
260
+ BEGIN
261
+ PERFORM pg_notify('evlog_' || log_name, '')
262
+ FROM (SELECT DISTINCT log_name FROM new_rows) s;
263
+ RETURN NULL;
264
+ END;
265
+ $fn$;
266
+
267
+ CREATE OR REPLACE TRIGGER event_log_notify_trg
268
+ AFTER INSERT ON event_log
269
+ REFERENCING NEW TABLE AS new_rows
270
+ FOR EACH STATEMENT EXECUTE FUNCTION event_log_notify();
271
+ "
272
+
273
+ let functionStatements = [dcbConditionWhereFn, dcbAppendFn, eventLogNotifyFn]
240
274
 
241
275
  let ensureSchema = async (pool: PgDriver.pool): unit => {
242
276
  for i in 0 to tableStatements->Array.length - 1 {
@@ -251,6 +285,6 @@ let ensureSchema = async (pool: PgDriver.pool): unit => {
251
285
  // Mirrors the reventless-local contract that reset wipes data, not structure.
252
286
  let truncateAll = async (pool: PgDriver.pool): unit => {
253
287
  await pool->PgDriver.exec(
254
- "TRUNCATE event_log, snapshot, dcb_event, dcb_scope, dcb_subscription RESTART IDENTITY",
288
+ "TRUNCATE event_log, snapshot, dcb_event, dcb_scope, dcb_subscription, event_log_subscription RESTART IDENTITY",
255
289
  )
256
290
  }
@@ -233,4 +233,72 @@ switch processEnv->Dict.get("PG_URL") {
233
233
  expect(again)->toBe(0)
234
234
  })
235
235
  })
236
+
237
+ describe("classic change feed (event_log, B2.5)", () => {
238
+ testPromise("drains classic events in commit order, with fields, then checkpoints", async () => {
239
+ let (_n, ops, _s) = EventLogStorage_Postgres.makeStorage(~pool, ~name="cf-orders", ~opts)
240
+ let _ = await ops.append(0, "agg-1", [evJson("m0", 1), evJson("m1", 2)])
241
+ let _ = await ops.append(0, "agg-2", [evJson("m2", 3)])
242
+
243
+ let collected = ref([])
244
+ let processed = await EventLogChangeFeed.drain(
245
+ pool,
246
+ ~logName="cf-orders",
247
+ ~subscriber="cf-sub",
248
+ ~handle=async evs => collected := collected.contents->Array.concat(evs),
249
+ )
250
+ expect(processed)->toBe(3)
251
+ expect(collected.contents->Array.length)->toBe(3)
252
+
253
+ // Fields round-trip from the stored row.
254
+ let first = collected.contents->Array.getUnsafe(0)
255
+ expect(first.aggregateId)->toBe("agg-1")
256
+ expect(first.seqNr)->toBe(0)
257
+ expect(first.msgId)->toEqual(Some("m0"))
258
+
259
+ // Cursors are strictly increasing in drain order.
260
+ let cursors = collected.contents->Array.map(e => e.cursor)
261
+ let increasing = ref(true)
262
+ for i in 1 to cursors->Array.length - 1 {
263
+ if cursors->Array.getUnsafe(i) <= cursors->Array.getUnsafe(i - 1) {
264
+ increasing := false
265
+ }
266
+ }
267
+ expect(increasing.contents)->toBe(true)
268
+
269
+ // Second drain from the saved checkpoint sees nothing new.
270
+ let again = await EventLogChangeFeed.drain(
271
+ pool,
272
+ ~logName="cf-orders",
273
+ ~subscriber="cf-sub",
274
+ ~handle=async _ => (),
275
+ )
276
+ expect(again)->toBe(0)
277
+ })
278
+
279
+ testPromise("concurrent appends across aggregates: every event drained exactly once", async () => {
280
+ let (_n, ops, _s) = EventLogStorage_Postgres.makeStorage(~pool, ~name="cf-concurrent", ~opts)
281
+ let n = 20
282
+ // One event per aggregate, all dispatched before awaiting — genuinely
283
+ // concurrent on the pool, so global_seq is assigned in interleaved/undefined
284
+ // commit order. The xmin fence must still drain every event exactly once.
285
+ let promises =
286
+ Array.make(~length=n, 0)->Array.mapWithIndex((_, i) =>
287
+ ops.append(0, "agg-" ++ Int.toString(i), [evJson("m" ++ Int.toString(i), i)])
288
+ )
289
+ let _ = await Promise.all(promises)
290
+
291
+ let collected = ref([])
292
+ let _ = await EventLogChangeFeed.drain(
293
+ pool,
294
+ ~logName="cf-concurrent",
295
+ ~subscriber="cf-conc-sub",
296
+ ~handle=async evs => collected := collected.contents->Array.concat(evs),
297
+ )
298
+ // No skips: all N events present. No dupes: N distinct cursors.
299
+ expect(collected.contents->Array.length)->toBe(n)
300
+ let distinctCursors = collected.contents->Array.map(e => e.cursor)->Set.fromArray
301
+ expect(distinctCursors->Set.size)->toBe(n)
302
+ })
303
+ })
236
304
  }
@@ -97,23 +97,36 @@ let withId = (item: JSON.t, partitionKey: string): JSON.t =>
97
97
  | _ => item
98
98
  }
99
99
 
100
- let makeStorage = (
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.storage => {
108
+ ): QueryDb_Adapter.operations => {
106
109
  let table = tableName(name)
107
- // Schema setup is async but makeStorage is synchronous; gate every operation on
108
- // this promise so the first access waits for table + index creation exactly
109
- // once (subsequent awaits resolve immediately).
110
- let ready = {
111
- open Promise
112
- ensureTable(~pool, ~table)->then(() => ensureIndexes(~pool, ~table, ~indexes))
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(ops),
247
+ operations: Pulumi.Output.make(makeOperations(~pool, ~name, ~indexes, ~subIdField)),
227
248
  }
228
249
  }
229
250
 
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
  }