@reventlessdev/reventless-postgres 3.0.0-alpha.19 → 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 (153) hide show
  1. package/CHANGELOG.md +1373 -0
  2. package/README.md +83 -0
  3. package/lib/bs/.compiler.log +2 -2
  4. package/lib/bs/ReventlessPostgres.cmt +0 -0
  5. package/lib/bs/compiler-info.json +3 -3
  6. package/lib/bs/src/DcbEventLogStorage_Postgres-ReventlessPostgres.cmi +0 -0
  7. package/lib/bs/src/DcbEventLogStorage_Postgres-ReventlessPostgres.cmj +0 -0
  8. package/lib/bs/src/DcbEventLogStorage_Postgres-ReventlessPostgres.cmt +0 -0
  9. package/lib/bs/src/DcbEventLogStorage_Postgres.ast +0 -0
  10. package/lib/bs/src/DcbEventLogStorage_Postgres.res +1 -1
  11. package/lib/bs/src/DcbEventLogStorage_Postgres.res.mjs +6 -0
  12. package/lib/bs/src/DcbEventLogStorage_Postgres_Ops-ReventlessPostgres.cmi +0 -0
  13. package/lib/bs/src/DcbEventLogStorage_Postgres_Ops-ReventlessPostgres.cmj +0 -0
  14. package/lib/bs/src/DcbEventLogStorage_Postgres_Ops-ReventlessPostgres.cmt +0 -0
  15. package/lib/bs/src/DcbEventLogStorage_Postgres_Ops.ast +0 -0
  16. package/lib/bs/src/DcbEventLogStorage_Postgres_Ops.res +112 -42
  17. package/lib/bs/src/DcbEventLogStorage_Postgres_Ops.res.mjs +57 -4
  18. package/lib/bs/src/EventLogChangeFeed-ReventlessPostgres.cmi +0 -0
  19. package/lib/bs/src/EventLogChangeFeed-ReventlessPostgres.cmt +0 -0
  20. package/lib/bs/src/EventLogChangeFeed.ast +0 -0
  21. package/lib/bs/src/EventLogChangeFeed.res +15 -6
  22. package/lib/bs/src/EventLogChangeFeed.res.mjs +3 -3
  23. package/lib/bs/src/EventLogStorage_Postgres-ReventlessPostgres.cmi +0 -0
  24. package/lib/bs/src/EventLogStorage_Postgres-ReventlessPostgres.cmj +0 -0
  25. package/lib/bs/src/EventLogStorage_Postgres-ReventlessPostgres.cmt +0 -0
  26. package/lib/bs/src/EventLogStorage_Postgres.ast +0 -0
  27. package/lib/bs/src/EventLogStorage_Postgres.res +1 -1
  28. package/lib/bs/src/EventLogStorage_Postgres.res.mjs +3 -0
  29. package/lib/bs/src/EventLogStorage_Postgres_Ops-ReventlessPostgres.cmi +0 -0
  30. package/lib/bs/src/EventLogStorage_Postgres_Ops-ReventlessPostgres.cmj +0 -0
  31. package/lib/bs/src/EventLogStorage_Postgres_Ops-ReventlessPostgres.cmt +0 -0
  32. package/lib/bs/src/EventLogStorage_Postgres_Ops.ast +0 -0
  33. package/lib/bs/src/EventLogStorage_Postgres_Ops.res +74 -14
  34. package/lib/bs/src/EventLogStorage_Postgres_Ops.res.mjs +31 -0
  35. package/lib/bs/src/PgChangeFeed-ReventlessPostgres.cmi +0 -0
  36. package/lib/bs/src/PgChangeFeed-ReventlessPostgres.cmt +0 -0
  37. package/lib/bs/src/PgChangeFeed.ast +0 -0
  38. package/lib/bs/src/PgChangeFeed.res +21 -9
  39. package/lib/bs/src/PgChangeFeed.res.mjs +6 -6
  40. package/lib/bs/src/PgDriver-ReventlessPostgres.cmi +0 -0
  41. package/lib/bs/src/PgDriver-ReventlessPostgres.cmt +0 -0
  42. package/lib/bs/src/PgDriver-ReventlessPostgres.cmti +0 -0
  43. package/lib/bs/src/PgDriver.ast +0 -0
  44. package/lib/bs/src/PgDriver.iast +0 -0
  45. package/lib/bs/src/PgDriver.res +1 -2
  46. package/lib/bs/src/PgSchema-ReventlessPostgres.cmi +0 -0
  47. package/lib/bs/src/PgSchema-ReventlessPostgres.cmt +0 -0
  48. package/lib/bs/src/PgSchema.ast +0 -0
  49. package/lib/bs/src/PgSchema.res +5 -0
  50. package/lib/bs/src/PgSchema.res.mjs +3 -0
  51. package/lib/bs/src/QueryDbStorage_Postgres-ReventlessPostgres.cmi +0 -0
  52. package/lib/bs/src/QueryDbStorage_Postgres-ReventlessPostgres.cmt +0 -0
  53. package/lib/bs/src/QueryDbStorage_Postgres.ast +0 -0
  54. package/lib/bs/src/QueryDbStorage_Postgres.res +7 -2
  55. package/lib/bs/src/QueryDbStorage_Postgres.res.mjs +1 -1
  56. package/lib/bs/src/QueryDbStorage_Postgres_Ops-ReventlessPostgres.cmi +0 -0
  57. package/lib/bs/src/QueryDbStorage_Postgres_Ops-ReventlessPostgres.cmt +0 -0
  58. package/lib/bs/src/QueryDbStorage_Postgres_Ops.ast +0 -0
  59. package/lib/bs/src/QueryDbStorage_Postgres_Ops.res +14 -14
  60. package/lib/bs/src/QueryEnginePostgres-ReventlessPostgres.cmi +0 -0
  61. package/lib/bs/src/QueryEnginePostgres-ReventlessPostgres.cmt +0 -0
  62. package/lib/bs/src/QueryEnginePostgres.ast +0 -0
  63. package/lib/bs/src/QueryEnginePostgres.res +141 -42
  64. package/lib/bs/src/QueryEnginePostgres.res.mjs +53 -30
  65. package/lib/bs/tests/PostgresIntegrationTest-ReventlessPostgres.cmi +0 -0
  66. package/lib/bs/tests/PostgresIntegrationTest-ReventlessPostgres.cmt +0 -0
  67. package/lib/bs/tests/PostgresIntegrationTest.ast +0 -0
  68. package/lib/bs/tests/PostgresIntegrationTest.res +157 -104
  69. package/lib/bs/tests/PostgresIntegrationTest.res.mjs +1 -1
  70. package/lib/bs/tests/QueryEnginePostgresListPageTest-ReventlessPostgres.cmi +0 -0
  71. package/lib/bs/tests/QueryEnginePostgresListPageTest-ReventlessPostgres.cmt +0 -0
  72. package/lib/bs/tests/QueryEnginePostgresListPageTest.ast +0 -0
  73. package/lib/bs/tests/QueryEnginePostgresListPageTest.res +29 -6
  74. package/lib/bs/tests/QueryEnginePostgresListPageTest.res.mjs +4 -4
  75. package/lib/ocaml/.compiler.log +2 -2
  76. package/lib/ocaml/DcbEventLogStorage_Postgres-ReventlessPostgres.cmi +0 -0
  77. package/lib/ocaml/DcbEventLogStorage_Postgres-ReventlessPostgres.cmj +0 -0
  78. package/lib/ocaml/DcbEventLogStorage_Postgres-ReventlessPostgres.cmt +0 -0
  79. package/lib/ocaml/DcbEventLogStorage_Postgres.ast +0 -0
  80. package/lib/ocaml/DcbEventLogStorage_Postgres.res +1 -1
  81. package/lib/ocaml/DcbEventLogStorage_Postgres_Ops-ReventlessPostgres.cmi +0 -0
  82. package/lib/ocaml/DcbEventLogStorage_Postgres_Ops-ReventlessPostgres.cmj +0 -0
  83. package/lib/ocaml/DcbEventLogStorage_Postgres_Ops-ReventlessPostgres.cmt +0 -0
  84. package/lib/ocaml/DcbEventLogStorage_Postgres_Ops.ast +0 -0
  85. package/lib/ocaml/DcbEventLogStorage_Postgres_Ops.res +112 -42
  86. package/lib/ocaml/EventLogChangeFeed-ReventlessPostgres.cmi +0 -0
  87. package/lib/ocaml/EventLogChangeFeed-ReventlessPostgres.cmt +0 -0
  88. package/lib/ocaml/EventLogChangeFeed.ast +0 -0
  89. package/lib/ocaml/EventLogChangeFeed.res +15 -6
  90. package/lib/ocaml/EventLogStorage_Postgres-ReventlessPostgres.cmi +0 -0
  91. package/lib/ocaml/EventLogStorage_Postgres-ReventlessPostgres.cmj +0 -0
  92. package/lib/ocaml/EventLogStorage_Postgres-ReventlessPostgres.cmt +0 -0
  93. package/lib/ocaml/EventLogStorage_Postgres.ast +0 -0
  94. package/lib/ocaml/EventLogStorage_Postgres.res +1 -1
  95. package/lib/ocaml/EventLogStorage_Postgres_Ops-ReventlessPostgres.cmi +0 -0
  96. package/lib/ocaml/EventLogStorage_Postgres_Ops-ReventlessPostgres.cmj +0 -0
  97. package/lib/ocaml/EventLogStorage_Postgres_Ops-ReventlessPostgres.cmt +0 -0
  98. package/lib/ocaml/EventLogStorage_Postgres_Ops.ast +0 -0
  99. package/lib/ocaml/EventLogStorage_Postgres_Ops.res +74 -14
  100. package/lib/ocaml/PgChangeFeed-ReventlessPostgres.cmi +0 -0
  101. package/lib/ocaml/PgChangeFeed-ReventlessPostgres.cmt +0 -0
  102. package/lib/ocaml/PgChangeFeed.ast +0 -0
  103. package/lib/ocaml/PgChangeFeed.res +21 -9
  104. package/lib/ocaml/PgDriver-ReventlessPostgres.cmi +0 -0
  105. package/lib/ocaml/PgDriver-ReventlessPostgres.cmt +0 -0
  106. package/lib/ocaml/PgDriver-ReventlessPostgres.cmti +0 -0
  107. package/lib/ocaml/PgDriver.ast +0 -0
  108. package/lib/ocaml/PgDriver.iast +0 -0
  109. package/lib/ocaml/PgDriver.res +1 -2
  110. package/lib/ocaml/PgSchema-ReventlessPostgres.cmi +0 -0
  111. package/lib/ocaml/PgSchema-ReventlessPostgres.cmt +0 -0
  112. package/lib/ocaml/PgSchema.ast +0 -0
  113. package/lib/ocaml/PgSchema.res +5 -0
  114. package/lib/ocaml/PostgresIntegrationTest.res +157 -104
  115. package/lib/ocaml/QueryDbStorage_Postgres-ReventlessPostgres.cmi +0 -0
  116. package/lib/ocaml/QueryDbStorage_Postgres-ReventlessPostgres.cmt +0 -0
  117. package/lib/ocaml/QueryDbStorage_Postgres.ast +0 -0
  118. package/lib/ocaml/QueryDbStorage_Postgres.res +7 -2
  119. package/lib/ocaml/QueryDbStorage_Postgres_Ops-ReventlessPostgres.cmi +0 -0
  120. package/lib/ocaml/QueryDbStorage_Postgres_Ops-ReventlessPostgres.cmt +0 -0
  121. package/lib/ocaml/QueryDbStorage_Postgres_Ops.ast +0 -0
  122. package/lib/ocaml/QueryDbStorage_Postgres_Ops.res +14 -14
  123. package/lib/ocaml/QueryEnginePostgres-ReventlessPostgres.cmi +0 -0
  124. package/lib/ocaml/QueryEnginePostgres-ReventlessPostgres.cmt +0 -0
  125. package/lib/ocaml/QueryEnginePostgres.ast +0 -0
  126. package/lib/ocaml/QueryEnginePostgres.res +141 -42
  127. package/lib/ocaml/QueryEnginePostgresListPageTest.res +29 -6
  128. package/lib/ocaml/ReventlessPostgres.cmt +0 -0
  129. package/package.json +13 -13
  130. package/src/DcbEventLogStorage_Postgres.res +1 -1
  131. package/src/DcbEventLogStorage_Postgres.res.mjs +6 -0
  132. package/src/DcbEventLogStorage_Postgres_Ops.res +112 -42
  133. package/src/DcbEventLogStorage_Postgres_Ops.res.mjs +57 -4
  134. package/src/EventLogChangeFeed.res +15 -6
  135. package/src/EventLogChangeFeed.res.mjs +3 -3
  136. package/src/EventLogStorage_Postgres.res +1 -1
  137. package/src/EventLogStorage_Postgres.res.mjs +3 -0
  138. package/src/EventLogStorage_Postgres_Ops.res +74 -14
  139. package/src/EventLogStorage_Postgres_Ops.res.mjs +31 -0
  140. package/src/PgChangeFeed.res +21 -9
  141. package/src/PgChangeFeed.res.mjs +6 -6
  142. package/src/PgDriver.res +1 -2
  143. package/src/PgSchema.res +5 -0
  144. package/src/PgSchema.res.mjs +3 -0
  145. package/src/QueryDbStorage_Postgres.res +7 -2
  146. package/src/QueryDbStorage_Postgres.res.mjs +1 -1
  147. package/src/QueryDbStorage_Postgres_Ops.res +14 -14
  148. package/src/QueryEnginePostgres.res +141 -42
  149. package/src/QueryEnginePostgres.res.mjs +53 -30
  150. package/tests/PostgresIntegrationTest.res +157 -104
  151. package/tests/QueryEnginePostgresListPageTest.res +29 -6
  152. package/tests/PostgresIntegrationTest.res.mjs +0 -377
  153. package/tests/QueryEnginePostgresListPageTest.res.mjs +0 -461
