@reventlessdev/reventless-postgres 3.0.0-alpha.19 → 3.0.0-alpha.191
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.
- package/CHANGELOG.md +1381 -0
- package/README.md +83 -0
- package/lib/bs/.compiler.log +2 -2
- package/lib/bs/ReventlessPostgres.cmt +0 -0
- package/lib/bs/compiler-info.json +3 -3
- package/lib/bs/src/DcbEventLogStorage_Postgres-ReventlessPostgres.cmi +0 -0
- package/lib/bs/src/DcbEventLogStorage_Postgres-ReventlessPostgres.cmj +0 -0
- package/lib/bs/src/DcbEventLogStorage_Postgres-ReventlessPostgres.cmt +0 -0
- package/lib/bs/src/DcbEventLogStorage_Postgres.ast +0 -0
- package/lib/bs/src/DcbEventLogStorage_Postgres.res +1 -1
- package/lib/bs/src/DcbEventLogStorage_Postgres.res.mjs +6 -0
- package/lib/bs/src/DcbEventLogStorage_Postgres_Ops-ReventlessPostgres.cmi +0 -0
- package/lib/bs/src/DcbEventLogStorage_Postgres_Ops-ReventlessPostgres.cmj +0 -0
- package/lib/bs/src/DcbEventLogStorage_Postgres_Ops-ReventlessPostgres.cmt +0 -0
- package/lib/bs/src/DcbEventLogStorage_Postgres_Ops.ast +0 -0
- package/lib/bs/src/DcbEventLogStorage_Postgres_Ops.res +112 -42
- package/lib/bs/src/DcbEventLogStorage_Postgres_Ops.res.mjs +57 -4
- package/lib/bs/src/EventLogChangeFeed-ReventlessPostgres.cmi +0 -0
- package/lib/bs/src/EventLogChangeFeed-ReventlessPostgres.cmt +0 -0
- package/lib/bs/src/EventLogChangeFeed.ast +0 -0
- package/lib/bs/src/EventLogChangeFeed.res +15 -6
- package/lib/bs/src/EventLogChangeFeed.res.mjs +3 -3
- package/lib/bs/src/EventLogStorage_Postgres-ReventlessPostgres.cmi +0 -0
- package/lib/bs/src/EventLogStorage_Postgres-ReventlessPostgres.cmj +0 -0
- package/lib/bs/src/EventLogStorage_Postgres-ReventlessPostgres.cmt +0 -0
- package/lib/bs/src/EventLogStorage_Postgres.ast +0 -0
- package/lib/bs/src/EventLogStorage_Postgres.res +1 -1
- package/lib/bs/src/EventLogStorage_Postgres.res.mjs +3 -0
- package/lib/bs/src/EventLogStorage_Postgres_Ops-ReventlessPostgres.cmi +0 -0
- package/lib/bs/src/EventLogStorage_Postgres_Ops-ReventlessPostgres.cmj +0 -0
- package/lib/bs/src/EventLogStorage_Postgres_Ops-ReventlessPostgres.cmt +0 -0
- package/lib/bs/src/EventLogStorage_Postgres_Ops.ast +0 -0
- package/lib/bs/src/EventLogStorage_Postgres_Ops.res +74 -14
- package/lib/bs/src/EventLogStorage_Postgres_Ops.res.mjs +31 -0
- package/lib/bs/src/PgChangeFeed-ReventlessPostgres.cmi +0 -0
- package/lib/bs/src/PgChangeFeed-ReventlessPostgres.cmt +0 -0
- package/lib/bs/src/PgChangeFeed.ast +0 -0
- package/lib/bs/src/PgChangeFeed.res +21 -9
- package/lib/bs/src/PgChangeFeed.res.mjs +6 -6
- package/lib/bs/src/PgDriver-ReventlessPostgres.cmi +0 -0
- package/lib/bs/src/PgDriver-ReventlessPostgres.cmt +0 -0
- package/lib/bs/src/PgDriver-ReventlessPostgres.cmti +0 -0
- package/lib/bs/src/PgDriver.ast +0 -0
- package/lib/bs/src/PgDriver.iast +0 -0
- package/lib/bs/src/PgDriver.res +1 -2
- package/lib/bs/src/PgSchema-ReventlessPostgres.cmi +0 -0
- package/lib/bs/src/PgSchema-ReventlessPostgres.cmt +0 -0
- package/lib/bs/src/PgSchema.ast +0 -0
- package/lib/bs/src/PgSchema.res +5 -0
- package/lib/bs/src/PgSchema.res.mjs +3 -0
- package/lib/bs/src/QueryDbStorage_Postgres-ReventlessPostgres.cmi +0 -0
- package/lib/bs/src/QueryDbStorage_Postgres-ReventlessPostgres.cmt +0 -0
- package/lib/bs/src/QueryDbStorage_Postgres.ast +0 -0
- package/lib/bs/src/QueryDbStorage_Postgres.res +7 -2
- package/lib/bs/src/QueryDbStorage_Postgres.res.mjs +1 -1
- package/lib/bs/src/QueryDbStorage_Postgres_Ops-ReventlessPostgres.cmi +0 -0
- package/lib/bs/src/QueryDbStorage_Postgres_Ops-ReventlessPostgres.cmt +0 -0
- package/lib/bs/src/QueryDbStorage_Postgres_Ops.ast +0 -0
- package/lib/bs/src/QueryDbStorage_Postgres_Ops.res +14 -14
- package/lib/bs/src/QueryEnginePostgres-ReventlessPostgres.cmi +0 -0
- package/lib/bs/src/QueryEnginePostgres-ReventlessPostgres.cmt +0 -0
- package/lib/bs/src/QueryEnginePostgres.ast +0 -0
- package/lib/bs/src/QueryEnginePostgres.res +141 -42
- package/lib/bs/src/QueryEnginePostgres.res.mjs +53 -30
- package/lib/bs/tests/PostgresIntegrationTest-ReventlessPostgres.cmi +0 -0
- package/lib/bs/tests/PostgresIntegrationTest-ReventlessPostgres.cmt +0 -0
- package/lib/bs/tests/PostgresIntegrationTest.ast +0 -0
- package/lib/bs/tests/PostgresIntegrationTest.res +157 -104
- package/lib/bs/tests/PostgresIntegrationTest.res.mjs +1 -1
- package/lib/bs/tests/QueryEnginePostgresListPageTest-ReventlessPostgres.cmi +0 -0
- package/lib/bs/tests/QueryEnginePostgresListPageTest-ReventlessPostgres.cmt +0 -0
- package/lib/bs/tests/QueryEnginePostgresListPageTest.ast +0 -0
- package/lib/bs/tests/QueryEnginePostgresListPageTest.res +29 -6
- package/lib/bs/tests/QueryEnginePostgresListPageTest.res.mjs +4 -4
- package/lib/ocaml/.compiler.log +2 -2
- package/lib/ocaml/DcbEventLogStorage_Postgres-ReventlessPostgres.cmi +0 -0
- package/lib/ocaml/DcbEventLogStorage_Postgres-ReventlessPostgres.cmj +0 -0
- package/lib/ocaml/DcbEventLogStorage_Postgres-ReventlessPostgres.cmt +0 -0
- package/lib/ocaml/DcbEventLogStorage_Postgres.ast +0 -0
- package/lib/ocaml/DcbEventLogStorage_Postgres.res +1 -1
- package/lib/ocaml/DcbEventLogStorage_Postgres_Ops-ReventlessPostgres.cmi +0 -0
- package/lib/ocaml/DcbEventLogStorage_Postgres_Ops-ReventlessPostgres.cmj +0 -0
- package/lib/ocaml/DcbEventLogStorage_Postgres_Ops-ReventlessPostgres.cmt +0 -0
- package/lib/ocaml/DcbEventLogStorage_Postgres_Ops.ast +0 -0
- package/lib/ocaml/DcbEventLogStorage_Postgres_Ops.res +112 -42
- package/lib/ocaml/EventLogChangeFeed-ReventlessPostgres.cmi +0 -0
- package/lib/ocaml/EventLogChangeFeed-ReventlessPostgres.cmt +0 -0
- package/lib/ocaml/EventLogChangeFeed.ast +0 -0
- package/lib/ocaml/EventLogChangeFeed.res +15 -6
- package/lib/ocaml/EventLogStorage_Postgres-ReventlessPostgres.cmi +0 -0
- package/lib/ocaml/EventLogStorage_Postgres-ReventlessPostgres.cmj +0 -0
- package/lib/ocaml/EventLogStorage_Postgres-ReventlessPostgres.cmt +0 -0
- package/lib/ocaml/EventLogStorage_Postgres.ast +0 -0
- package/lib/ocaml/EventLogStorage_Postgres.res +1 -1
- package/lib/ocaml/EventLogStorage_Postgres_Ops-ReventlessPostgres.cmi +0 -0
- package/lib/ocaml/EventLogStorage_Postgres_Ops-ReventlessPostgres.cmj +0 -0
- package/lib/ocaml/EventLogStorage_Postgres_Ops-ReventlessPostgres.cmt +0 -0
- package/lib/ocaml/EventLogStorage_Postgres_Ops.ast +0 -0
- package/lib/ocaml/EventLogStorage_Postgres_Ops.res +74 -14
- package/lib/ocaml/PgChangeFeed-ReventlessPostgres.cmi +0 -0
- package/lib/ocaml/PgChangeFeed-ReventlessPostgres.cmt +0 -0
- package/lib/ocaml/PgChangeFeed.ast +0 -0
- package/lib/ocaml/PgChangeFeed.res +21 -9
- package/lib/ocaml/PgDriver-ReventlessPostgres.cmi +0 -0
- package/lib/ocaml/PgDriver-ReventlessPostgres.cmt +0 -0
- package/lib/ocaml/PgDriver-ReventlessPostgres.cmti +0 -0
- package/lib/ocaml/PgDriver.ast +0 -0
- package/lib/ocaml/PgDriver.iast +0 -0
- package/lib/ocaml/PgDriver.res +1 -2
- package/lib/ocaml/PgSchema-ReventlessPostgres.cmi +0 -0
- package/lib/ocaml/PgSchema-ReventlessPostgres.cmt +0 -0
- package/lib/ocaml/PgSchema.ast +0 -0
- package/lib/ocaml/PgSchema.res +5 -0
- package/lib/ocaml/PostgresIntegrationTest.res +157 -104
- package/lib/ocaml/QueryDbStorage_Postgres-ReventlessPostgres.cmi +0 -0
- package/lib/ocaml/QueryDbStorage_Postgres-ReventlessPostgres.cmt +0 -0
- package/lib/ocaml/QueryDbStorage_Postgres.ast +0 -0
- package/lib/ocaml/QueryDbStorage_Postgres.res +7 -2
- package/lib/ocaml/QueryDbStorage_Postgres_Ops-ReventlessPostgres.cmi +0 -0
- package/lib/ocaml/QueryDbStorage_Postgres_Ops-ReventlessPostgres.cmt +0 -0
- package/lib/ocaml/QueryDbStorage_Postgres_Ops.ast +0 -0
- package/lib/ocaml/QueryDbStorage_Postgres_Ops.res +14 -14
- package/lib/ocaml/QueryEnginePostgres-ReventlessPostgres.cmi +0 -0
- package/lib/ocaml/QueryEnginePostgres-ReventlessPostgres.cmt +0 -0
- package/lib/ocaml/QueryEnginePostgres.ast +0 -0
- package/lib/ocaml/QueryEnginePostgres.res +141 -42
- package/lib/ocaml/QueryEnginePostgresListPageTest.res +29 -6
- package/lib/ocaml/ReventlessPostgres.cmt +0 -0
- package/package.json +13 -13
- package/src/DcbEventLogStorage_Postgres.res +1 -1
- package/src/DcbEventLogStorage_Postgres.res.mjs +6 -0
- package/src/DcbEventLogStorage_Postgres_Ops.res +112 -42
- package/src/DcbEventLogStorage_Postgres_Ops.res.mjs +57 -4
- package/src/EventLogChangeFeed.res +15 -6
- package/src/EventLogChangeFeed.res.mjs +3 -3
- package/src/EventLogStorage_Postgres.res +1 -1
- package/src/EventLogStorage_Postgres.res.mjs +3 -0
- package/src/EventLogStorage_Postgres_Ops.res +74 -14
- package/src/EventLogStorage_Postgres_Ops.res.mjs +31 -0
- package/src/PgChangeFeed.res +21 -9
- package/src/PgChangeFeed.res.mjs +6 -6
- package/src/PgDriver.res +1 -2
- package/src/PgSchema.res +5 -0
- package/src/PgSchema.res.mjs +3 -0
- package/src/QueryDbStorage_Postgres.res +7 -2
- package/src/QueryDbStorage_Postgres.res.mjs +1 -1
- package/src/QueryDbStorage_Postgres_Ops.res +14 -14
- package/src/QueryEnginePostgres.res +141 -42
- package/src/QueryEnginePostgres.res.mjs +53 -30
- package/tests/PostgresIntegrationTest.res +157 -104
- package/tests/QueryEnginePostgresListPageTest.res +29 -6
- package/tests/PostgresIntegrationTest.res.mjs +0 -377
- package/tests/QueryEnginePostgresListPageTest.res.mjs +0 -461
|
@@ -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
|
-
|
|
49
|
-
|
|
50
|
-
|
|
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
|
|
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
|
-
(
|
|
110
|
-
|
|
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
|
-
|
|
114
|
-
|
|
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(
|
|
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(() =>
|
|
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 */
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -18,7 +18,12 @@
|
|
|
18
18
|
open ReventlessCore
|
|
19
19
|
open Reventless
|
|
20
20
|
|
|
21
|
-
module
|
|
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(
|
|
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
|
-
|
|
63
|
-
|
|
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<
|
|
104
|
-
|
|
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(
|
|
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 {
|
|
@@ -2,15 +2,15 @@
|
|
|
2
2
|
|
|
3
3
|
import * as Stdlib_Option from "@rescript/runtime/lib/es6/Stdlib_Option.js";
|
|
4
4
|
import * as PgDriver$ReventlessPostgres from "./PgDriver.res.mjs";
|
|
5
|
-
import * as
|
|
5
|
+
import * as DcbEventLogStorage_Postgres_Ops$ReventlessPostgres from "./DcbEventLogStorage_Postgres_Ops.res.mjs";
|
|
6
6
|
|
|
7
7
|
async function readBatch(pool, logName, after, limitOpt) {
|
|
8
8
|
let limit = limitOpt !== undefined ? limitOpt : 500;
|
|
9
|
-
let b =
|
|
10
|
-
let where =
|
|
11
|
-
let sql = `SELECT ` +
|
|
9
|
+
let b = DcbEventLogStorage_Postgres_Ops$ReventlessPostgres.mkBuilder();
|
|
10
|
+
let where = DcbEventLogStorage_Postgres_Ops$ReventlessPostgres.buildReadWhere(b, logName, [], Stdlib_Option.flatMap(after, DcbEventLogStorage_Postgres_Ops$ReventlessPostgres.decodeCursor), true);
|
|
11
|
+
let sql = `SELECT ` + DcbEventLogStorage_Postgres_Ops$ReventlessPostgres.selectColumns + ` FROM dcb_event WHERE ` + where + ` ORDER BY transaction_id ASC, position ASC LIMIT ` + limit.toString();
|
|
12
12
|
let rows = await PgDriver$ReventlessPostgres.query(pool, sql, b.params);
|
|
13
|
-
let events = rows.map(
|
|
13
|
+
let events = rows.map(DcbEventLogStorage_Postgres_Ops$ReventlessPostgres.rowToEvent);
|
|
14
14
|
let last = events[events.length - 1 | 0];
|
|
15
15
|
let cursor = last !== undefined ? last.position : undefined;
|
|
16
16
|
return {
|
|
@@ -31,7 +31,7 @@ async function loadCheckpoint(pool, subscriber) {
|
|
|
31
31
|
}
|
|
32
32
|
|
|
33
33
|
async function saveCheckpoint(pool, subscriber, cursor) {
|
|
34
|
-
let match =
|
|
34
|
+
let match = DcbEventLogStorage_Postgres_Ops$ReventlessPostgres.decodeCursor(cursor);
|
|
35
35
|
if (match !== undefined) {
|
|
36
36
|
await PgDriver$ReventlessPostgres.query(pool, "INSERT INTO dcb_subscription(subscriber, last_tx, last_position)\n VALUES ($1, $2::xid8, $3::bigint)\n ON CONFLICT (subscriber)\n DO UPDATE SET last_tx = EXCLUDED.last_tx, last_position = EXCLUDED.last_position", [
|
|
37
37
|
subscriber,
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
package/lib/bs/src/PgDriver.ast
CHANGED
|
Binary file
|
package/lib/bs/src/PgDriver.iast
CHANGED
|
Binary file
|
package/lib/bs/src/PgDriver.res
CHANGED
|
@@ -47,8 +47,7 @@ type queryResult = {rows: array<dict<JSON.t>>}
|
|
|
47
47
|
|
|
48
48
|
let query = async (pool, sql, params) => (await _query(pool, sql, params)).rows
|
|
49
49
|
|
|
50
|
-
let queryOne = async (pool, sql, params) =>
|
|
51
|
-
(await _query(pool, sql, params)).rows->Array.get(0)
|
|
50
|
+
let queryOne = async (pool, sql, params) => (await _query(pool, sql, params)).rows->Array.get(0)
|
|
52
51
|
|
|
53
52
|
let exec = async (pool, sql) => {
|
|
54
53
|
let _ = await _queryText(pool, sql)
|
|
Binary file
|
|
Binary file
|
package/lib/bs/src/PgSchema.ast
CHANGED
|
Binary file
|
package/lib/bs/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
|
];
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
// that pure module (so every existing `QueryDbStorage_Postgres.*` reference keeps
|
|
7
7
|
// working) and adds only the deploy-time `Pulumi.Output`-shaped `storage` record
|
|
8
8
|
// and the `Make` provider functor.
|
|
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.ReadModel
|
|
@@ -26,7 +26,11 @@ let makeStorage = (
|
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
// Standalone/deploy storage: inject the pool via a provider module.
|
|
29
|
-
module Make = (
|
|
29
|
+
module Make = (
|
|
30
|
+
P: {
|
|
31
|
+
let pool: PgDriver.pool
|
|
32
|
+
},
|
|
33
|
+
) => {
|
|
30
34
|
type api = unit
|
|
31
35
|
type role = unit
|
|
32
36
|
|
|
@@ -37,6 +41,7 @@ module Make = (P: {let pool: PgDriver.pool}) => {
|
|
|
37
41
|
~ttl as _=?,
|
|
38
42
|
~api as _,
|
|
39
43
|
~apiRole as _,
|
|
44
|
+
~owner as _,
|
|
40
45
|
~opts as _,
|
|
41
46
|
) => makeStorage(~pool=P.pool, ~name, ~indexes, ~subIdField)
|
|
42
47
|
}
|
|
@@ -12,7 +12,7 @@ function makeStorage(pool, name, indexes, subIdField) {
|
|
|
12
12
|
}
|
|
13
13
|
|
|
14
14
|
function Make(P) {
|
|
15
|
-
let make = (name, indexes, subIdField, param, param$1, param$2, param$3) => makeStorage(P.pool, name, indexes, subIdField);
|
|
15
|
+
let make = (name, indexes, subIdField, param, param$1, param$2, param$3, param$4) => makeStorage(P.pool, name, indexes, subIdField);
|
|
16
16
|
return {
|
|
17
17
|
make: make
|
|
18
18
|
};
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
// a deploy-time dep into its ESM import graph (which fails resolution on real
|
|
7
7
|
// Lambda). The thin `QueryDbStorage_Postgres` wrapper `include`s this and adds the
|
|
8
8
|
// deploy-time `Pulumi.Output`-shaped `storage` record + the `Make` functor.
|
|
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.ReadModel
|
|
@@ -14,14 +14,12 @@ open Reventless.ReadModel
|
|
|
14
14
|
let tableName = (name: string): string => "qdb_" ++ name->String.replaceAll("-", "_")
|
|
15
15
|
|
|
16
16
|
// Collapse anything outside [A-Za-z0-9_] to _ — predictable, injection-safe idents.
|
|
17
|
-
let sanitizeIdent = (s: string): string =>
|
|
18
|
-
s->String.replaceRegExp(%re("/[^A-Za-z0-9_]/g"), "_")
|
|
17
|
+
let sanitizeIdent = (s: string): string => s->String.replaceRegExp(/[^A-Za-z0-9_]/g, "_")
|
|
19
18
|
|
|
20
19
|
let notExpiredClause = "(expires_at IS NULL OR expires_at > extract(epoch from now()))"
|
|
21
20
|
|
|
22
21
|
// JSONB text extraction for a single top-level field.
|
|
23
|
-
let jsonField = (field: string): string =>
|
|
24
|
-
`(item->>'${field->String.replaceAll("'", "''")}')`
|
|
22
|
+
let jsonField = (field: string): string => `(item->>'${field->String.replaceAll("'", "''")}')`
|
|
25
23
|
|
|
26
24
|
let compositeExpr = (fields: array<string>, sep: string): string => {
|
|
27
25
|
let escapedSep = sep->String.replaceAll("'", "''")
|
|
@@ -120,10 +118,13 @@ let makeOperations = (
|
|
|
120
118
|
|
|
121
119
|
let rowsFor = async (id: string): array<JSON.t> => {
|
|
122
120
|
await ready()
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
121
|
+
|
|
122
|
+
(
|
|
123
|
+
await pool->PgDriver.query(
|
|
124
|
+
`SELECT item FROM ${table} WHERE partition_key = $1 AND ${notExpiredClause} ORDER BY sub_key ASC`,
|
|
125
|
+
[JSON.Encode.string(id)],
|
|
126
|
+
)
|
|
127
|
+
)->Array.map(decodeItem)
|
|
127
128
|
}
|
|
128
129
|
|
|
129
130
|
let saveOne = async (id: string, state: JSON.t, ttl: option<int>) => {
|
|
@@ -149,11 +150,10 @@ let makeOperations = (
|
|
|
149
150
|
|
|
150
151
|
let load: QueryDb.load<string, JSON.t> = async id => Ok(await rowsFor(id))
|
|
151
152
|
|
|
152
|
-
let loadStream: QueryDb.loadStream<string, JSON.t> =
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
->Stream.flatMap(arr => Stream.fromIterable(arr))
|
|
153
|
+
let loadStream: QueryDb.loadStream<string, JSON.t> = id =>
|
|
154
|
+
Effect.promise(() => rowsFor(id))
|
|
155
|
+
->Stream.fromEffect
|
|
156
|
+
->Stream.flatMap(arr => Stream.fromIterable(arr))
|
|
157
157
|
|
|
158
158
|
let save: QueryDb.save<string, JSON.t> = async (id, state, _saveMode, ttl) => {
|
|
159
159
|
await saveOne(id, state, ttl)
|
|
Binary file
|
|
Binary file
|
|
Binary file
|