@reventlessdev/reventless-postgres 3.0.0-alpha.6 → 3.0.0-alpha.7

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 (111) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/lib/bs/.compiler.log +2 -2
  3. package/lib/bs/ReventlessPostgres.cmi +0 -0
  4. package/lib/bs/ReventlessPostgres.cmt +0 -0
  5. package/lib/bs/ReventlessPostgres.mlmap +3 -0
  6. package/lib/bs/compiler-info.json +1 -1
  7. package/lib/bs/src/DcbEventLogStorage_Postgres-ReventlessPostgres.cmi +0 -0
  8. package/lib/bs/src/DcbEventLogStorage_Postgres-ReventlessPostgres.cmj +0 -0
  9. package/lib/bs/src/DcbEventLogStorage_Postgres-ReventlessPostgres.cmt +0 -0
  10. package/lib/bs/src/DcbEventLogStorage_Postgres.ast +0 -0
  11. package/lib/bs/src/DcbEventLogStorage_Postgres.res +25 -340
  12. package/lib/bs/src/DcbEventLogStorage_Postgres.res.mjs +32 -348
  13. package/lib/bs/src/DcbEventLogStorage_Postgres_Ops-ReventlessPostgres.cmi +0 -0
  14. package/lib/bs/src/DcbEventLogStorage_Postgres_Ops-ReventlessPostgres.cmj +0 -0
  15. package/lib/bs/src/DcbEventLogStorage_Postgres_Ops-ReventlessPostgres.cmt +0 -0
  16. package/lib/bs/src/DcbEventLogStorage_Postgres_Ops.ast +0 -0
  17. package/lib/bs/src/DcbEventLogStorage_Postgres_Ops.res +350 -0
  18. package/lib/bs/src/DcbEventLogStorage_Postgres_Ops.res.mjs +380 -0
  19. package/lib/bs/src/EventLogChangeFeed-ReventlessPostgres.cmi +0 -0
  20. package/lib/bs/src/EventLogChangeFeed-ReventlessPostgres.cmt +0 -0
  21. package/lib/bs/src/EventLogStorage_Postgres-ReventlessPostgres.cmi +0 -0
  22. package/lib/bs/src/EventLogStorage_Postgres-ReventlessPostgres.cmj +0 -0
  23. package/lib/bs/src/EventLogStorage_Postgres-ReventlessPostgres.cmt +0 -0
  24. package/lib/bs/src/EventLogStorage_Postgres.ast +0 -0
  25. package/lib/bs/src/EventLogStorage_Postgres.res +12 -170
  26. package/lib/bs/src/EventLogStorage_Postgres.res.mjs +20 -186
  27. package/lib/bs/src/EventLogStorage_Postgres_Ops-ReventlessPostgres.cmi +0 -0
  28. package/lib/bs/src/EventLogStorage_Postgres_Ops-ReventlessPostgres.cmj +0 -0
  29. package/lib/bs/src/EventLogStorage_Postgres_Ops-ReventlessPostgres.cmt +0 -0
  30. package/lib/bs/src/EventLogStorage_Postgres_Ops.ast +0 -0
  31. package/lib/bs/src/EventLogStorage_Postgres_Ops.res +180 -0
  32. package/lib/bs/src/EventLogStorage_Postgres_Ops.res.mjs +202 -0
  33. package/lib/bs/src/PgChangeFeed-ReventlessPostgres.cmi +0 -0
  34. package/lib/bs/src/PgChangeFeed-ReventlessPostgres.cmt +0 -0
  35. package/lib/bs/src/PgSchema-ReventlessPostgres.cmi +0 -0
  36. package/lib/bs/src/PgSchema-ReventlessPostgres.cmt +0 -0
  37. package/lib/bs/src/QueryDbStorage_Postgres-ReventlessPostgres.cmi +0 -0
  38. package/lib/bs/src/QueryDbStorage_Postgres-ReventlessPostgres.cmt +0 -0
  39. package/lib/bs/src/QueryDbStorage_Postgres.ast +0 -0
  40. package/lib/bs/src/QueryDbStorage_Postgres.res +9 -232
  41. package/lib/bs/src/QueryDbStorage_Postgres.res.mjs +25 -204
  42. package/lib/bs/src/QueryDbStorage_Postgres_Ops-ReventlessPostgres.cmi +0 -0
  43. package/lib/bs/src/QueryDbStorage_Postgres_Ops-ReventlessPostgres.cmj +0 -0
  44. package/lib/bs/src/QueryDbStorage_Postgres_Ops-ReventlessPostgres.cmt +0 -0
  45. package/lib/bs/src/QueryDbStorage_Postgres_Ops.ast +0 -0
  46. package/lib/bs/src/QueryDbStorage_Postgres_Ops.res +229 -0
  47. package/lib/bs/src/QueryDbStorage_Postgres_Ops.res.mjs +222 -0
  48. package/lib/bs/src/QueryEnginePostgres-ReventlessPostgres.cmi +0 -0
  49. package/lib/bs/src/QueryEnginePostgres-ReventlessPostgres.cmt +0 -0
  50. package/lib/bs/tests/PostgresIntegrationTest-ReventlessPostgres.cmi +0 -0
  51. package/lib/bs/tests/PostgresIntegrationTest-ReventlessPostgres.cmt +0 -0
  52. package/lib/bs/tests/QueryEnginePostgresListPageTest-ReventlessPostgres.cmi +0 -0
  53. package/lib/bs/tests/QueryEnginePostgresListPageTest-ReventlessPostgres.cmt +0 -0
  54. package/lib/ocaml/.compiler.log +2 -2
  55. package/lib/ocaml/DcbEventLogStorage_Postgres-ReventlessPostgres.cmi +0 -0
  56. package/lib/ocaml/DcbEventLogStorage_Postgres-ReventlessPostgres.cmj +0 -0
  57. package/lib/ocaml/DcbEventLogStorage_Postgres-ReventlessPostgres.cmt +0 -0
  58. package/lib/ocaml/DcbEventLogStorage_Postgres.ast +0 -0
  59. package/lib/ocaml/DcbEventLogStorage_Postgres.res +25 -340
  60. package/lib/ocaml/DcbEventLogStorage_Postgres_Ops-ReventlessPostgres.cmi +0 -0
  61. package/lib/ocaml/DcbEventLogStorage_Postgres_Ops-ReventlessPostgres.cmj +0 -0
  62. package/lib/ocaml/DcbEventLogStorage_Postgres_Ops-ReventlessPostgres.cmt +0 -0
  63. package/lib/ocaml/DcbEventLogStorage_Postgres_Ops.ast +0 -0
  64. package/lib/ocaml/DcbEventLogStorage_Postgres_Ops.res +350 -0
  65. package/lib/ocaml/EventLogChangeFeed-ReventlessPostgres.cmi +0 -0
  66. package/lib/ocaml/EventLogChangeFeed-ReventlessPostgres.cmt +0 -0
  67. package/lib/ocaml/EventLogStorage_Postgres-ReventlessPostgres.cmi +0 -0
  68. package/lib/ocaml/EventLogStorage_Postgres-ReventlessPostgres.cmj +0 -0
  69. package/lib/ocaml/EventLogStorage_Postgres-ReventlessPostgres.cmt +0 -0
  70. package/lib/ocaml/EventLogStorage_Postgres.ast +0 -0
  71. package/lib/ocaml/EventLogStorage_Postgres.res +12 -170
  72. package/lib/ocaml/EventLogStorage_Postgres_Ops-ReventlessPostgres.cmi +0 -0
  73. package/lib/ocaml/EventLogStorage_Postgres_Ops-ReventlessPostgres.cmj +0 -0
  74. package/lib/ocaml/EventLogStorage_Postgres_Ops-ReventlessPostgres.cmt +0 -0
  75. package/lib/ocaml/EventLogStorage_Postgres_Ops.ast +0 -0
  76. package/lib/ocaml/EventLogStorage_Postgres_Ops.res +180 -0
  77. package/lib/ocaml/PgChangeFeed-ReventlessPostgres.cmi +0 -0
  78. package/lib/ocaml/PgChangeFeed-ReventlessPostgres.cmt +0 -0
  79. package/lib/ocaml/PgSchema-ReventlessPostgres.cmi +0 -0
  80. package/lib/ocaml/PgSchema-ReventlessPostgres.cmt +0 -0
  81. package/lib/ocaml/PostgresIntegrationTest-ReventlessPostgres.cmi +0 -0
  82. package/lib/ocaml/PostgresIntegrationTest-ReventlessPostgres.cmt +0 -0
  83. package/lib/ocaml/QueryDbStorage_Postgres-ReventlessPostgres.cmi +0 -0
  84. package/lib/ocaml/QueryDbStorage_Postgres-ReventlessPostgres.cmt +0 -0
  85. package/lib/ocaml/QueryDbStorage_Postgres.ast +0 -0
  86. package/lib/ocaml/QueryDbStorage_Postgres.res +9 -232
  87. package/lib/ocaml/QueryDbStorage_Postgres_Ops-ReventlessPostgres.cmi +0 -0
  88. package/lib/ocaml/QueryDbStorage_Postgres_Ops-ReventlessPostgres.cmj +0 -0
  89. package/lib/ocaml/QueryDbStorage_Postgres_Ops-ReventlessPostgres.cmt +0 -0
  90. package/lib/ocaml/QueryDbStorage_Postgres_Ops.ast +0 -0
  91. package/lib/ocaml/QueryDbStorage_Postgres_Ops.res +229 -0
  92. package/lib/ocaml/QueryEnginePostgres-ReventlessPostgres.cmi +0 -0
  93. package/lib/ocaml/QueryEnginePostgres-ReventlessPostgres.cmt +0 -0
  94. package/lib/ocaml/QueryEnginePostgresListPageTest-ReventlessPostgres.cmi +0 -0
  95. package/lib/ocaml/QueryEnginePostgresListPageTest-ReventlessPostgres.cmt +0 -0
  96. package/lib/ocaml/ReventlessPostgres.cmi +0 -0
  97. package/lib/ocaml/ReventlessPostgres.cmt +0 -0
  98. package/lib/ocaml/ReventlessPostgres.mlmap +3 -0
  99. package/package.json +3 -3
  100. package/src/DcbEventLogStorage_Postgres.res +25 -340
  101. package/src/DcbEventLogStorage_Postgres.res.mjs +32 -348
  102. package/src/DcbEventLogStorage_Postgres_Ops.res +350 -0
  103. package/src/DcbEventLogStorage_Postgres_Ops.res.mjs +380 -0
  104. package/src/EventLogStorage_Postgres.res +12 -170
  105. package/src/EventLogStorage_Postgres.res.mjs +20 -186
  106. package/src/EventLogStorage_Postgres_Ops.res +180 -0
  107. package/src/EventLogStorage_Postgres_Ops.res.mjs +202 -0
  108. package/src/QueryDbStorage_Postgres.res +9 -232
  109. package/src/QueryDbStorage_Postgres.res.mjs +25 -204
  110. package/src/QueryDbStorage_Postgres_Ops.res +229 -0
  111. package/src/QueryDbStorage_Postgres_Ops.res.mjs +222 -0