@@ -6,7 +6,7 @@
6
6
  // would leak into the Lambda import graph and fail resolution on real Lambda. The
7
7
  // thin `DcbEventLogStorage_Postgres` wrapper `include`s this and adds the
8
8
  // deploy-time `Pulumi.Output`-shaped adapter for local/deploy consumers.
9
- // See docs/plans/deployed-lambda-esm-self-containment.md (Rung-3 finding).
9
+ // See docs/plans/done/deployed-lambda-esm-self-containment.md (Rung-3 finding).
10
10
 
11
11
  open ReventlessCore
12
12
  open Reventless
@@ -67,7 +67,7 @@ let rowToEvent = (row: dict<JSON.t>): DcbEventLog_Adapter.rawSequencedEvent => {
67
67
  | _ => ""
68
68
  }
69
69
  let meta = switch row->Dict.get("meta") {
70
- | Some(metaJson) => metaJson->S.parseJsonOrThrow(Reventless.Message.metaSchema)
70
+ | Some(metaJson) => metaJson->Reventless.Util_Sury.fromJson(Reventless.Message.metaSchema)
71
71
  | None => JsError.throwWithMessage("missing meta column in dcb_event row")
72
72
  }
73
73
  {
@@ -143,7 +143,7 @@ let buildReadWhere = (
143
143
  // Strip the zero-padding a cursor carries for string-sortability, leaving a
144
144
  // plain decimal both `::xid8` and `::bigint` parse unambiguously ("0" if empty).
145
145
  let stripZeros = (s: string): string => {
146
- let t = s->String.replaceRegExp(%re("/^0+/"), "")
146
+ let t = s->String.replaceRegExp(/^0+/, "")
147
147
  t == "" ? "0" : t
148
148
  }
149
149
 
@@ -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 = (
@@ -222,49 +291,45 @@ let makeOps = (
222
291
  }
223
292
  }
224
293
 
225
- let append = async (
226
- newEvents: array<DcbEventLog_Adapter.rawStoredEvent>,
227
- ~condition=?,
228
- ): result<DcbTag.sequencePosition, ReventlessInfra.DcbEventLog.appendError> => {
294
+ let append = async (newEvents: array<DcbEventLog_Adapter.rawStoredEvent>, ~condition=?): result<
295
+ DcbTag.sequencePosition,
296
+ ReventlessInfra.DcbEventLog.appendError,
297
+ > => {
229
298
  let eventsJson = JSON.Array(
230
- newEvents->Array.map(ev =>
231
- JSON.Object(
299
+ newEvents->Array.map(ev => JSON.Object(
300
+ Dict.fromArray([
301
+ ("event_type", JSON.Encode.string(ev.eventType)),
302
+ (
303
+ "tags",
304
+ JSON.Array(ev.tags->Array.map(t => JSON.Encode.string(t.key ++ "=" ++ t.value))),
305
+ ),
306
+ ("data", ev.data),
307
+ ("meta", ev.meta->Reventless.Util_Sury.toJson(Reventless.Message.metaSchema)),
308
+ ]),
309
+ )),
310
+ )
311
+ let conditionParam = switch condition {
312
+ | Some(cond: DcbTag.appendCondition) =>
313
+ let queryJson = JSON.Array(
314
+ cond.query->Array.map(clause => JSON.Object(
232
315
  Dict.fromArray([
233
- ("event_type", JSON.Encode.string(ev.eventType)),
316
+ (
317
+ "eventTypes",
318
+ switch clause.eventTypes {
319
+ | Some(types) => JSON.Array(types->Array.map(JSON.Encode.string))
320
+ | None => JSON.Encode.null
321
+ },
322
+ ),
234
323
  (
235
324
  "tags",
236
- JSON.Array(ev.tags->Array.map(t => JSON.Encode.string(t.key ++ "=" ++ t.value))),
325
+ switch clause.tags {
326
+ | Some(tags) =>
327
+ JSON.Array(tags->Array.map(t => JSON.Encode.string(t.key ++ "=" ++ t.value)))
328
+ | None => JSON.Encode.null
329
+ },
237
330
  ),
238
- ("data", ev.data),
239
- ("meta", ev.meta->S.reverseConvertToJsonOrThrow(Reventless.Message.metaSchema)),
240
331
  ]),
241
- )
242
- ),
243
- )
244
- let conditionParam = switch condition {
245
- | Some(cond: DcbTag.appendCondition) =>
246
- let queryJson = JSON.Array(
247
- cond.query->Array.map(clause =>
248
- JSON.Object(
249
- Dict.fromArray([
250
- (
251
- "eventTypes",
252
- switch clause.eventTypes {
253
- | Some(types) => JSON.Array(types->Array.map(JSON.Encode.string))
254
- | None => JSON.Encode.null
255
- },
256
- ),
257
- (
258
- "tags",
259
- switch clause.tags {
260
- | Some(tags) =>
261
- JSON.Array(tags->Array.map(t => JSON.Encode.string(t.key ++ "=" ++ t.value)))
262
- | None => JSON.Encode.null
263
- },
264
- ),
265
- ]),
266
- )
267
- ),
332
+ )),
268
333
  )
269
334
  let fields = [("query", queryJson)]
270
335
  switch cond.after {
@@ -306,7 +371,10 @@ let makeOps = (
306
371
  ? Error(ReventlessInfra.DcbEventLog.Conflict)
307
372
  : Error(
308
373
  ReventlessInfra.DcbEventLog.StorageFailure(
309
- exn->JsExn.fromException->Option.flatMap(JsExn.message)->Option.getOr("storage error"),
374
+ exn
375
+ ->JsExn.fromException
376
+ ->Option.flatMap(JsExn.message)
377
+ ->Option.getOr("storage error"),
310
378
  ),
311
379
  )
312
380
  }
@@ -324,7 +392,9 @@ let makeOps = (
324
392
  Effect.promise(() => {
325
393
  let b = mkBuilder()
326
394
  let where = buildReadWhere(b, ~name, ~query, ~after=cursor, ~applyFence=true)
327
- 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)}`
395
+ 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(
396
+ pageSize,
397
+ )}`
328
398
  pool->PgDriver.query(sql, b.params)
329
399
  })->Effect.map(rows => {
330
400
  let events = rows->Array.map(rowToEvent)
@@ -1,9 +1,9 @@
1
1
  // Generated by ReScript, PLEASE EDIT WITH CARE
2
2
 
3
- import * as S from "sury/src/S.res.mjs";
4
3
  import * as Stream from "@reventlessdev/rescript-effect/src/Stream.res.mjs";
5
4
  import * as Stdlib_Int from "@rescript/runtime/lib/es6/Stdlib_Int.js";
6
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";
7
7
  import * as Stdlib_JsExn from "@rescript/runtime/lib/es6/Stdlib_JsExn.js";
8
8
  import * as Stdlib_Option from "@rescript/runtime/lib/es6/Stdlib_Option.js";
9
9
  import * as Effect from "effect/Effect";
@@ -11,6 +11,7 @@ import * as Stdlib_JsError from "@rescript/runtime/lib/es6/Stdlib_JsError.js";
11
11
  import * as Primitive_option from "@rescript/runtime/lib/es6/Primitive_option.js";
12
12
  import * as Message$Reventless from "@reventlessdev/reventless-spec/src/types/Message.res.mjs";
13
13
  import * as Primitive_exceptions from "@rescript/runtime/lib/es6/Primitive_exceptions.js";
14
+ import * as Util_Sury$Reventless from "@reventlessdev/reventless-spec/src/util/Util_Sury.res.mjs";
14
15
  import * as PgDriver$ReventlessPostgres from "./PgDriver.res.mjs";
15
16
 
16
17
  function strategyParam(s) {
@@ -68,7 +69,7 @@ function rowToEvent(row) {
68
69
  }
69
70
  };
70
71
  let metaJson = row["meta"];
71
- let meta = metaJson !== undefined ? S.parseJsonOrThrow(metaJson, Message$Reventless.metaSchema) : Stdlib_JsError.throwWithMessage("missing meta column in dcb_event row");
72
+ let meta = metaJson !== undefined ? Util_Sury$Reventless.fromJson(metaJson, Message$Reventless.metaSchema) : Stdlib_JsError.throwWithMessage("missing meta column in dcb_event row");
72
73
  return {
73
74
  position: str("cursor"),
74
75
  eventType: str("event_type"),
@@ -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;
@@ -226,7 +277,7 @@ function makeOps(pool, name, param, param$1, param$2, param$3, lockStrategyOpt,
226
277
  ],
227
278
  [
228
279
  "meta",
229
- S.reverseConvertToJsonOrThrow(ev.meta, Message$Reventless.metaSchema)
280
+ Util_Sury$Reventless.toJson(ev.meta, Message$Reventless.metaSchema)
230
281
  ]
231
282
  ])));
232
283
  let conditionParam;
@@ -375,6 +426,8 @@ export {
375
426
  pageSize,
376
427
  isConflict,
377
428
  countAll,
429
+ readBackwards,
430
+ positionsOfAppend,
378
431
  makeOps,
379
432
  }
380
- /* S Not a pure module */
433
+ /* Stream Not a pure module */
@@ -15,8 +15,11 @@
15
15
  // later commit) is never skipped by a checkpointing reader. `global_seq` alone,
16
16
  // being an IDENTITY assigned at INSERT, could NOT provide that guarantee.
17
17
 
18
- // Reuse the shared `<a>:<b>` cursor codec from the DCB storage — identical format.
19
- module Dcb = DcbEventLogStorage_Postgres
18
+ // Reuse the shared `<a>:<b>` cursor codec from the DCB storage — identical
19
+ // format. Point at the runtime-pure `_Ops` module (no `@pulumi/pulumi`): this
20
+ // feed is drained by the deployed change-feed relay Lambda, whose cold-start
21
+ // graph must stay Pulumi-free.
22
+ module Dcb = DcbEventLogStorage_Postgres_Ops
20
23
 
21
24
  // One classic event as the feed surfaces it. `payload` is the stored event JSON
22
25
  // verbatim; a bus/topic relay transforms it into its target shape (e.g. the AWS
@@ -87,11 +90,12 @@ let readBatch = async (
87
90
  params->Array.push(JSON.Encode.string(tx))
88
91
  params->Array.push(JSON.Encode.string(pos))
89
92
  where :=
90
- where.contents ++
91
- " AND (transaction_id > $2::xid8 OR (transaction_id = $2::xid8 AND global_seq > $3::bigint))"
93
+ where.contents ++ " AND (transaction_id > $2::xid8 OR (transaction_id = $2::xid8 AND global_seq > $3::bigint))"
92
94
  | None => ()
93
95
  }
94
- let sql = `SELECT ${selectColumns} FROM event_log WHERE ${where.contents} ORDER BY transaction_id ASC, global_seq ASC LIMIT ${Int.toString(limit)}`
96
+ let sql = `SELECT ${selectColumns} FROM event_log WHERE ${where.contents} ORDER BY transaction_id ASC, global_seq ASC LIMIT ${Int.toString(
97
+ limit,
98
+ )}`
95
99
  let rows = await pool->PgDriver.query(sql, params)
96
100
  let events = rows->Array.map(rowToEvent)
97
101
  let cursor = switch events->Array.get(events->Array.length - 1) {
@@ -163,7 +167,12 @@ let drain = async (
163
167
  let cursor = ref(await loadCheckpoint(pool, ~subscriber))
164
168
  let continue = ref(true)
165
169
  while continue.contents {
166
- let {events, cursor: newCursor} = await readBatch(pool, ~logName, ~after=?cursor.contents, ~limit)
170
+ let {events, cursor: newCursor} = await readBatch(
171
+ pool,
172
+ ~logName,
173
+ ~after=?cursor.contents,
174
+ ~limit,
175
+ )
167
176
  if events->Array.length == 0 {
168
177
  continue := false
169
178
  } else {
@@ -3,7 +3,7 @@
3
3
  import * as Stdlib_Int from "@rescript/runtime/lib/es6/Stdlib_Int.js";
4
4
  import * as Stdlib_Option from "@rescript/runtime/lib/es6/Stdlib_Option.js";
5
5
  import * as PgDriver$ReventlessPostgres from "./PgDriver.res.mjs";
6
- import * as DcbEventLogStorage_Postgres$ReventlessPostgres from "./DcbEventLogStorage_Postgres.res.mjs";
6
+ import * as DcbEventLogStorage_Postgres_Ops$ReventlessPostgres from "./DcbEventLogStorage_Postgres_Ops.res.mjs";
7
7
 
8
8
  let selectColumns = "\n lpad(transaction_id::text, 20, '0') || ':' || lpad(global_seq::text, 20, '0') AS cursor,\n aggregate_id,\n seq_nr,\n payload,\n msg_id\n";
9
9
 
@@ -43,7 +43,7 @@ async function readBatch(pool, logName, after, limitOpt) {
43
43
  let limit = limitOpt !== undefined ? limitOpt : 500;
44
44
  let params = [logName];
45
45
  let where = "log_name = $1 AND transaction_id < pg_snapshot_xmin(pg_current_snapshot())";
46
- let match = Stdlib_Option.flatMap(after, DcbEventLogStorage_Postgres$ReventlessPostgres.decodeCursor);
46
+ let match = Stdlib_Option.flatMap(after, DcbEventLogStorage_Postgres_Ops$ReventlessPostgres.decodeCursor);
47
47
  if (match !== undefined) {
48
48
  params.push(match[0]);
49
49
  params.push(match[1]);
@@ -72,7 +72,7 @@ async function loadCheckpoint(pool, subscriber) {
72
72
  }
73
73
 
74
74
  async function saveCheckpoint(pool, subscriber, cursor) {
75
- let match = DcbEventLogStorage_Postgres$ReventlessPostgres.decodeCursor(cursor);
75
+ let match = DcbEventLogStorage_Postgres_Ops$ReventlessPostgres.decodeCursor(cursor);
76
76
  if (match !== undefined) {
77
77
  await PgDriver$ReventlessPostgres.query(pool, "INSERT INTO event_log_subscription(subscriber, last_tx, last_global_seq)\n VALUES ($1, $2::xid8, $3::bigint)\n ON CONFLICT (subscriber)\n DO UPDATE SET last_tx = EXCLUDED.last_tx, last_global_seq = EXCLUDED.last_global_seq", [
78
78
  subscriber,
@@ -7,7 +7,7 @@
7
7
  // real Lambda). This module `include`s that pure module and adds only the
8
8
  // deploy-time `EventLog_Adapter` shape (`operations` wrapped in `Pulumi.Output`),
9
9
  // keeping the historic `makeStorage` 3-tuple `(name, ops, adapter)` API unchanged.
10
- // See docs/plans/deployed-lambda-esm-self-containment.md (Rung-3 finding).
10
+ // See docs/plans/done/deployed-lambda-esm-self-containment.md (Rung-3 finding).
11
11
 
12
12
  open ReventlessCore
13
13
  include EventLogStorage_Postgres_Ops
@@ -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
  }
@@ -7,7 +7,7 @@
7
7
  // in no Lambda — not the layer, not `/var/runtime`). The thin
8
8
  // `EventLogStorage_Postgres` wrapper adds the deploy-time `Pulumi.Output`-shaped
9
9
  // adapter on top for local/deploy consumers; the runtime imports `makeOps` here.
10
- // See docs/plans/deployed-lambda-esm-self-containment.md (Rung-3 finding).
10
+ // See docs/plans/done/deployed-lambda-esm-self-containment.md (Rung-3 finding).
11
11
 
12
12
  open ReventlessCore
13
13
 
@@ -42,14 +42,62 @@ 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
- let makeOps = (
48
- ~pool: PgDriver.pool,
49
- ~name: string,
50
- ~opts as _,
51
- ~onAppended=noTracking,
52
- ): (string, EventLog_Adapter.operations) => {
97
+ let makeOps = (~pool: PgDriver.pool, ~name: string, ~opts as _, ~onAppended=noTracking): (
98
+ string,
99
+ EventLog_Adapter.operations,
100
+ ) => {
53
101
  let appendTracked = async (~track, seqNr: int, id: string, jsons: array<JSON.t>) => {
54
102
  // Empty append is a legitimate no-op (at-least-once retries, idempotent
55
103
  // commands), NOT a conflict — return before the guarded insert whose 0-row
@@ -95,7 +143,10 @@ let makeOps = (
95
143
  ? Error(EventLog.Conflict)
96
144
  : Error(
97
145
  EventLog.StorageFailure(
98
- exn->JsExn.fromException->Option.flatMap(JsExn.message)->Option.getOr("storage error"),
146
+ exn
147
+ ->JsExn.fromException
148
+ ->Option.flatMap(JsExn.message)
149
+ ->Option.getOr("storage error"),
99
150
  ),
100
151
  )
101
152
  }
@@ -106,12 +157,14 @@ let makeOps = (
106
157
  appendTracked(~track=true, seqNr, id, jsons)
107
158
 
108
159
  let replayArray = async (id: string, ~fromSeq=0): array<JSON.t> =>
109
- (await pool->PgDriver.query(
110
- "SELECT payload FROM event_log
160
+ (
161
+ await pool->PgDriver.query(
162
+ "SELECT payload FROM event_log
111
163
  WHERE log_name = $1 AND aggregate_id = $2 AND seq_nr >= $3::bigint
112
164
  ORDER BY seq_nr ASC",
113
- [JSON.Encode.string(name), JSON.Encode.string(id), JSON.Encode.int(fromSeq)],
114
- ))->Array.map(decodePayload)
165
+ [JSON.Encode.string(name), JSON.Encode.string(id), JSON.Encode.int(fromSeq)],
166
+ )
167
+ )->Array.map(decodePayload)
115
168
 
116
169
  let replay: EventLog.replay<string, JSON.t> = id => replayArray(id)
117
170
 
@@ -152,14 +205,21 @@ let makeOps = (
152
205
  Ok()
153
206
  } catch {
154
207
  | exn =>
155
- Error(exn->JsExn.fromException->Option.flatMap(JsExn.message)->Option.getOr("snapshot write error"))
208
+ Error(
209
+ exn
210
+ ->JsExn.fromException
211
+ ->Option.flatMap(JsExn.message)
212
+ ->Option.getOr("snapshot write error"),
213
+ )
156
214
  }
157
215
 
158
216
  let appendStream: EventLog.appendStream<string, JSON.t> = (startingSeqNr, id, stream) =>
159
217
  stream
160
218
  ->Stream.runCollect
161
219
  ->Effect.flatMap(jsons =>
162
- Effect.promise(() => appendTracked(~track=false, startingSeqNr, id, jsons))->Effect.flatMap(result =>
220
+ Effect.promise(() =>
221
+ appendTracked(~track=false, startingSeqNr, id, jsons)
222
+ )->Effect.flatMap(result =>
163
223
  switch result {
164
224
  | Ok() => Effect.succeed()
165
225
  | Error(EventLog.Conflict) => Effect.fail("conflict")
@@ -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 */
@@ -18,7 +18,12 @@
18
18
  open ReventlessCore
19
19
  open Reventless
20
20
 
21
- module Dcb = DcbEventLogStorage_Postgres
21
+ // Runtime-pure ops module (no `@pulumi/pulumi`) — this feed is drained by the
22
+ // deployed change-feed relay Lambda, so its cold-start graph must stay
23
+ // Pulumi-free. `_Ops` carries every helper used below (mkBuilder,
24
+ // buildReadWhere, decodeCursor, selectColumns, rowToEvent); the deploy-time
25
+ // DcbEventLogStorage_Postgres only `include`s it and adds the Pulumi adapter.
26
+ module Dcb = DcbEventLogStorage_Postgres_Ops
22
27
 
23
28
  // One page of the feed. `events` are in (transaction_id, position) order;
24
29
  // `cursor` is the checkpoint to persist and to pass as the next `after` (None
@@ -44,7 +49,9 @@ let readBatch = async (
44
49
  ~after=after->Option.flatMap(Dcb.decodeCursor),
45
50
  ~applyFence=true,
46
51
  )
47
- let sql = `SELECT ${Dcb.selectColumns} FROM dcb_event WHERE ${where} ORDER BY transaction_id ASC, position ASC LIMIT ${Int.toString(limit)}`
52
+ let sql = `SELECT ${Dcb.selectColumns} FROM dcb_event WHERE ${where} ORDER BY transaction_id ASC, position ASC LIMIT ${Int.toString(
53
+ limit,
54
+ )}`
48
55
  let rows = await pool->PgDriver.query(sql, b.params)
49
56
  let events = rows->Array.map(Dcb.rowToEvent)
50
57
  let cursor = switch events->Array.get(events->Array.length - 1) {
@@ -58,10 +65,9 @@ let readBatch = async (
58
65
 
59
66
  // The persisted checkpoint for `subscriber`, encoded as a cursor, or None if the
60
67
  // subscriber has never committed (→ replay from the beginning).
61
- let loadCheckpoint = async (
62
- pool: PgDriver.pool,
63
- ~subscriber: string,
64
- ): option<DcbTag.sequencePosition> =>
68
+ let loadCheckpoint = async (pool: PgDriver.pool, ~subscriber: string): option<
69
+ DcbTag.sequencePosition,
70
+ > =>
65
71
  switch await pool->PgDriver.queryOne(
66
72
  "SELECT lpad(last_tx::text, 20, '0') || ':' || lpad(last_position::text, 20, '0') AS cursor
67
73
  FROM dcb_subscription WHERE subscriber = $1 AND last_position > 0",
@@ -100,8 +106,9 @@ let saveCheckpoint = async (
100
106
  // dedicated client (caller owns it — release via PgDriver.unlisten). Pair with a
101
107
  // low-frequency fallback tick in the consumer loop so a missed NOTIFY only adds
102
108
  // latency, never a permanent stall.
103
- let listen = (pool: PgDriver.pool, ~logName: string, ~onWake: unit => unit): promise<PgDriver.client> =>
104
- pool->PgDriver.listen(~channel="dcb_" ++ logName, ~onNotify=_ => onWake())
109
+ let listen = (pool: PgDriver.pool, ~logName: string, ~onWake: unit => unit): promise<
110
+ PgDriver.client,
111
+ > => pool->PgDriver.listen(~channel="dcb_" ++ logName, ~onNotify=_ => onWake())
105
112
 
106
113
  let unlisten = (client: PgDriver.client, ~logName: string): promise<unit> =>
107
114
  client->PgDriver.unlisten(~channel="dcb_" ++ logName)
@@ -124,7 +131,12 @@ let drain = async (
124
131
  let cursor = ref(await loadCheckpoint(pool, ~subscriber))
125
132
  let continue = ref(true)
126
133
  while continue.contents {
127
- let {events, cursor: newCursor} = await readBatch(pool, ~logName, ~after=?cursor.contents, ~limit)
134
+ let {events, cursor: newCursor} = await readBatch(
135
+ pool,
136
+ ~logName,
137
+ ~after=?cursor.contents,
138
+ ~limit,
139
+ )
128
140
  if events->Array.length == 0 {
129
141
  continue := false
130
142
  } else {