@reventlessdev/reventless-postgres 3.0.0-alpha.189 → 3.0.0-alpha.190

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 (82) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/lib/bs/.compiler.log +2 -2
  3. package/lib/bs/compiler-info.json +1 -1
  4. package/lib/bs/src/DcbEventLogStorage_Postgres-ReventlessPostgres.cmi +0 -0
  5. package/lib/bs/src/DcbEventLogStorage_Postgres-ReventlessPostgres.cmj +0 -0
  6. package/lib/bs/src/DcbEventLogStorage_Postgres-ReventlessPostgres.cmt +0 -0
  7. package/lib/bs/src/DcbEventLogStorage_Postgres.res.mjs +6 -0
  8. package/lib/bs/src/DcbEventLogStorage_Postgres_Ops-ReventlessPostgres.cmi +0 -0
  9. package/lib/bs/src/DcbEventLogStorage_Postgres_Ops-ReventlessPostgres.cmj +0 -0
  10. package/lib/bs/src/DcbEventLogStorage_Postgres_Ops-ReventlessPostgres.cmt +0 -0
  11. package/lib/bs/src/DcbEventLogStorage_Postgres_Ops.ast +0 -0
  12. package/lib/bs/src/DcbEventLogStorage_Postgres_Ops.res +69 -0
  13. package/lib/bs/src/DcbEventLogStorage_Postgres_Ops.res.mjs +53 -0
  14. package/lib/bs/src/EventLogChangeFeed-ReventlessPostgres.cmi +0 -0
  15. package/lib/bs/src/EventLogChangeFeed-ReventlessPostgres.cmt +0 -0
  16. package/lib/bs/src/EventLogStorage_Postgres-ReventlessPostgres.cmi +0 -0
  17. package/lib/bs/src/EventLogStorage_Postgres-ReventlessPostgres.cmj +0 -0
  18. package/lib/bs/src/EventLogStorage_Postgres-ReventlessPostgres.cmt +0 -0
  19. package/lib/bs/src/EventLogStorage_Postgres.res.mjs +3 -0
  20. package/lib/bs/src/EventLogStorage_Postgres_Ops-ReventlessPostgres.cmi +0 -0
  21. package/lib/bs/src/EventLogStorage_Postgres_Ops-ReventlessPostgres.cmj +0 -0
  22. package/lib/bs/src/EventLogStorage_Postgres_Ops-ReventlessPostgres.cmt +0 -0
  23. package/lib/bs/src/EventLogStorage_Postgres_Ops.ast +0 -0
  24. package/lib/bs/src/EventLogStorage_Postgres_Ops.res +50 -0
  25. package/lib/bs/src/EventLogStorage_Postgres_Ops.res.mjs +31 -0
  26. package/lib/bs/src/PgChangeFeed-ReventlessPostgres.cmi +0 -0
  27. package/lib/bs/src/PgChangeFeed-ReventlessPostgres.cmt +0 -0
  28. package/lib/bs/src/PgSchema-ReventlessPostgres.cmi +0 -0
  29. package/lib/bs/src/PgSchema-ReventlessPostgres.cmt +0 -0
  30. package/lib/bs/src/PgSchema.ast +0 -0
  31. package/lib/bs/src/PgSchema.res +5 -0
  32. package/lib/bs/src/PgSchema.res.mjs +3 -0
  33. package/lib/bs/src/QueryDbStorage_Postgres-ReventlessPostgres.cmi +0 -0
  34. package/lib/bs/src/QueryDbStorage_Postgres-ReventlessPostgres.cmt +0 -0
  35. package/lib/bs/src/QueryDbStorage_Postgres_Ops-ReventlessPostgres.cmi +0 -0
  36. package/lib/bs/src/QueryDbStorage_Postgres_Ops-ReventlessPostgres.cmt +0 -0
  37. package/lib/bs/src/QueryEnginePostgres-ReventlessPostgres.cmi +0 -0
  38. package/lib/bs/src/QueryEnginePostgres-ReventlessPostgres.cmt +0 -0
  39. package/lib/bs/tests/PostgresIntegrationTest-ReventlessPostgres.cmi +0 -0
  40. package/lib/bs/tests/PostgresIntegrationTest-ReventlessPostgres.cmt +0 -0
  41. package/lib/bs/tests/QueryEnginePostgresListPageTest-ReventlessPostgres.cmi +0 -0
  42. package/lib/bs/tests/QueryEnginePostgresListPageTest-ReventlessPostgres.cmt +0 -0
  43. package/lib/ocaml/.compiler.log +2 -2
  44. package/lib/ocaml/DcbEventLogStorage_Postgres-ReventlessPostgres.cmi +0 -0
  45. package/lib/ocaml/DcbEventLogStorage_Postgres-ReventlessPostgres.cmj +0 -0
  46. package/lib/ocaml/DcbEventLogStorage_Postgres-ReventlessPostgres.cmt +0 -0
  47. package/lib/ocaml/DcbEventLogStorage_Postgres_Ops-ReventlessPostgres.cmi +0 -0
  48. package/lib/ocaml/DcbEventLogStorage_Postgres_Ops-ReventlessPostgres.cmj +0 -0
  49. package/lib/ocaml/DcbEventLogStorage_Postgres_Ops-ReventlessPostgres.cmt +0 -0
  50. package/lib/ocaml/DcbEventLogStorage_Postgres_Ops.ast +0 -0
  51. package/lib/ocaml/DcbEventLogStorage_Postgres_Ops.res +69 -0
  52. package/lib/ocaml/EventLogChangeFeed-ReventlessPostgres.cmi +0 -0
  53. package/lib/ocaml/EventLogChangeFeed-ReventlessPostgres.cmt +0 -0
  54. package/lib/ocaml/EventLogStorage_Postgres-ReventlessPostgres.cmi +0 -0
  55. package/lib/ocaml/EventLogStorage_Postgres-ReventlessPostgres.cmj +0 -0
  56. package/lib/ocaml/EventLogStorage_Postgres-ReventlessPostgres.cmt +0 -0
  57. package/lib/ocaml/EventLogStorage_Postgres_Ops-ReventlessPostgres.cmi +0 -0
  58. package/lib/ocaml/EventLogStorage_Postgres_Ops-ReventlessPostgres.cmj +0 -0
  59. package/lib/ocaml/EventLogStorage_Postgres_Ops-ReventlessPostgres.cmt +0 -0
  60. package/lib/ocaml/EventLogStorage_Postgres_Ops.ast +0 -0
  61. package/lib/ocaml/EventLogStorage_Postgres_Ops.res +50 -0
  62. package/lib/ocaml/PgChangeFeed-ReventlessPostgres.cmi +0 -0
  63. package/lib/ocaml/PgChangeFeed-ReventlessPostgres.cmt +0 -0
  64. package/lib/ocaml/PgSchema-ReventlessPostgres.cmi +0 -0
  65. package/lib/ocaml/PgSchema-ReventlessPostgres.cmt +0 -0
  66. package/lib/ocaml/PgSchema.ast +0 -0
  67. package/lib/ocaml/PgSchema.res +5 -0
  68. package/lib/ocaml/QueryDbStorage_Postgres-ReventlessPostgres.cmi +0 -0
  69. package/lib/ocaml/QueryDbStorage_Postgres-ReventlessPostgres.cmt +0 -0
  70. package/lib/ocaml/QueryDbStorage_Postgres_Ops-ReventlessPostgres.cmi +0 -0
  71. package/lib/ocaml/QueryDbStorage_Postgres_Ops-ReventlessPostgres.cmt +0 -0
  72. package/lib/ocaml/QueryEnginePostgres-ReventlessPostgres.cmi +0 -0
  73. package/lib/ocaml/QueryEnginePostgres-ReventlessPostgres.cmt +0 -0
  74. package/package.json +5 -5
  75. package/src/DcbEventLogStorage_Postgres.res.mjs +6 -0
  76. package/src/DcbEventLogStorage_Postgres_Ops.res +69 -0
  77. package/src/DcbEventLogStorage_Postgres_Ops.res.mjs +53 -0
  78. package/src/EventLogStorage_Postgres.res.mjs +3 -0
  79. package/src/EventLogStorage_Postgres_Ops.res +50 -0
  80. package/src/EventLogStorage_Postgres_Ops.res.mjs +31 -0
  81. package/src/PgSchema.res +5 -0
  82. package/src/PgSchema.res.mjs +3 -0