@@ -0,0 +1,180 @@
1
+ // Runtime-pure classic (OCC) event-log operations for the Postgres backend.
2
+ //
3
+ // This module holds everything the DEPLOYED Lambda runtime needs and imports NO
4
+ // `@pulumi/pulumi` — deploy-time-only. The deployed aggregate entry point pulls
5
+ // these ops in unconditionally at cold start, so any deploy-time dependency here
6
+ // leaks into the Lambda import graph and fails resolution (`@pulumi/pulumi` exists
7
+ // in no Lambda — not the layer, not `/var/runtime`). The thin
8
+ // `EventLogStorage_Postgres` wrapper adds the deploy-time `Pulumi.Output`-shaped
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).
11
+
12
+ open ReventlessCore
13
+
14
+ // Injected by reventless-local to feed the projection-checkpoint low-watermark
15
+ // (ProjectionPending, aggregate axis). Standalone/deploy use passes nothing.
16
+ type onAppended = array<(string, int)> => unit
17
+ let noTracking: onAppended = _ => ()
18
+
19
+ let decodePayload = (row: dict<JSON.t>): JSON.t =>
20
+ row->Dict.get("payload")->Option.getOr(JSON.Encode.null)
21
+
22
+ let coerceInt = (v: JSON.t): int =>
23
+ switch v {
24
+ | JSON.Number(n) => Float.toInt(n)
25
+ | JSON.String(s) => s->Int.fromString->Option.getOr(0)
26
+ | _ => 0
27
+ }
28
+
29
+ // Total events across all aggregates. Seeds the event-tap counter at startup so
30
+ // timeline numbering continues across restarts.
31
+ let countAll = async (pool: PgDriver.pool): int =>
32
+ switch await pool->PgDriver.queryOne("SELECT COUNT(*)::bigint AS c FROM event_log", []) {
33
+ | Some(row) => row->Dict.get("c")->Option.map(coerceInt)->Option.getOr(0)
34
+ | None => 0
35
+ }
36
+
37
+ let isUniqueViolation = (exn: exn): bool => {
38
+ let msg = exn->JsExn.fromException->Option.flatMap(JsExn.message)->Option.getOr("")
39
+ // pg surfaces the SQLSTATE on `.code`; the message also carries the constraint.
40
+ msg->String.includes("duplicate key") ||
41
+ msg->String.includes("unique") ||
42
+ msg->String.includes("23505")
43
+ }
44
+
45
+ // Builds the classic runtime op set bound to `pool`. Returns `(name, ops)`; the
46
+ // 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) => {
53
+ let appendTracked = async (~track, seqNr: int, id: string, jsons: array<JSON.t>) => {
54
+ // Empty append is a legitimate no-op (at-least-once retries, idempotent
55
+ // commands), NOT a conflict — return before the guarded insert whose 0-row
56
+ // result would otherwise be read as a conflict.
57
+ if jsons->Array.length == 0 {
58
+ Ok()
59
+ } else {
60
+ try {
61
+ let rows = await pool->PgDriver.query(
62
+ "INSERT INTO event_log(log_name, aggregate_id, seq_nr, payload, msg_id)
63
+ SELECT $1, $2, $3::bigint + (ord - 1), value, value->>'msgId'
64
+ FROM jsonb_array_elements($4::jsonb) WITH ORDINALITY AS t(value, ord)
65
+ WHERE $3::bigint = (SELECT COALESCE(MAX(seq_nr), -1) + 1
66
+ FROM event_log
67
+ WHERE log_name = $1 AND aggregate_id = $2)
68
+ RETURNING global_seq, msg_id",
69
+ [
70
+ JSON.Encode.string(name),
71
+ JSON.Encode.string(id),
72
+ JSON.Encode.int(seqNr),
73
+ JSON.Encode.string(JSON.stringify(JSON.Array(jsons))),
74
+ ],
75
+ )
76
+ if rows->Array.length == 0 {
77
+ // WHERE guard rejected the seq — a genuine OCC conflict (gap or stale).
78
+ Error(EventLog.Conflict)
79
+ } else {
80
+ if track {
81
+ onAppended(
82
+ rows->Array.filterMap(row =>
83
+ switch (row->Dict.get("msg_id"), row->Dict.get("global_seq")) {
84
+ | (Some(JSON.String(msgId)), Some(gseq)) => Some((msgId, coerceInt(gseq)))
85
+ | _ => None
86
+ }
87
+ ),
88
+ )
89
+ }
90
+ Ok()
91
+ }
92
+ } catch {
93
+ | exn =>
94
+ isUniqueViolation(exn)
95
+ ? Error(EventLog.Conflict)
96
+ : Error(
97
+ EventLog.StorageFailure(
98
+ exn->JsExn.fromException->Option.flatMap(JsExn.message)->Option.getOr("storage error"),
99
+ ),
100
+ )
101
+ }
102
+ }
103
+ }
104
+
105
+ let append: EventLog.append<string, JSON.t> = (seqNr, id, jsons) =>
106
+ appendTracked(~track=true, seqNr, id, jsons)
107
+
108
+ let replayArray = async (id: string, ~fromSeq=0): array<JSON.t> =>
109
+ (await pool->PgDriver.query(
110
+ "SELECT payload FROM event_log
111
+ WHERE log_name = $1 AND aggregate_id = $2 AND seq_nr >= $3::bigint
112
+ ORDER BY seq_nr ASC",
113
+ [JSON.Encode.string(name), JSON.Encode.string(id), JSON.Encode.int(fromSeq)],
114
+ ))->Array.map(decodePayload)
115
+
116
+ let replay: EventLog.replay<string, JSON.t> = id => replayArray(id)
117
+
118
+ let replayStream = (id, ~fromSeq=?) =>
119
+ Effect.promise(() => replayArray(id, ~fromSeq?))
120
+ ->Stream.fromEffect
121
+ ->Stream.flatMap(arr => Stream.fromIterable(arr))
122
+
123
+ let latestSnapshot: EventLog.latestSnapshot<string> = async id =>
124
+ switch await pool->PgDriver.queryOne(
125
+ "SELECT seq_nr, state, schema_hash FROM snapshot WHERE log_name = $1 AND aggregate_id = $2",
126
+ [JSON.Encode.string(name), JSON.Encode.string(id)],
127
+ ) {
128
+ | Some(row) =>
129
+ switch (row->Dict.get("seq_nr"), row->Dict.get("state"), row->Dict.get("schema_hash")) {
130
+ | (Some(seq), Some(state), Some(JSON.String(schemaHash))) =>
131
+ Ok(Some({EventLog.seqNr: coerceInt(seq), state, schemaHash}))
132
+ | _ => Error("snapshot row has unexpected shape")
133
+ }
134
+ | None => Ok(None)
135
+ }
136
+
137
+ let writeSnapshot: EventLog.writeSnapshot<string> = async (id, snap) =>
138
+ try {
139
+ let _ = await pool->PgDriver.query(
140
+ "INSERT INTO snapshot(log_name, aggregate_id, seq_nr, state, schema_hash)
141
+ VALUES ($1, $2, $3::bigint, $4::jsonb, $5)
142
+ ON CONFLICT (log_name, aggregate_id)
143
+ DO UPDATE SET seq_nr = EXCLUDED.seq_nr, state = EXCLUDED.state, schema_hash = EXCLUDED.schema_hash",
144
+ [
145
+ JSON.Encode.string(name),
146
+ JSON.Encode.string(id),
147
+ JSON.Encode.int(snap.EventLog.seqNr),
148
+ JSON.Encode.string(JSON.stringify(snap.state)),
149
+ JSON.Encode.string(snap.schemaHash),
150
+ ],
151
+ )
152
+ Ok()
153
+ } catch {
154
+ | exn =>
155
+ Error(exn->JsExn.fromException->Option.flatMap(JsExn.message)->Option.getOr("snapshot write error"))
156
+ }
157
+
158
+ let appendStream: EventLog.appendStream<string, JSON.t> = (startingSeqNr, id, stream) =>
159
+ stream
160
+ ->Stream.runCollect
161
+ ->Effect.flatMap(jsons =>
162
+ Effect.promise(() => appendTracked(~track=false, startingSeqNr, id, jsons))->Effect.flatMap(result =>
163
+ switch result {
164
+ | Ok() => Effect.succeed()
165
+ | Error(EventLog.Conflict) => Effect.fail("conflict")
166
+ | Error(StorageFailure(msg)) => Effect.fail(msg)
167
+ }
168
+ )
169
+ )
170
+
171
+ let ops: EventLog_Adapter.operations = {
172
+ append,
173
+ replay,
174
+ replayStream,
175
+ appendStream,
176
+ latestSnapshot,
177
+ writeSnapshot,
178
+ }
179
+ (name, ops)
180
+ }
@@ -1,239 +1,16 @@
1
- // Postgres-backed QueryDb storage JSONB document tables, one per read model.
1
+ // Deploy/local wrapper over the runtime-pure QueryDbStorage_Postgres_Ops.
2
2
  //
