@reventlessdev/reventless-postgres 3.0.0-alpha.20 → 3.0.0-alpha.200

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 +1443 -0
  2. package/README.md +1 -1
  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 +116 -44
  17. package/lib/bs/src/DcbEventLogStorage_Postgres_Ops.res.mjs +64 -7
  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 +34 -4
  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 +8 -0
  50. package/lib/bs/src/PgSchema.res.mjs +4 -1
  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 +116 -44
  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 +8 -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 +116 -44
  133. package/src/DcbEventLogStorage_Postgres_Ops.res.mjs +64 -7
  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 +34 -4
  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 +8 -0
  144. package/src/PgSchema.res.mjs +4 -1
  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
  {
@@ -118,10 +118,12 @@ let buildReadWhere = (
118
118
  let clauseSqls = query->Array.map(clause => {
119
119
  let clauseParts = []
120
120
  switch clause.eventTypes {
121
- | Some(types) if types->Array.length > 0 =>
121
+ // A clause with no event type matches nothing (`DcbTag.clauseMatchesNothing`).
122
+ | Some([]) => clauseParts->Array.push("FALSE")
123
+ | Some(types) =>
122
124
  let ph = types->Array.map(t => b->param(JSON.Encode.string(t)))->Array.join(",")
123
125
  clauseParts->Array.push(`event_type IN (${ph})`)
124
- | _ => ()
126
+ | None => ()
125
127
  }
126
128
  switch clause.tags {
127
129
  | Some(tags) if tags->Array.length > 0 =>
@@ -143,7 +145,7 @@ let buildReadWhere = (
143
145
  // Strip the zero-padding a cursor carries for string-sortability, leaving a
144
146
  // plain decimal both `::xid8` and `::bigint` parse unambiguously ("0" if empty).
145
147
  let stripZeros = (s: string): string => {
146
- let t = s->String.replaceRegExp(%re("/^0+/"), "")
148
+ let t = s->String.replaceRegExp(/^0+/, "")
147
149
  t == "" ? "0" : t
148
150
  }
149
151
 
@@ -175,6 +177,75 @@ let countAll = async (pool: PgDriver.pool): int =>
175
177
  | None => 0
176
178
  }
177
179
 
180
+ /** Where a backwards read of one log starts: strictly below a position, or at
181
+ and below it. */
182
+ type bound =
183
+ | Below(float)
184
+ | AtMost(float)
185
+
186
+ /** One log's events newest first by `position`, narrowed to one correlation id
187
+ when given. Rows are `(position, event)`; the event's own `position` field is
188
+ the read cursor, as `read` returns it. */
189
+ let readBackwards = async (
190
+ pool: PgDriver.pool,
191
+ ~name: string,
192
+ ~bound: option<bound>,
193
+ ~limit: int,
194
+ ~correlationId: option<string>,
195
+ ): array<(float, DcbEventLog_Adapter.rawSequencedEvent)> => {
196
+ let b = mkBuilder()
197
+ let parts = [`log_name = ${b->param(JSON.Encode.string(name))}`]
198
+ switch bound {
199
+ | Some(Below(p)) => parts->Array.push(`position < ${b->param(JSON.Encode.float(p))}::bigint`)
200
+ | Some(AtMost(p)) => parts->Array.push(`position <= ${b->param(JSON.Encode.float(p))}::bigint`)
201
+ | None => ()
202
+ }
203
+ switch correlationId {
204
+ | Some(id) => parts->Array.push(`(meta->>'correlationId') = ${b->param(JSON.Encode.string(id))}`)
205
+ | None => ()
206
+ }
207
+ let limitP = b->param(JSON.Encode.int(limit))
208
+
209
+ (
210
+ await pool->PgDriver.query(
211
+ `SELECT ${selectColumns}, position::text AS pos_raw FROM dcb_event WHERE ${parts->Array.join(
212
+ " AND ",
213
+ )} ORDER BY position DESC LIMIT ${limitP}::int`,
214
+ b.params,
215
+ )
216
+ )->Array.map(row => (
217
+ switch row->Dict.get("pos_raw") {
218
+ | Some(JSON.String(s)) => s->Float.fromString->Option.getOr(0.)
219
+ | _ => 0.
220
+ },
221
+ rowToEvent(row),
222
+ ))
223
+ }
224
+
225
+ /** The position of each event one append wrote, by message id. The cursor an
226
+ append returns names its transaction, and every event of the append shares
227
+ it. */
228
+ let positionsOfAppend = async (pool: PgDriver.pool, ~name: string, ~cursor: string): array<(
229
+ string,
230
+ float,
231
+ )> =>
232
+ switch decodeCursor(cursor) {
233
+ | None => []
234
+ | Some((tx, _)) =>
235
+ (
236
+ await pool->PgDriver.query(
237
+ "SELECT position::text AS pos, meta->>'msgId' AS msg_id FROM dcb_event WHERE log_name = $1 AND transaction_id = $2::xid8",
238
+ [JSON.Encode.string(name), JSON.Encode.string(tx)],
239
+ )
240
+ )->Array.filterMap(row =>
241
+ switch (row->Dict.get("msg_id"), row->Dict.get("pos")) {
242
+ | (Some(JSON.String(msgId)), Some(JSON.String(pos))) =>
243
+ pos->Float.fromString->Option.map(p => (msgId, p))
244
+ | _ => None
245
+ }
246
+ )
247
+ }
248
+
178
249
  // Builds the DCB runtime op set bound to `pool`. Returns `(name, ops)`; the
179
250
  // deploy-time `DcbEventLog_Adapter` wrapper lives in `DcbEventLogStorage_Postgres`.
180
251
  let makeOps = (
@@ -222,49 +293,45 @@ let makeOps = (
222
293
  }
223
294
  }
224
295
 
225
- let append = async (
226
- newEvents: array<DcbEventLog_Adapter.rawStoredEvent>,
227
- ~condition=?,
228
- ): result<DcbTag.sequencePosition, ReventlessInfra.DcbEventLog.appendError> => {
296
+ let append = async (newEvents: array<DcbEventLog_Adapter.rawStoredEvent>, ~condition=?): result<
297
+ DcbTag.sequencePosition,
298
+ ReventlessInfra.DcbEventLog.appendError,
299
+ > => {
229
300
  let eventsJson = JSON.Array(
230
- newEvents->Array.map(ev =>
231
- JSON.Object(
301
+ newEvents->Array.map(ev => JSON.Object(
302
+ Dict.fromArray([
303
+ ("event_type", JSON.Encode.string(ev.eventType)),
304
+ (
305
+ "tags",
306
+ JSON.Array(ev.tags->Array.map(t => JSON.Encode.string(t.key ++ "=" ++ t.value))),
307
+ ),
308
+ ("data", ev.data),
309
+ ("meta", ev.meta->Reventless.Util_Sury.toJson(Reventless.Message.metaSchema)),
310
+ ]),
311
+ )),
312
+ )
313
+ let conditionParam = switch condition {
314
+ | Some(cond: DcbTag.appendCondition) =>
315
+ let queryJson = JSON.Array(
316
+ cond.query->Array.map(clause => JSON.Object(
232
317
  Dict.fromArray([
233
- ("event_type", JSON.Encode.string(ev.eventType)),
318
+ (
319
+ "eventTypes",
320
+ switch clause.eventTypes {
321
+ | Some(types) => JSON.Array(types->Array.map(JSON.Encode.string))
322
+ | None => JSON.Encode.null
323
+ },
324
+ ),
234
325
  (
235
326
  "tags",
236
- JSON.Array(ev.tags->Array.map(t => JSON.Encode.string(t.key ++ "=" ++ t.value))),
327
+ switch clause.tags {
328
+ | Some(tags) =>
329
+ JSON.Array(tags->Array.map(t => JSON.Encode.string(t.key ++ "=" ++ t.value)))
330
+ | None => JSON.Encode.null
331
+ },
237
332
  ),
238
- ("data", ev.data),
239
- ("meta", ev.meta->S.reverseConvertToJsonOrThrow(Reventless.Message.metaSchema)),
240
333
  ]),
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
- ),
334
+ )),
268
335
  )
269
336
  let fields = [("query", queryJson)]
270
337
  switch cond.after {
@@ -306,7 +373,10 @@ let makeOps = (
306
373
  ? Error(ReventlessInfra.DcbEventLog.Conflict)
307
374
  : Error(
308
375
  ReventlessInfra.DcbEventLog.StorageFailure(
309
- exn->JsExn.fromException->Option.flatMap(JsExn.message)->Option.getOr("storage error"),
376
+ exn
377
+ ->JsExn.fromException
378
+ ->Option.flatMap(JsExn.message)
379
+ ->Option.getOr("storage error"),
310
380
  ),
311
381
  )
312
382
  }
@@ -324,7 +394,9 @@ let makeOps = (
324
394
  Effect.promise(() => {
325
395
  let b = mkBuilder()
326
396
  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)}`
397
+ 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(
398
+ pageSize,
399
+ )}`
328
400
  pool->PgDriver.query(sql, b.params)
329
401
  })->Effect.map(rows => {
330
402
  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"),
@@ -104,9 +105,13 @@ function buildReadWhere(b, name, query, after, applyFence) {
104
105
  let clauseSqls = query.map(clause => {
105
106
  let clauseParts = [];
106
107
  let types = clause.eventTypes;
107
- if (types !== undefined && types.length !== 0) {
108
- let ph = types.map(t => param(b, t)).join(",");
109
- clauseParts.push(`event_type IN (` + ph + `)`);
108
+ if (types !== undefined) {
109
+ if (types.length !== 0) {
110
+ let ph = types.map(t => param(b, t)).join(",");
111
+ clauseParts.push(`event_type IN (` + ph + `)`);
112
+ } else {
113
+ clauseParts.push("FALSE");
114
+ }
110
115
  }
111
116
  let tags = clause.tags;
112
117
  if (tags !== undefined && tags.length !== 0) {
@@ -174,6 +179,56 @@ async function countAll(pool) {
174
179
  }
175
180
  }
176
181
 
182
+ async function readBackwards(pool, name, bound, limit, correlationId) {
183
+ let b = {
184
+ params: []
185
+ };
186
+ let parts = [`log_name = ` + param(b, name)];
187
+ if (bound !== undefined) {
188
+ if (bound.TAG === "Below") {
189
+ parts.push(`position < ` + param(b, bound._0) + `::bigint`);
190
+ } else {
191
+ parts.push(`position <= ` + param(b, bound._0) + `::bigint`);
192
+ }
193
+ }
194
+ if (correlationId !== undefined) {
195
+ parts.push(`(meta->>'correlationId') = ` + param(b, correlationId));
196
+ }
197
+ let limitP = param(b, limit);
198
+ 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 => {
199
+ let match = row["pos_raw"];
200
+ let tmp;
201
+ tmp = match !== undefined ? (
202
+ typeof match === "string" ? Stdlib_Option.getOr(Stdlib_Float.fromString(match), 0) : 0
203
+ ) : 0;
204
+ return [
205
+ tmp,
206
+ rowToEvent(row)
207
+ ];
208
+ });
209
+ }
210
+
211
+ async function positionsOfAppend(pool, name, cursor) {
212
+ let match = decodeCursor(cursor);
213
+ if (match !== undefined) {
214
+ 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", [
215
+ name,
216
+ match[0]
217
+ ]), row => {
218
+ let match = row["msg_id"];
219
+ let match$1 = row["pos"];
220
+ if (typeof match === "string" && typeof match$1 === "string") {
221
+ return Stdlib_Option.map(Stdlib_Float.fromString(match$1), p => [
222
+ match,
223
+ p
224
+ ]);
225
+ }
226
+ });
227
+ } else {
228
+ return [];
229
+ }
230
+ }
231
+
177
232
  function makeOps(pool, name, param, param$1, param$2, param$3, lockStrategyOpt, onAppendedOpt) {
178
233
  let lockStrategy = lockStrategyOpt !== undefined ? lockStrategyOpt : "AdvisoryLocks";
179
234
  let onAppended = onAppendedOpt !== undefined ? onAppendedOpt : noTracking;
@@ -226,7 +281,7 @@ function makeOps(pool, name, param, param$1, param$2, param$3, lockStrategyOpt,
226
281
  ],
227
282
  [
228
283
  "meta",
229
- S.reverseConvertToJsonOrThrow(ev.meta, Message$Reventless.metaSchema)
284
+ Util_Sury$Reventless.toJson(ev.meta, Message$Reventless.metaSchema)
230
285
  ]
231
286
  ])));
232
287
  let conditionParam;
@@ -375,6 +430,8 @@ export {
375
430
  pageSize,
376
431
  isConflict,
377
432
  countAll,
433
+ readBackwards,
434
+ positionsOfAppend,
378
435
  makeOps,
379
436
  }
380
- /* S Not a pure module */
437
+ /* 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")
@@ -1,12 +1,11 @@
1
1
  // Generated by ReScript, PLEASE EDIT WITH CARE
2
2
 
3
- import * as Stream from "@reventlessdev/rescript-effect/src/Stream.res.mjs";
4
3
  import * as Stdlib_Int from "@rescript/runtime/lib/es6/Stdlib_Int.js";
5
4
  import * as Stdlib_Array from "@rescript/runtime/lib/es6/Stdlib_Array.js";
6
5
  import * as Stdlib_JsExn from "@rescript/runtime/lib/es6/Stdlib_JsExn.js";
7
6
  import * as Stdlib_Option from "@rescript/runtime/lib/es6/Stdlib_Option.js";
8
7
  import * as Effect from "effect/Effect";
9
- import * as Stream$1 from "effect/Stream";
8
+ import * as Stream from "effect/Stream";
10
9
  import * as Primitive_exceptions from "@rescript/runtime/lib/es6/Primitive_exceptions.js";
11
10
  import * as PgDriver$ReventlessPostgres from "./PgDriver.res.mjs";
12
11
 
@@ -47,6 +46,36 @@ function isUniqueViolation(exn) {
47
46
  }
48
47
  }
49
48
 
49
+ async function readBackwards(pool, name, bound, limit, correlationId) {
50
+ let params = [name];
51
+ let param = v => {
52
+ params.push(v);
53
+ return "$" + params.length.toString();
54
+ };
55
+ let parts = ["log_name = $1"];
56
+ if (bound !== undefined) {
57
+ if (bound.TAG === "Below") {
58
+ parts.push(`global_seq < ` + param(bound._0) + `::bigint`);
59
+ } else {
60
+ parts.push(`global_seq <= ` + param(bound._0) + `::bigint`);
61
+ }
62
+ }
63
+ if (correlationId !== undefined) {
64
+ parts.push(`(payload->>'correlationId') = ` + param(correlationId));
65
+ }
66
+ let limitP = param(limit);
67
+ 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 => {
68
+ let match = row["aggregate_id"];
69
+ let tmp;
70
+ tmp = typeof match === "string" ? match : "";
71
+ return [
72
+ Stdlib_Option.mapOr(row["pos"], 0, coerceInt),
73
+ tmp,
74
+ decodePayload(row)
75
+ ];
76
+ });
77
+ }
78
+
50
79
  function makeOps(pool, name, param, onAppendedOpt) {
51
80
  let onAppended = onAppendedOpt !== undefined ? onAppendedOpt : noTracking;
52
81
  let appendTracked = async (track, seqNr, id, jsons) => {
@@ -114,7 +143,7 @@ function makeOps(pool, name, param, onAppendedOpt) {
114
143
  ])).map(decodePayload);
115
144
  };
116
145
  let replay = id => replayArray(id, undefined);
117
- let replayStream = (id, fromSeq) => Stream$1.flatMap(Stream$1.fromEffect(Effect.promise(() => replayArray(id, fromSeq))), arr => Stream$1.fromIterable(arr));
146
+ let replayStream = (id, fromSeq) => Stream.flatMap(Stream.fromEffect(Effect.promise(() => replayArray(id, fromSeq))), arr => Stream.fromIterable(arr));
118
147
  let latestSnapshot = async id => {
119
148
  let row = await PgDriver$ReventlessPostgres.queryOne(pool, "SELECT seq_nr, state, schema_hash FROM snapshot WHERE log_name = $1 AND aggregate_id = $2", [
120
149
  name,
@@ -197,6 +226,7 @@ export {
197
226
  coerceInt,
198
227
  countAll,
199
228
  isUniqueViolation,
229
+ readBackwards,
200
230
  makeOps,
201
231
  }
202
- /* Stream Not a pure module */
232
+ /* Stdlib_JsExn Not a pure module */