package/CHANGELOG.md CHANGED
@@ -3,6 +3,13 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ # 3.0.0-alpha.190 (2026-11-01)
7
+
8
+ ### Features
9
+
10
+ * **local:** the local platform serves its logs and events to whoever watches it ([0c29b0d](https://github.com/ReventlessDev/reventless-core/commit/0c29b0d08c668ad5b948a8017178dad8692647d0))
11
+
12
+
6
13
  # 3.0.0-alpha.189 (2026-10-30)
7
14
 
8
15
  **Note:** Version bump only for package @reventlessdev/reventless-postgres
@@ -1,2 +1,2 @@
1
- #Start(1793371831630)
2
- #Done(1793371833037)
1
+ #Start(1793557493171)
2
+ #Done(1793557495081)
@@ -4,5 +4,5 @@
4
4
  "bsc_hash": "1006241435e8d5052329c523202a7fe6359a37e1e1688c023d37d6a7ebb5fb6d",
5
5
  "rescript_config_hash": "af2af60a0fa33b8249a96ff2758f36482843126dc4e07b9302842d011e0ad8c6",
6
6
  "runtime_path": "/home/runner/work/reventless-core/reventless-core/node_modules/@rescript/runtime",
7
- "generated_at": "1793371833046"
7
+ "generated_at": "1793557495086"
8
8
  }
@@ -46,6 +46,10 @@ let isConflict = DcbEventLogStorage_Postgres_Ops$ReventlessPostgres.isConflict;
46
46
 
47
47
  let countAll = DcbEventLogStorage_Postgres_Ops$ReventlessPostgres.countAll;
48
48
 
49
+ let readBackwards = DcbEventLogStorage_Postgres_Ops$ReventlessPostgres.readBackwards;
50
+
51
+ let positionsOfAppend = DcbEventLogStorage_Postgres_Ops$ReventlessPostgres.positionsOfAppend;
52
+
49
53
  let makeOps = DcbEventLogStorage_Postgres_Ops$ReventlessPostgres.makeOps;
50
54
 
51
55
  export {
@@ -63,6 +67,8 @@ export {
63
67
  pageSize,
64
68
  isConflict,
65
69
  countAll,
70
+ readBackwards,
71
+ positionsOfAppend,
66
72
  makeOps,
67
73
  makeStorage,
68
74
  }
@@ -175,6 +175,75 @@ let countAll = async (pool: PgDriver.pool): int =>
175
175
  | None => 0
176
176
  }
177
177
 
178
+ /** Where a backwards read of one log starts: strictly below a position, or at
179
+ and below it. */
180
+ type bound =
181
+ | Below(float)
182
+ | AtMost(float)
183
+
184
+ /** One log's events newest first by `position`, narrowed to one correlation id
185
+ when given. Rows are `(position, event)`; the event's own `position` field is
186
+ the read cursor, as `read` returns it. */
187
+ let readBackwards = async (
188
+ pool: PgDriver.pool,
189
+ ~name: string,
190
+ ~bound: option<bound>,
191
+ ~limit: int,
192
+ ~correlationId: option<string>,
193
+ ): array<(float, DcbEventLog_Adapter.rawSequencedEvent)> => {
194
+ let b = mkBuilder()
195
+ let parts = [`log_name = ${b->param(JSON.Encode.string(name))}`]
196
+ switch bound {
197
+ | Some(Below(p)) => parts->Array.push(`position < ${b->param(JSON.Encode.float(p))}::bigint`)
198
+ | Some(AtMost(p)) => parts->Array.push(`position <= ${b->param(JSON.Encode.float(p))}::bigint`)
199
+ | None => ()
200
+ }
201
+ switch correlationId {
202
+ | Some(id) => parts->Array.push(`(meta->>'correlationId') = ${b->param(JSON.Encode.string(id))}`)
203
+ | None => ()
204
+ }
205
+ let limitP = b->param(JSON.Encode.int(limit))
206
+
207
+ (
208
+ await pool->PgDriver.query(
209
+ `SELECT ${selectColumns}, position::text AS pos_raw FROM dcb_event WHERE ${parts->Array.join(
210
+ " AND ",
211
+ )} ORDER BY position DESC LIMIT ${limitP}::int`,
212
+ b.params,
213
+ )
214
+ )->Array.map(row => (
215
+ switch row->Dict.get("pos_raw") {
216
+ | Some(JSON.String(s)) => s->Float.fromString->Option.getOr(0.)
217
+ | _ => 0.
218
+ },
219
+ rowToEvent(row),
220
+ ))
221
+ }
222
+
223
+ /** The position of each event one append wrote, by message id. The cursor an
224
+ append returns names its transaction, and every event of the append shares
225
+ it. */
226
+ let positionsOfAppend = async (pool: PgDriver.pool, ~name: string, ~cursor: string): array<(
227
+ string,
228
+ float,
229
+ )> =>
230
+ switch decodeCursor(cursor) {
231
+ | None => []
232
+ | Some((tx, _)) =>
233
+ (
234
+ await pool->PgDriver.query(
235
+ "SELECT position::text AS pos, meta->>'msgId' AS msg_id FROM dcb_event WHERE log_name = $1 AND transaction_id = $2::xid8",
236
+ [JSON.Encode.string(name), JSON.Encode.string(tx)],
237
+ )
238
+ )->Array.filterMap(row =>
239
+ switch (row->Dict.get("msg_id"), row->Dict.get("pos")) {
240
+ | (Some(JSON.String(msgId)), Some(JSON.String(pos))) =>
241
+ pos->Float.fromString->Option.map(p => (msgId, p))
242
+ | _ => None
243
+ }
244
+ )
245
+ }
246
+
178
247
  // Builds the DCB runtime op set bound to `pool`. Returns `(name, ops)`; the
179
248
  // deploy-time `DcbEventLog_Adapter` wrapper lives in `DcbEventLogStorage_Postgres`.
180
249
  let makeOps = (
@@ -3,6 +3,7 @@
3
3
  import * as Stream from "@reventlessdev/rescript-effect/src/Stream.res.mjs";
4
4
  import * as Stdlib_Int from "@rescript/runtime/lib/es6/Stdlib_Int.js";
5
5
  import * as Stdlib_Array from "@rescript/runtime/lib/es6/Stdlib_Array.js";
6
+ import * as Stdlib_Float from "@rescript/runtime/lib/es6/Stdlib_Float.js";
6
7
  import * as Stdlib_JsExn from "@rescript/runtime/lib/es6/Stdlib_JsExn.js";
7
8
  import * as Stdlib_Option from "@rescript/runtime/lib/es6/Stdlib_Option.js";
8
9
  import * as Effect from "effect/Effect";
@@ -174,6 +175,56 @@ async function countAll(pool) {
174
175
  }
175
176
  }
176
177
 
178
+ async function readBackwards(pool, name, bound, limit, correlationId) {
179
+ let b = {
180
+ params: []
181
+ };
182
+ let parts = [`log_name = ` + param(b, name)];
183
+ if (bound !== undefined) {
184
+ if (bound.TAG === "Below") {
185
+ parts.push(`position < ` + param(b, bound._0) + `::bigint`);
186
+ } else {
187
+ parts.push(`position <= ` + param(b, bound._0) + `::bigint`);
188
+ }
189
+ }
190
+ if (correlationId !== undefined) {
191
+ parts.push(`(meta->>'correlationId') = ` + param(b, correlationId));
192
+ }
193
+ let limitP = param(b, limit);
194
+ return (await PgDriver$ReventlessPostgres.query(pool, `SELECT ` + selectColumns + `, position::text AS pos_raw FROM dcb_event WHERE ` + parts.join(" AND ") + ` ORDER BY position DESC LIMIT ` + limitP + `::int`, b.params)).map(row => {
195
+ let match = row["pos_raw"];
196
+ let tmp;
197
+ tmp = match !== undefined ? (
198
+ typeof match === "string" ? Stdlib_Option.getOr(Stdlib_Float.fromString(match), 0) : 0
199
+ ) : 0;
200
+ return [
201
+ tmp,
202
+ rowToEvent(row)
203
+ ];
204
+ });
205
+ }
206
+
207
+ async function positionsOfAppend(pool, name, cursor) {
208
+ let match = decodeCursor(cursor);
209
+ if (match !== undefined) {
210
+ return Stdlib_Array.filterMap(await PgDriver$ReventlessPostgres.query(pool, "SELECT position::text AS pos, meta->>'msgId' AS msg_id FROM dcb_event WHERE log_name = $1 AND transaction_id = $2::xid8", [
211
+ name,
212
+ match[0]
213
+ ]), row => {
214
+ let match = row["msg_id"];
215
+ let match$1 = row["pos"];
216
+ if (typeof match === "string" && typeof match$1 === "string") {
217
+ return Stdlib_Option.map(Stdlib_Float.fromString(match$1), p => [
218
+ match,
219
+ p
220
+ ]);
221
+ }
222
+ });
223
+ } else {
224
+ return [];
225
+ }
226
+ }
227
+
177
228
  function makeOps(pool, name, param, param$1, param$2, param$3, lockStrategyOpt, onAppendedOpt) {
178
229
  let lockStrategy = lockStrategyOpt !== undefined ? lockStrategyOpt : "AdvisoryLocks";
179
230
  let onAppended = onAppendedOpt !== undefined ? onAppendedOpt : noTracking;
@@ -375,6 +426,8 @@ export {
375
426
  pageSize,
376
427
  isConflict,
377
428
  countAll,
429
+ readBackwards,
430
+ positionsOfAppend,
378
431
  makeOps,
379
432
  }
380
433
  /* Stream Not a pure module */
@@ -27,6 +27,8 @@ let countAll = EventLogStorage_Postgres_Ops$ReventlessPostgres.countAll;
27
27
 
28
28
  let isUniqueViolation = EventLogStorage_Postgres_Ops$ReventlessPostgres.isUniqueViolation;
29
29
 
30
+ let readBackwards = EventLogStorage_Postgres_Ops$ReventlessPostgres.readBackwards;
31
+
30
32
  let makeOps = EventLogStorage_Postgres_Ops$ReventlessPostgres.makeOps;
31
33
 
32
34
  export {
@@ -35,6 +37,7 @@ export {
35
37
  coerceInt,
36
38
  countAll,
37
39
  isUniqueViolation,
40
+ readBackwards,
38
41
  makeOps,
39
42
  makeStorage,
40
43
  }
@@ -42,6 +42,56 @@ let isUniqueViolation = (exn: exn): bool => {
42
42
  msg->String.includes("23505")
43
43
  }
44
44
 
45
+ /** Where a backwards read of one log starts: strictly below a position, or at
46
+ and below it. */
47
+ type bound =
48
+ | Below(float)
49
+ | AtMost(float)
50
+
51
+ /** One log's stored events newest first, across its entities, by `global_seq`,
52
+ narrowed to one correlation id when given. Rows are `(global_seq, aggregate
53
+ id, stored event)`. */
54
+ let readBackwards = async (
55
+ pool: PgDriver.pool,
56
+ ~name: string,
57
+ ~bound: option<bound>,
58
+ ~limit: int,
59
+ ~correlationId: option<string>,
60
+ ): array<(float, string, JSON.t)> => {
61
+ let params = [JSON.Encode.string(name)]
62
+ let param = v => {
63
+ params->Array.push(v)
64
+ "$" ++ params->Array.length->Int.toString
65
+ }
66
+ let parts = ["log_name = $1"]
67
+ switch bound {
68
+ | Some(Below(p)) => parts->Array.push(`global_seq < ${param(JSON.Encode.float(p))}::bigint`)
69
+ | Some(AtMost(p)) => parts->Array.push(`global_seq <= ${param(JSON.Encode.float(p))}::bigint`)
70
+ | None => ()
71
+ }
72
+ switch correlationId {
73
+ | Some(id) => parts->Array.push(`(payload->>'correlationId') = ${param(JSON.Encode.string(id))}`)
74
+ | None => ()
75
+ }
76
+ let limitP = param(JSON.Encode.int(limit))
77
+
78
+ (
79
+ await pool->PgDriver.query(
80
+ `SELECT global_seq::text AS pos, aggregate_id, payload FROM event_log WHERE ${parts->Array.join(
81
+ " AND ",
82
+ )} ORDER BY global_seq DESC LIMIT ${limitP}::int`,
83
+ params,
84
+ )
85
+ )->Array.map(row => (
86
+ row->Dict.get("pos")->Option.mapOr(0, coerceInt)->Int.toFloat,
87
+ switch row->Dict.get("aggregate_id") {
88
+ | Some(JSON.String(id)) => id
89
+ | _ => ""
90
+ },
91
+ decodePayload(row),
92
+ ))
93
+ }
94
+
45
95
  // Builds the classic runtime op set bound to `pool`. Returns `(name, ops)`; the
46
96
  // deploy-time `EventLog_Adapter` wrapper lives in `EventLogStorage_Postgres`.
47
97
  let makeOps = (~pool: PgDriver.pool, ~name: string, ~opts as _, ~onAppended=noTracking): (
@@ -47,6 +47,36 @@ function isUniqueViolation(exn) {
47
47
  }
48
48
  }
49
49
 
50
+ async function readBackwards(pool, name, bound, limit, correlationId) {
51
+ let params = [name];
52
+ let param = v => {
53
+ params.push(v);
54
+ return "$" + params.length.toString();
55
+ };
56
+ let parts = ["log_name = $1"];
57
+ if (bound !== undefined) {
58
+ if (bound.TAG === "Below") {
59
+ parts.push(`global_seq < ` + param(bound._0) + `::bigint`);
60
+ } else {
61
+ parts.push(`global_seq <= ` + param(bound._0) + `::bigint`);
62
+ }
63
+ }
64
+ if (correlationId !== undefined) {
65
+ parts.push(`(payload->>'correlationId') = ` + param(correlationId));
66
+ }
67
+ let limitP = param(limit);
68
+ return (await PgDriver$ReventlessPostgres.query(pool, `SELECT global_seq::text AS pos, aggregate_id, payload FROM event_log WHERE ` + parts.join(" AND ") + ` ORDER BY global_seq DESC LIMIT ` + limitP + `::int`, params)).map(row => {
69
+ let match = row["aggregate_id"];
70
+ let tmp;
71
+ tmp = typeof match === "string" ? match : "";
72
+ return [
73
+ Stdlib_Option.mapOr(row["pos"], 0, coerceInt),
74
+ tmp,
75
+ decodePayload(row)
76
+ ];
77
+ });
78
+ }
79
+
50
80
  function makeOps(pool, name, param, onAppendedOpt) {
51
81
  let onAppended = onAppendedOpt !== undefined ? onAppendedOpt : noTracking;
52
82
  let appendTracked = async (track, seqNr, id, jsons) => {
@@ -197,6 +227,7 @@ export {
197
227
  coerceInt,
198
228
  countAll,
199
229
  isUniqueViolation,
230
+ readBackwards,
200
231
  makeOps,
201
232
  }
202
233
  /* Stream Not a pure module */
Binary file
@@ -44,6 +44,10 @@ let tableStatements = [
44
44
  // every future insert with no change to EventLogStorage_Postgres.append.
45
45
  "ALTER TABLE event_log ADD COLUMN IF NOT EXISTS transaction_id xid8 NOT NULL DEFAULT pg_current_xact_id()",
46
46
  "CREATE INDEX IF NOT EXISTS event_log_tx_gseq ON event_log (log_name, transaction_id, global_seq)",
47
+ // Reading one log newest first across its entities, and finding every event of
48
+ // one flow. The stored event is flat, so the correlation id is a top-level key.
49
+ "CREATE INDEX IF NOT EXISTS event_log_log_gseq ON event_log (log_name, global_seq)",
50
+ "CREATE INDEX IF NOT EXISTS event_log_correlation ON event_log (log_name, (payload->>'correlationId'), global_seq)",
47
51
  "CREATE TABLE IF NOT EXISTS event_log_subscription (
48
52
  subscriber text PRIMARY KEY,
49
53
  last_tx xid8 NOT NULL DEFAULT '0'::xid8,
@@ -64,6 +68,7 @@ let tableStatements = [
64
68
  "CREATE INDEX IF NOT EXISTS dcb_event_tags_gin ON dcb_event USING gin (tags)",
65
69
  "CREATE INDEX IF NOT EXISTS dcb_event_type_pos ON dcb_event (log_name, event_type, position)",
66
70
  "CREATE INDEX IF NOT EXISTS dcb_event_tx_pos ON dcb_event (log_name, transaction_id, position)",
71
+ "CREATE INDEX IF NOT EXISTS dcb_event_correlation ON dcb_event (log_name, (meta->>'correlationId'), position)",
67
72
  // --- Row-lock companion (B4) ---
68
73
  "CREATE TABLE IF NOT EXISTS dcb_scope (
69
74
  log_name text NOT NULL,
@@ -7,11 +7,14 @@ let tableStatements = [
7
7
  "CREATE TABLE IF NOT EXISTS snapshot (\n log_name text NOT NULL,\n aggregate_id text NOT NULL,\n seq_nr bigint NOT NULL,\n state jsonb NOT NULL,\n schema_hash text NOT NULL,\n PRIMARY KEY (log_name, aggregate_id)\n )",
8
8
  "ALTER TABLE event_log ADD COLUMN IF NOT EXISTS transaction_id xid8 NOT NULL DEFAULT pg_current_xact_id()",
9
9
  "CREATE INDEX IF NOT EXISTS event_log_tx_gseq ON event_log (log_name, transaction_id, global_seq)",
10
+ "CREATE INDEX IF NOT EXISTS event_log_log_gseq ON event_log (log_name, global_seq)",
11
+ "CREATE INDEX IF NOT EXISTS event_log_correlation ON event_log (log_name, (payload->>'correlationId'), global_seq)",
10
12
  "CREATE TABLE IF NOT EXISTS event_log_subscription (\n subscriber text PRIMARY KEY,\n last_tx xid8 NOT NULL DEFAULT '0'::xid8,\n last_global_seq bigint NOT NULL DEFAULT 0\n )",
11
13
  "CREATE TABLE IF NOT EXISTS dcb_event (\n log_name text NOT NULL,\n position bigint GENERATED ALWAYS AS IDENTITY,\n transaction_id xid8 NOT NULL DEFAULT pg_current_xact_id(),\n event_type text NOT NULL,\n tags text[] NOT NULL DEFAULT '{}',\n data jsonb NOT NULL,\n meta jsonb NOT NULL,\n recorded_at timestamptz NOT NULL DEFAULT now(),\n PRIMARY KEY (log_name, position)\n )",
12
14
  "CREATE INDEX IF NOT EXISTS dcb_event_tags_gin ON dcb_event USING gin (tags)",
13
15
  "CREATE INDEX IF NOT EXISTS dcb_event_type_pos ON dcb_event (log_name, event_type, position)",
14
16
  "CREATE INDEX IF NOT EXISTS dcb_event_tx_pos ON dcb_event (log_name, transaction_id, position)",
17
+ "CREATE INDEX IF NOT EXISTS dcb_event_correlation ON dcb_event (log_name, (meta->>'correlationId'), position)",
15
18
  "CREATE TABLE IF NOT EXISTS dcb_scope (\n log_name text NOT NULL,\n scope_hash bigint NOT NULL,\n PRIMARY KEY (log_name, scope_hash)\n )",
16
19
  "CREATE TABLE IF NOT EXISTS dcb_subscription (\n subscriber text PRIMARY KEY,\n last_tx xid8 NOT NULL DEFAULT '0'::xid8,\n last_position bigint NOT NULL DEFAULT 0\n )"
17
20
  ];
@@ -1,2 +1,2 @@
1
- #Start(1793371831630)
2
- #Done(1793371833037)
1
+ #Start(1793557493171)
2
+ #Done(1793557495081)
@@ -175,6 +175,75 @@ let countAll = async (pool: PgDriver.pool): int =>
175
175
  | None => 0
176
176
  }
177
177
 
178
+ /** Where a backwards read of one log starts: strictly below a position, or at
179
+ and below it. */
180
+ type bound =
181
+ | Below(float)
182
+ | AtMost(float)
183
+
184
+ /** One log's events newest first by `position`, narrowed to one correlation id
185
+ when given. Rows are `(position, event)`; the event's own `position` field is
186
+ the read cursor, as `read` returns it. */
187
+ let readBackwards = async (
188
+ pool: PgDriver.pool,
189
+ ~name: string,
190
+ ~bound: option<bound>,
191
+ ~limit: int,
192
+ ~correlationId: option<string>,
193
+ ): array<(float, DcbEventLog_Adapter.rawSequencedEvent)> => {
194
+ let b = mkBuilder()
195
+ let parts = [`log_name = ${b->param(JSON.Encode.string(name))}`]
196
+ switch bound {
197
+ | Some(Below(p)) => parts->Array.push(`position < ${b->param(JSON.Encode.float(p))}::bigint`)
198
+ | Some(AtMost(p)) => parts->Array.push(`position <= ${b->param(JSON.Encode.float(p))}::bigint`)
199
+ | None => ()
200
+ }
201
+ switch correlationId {
202
+ | Some(id) => parts->Array.push(`(meta->>'correlationId') = ${b->param(JSON.Encode.string(id))}`)
203
+ | None => ()
204
+ }
205
+ let limitP = b->param(JSON.Encode.int(limit))
206
+
207
+ (
208
+ await pool->PgDriver.query(
209
+ `SELECT ${selectColumns}, position::text AS pos_raw FROM dcb_event WHERE ${parts->Array.join(
210
+ " AND ",
211
+ )} ORDER BY position DESC LIMIT ${limitP}::int`,
212
+ b.params,
213
+ )
214
+ )->Array.map(row => (
215
+ switch row->Dict.get("pos_raw") {
216
+ | Some(JSON.String(s)) => s->Float.fromString->Option.getOr(0.)
217
+ | _ => 0.
218
+ },
219
+ rowToEvent(row),
220
+ ))
221
+ }
222
+
223
+ /** The position of each event one append wrote, by message id. The cursor an
224
+ append returns names its transaction, and every event of the append shares
225
+ it. */
226
+ let positionsOfAppend = async (pool: PgDriver.pool, ~name: string, ~cursor: string): array<(
227
+ string,
228
+ float,
229
+ )> =>
230
+ switch decodeCursor(cursor) {
231
+ | None => []
232
+ | Some((tx, _)) =>
233
+ (
234
+ await pool->PgDriver.query(
235
+ "SELECT position::text AS pos, meta->>'msgId' AS msg_id FROM dcb_event WHERE log_name = $1 AND transaction_id = $2::xid8",
236
+ [JSON.Encode.string(name), JSON.Encode.string(tx)],
237
+ )
238
+ )->Array.filterMap(row =>
239
+ switch (row->Dict.get("msg_id"), row->Dict.get("pos")) {
240
+ | (Some(JSON.String(msgId)), Some(JSON.String(pos))) =>
241
+ pos->Float.fromString->Option.map(p => (msgId, p))
242
+ | _ => None
243
+ }
244
+ )
245
+ }
246
+
178
247
  // Builds the DCB runtime op set bound to `pool`. Returns `(name, ops)`; the
179
248
  // deploy-time `DcbEventLog_Adapter` wrapper lives in `DcbEventLogStorage_Postgres`.
180
249
  let makeOps = (
@@ -42,6 +42,56 @@ let isUniqueViolation = (exn: exn): bool => {
42
42
  msg->String.includes("23505")
43
43
  }
44
44
 
45
+ /** Where a backwards read of one log starts: strictly below a position, or at
46
+ and below it. */
47
+ type bound =
48
+ | Below(float)
49
+ | AtMost(float)
50
+
51
+ /** One log's stored events newest first, across its entities, by `global_seq`,
52
+ narrowed to one correlation id when given. Rows are `(global_seq, aggregate
53
+ id, stored event)`. */
54
+ let readBackwards = async (
55
+ pool: PgDriver.pool,
56
+ ~name: string,
57
+ ~bound: option<bound>,
58
+ ~limit: int,
59
+ ~correlationId: option<string>,
60
+ ): array<(float, string, JSON.t)> => {
61
+ let params = [JSON.Encode.string(name)]
62
+ let param = v => {
63
+ params->Array.push(v)
64
+ "$" ++ params->Array.length->Int.toString
65
+ }
66
+ let parts = ["log_name = $1"]
67
+ switch bound {
68
+ | Some(Below(p)) => parts->Array.push(`global_seq < ${param(JSON.Encode.float(p))}::bigint`)
69
+ | Some(AtMost(p)) => parts->Array.push(`global_seq <= ${param(JSON.Encode.float(p))}::bigint`)
70
+ | None => ()
71
+ }
72
+ switch correlationId {
73
+ | Some(id) => parts->Array.push(`(payload->>'correlationId') = ${param(JSON.Encode.string(id))}`)
74
+ | None => ()
75
+ }
76
+ let limitP = param(JSON.Encode.int(limit))
77
+
78
+ (
79
+ await pool->PgDriver.query(
80
+ `SELECT global_seq::text AS pos, aggregate_id, payload FROM event_log WHERE ${parts->Array.join(
81
+ " AND ",
82
+ )} ORDER BY global_seq DESC LIMIT ${limitP}::int`,
83
+ params,
84
+ )
85
+ )->Array.map(row => (
86
+ row->Dict.get("pos")->Option.mapOr(0, coerceInt)->Int.toFloat,
87
+ switch row->Dict.get("aggregate_id") {
88
+ | Some(JSON.String(id)) => id
89
+ | _ => ""
90
+ },
91
+ decodePayload(row),
92
+ ))
93
+ }
94
+
45
95
  // Builds the classic runtime op set bound to `pool`. Returns `(name, ops)`; the
46
96
  // deploy-time `EventLog_Adapter` wrapper lives in `EventLogStorage_Postgres`.
47
97
  let makeOps = (~pool: PgDriver.pool, ~name: string, ~opts as _, ~onAppended=noTracking): (
Binary file
@@ -44,6 +44,10 @@ let tableStatements = [
44
44
  // every future insert with no change to EventLogStorage_Postgres.append.
45
45
  "ALTER TABLE event_log ADD COLUMN IF NOT EXISTS transaction_id xid8 NOT NULL DEFAULT pg_current_xact_id()",
46
46
  "CREATE INDEX IF NOT EXISTS event_log_tx_gseq ON event_log (log_name, transaction_id, global_seq)",
47
+ // Reading one log newest first across its entities, and finding every event of
48
+ // one flow. The stored event is flat, so the correlation id is a top-level key.
49
+ "CREATE INDEX IF NOT EXISTS event_log_log_gseq ON event_log (log_name, global_seq)",
50
+ "CREATE INDEX IF NOT EXISTS event_log_correlation ON event_log (log_name, (payload->>'correlationId'), global_seq)",
47
51
  "CREATE TABLE IF NOT EXISTS event_log_subscription (
48
52
  subscriber text PRIMARY KEY,
49
53
  last_tx xid8 NOT NULL DEFAULT '0'::xid8,
@@ -64,6 +68,7 @@ let tableStatements = [
64
68
  "CREATE INDEX IF NOT EXISTS dcb_event_tags_gin ON dcb_event USING gin (tags)",
65
69
  "CREATE INDEX IF NOT EXISTS dcb_event_type_pos ON dcb_event (log_name, event_type, position)",
66
70
  "CREATE INDEX IF NOT EXISTS dcb_event_tx_pos ON dcb_event (log_name, transaction_id, position)",
71
+ "CREATE INDEX IF NOT EXISTS dcb_event_correlation ON dcb_event (log_name, (meta->>'correlationId'), position)",
67
72
  // --- Row-lock companion (B4) ---
68
73
  "CREATE TABLE IF NOT EXISTS dcb_scope (
69
74
  log_name text NOT NULL,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reventlessdev/reventless-postgres",
3
- "version": "3.0.0-alpha.189",
3
+ "version": "3.0.0-alpha.190",
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",
25
- "@reventlessdev/rescript-effect": "1.0.0-alpha.1",
26
25
  "@reventlessdev/rescript-pulumi-pulumi": "3.0.0-alpha.0",
27
- "@reventlessdev/reventless-core": "3.0.0-alpha.325",
28
- "@reventlessdev/reventless-infra": "3.0.0-alpha.220",
26
+ "@reventlessdev/reventless-core": "3.0.0-alpha.326",
27
+ "@reventlessdev/rescript-effect": "1.0.0-alpha.1",
28
+ "@reventlessdev/reventless-infra": "3.0.0-alpha.221",
29
29
  "@reventlessdev/rescript-jest": "1.0.0-alpha.13",
30
- "@reventlessdev/reventless-spec": "3.0.0-alpha.191"
30
+ "@reventlessdev/reventless-spec": "3.0.0-alpha.192"
31
31
  },
32
32
  "devDependencies": {
33
33
  "@types/pg": "^8.11.10",
@@ -46,6 +46,10 @@ let isConflict = DcbEventLogStorage_Postgres_Ops$ReventlessPostgres.isConflict;
46
46
 
47
47
  let countAll = DcbEventLogStorage_Postgres_Ops$ReventlessPostgres.countAll;
48
48
 
49
+ let readBackwards = DcbEventLogStorage_Postgres_Ops$ReventlessPostgres.readBackwards;
50
+
51
+ let positionsOfAppend = DcbEventLogStorage_Postgres_Ops$ReventlessPostgres.positionsOfAppend;
52
+
49
53
  let makeOps = DcbEventLogStorage_Postgres_Ops$ReventlessPostgres.makeOps;
50
54
 
51
55
  export {
@@ -63,6 +67,8 @@ export {
63
67
  pageSize,
64
68
  isConflict,
65
69
  countAll,
70
+ readBackwards,
71
+ positionsOfAppend,
66
72
  makeOps,
67
73
  makeStorage,
68
74
  }
@@ -175,6 +175,75 @@ let countAll = async (pool: PgDriver.pool): int =>
175
175
  | None => 0
176
176
  }
177
177
 
178
+ /** Where a backwards read of one log starts: strictly below a position, or at
179
+ and below it. */
180
+ type bound =
181
+ | Below(float)
182
+ | AtMost(float)
183
+
184
+ /** One log's events newest first by `position`, narrowed to one correlation id
185
+ when given. Rows are `(position, event)`; the event's own `position` field is
186
+ the read cursor, as `read` returns it. */
187
+ let readBackwards = async (
188
+ pool: PgDriver.pool,
189
+ ~name: string,
190
+ ~bound: option<bound>,
191
+ ~limit: int,
192
+ ~correlationId: option<string>,
193
+ ): array<(float, DcbEventLog_Adapter.rawSequencedEvent)> => {
194
+ let b = mkBuilder()
195
+ let parts = [`log_name = ${b->param(JSON.Encode.string(name))}`]
196
+ switch bound {
197
+ | Some(Below(p)) => parts->Array.push(`position < ${b->param(JSON.Encode.float(p))}::bigint`)
198
+ | Some(AtMost(p)) => parts->Array.push(`position <= ${b->param(JSON.Encode.float(p))}::bigint`)
199
+ | None => ()
200
+ }
201
+ switch correlationId {
202
+ | Some(id) => parts->Array.push(`(meta->>'correlationId') = ${b->param(JSON.Encode.string(id))}`)
203
+ | None => ()
204
+ }
205
+ let limitP = b->param(JSON.Encode.int(limit))
206
+
207
+ (
208
+ await pool->PgDriver.query(
209
+ `SELECT ${selectColumns}, position::text AS pos_raw FROM dcb_event WHERE ${parts->Array.join(
210
+ " AND ",
211
+ )} ORDER BY position DESC LIMIT ${limitP}::int`,
212
+ b.params,
213
+ )
214
+ )->Array.map(row => (
215
+ switch row->Dict.get("pos_raw") {
216
+ | Some(JSON.String(s)) => s->Float.fromString->Option.getOr(0.)
217
+ | _ => 0.
218
+ },
219
+ rowToEvent(row),
220
+ ))
221
+ }
222
+
223
+ /** The position of each event one append wrote, by message id. The cursor an
224
+ append returns names its transaction, and every event of the append shares
225
+ it. */
226
+ let positionsOfAppend = async (pool: PgDriver.pool, ~name: string, ~cursor: string): array<(
227
+ string,
228
+ float,
229
+ )> =>
230
+ switch decodeCursor(cursor) {
231
+ | None => []
232
+ | Some((tx, _)) =>
233
+ (
234
+ await pool->PgDriver.query(
235
+ "SELECT position::text AS pos, meta->>'msgId' AS msg_id FROM dcb_event WHERE log_name = $1 AND transaction_id = $2::xid8",
236
+ [JSON.Encode.string(name), JSON.Encode.string(tx)],
237
+ )
238
+ )->Array.filterMap(row =>
239
+ switch (row->Dict.get("msg_id"), row->Dict.get("pos")) {
240
+ | (Some(JSON.String(msgId)), Some(JSON.String(pos))) =>
241
+ pos->Float.fromString->Option.map(p => (msgId, p))
242
+ | _ => None
243
+ }
244
+ )
245
+ }
246
+
178
247
  // Builds the DCB runtime op set bound to `pool`. Returns `(name, ops)`; the
179
248
  // deploy-time `DcbEventLog_Adapter` wrapper lives in `DcbEventLogStorage_Postgres`.
180
249
  let makeOps = (
@@ -3,6 +3,7 @@
3
3
  import * as Stream from "@reventlessdev/rescript-effect/src/Stream.res.mjs";
4
4
  import * as Stdlib_Int from "@rescript/runtime/lib/es6/Stdlib_Int.js";
5
5
  import * as Stdlib_Array from "@rescript/runtime/lib/es6/Stdlib_Array.js";
6
+ import * as Stdlib_Float from "@rescript/runtime/lib/es6/Stdlib_Float.js";
6
7
  import * as Stdlib_JsExn from "@rescript/runtime/lib/es6/Stdlib_JsExn.js";
7
8
  import * as Stdlib_Option from "@rescript/runtime/lib/es6/Stdlib_Option.js";
8
9
  import * as Effect from "effect/Effect";
@@ -174,6 +175,56 @@ async function countAll(pool) {
174
175
  }
175
176
  }
176
177
 
178
+ async function readBackwards(pool, name, bound, limit, correlationId) {
179
+ let b = {
180
+ params: []
181
+ };
182
+ let parts = [`log_name = ` + param(b, name)];
183
+ if (bound !== undefined) {
184
+ if (bound.TAG === "Below") {
185
+ parts.push(`position < ` + param(b, bound._0) + `::bigint`);
186
+ } else {
187
+ parts.push(`position <= ` + param(b, bound._0) + `::bigint`);
188
+ }
189
+ }
190
+ if (correlationId !== undefined) {
191
+ parts.push(`(meta->>'correlationId') = ` + param(b, correlationId));
192
+ }
193
+ let limitP = param(b, limit);
194
+ return (await PgDriver$ReventlessPostgres.query(pool, `SELECT ` + selectColumns + `, position::text AS pos_raw FROM dcb_event WHERE ` + parts.join(" AND ") + ` ORDER BY position DESC LIMIT ` + limitP + `::int`, b.params)).map(row => {
195
+ let match = row["pos_raw"];
196
+ let tmp;
197
+ tmp = match !== undefined ? (
198
+ typeof match === "string" ? Stdlib_Option.getOr(Stdlib_Float.fromString(match), 0) : 0
199
+ ) : 0;
200
+ return [
201
+ tmp,
202
+ rowToEvent(row)
203
+ ];
204
+ });
205
+ }
206
+
207
+ async function positionsOfAppend(pool, name, cursor) {
208
+ let match = decodeCursor(cursor);
209
+ if (match !== undefined) {
210
+ return Stdlib_Array.filterMap(await PgDriver$ReventlessPostgres.query(pool, "SELECT position::text AS pos, meta->>'msgId' AS msg_id FROM dcb_event WHERE log_name = $1 AND transaction_id = $2::xid8", [
211
+ name,
212
+ match[0]
213
+ ]), row => {
214
+ let match = row["msg_id"];
215
+ let match$1 = row["pos"];
216
+ if (typeof match === "string" && typeof match$1 === "string") {
217
+ return Stdlib_Option.map(Stdlib_Float.fromString(match$1), p => [
218
+ match,
219
+ p
220
+ ]);
221
+ }
222
+ });
223
+ } else {
224
+ return [];
225
+ }
226
+ }
227
+
177
228
  function makeOps(pool, name, param, param$1, param$2, param$3, lockStrategyOpt, onAppendedOpt) {
178
229
  let lockStrategy = lockStrategyOpt !== undefined ? lockStrategyOpt : "AdvisoryLocks";
179
230
  let onAppended = onAppendedOpt !== undefined ? onAppendedOpt : noTracking;
@@ -375,6 +426,8 @@ export {
375
426
  pageSize,
376
427
  isConflict,
377
428
  countAll,
429
+ readBackwards,
430
+ positionsOfAppend,
378
431
  makeOps,
379
432
  }
380
433
  /* Stream Not a pure module */
@@ -27,6 +27,8 @@ let countAll = EventLogStorage_Postgres_Ops$ReventlessPostgres.countAll;
27
27
 
28
28
  let isUniqueViolation = EventLogStorage_Postgres_Ops$ReventlessPostgres.isUniqueViolation;
29
29
 
30
+ let readBackwards = EventLogStorage_Postgres_Ops$ReventlessPostgres.readBackwards;
31
+
30
32
  let makeOps = EventLogStorage_Postgres_Ops$ReventlessPostgres.makeOps;
31
33
 
32
34
  export {
@@ -35,6 +37,7 @@ export {
35
37
  coerceInt,
36
38
  countAll,
37
39
  isUniqueViolation,
40
+ readBackwards,
38
41
  makeOps,
39
42
  makeStorage,
40
43
  }
@@ -42,6 +42,56 @@ let isUniqueViolation = (exn: exn): bool => {
42
42
  msg->String.includes("23505")
43
43
  }
44
44
 
45
+ /** Where a backwards read of one log starts: strictly below a position, or at
46
+ and below it. */
47
+ type bound =
48
+ | Below(float)
49
+ | AtMost(float)
50
+
51
+ /** One log's stored events newest first, across its entities, by `global_seq`,
52
+ narrowed to one correlation id when given. Rows are `(global_seq, aggregate
53
+ id, stored event)`. */
54
+ let readBackwards = async (
55
+ pool: PgDriver.pool,
56
+ ~name: string,
57
+ ~bound: option<bound>,
58
+ ~limit: int,
59
+ ~correlationId: option<string>,
60
+ ): array<(float, string, JSON.t)> => {
61
+ let params = [JSON.Encode.string(name)]
62
+ let param = v => {
63
+ params->Array.push(v)
64
+ "$" ++ params->Array.length->Int.toString
65
+ }
66
+ let parts = ["log_name = $1"]
67
+ switch bound {
68
+ | Some(Below(p)) => parts->Array.push(`global_seq < ${param(JSON.Encode.float(p))}::bigint`)
69
+ | Some(AtMost(p)) => parts->Array.push(`global_seq <= ${param(JSON.Encode.float(p))}::bigint`)
70
+ | None => ()
71
+ }
72
+ switch correlationId {
73
+ | Some(id) => parts->Array.push(`(payload->>'correlationId') = ${param(JSON.Encode.string(id))}`)
74
+ | None => ()
75
+ }
76
+ let limitP = param(JSON.Encode.int(limit))
77
+
78
+ (
79
+ await pool->PgDriver.query(
80
+ `SELECT global_seq::text AS pos, aggregate_id, payload FROM event_log WHERE ${parts->Array.join(
81
+ " AND ",
82
+ )} ORDER BY global_seq DESC LIMIT ${limitP}::int`,
83
+ params,
84
+ )
85
+ )->Array.map(row => (
86
+ row->Dict.get("pos")->Option.mapOr(0, coerceInt)->Int.toFloat,
87
+ switch row->Dict.get("aggregate_id") {
88
+ | Some(JSON.String(id)) => id
89
+ | _ => ""
90
+ },
91
+ decodePayload(row),
92
+ ))
93
+ }
94
+
45
95
  // Builds the classic runtime op set bound to `pool`. Returns `(name, ops)`; the
46
96
  // deploy-time `EventLog_Adapter` wrapper lives in `EventLogStorage_Postgres`.
47
97
  let makeOps = (~pool: PgDriver.pool, ~name: string, ~opts as _, ~onAppended=noTracking): (
@@ -47,6 +47,36 @@ function isUniqueViolation(exn) {
47
47
  }
48
48
  }
49
49
 
50
+ async function readBackwards(pool, name, bound, limit, correlationId) {
51
+ let params = [name];
52
+ let param = v => {
53
+ params.push(v);
54
+ return "$" + params.length.toString();
55
+ };
56
+ let parts = ["log_name = $1"];
57
+ if (bound !== undefined) {
58
+ if (bound.TAG === "Below") {
59
+ parts.push(`global_seq < ` + param(bound._0) + `::bigint`);
60
+ } else {
61
+ parts.push(`global_seq <= ` + param(bound._0) + `::bigint`);
62
+ }
63
+ }
64
+ if (correlationId !== undefined) {
65
+ parts.push(`(payload->>'correlationId') = ` + param(correlationId));
66
+ }
67
+ let limitP = param(limit);
68
+ return (await PgDriver$ReventlessPostgres.query(pool, `SELECT global_seq::text AS pos, aggregate_id, payload FROM event_log WHERE ` + parts.join(" AND ") + ` ORDER BY global_seq DESC LIMIT ` + limitP + `::int`, params)).map(row => {
69
+ let match = row["aggregate_id"];
70
+ let tmp;
71
+ tmp = typeof match === "string" ? match : "";
72
+ return [
73
+ Stdlib_Option.mapOr(row["pos"], 0, coerceInt),
74
+ tmp,
75
+ decodePayload(row)
76
+ ];
77
+ });
78
+ }
79
+
50
80
  function makeOps(pool, name, param, onAppendedOpt) {
51
81
  let onAppended = onAppendedOpt !== undefined ? onAppendedOpt : noTracking;
52
82
  let appendTracked = async (track, seqNr, id, jsons) => {
@@ -197,6 +227,7 @@ export {
197
227
  coerceInt,
198
228
  countAll,
199
229
  isUniqueViolation,
230
+ readBackwards,
200
231
  makeOps,
201
232
  }
202
233
  /* Stream Not a pure module */
package/src/PgSchema.res CHANGED
@@ -44,6 +44,10 @@ let tableStatements = [
44
44
  // every future insert with no change to EventLogStorage_Postgres.append.
45
45
  "ALTER TABLE event_log ADD COLUMN IF NOT EXISTS transaction_id xid8 NOT NULL DEFAULT pg_current_xact_id()",
46
46
  "CREATE INDEX IF NOT EXISTS event_log_tx_gseq ON event_log (log_name, transaction_id, global_seq)",
47
+ // Reading one log newest first across its entities, and finding every event of
48
+ // one flow. The stored event is flat, so the correlation id is a top-level key.
49
+ "CREATE INDEX IF NOT EXISTS event_log_log_gseq ON event_log (log_name, global_seq)",
50
+ "CREATE INDEX IF NOT EXISTS event_log_correlation ON event_log (log_name, (payload->>'correlationId'), global_seq)",
47
51
  "CREATE TABLE IF NOT EXISTS event_log_subscription (
48
52
  subscriber text PRIMARY KEY,
49
53
  last_tx xid8 NOT NULL DEFAULT '0'::xid8,
@@ -64,6 +68,7 @@ let tableStatements = [
64
68
  "CREATE INDEX IF NOT EXISTS dcb_event_tags_gin ON dcb_event USING gin (tags)",
65
69
  "CREATE INDEX IF NOT EXISTS dcb_event_type_pos ON dcb_event (log_name, event_type, position)",
66
70
  "CREATE INDEX IF NOT EXISTS dcb_event_tx_pos ON dcb_event (log_name, transaction_id, position)",
71
+ "CREATE INDEX IF NOT EXISTS dcb_event_correlation ON dcb_event (log_name, (meta->>'correlationId'), position)",
67
72
  // --- Row-lock companion (B4) ---
68
73
  "CREATE TABLE IF NOT EXISTS dcb_scope (
69
74
  log_name text NOT NULL,
@@ -7,11 +7,14 @@ let tableStatements = [
7
7
  "CREATE TABLE IF NOT EXISTS snapshot (\n log_name text NOT NULL,\n aggregate_id text NOT NULL,\n seq_nr bigint NOT NULL,\n state jsonb NOT NULL,\n schema_hash text NOT NULL,\n PRIMARY KEY (log_name, aggregate_id)\n )",
8
8
  "ALTER TABLE event_log ADD COLUMN IF NOT EXISTS transaction_id xid8 NOT NULL DEFAULT pg_current_xact_id()",
9
9
  "CREATE INDEX IF NOT EXISTS event_log_tx_gseq ON event_log (log_name, transaction_id, global_seq)",
10
+ "CREATE INDEX IF NOT EXISTS event_log_log_gseq ON event_log (log_name, global_seq)",
11
+ "CREATE INDEX IF NOT EXISTS event_log_correlation ON event_log (log_name, (payload->>'correlationId'), global_seq)",
10
12
  "CREATE TABLE IF NOT EXISTS event_log_subscription (\n subscriber text PRIMARY KEY,\n last_tx xid8 NOT NULL DEFAULT '0'::xid8,\n last_global_seq bigint NOT NULL DEFAULT 0\n )",
11
13
  "CREATE TABLE IF NOT EXISTS dcb_event (\n log_name text NOT NULL,\n position bigint GENERATED ALWAYS AS IDENTITY,\n transaction_id xid8 NOT NULL DEFAULT pg_current_xact_id(),\n event_type text NOT NULL,\n tags text[] NOT NULL DEFAULT '{}',\n data jsonb NOT NULL,\n meta jsonb NOT NULL,\n recorded_at timestamptz NOT NULL DEFAULT now(),\n PRIMARY KEY (log_name, position)\n )",
12
14
  "CREATE INDEX IF NOT EXISTS dcb_event_tags_gin ON dcb_event USING gin (tags)",
13
15
  "CREATE INDEX IF NOT EXISTS dcb_event_type_pos ON dcb_event (log_name, event_type, position)",
14
16
  "CREATE INDEX IF NOT EXISTS dcb_event_tx_pos ON dcb_event (log_name, transaction_id, position)",
17
+ "CREATE INDEX IF NOT EXISTS dcb_event_correlation ON dcb_event (log_name, (meta->>'correlationId'), position)",
15
18
  "CREATE TABLE IF NOT EXISTS dcb_scope (\n log_name text NOT NULL,\n scope_hash bigint NOT NULL,\n PRIMARY KEY (log_name, scope_hash)\n )",
16
19
  "CREATE TABLE IF NOT EXISTS dcb_subscription (\n subscriber text PRIMARY KEY,\n last_tx xid8 NOT NULL DEFAULT '0'::xid8,\n last_position bigint NOT NULL DEFAULT 0\n )"
17
20
  ];