3
- // Implements the clean QueryDb_Adapter.storageMaker (the shape
4
- // QueryDbStorage_DynamoDb implements): no in-process bus. The reventless-local
5
- // bridge that publishes state-changes to live GraphQL subscriptions wraps this
6
- // at integration time (Phase E), the way QueryDbStorage_Sqlite wraps its data
7
- // access today.
8
- //
9
- // Table `qdb_<name>`: partition_key text, sub_key text, item jsonb, expires_at
10
- // double precision (unix epoch seconds; null = never), PRIMARY KEY
11
- // (partition_key, sub_key). `item jsonb` returns already parsed. TTL is lazy —
12
- // every read carries `(expires_at IS NULL OR expires_at > epoch_now)`.
13
- //
14
- // GSI: each declared indexConfig becomes an expression index on `item->>'field'`
15
- // (composite keys concatenated with the configured separator). projectionType is
16
- // irrelevant in a relational store (every column is on the same row).
3
+ // `makeOperations` + all SQL helpers live in QueryDbStorage_Postgres_Ops, which
4
+ // imports NO `@pulumi/pulumi` so the deployed-Lambda runtime binds ops without
5
+ // dragging a deploy-time dep into its ESM import graph. This module `include`s
6
+ // that pure module (so every existing `QueryDbStorage_Postgres.*` reference keeps
7
+ // working) and adds only the deploy-time `Pulumi.Output`-shaped `storage` record
8
+ // and the `Make` provider functor.
9
+ // See docs/plans/deployed-lambda-esm-self-containment.md (Rung-3 finding).
17
10
 
18
11
  open ReventlessCore
19
12
  open Reventless.ReadModel
20
-
21
- let tableName = (name: string): string => "qdb_" ++ name->String.replaceAll("-", "_")
22
-
23
- // Collapse anything outside [A-Za-z0-9_] to _ — predictable, injection-safe idents.
24
- let sanitizeIdent = (s: string): string =>
25
- s->String.replaceRegExp(%re("/[^A-Za-z0-9_]/g"), "_")
26
-
27
- let notExpiredClause = "(expires_at IS NULL OR expires_at > extract(epoch from now()))"
28
-
29
- // JSONB text extraction for a single top-level field.
30
- let jsonField = (field: string): string =>
31
- `(item->>'${field->String.replaceAll("'", "''")}')`
32
-
33
- let compositeExpr = (fields: array<string>, sep: string): string => {
34
- let escapedSep = sep->String.replaceAll("'", "''")
35
- fields->Array.map(jsonField)->Array.join(` || '${escapedSep}' || `)
36
- }
37
-
38
- let indexExpressions = (idx: indexConfig): (string, option<string>) => {
39
- let pkExpr = switch idx.pkFields {
40
- | Some(fields) if fields->Array.length > 0 => compositeExpr(fields, idx.pkSep->Option.getOr("/"))
41
- | _ =>
42
- switch idx.idField {
43
- | Some(field) => jsonField(field)
44
- | None => jsonField(idx.index)
45
- }
46
- }
47
- let skExpr = switch idx.skFields {
48
- | Some(fields) if fields->Array.length > 0 =>
49
- Some(compositeExpr(fields, idx.skSep->Option.getOr("/")))
50
- | _ =>
51
- switch idx.subIdField {
52
- | Some(field) => Some(jsonField(field))
53
- | None => None
54
- }
55
- }
56
- (pkExpr, skExpr)
57
- }
58
-
59
- let ensureTable = async (~pool, ~table) => {
60
- await pool->PgDriver.exec(
61
- `CREATE TABLE IF NOT EXISTS ${table} (partition_key text NOT NULL, sub_key text NOT NULL DEFAULT '', item jsonb NOT NULL, expires_at double precision, PRIMARY KEY (partition_key, sub_key))`,
62
- )
63
- }
64
-
65
- let ensureIndexes = async (~pool, ~table, ~indexes: array<indexConfig>) => {
66
- for i in 0 to indexes->Array.length - 1 {
67
- let idx = indexes->Array.getUnsafe(i)
68
- let (pkExpr, skExprOpt) = indexExpressions(idx)
69
- let indexName = `idx_${table}_${sanitizeIdent(idx.index)}`
70
- let cols = switch skExprOpt {
71
- | Some(skExpr) => `${pkExpr}, ${skExpr}`
72
- | None => pkExpr
73
- }
74
- await pool->PgDriver.exec(`CREATE INDEX IF NOT EXISTS ${indexName} ON ${table} (${cols})`)
75
- }
76
- }
77
-
78
- let decodeItem = (row: dict<JSON.t>): JSON.t =>
79
- row->Dict.get("item")->Option.getOr(JSON.Encode.null)
80
-
81
- let computeSubKey = (item: JSON.t, subIdField: option<string>): string =>
82
- switch subIdField {
83
- | None => ""
84
- | Some(field) =>
85
- switch item->JSON.Decode.object->Option.flatMap(o => o->Dict.get(field)) {
86
- | Some(JSON.String(s)) => s
87
- | _ => ""
88
- }
89
- }
90
-
91
- let withId = (item: JSON.t, partitionKey: string): JSON.t =>
92
- switch item->JSON.Decode.object {
93
- | Some(obj) if obj->Dict.get("id")->Option.isNone =>
94
- let copy = Dict.copy(obj)
95
- copy->Dict.set("id", JSON.Encode.string(partitionKey))
96
- JSON.Encode.object(copy)
97
- | _ => item
98
- }
99
-
100
- // Plain operation set for a read model's qdb table. Exposed separately from
101
- // `makeStorage` so the deployed-Lambda runtime (reventless-aws) can bind ops
102
- // without touching the Pulumi-shaped `storage` record.
103
- let makeOperations = (
104
- ~pool: PgDriver.pool,
105
- ~name: string,
106
- ~indexes: array<indexConfig>,
107
- ~subIdField: option<string>,
108
- ): QueryDb_Adapter.operations => {
109
- let table = tableName(name)
110
- // Schema setup is async and LAZY: kicked off by the first operation, not at
111
- // construction. A deploy-time caller may build the ops on a machine that
112
- // cannot reach the database (VPC-private RDS) — only the runtime, which
113
- // actually calls an operation, may open a connection. Subsequent operations
114
- // await the same promise (table + index creation runs exactly once).
115
- let readyRef: ref<option<promise<unit>>> = ref(None)
116
- let ready = () =>
117
- switch readyRef.contents {
118
- | Some(p) => p
119
- | None =>
120
- let p = {
121
- open Promise
122
- ensureTable(~pool, ~table)->then(() => ensureIndexes(~pool, ~table, ~indexes))
123
- }
124
- readyRef := Some(p)
125
- p
126
- }
127
-
128
- let rowsFor = async (id: string): array<JSON.t> => {
129
- await ready()
130
- (await pool->PgDriver.query(
131
- `SELECT item FROM ${table} WHERE partition_key = $1 AND ${notExpiredClause} ORDER BY sub_key ASC`,
132
- [JSON.Encode.string(id)],
133
- ))->Array.map(decodeItem)
134
- }
135
-
136
- let saveOne = async (id: string, state: JSON.t, ttl: option<int>) => {
137
- await ready()
138
- let subKey = computeSubKey(state, subIdField)
139
- let expiresAt = switch ttl {
140
- | Some(t) => JSON.Encode.int(t)
141
- | None => JSON.Encode.null
142
- }
143
- let _ = await pool->PgDriver.query(
144
- `INSERT INTO ${table}(partition_key, sub_key, item, expires_at)
145
- VALUES ($1, $2, $3::jsonb, $4)
146
- ON CONFLICT (partition_key, sub_key)
147
- DO UPDATE SET item = EXCLUDED.item, expires_at = EXCLUDED.expires_at`,
148
- [
149
- JSON.Encode.string(id),
150
- JSON.Encode.string(subKey),
151
- JSON.Encode.string(JSON.stringify(state)),
152
- expiresAt,
153
- ],
154
- )
155
- }
156
-
157
- let load: QueryDb.load<string, JSON.t> = async id => Ok(await rowsFor(id))
158
-
159
- let loadStream: QueryDb.loadStream<string, JSON.t> =
160
- id =>
161
- Effect.promise(() => rowsFor(id))
162
- ->Stream.fromEffect
163
- ->Stream.flatMap(arr => Stream.fromIterable(arr))
164
-
165
- let save: QueryDb.save<string, JSON.t> = async (id, state, _saveMode, ttl) => {
166
- await saveOne(id, state, ttl)
167
- Ok()
168
- }
169
-
170
- let saveBatch: QueryDb.saveBatch<string, JSON.t> = async batch => {
171
- // pg has no synchronous multi-statement transaction helper here; the upserts
172
- // are independent (distinct keys) so a sequential apply is equivalent.
173
- for i in 0 to batch->Array.length - 1 {
174
- let (id, state, ttl) = batch->Array.getUnsafe(i)
175
- await saveOne(id, state, ttl)
176
- }
177
- Ok()
178
- }
179
-
180
- // Mirror DynamoDB's `ADD #field :inc`: read the counter on the partition item
181
- // (counters are single-state, sub_key=""), add inc, upsert, return NEW total.
182
- let count: QueryDb.count<string> = async (id, fieldName, inc) => {
183
- await ready()
184
- let existing = (await rowsFor(id))->Array.get(0)->Option.flatMap(JSON.Decode.object)
185
- let current =
186
- existing
187
- ->Option.flatMap(o => o->Dict.get(fieldName))
188
- ->Option.flatMap(JSON.Decode.float)
189
- ->Option.mapOr(0, Float.toInt)
190
- let next = current + inc
191
- let obj = switch existing {
192
- | Some(o) => Dict.copy(o)
193
- | None => Dict.make()
194
- }
195
- obj->Dict.set("id", JSON.Encode.string(id))
196
- obj->Dict.set(fieldName, JSON.Encode.int(next))
197
- await saveOne(id, JSON.Encode.object(obj), None)
198
- Ok(next)
199
- }
200
-
201
- let delete: QueryDb.delete<string> = async (id, subIdOpt) => {
202
- await ready()
203
- let _ = switch subIdOpt {
204
- | None =>
205
- await pool->PgDriver.query(
206
- `DELETE FROM ${table} WHERE partition_key = $1`,
207
- [JSON.Encode.string(id)],
208
- )
209
- | Some((_, subValue)) =>
210
- await pool->PgDriver.query(
211
- `DELETE FROM ${table} WHERE partition_key = $1 AND sub_key = $2`,
212
- [JSON.Encode.string(id), JSON.Encode.string(subValue)],
213
- )
214
- }
215
- Ok()
216
- }
217
-
218
- let deleteBatch: QueryDb.deleteBatch<string> = async ids => {
219
- for i in 0 to ids->Array.length - 1 {
220
- let (id, subIdOpt) = ids->Array.getUnsafe(i)
221
- let _ = await delete(id, subIdOpt)
222
- }
223
- Ok()
224
- }
225
-
226
- let ops: QueryDb_Adapter.operations = {
227
- load,
228
- loadStream,
229
- save,
230
- saveBatch,
231
- count,
232
- delete,
233
- deleteBatch,
234
- }
235
- ops
236
- }
13
+ include QueryDbStorage_Postgres_Ops
237
14
 
238
15
  let makeStorage = (
239
16
  ~pool: PgDriver.pool,
@@ -0,0 +1,229 @@
1
+ // Runtime-pure QueryDb operations for the Postgres backend — JSONB document
2
+ // tables, one per read model.
3
+ //
4
+ // Holds the operation set + all SQL helpers and imports NO `@pulumi/pulumi`
5
+ // (deploy-time only), so the deployed-Lambda runtime can bind ops without dragging
6
+ // a deploy-time dep into its ESM import graph (which fails resolution on real
7
+ // Lambda). The thin `QueryDbStorage_Postgres` wrapper `include`s this and adds the
8
+ // deploy-time `Pulumi.Output`-shaped `storage` record + the `Make` functor.
9
+ // See docs/plans/deployed-lambda-esm-self-containment.md (Rung-3 finding).
10
+
11
+ open ReventlessCore
12
+ open Reventless.ReadModel
13
+
14
+ let tableName = (name: string): string => "qdb_" ++ name->String.replaceAll("-", "_")
15
+
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"), "_")
19
+
20
+ let notExpiredClause = "(expires_at IS NULL OR expires_at > extract(epoch from now()))"
21
+
22
+ // JSONB text extraction for a single top-level field.
23
+ let jsonField = (field: string): string =>
24
+ `(item->>'${field->String.replaceAll("'", "''")}')`
25
+
26
+ let compositeExpr = (fields: array<string>, sep: string): string => {
27
+ let escapedSep = sep->String.replaceAll("'", "''")
28
+ fields->Array.map(jsonField)->Array.join(` || '${escapedSep}' || `)
29
+ }
30
+
31
+ let indexExpressions = (idx: indexConfig): (string, option<string>) => {
32
+ let pkExpr = switch idx.pkFields {
33
+ | Some(fields) if fields->Array.length > 0 => compositeExpr(fields, idx.pkSep->Option.getOr("/"))
34
+ | _ =>
35
+ switch idx.idField {
36
+ | Some(field) => jsonField(field)
37
+ | None => jsonField(idx.index)
38
+ }
39
+ }
40
+ let skExpr = switch idx.skFields {
41
+ | Some(fields) if fields->Array.length > 0 =>
42
+ Some(compositeExpr(fields, idx.skSep->Option.getOr("/")))
43
+ | _ =>
44
+ switch idx.subIdField {
45
+ | Some(field) => Some(jsonField(field))
46
+ | None => None
47
+ }
48
+ }
49
+ (pkExpr, skExpr)
50
+ }
51
+
52
+ let ensureTable = async (~pool, ~table) => {
53
+ await pool->PgDriver.exec(
54
+ `CREATE TABLE IF NOT EXISTS ${table} (partition_key text NOT NULL, sub_key text NOT NULL DEFAULT '', item jsonb NOT NULL, expires_at double precision, PRIMARY KEY (partition_key, sub_key))`,
55
+ )
56
+ }
57
+
58
+ let ensureIndexes = async (~pool, ~table, ~indexes: array<indexConfig>) => {
59
+ for i in 0 to indexes->Array.length - 1 {
60
+ let idx = indexes->Array.getUnsafe(i)
61
+ let (pkExpr, skExprOpt) = indexExpressions(idx)
62
+ let indexName = `idx_${table}_${sanitizeIdent(idx.index)}`
63
+ let cols = switch skExprOpt {
64
+ | Some(skExpr) => `${pkExpr}, ${skExpr}`
65
+ | None => pkExpr
66
+ }
67
+ await pool->PgDriver.exec(`CREATE INDEX IF NOT EXISTS ${indexName} ON ${table} (${cols})`)
68
+ }
69
+ }
70
+
71
+ let decodeItem = (row: dict<JSON.t>): JSON.t =>
72
+ row->Dict.get("item")->Option.getOr(JSON.Encode.null)
73
+
74
+ let computeSubKey = (item: JSON.t, subIdField: option<string>): string =>
75
+ switch subIdField {
76
+ | None => ""
77
+ | Some(field) =>
78
+ switch item->JSON.Decode.object->Option.flatMap(o => o->Dict.get(field)) {
79
+ | Some(JSON.String(s)) => s
80
+ | _ => ""
81
+ }
82
+ }
83
+
84
+ let withId = (item: JSON.t, partitionKey: string): JSON.t =>
85
+ switch item->JSON.Decode.object {
86
+ | Some(obj) if obj->Dict.get("id")->Option.isNone =>
87
+ let copy = Dict.copy(obj)
88
+ copy->Dict.set("id", JSON.Encode.string(partitionKey))
89
+ JSON.Encode.object(copy)
90
+ | _ => item
91
+ }
92
+
93
+ // Plain operation set for a read model's qdb table. Exposed separately from
94
+ // `makeStorage` so the deployed-Lambda runtime (reventless-aws) can bind ops
95
+ // without touching the Pulumi-shaped `storage` record.
96
+ let makeOperations = (
97
+ ~pool: PgDriver.pool,
98
+ ~name: string,
99
+ ~indexes: array<indexConfig>,
100
+ ~subIdField: option<string>,
101
+ ): QueryDb_Adapter.operations => {
102
+ let table = tableName(name)
103
+ // Schema setup is async and LAZY: kicked off by the first operation, not at
104
+ // construction. A deploy-time caller may build the ops on a machine that
105
+ // cannot reach the database (VPC-private RDS) — only the runtime, which
106
+ // actually calls an operation, may open a connection. Subsequent operations
107
+ // await the same promise (table + index creation runs exactly once).
108
+ let readyRef: ref<option<promise<unit>>> = ref(None)
109
+ let ready = () =>
110
+ switch readyRef.contents {
111
+ | Some(p) => p
112
+ | None =>
113
+ let p = {
114
+ open Promise
115
+ ensureTable(~pool, ~table)->then(() => ensureIndexes(~pool, ~table, ~indexes))
116
+ }
117
+ readyRef := Some(p)
118
+ p
119
+ }
120
+
121
+ let rowsFor = async (id: string): array<JSON.t> => {
122
+ await ready()
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
+ ))->Array.map(decodeItem)
127
+ }
128
+
129
+ let saveOne = async (id: string, state: JSON.t, ttl: option<int>) => {
130
+ await ready()
131
+ let subKey = computeSubKey(state, subIdField)
132
+ let expiresAt = switch ttl {
133
+ | Some(t) => JSON.Encode.int(t)
134
+ | None => JSON.Encode.null
135
+ }
136
+ let _ = await pool->PgDriver.query(
137
+ `INSERT INTO ${table}(partition_key, sub_key, item, expires_at)
138
+ VALUES ($1, $2, $3::jsonb, $4)
139
+ ON CONFLICT (partition_key, sub_key)
140
+ DO UPDATE SET item = EXCLUDED.item, expires_at = EXCLUDED.expires_at`,
141
+ [
142
+ JSON.Encode.string(id),
143
+ JSON.Encode.string(subKey),
144
+ JSON.Encode.string(JSON.stringify(state)),
145
+ expiresAt,
146
+ ],
147
+ )
148
+ }
149
+
150
+ let load: QueryDb.load<string, JSON.t> = async id => Ok(await rowsFor(id))
151
+
152
+ let loadStream: QueryDb.loadStream<string, JSON.t> =
153
+ id =>
154
+ Effect.promise(() => rowsFor(id))
155
+ ->Stream.fromEffect
156
+ ->Stream.flatMap(arr => Stream.fromIterable(arr))
157
+
158
+ let save: QueryDb.save<string, JSON.t> = async (id, state, _saveMode, ttl) => {
159
+ await saveOne(id, state, ttl)
160
+ Ok()
161
+ }
162
+
163
+ let saveBatch: QueryDb.saveBatch<string, JSON.t> = async batch => {
164
+ // pg has no synchronous multi-statement transaction helper here; the upserts
165
+ // are independent (distinct keys) so a sequential apply is equivalent.
166
+ for i in 0 to batch->Array.length - 1 {
167
+ let (id, state, ttl) = batch->Array.getUnsafe(i)
168
+ await saveOne(id, state, ttl)
169
+ }
170
+ Ok()
171
+ }
172
+
173
+ // Mirror DynamoDB's `ADD #field :inc`: read the counter on the partition item
174
+ // (counters are single-state, sub_key=""), add inc, upsert, return NEW total.
175
+ let count: QueryDb.count<string> = async (id, fieldName, inc) => {
176
+ await ready()
177
+ let existing = (await rowsFor(id))->Array.get(0)->Option.flatMap(JSON.Decode.object)
178
+ let current =
179
+ existing
180
+ ->Option.flatMap(o => o->Dict.get(fieldName))
181
+ ->Option.flatMap(JSON.Decode.float)
182
+ ->Option.mapOr(0, Float.toInt)
183
+ let next = current + inc
184
+ let obj = switch existing {
185
+ | Some(o) => Dict.copy(o)
186
+ | None => Dict.make()
187
+ }
188
+ obj->Dict.set("id", JSON.Encode.string(id))
189
+ obj->Dict.set(fieldName, JSON.Encode.int(next))
190
+ await saveOne(id, JSON.Encode.object(obj), None)
191
+ Ok(next)
192
+ }
193
+
194
+ let delete: QueryDb.delete<string> = async (id, subIdOpt) => {
195
+ await ready()
196
+ let _ = switch subIdOpt {
197
+ | None =>
198
+ await pool->PgDriver.query(
199
+ `DELETE FROM ${table} WHERE partition_key = $1`,
200
+ [JSON.Encode.string(id)],
201
+ )
202
+ | Some((_, subValue)) =>
203
+ await pool->PgDriver.query(
204
+ `DELETE FROM ${table} WHERE partition_key = $1 AND sub_key = $2`,
205
+ [JSON.Encode.string(id), JSON.Encode.string(subValue)],
206
+ )
207
+ }
208
+ Ok()
209
+ }
210
+
211
+ let deleteBatch: QueryDb.deleteBatch<string> = async ids => {
212
+ for i in 0 to ids->Array.length - 1 {
213
+ let (id, subIdOpt) = ids->Array.getUnsafe(i)
214
+ let _ = await delete(id, subIdOpt)
215
+ }
216
+ Ok()
217
+ }
218
+
219
+ let ops: QueryDb_Adapter.operations = {
220
+ load,
221
+ loadStream,
222
+ save,
223
+ saveBatch,
224
+ count,
225
+ delete,
226
+ deleteBatch,
227
+ }
228
+ ops
229
+ }