@reventlessdev/reventless-postgres 3.0.0-alpha.13 → 3.0.0-alpha.131
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 +949 -0
- package/README.md +83 -0
- package/lib/bs/.compiler.log +2 -2
- package/lib/bs/ReventlessPostgres.cmt +0 -0
- package/lib/bs/ReventlessPostgres.mlmap +0 -2
- package/lib/bs/compiler-info.json +2 -2
- package/lib/bs/src/DcbEventLogStorage_Postgres-ReventlessPostgres.cmi +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_Ops-ReventlessPostgres.cmi +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 +43 -42
- package/lib/bs/src/DcbEventLogStorage_Postgres_Ops.res.mjs +4 -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.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_Ops-ReventlessPostgres.cmi +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 +24 -14
- 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/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/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.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.cmt +0 -0
- package/lib/ocaml/DcbEventLogStorage_Postgres_Ops.ast +0 -0
- package/lib/ocaml/DcbEventLogStorage_Postgres_Ops.res +43 -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.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.cmt +0 -0
- package/lib/ocaml/EventLogStorage_Postgres_Ops.ast +0 -0
- package/lib/ocaml/EventLogStorage_Postgres_Ops.res +24 -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/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/rescript.json +2 -1
- package/src/DcbEventLogStorage_Postgres.res +1 -1
- package/src/DcbEventLogStorage_Postgres_Ops.res +43 -42
- package/src/DcbEventLogStorage_Postgres_Ops.res.mjs +4 -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_Ops.res +24 -14
- package/src/PgChangeFeed.res +21 -9
- package/src/PgChangeFeed.res.mjs +6 -6
- package/src/PgDriver.res +1 -2
- 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/lib/ocaml/PostgresIntegrationTest-ReventlessPostgres.cmi +0 -0
- package/lib/ocaml/PostgresIntegrationTest-ReventlessPostgres.cmj +0 -0
- package/lib/ocaml/PostgresIntegrationTest-ReventlessPostgres.cmt +0 -0
- package/lib/ocaml/PostgresIntegrationTest.ast +0 -0
- package/lib/ocaml/QueryEnginePostgresListPageTest-ReventlessPostgres.cmi +0 -0
- package/lib/ocaml/QueryEnginePostgresListPageTest-ReventlessPostgres.cmj +0 -0
- package/lib/ocaml/QueryEnginePostgresListPageTest-ReventlessPostgres.cmt +0 -0
- package/lib/ocaml/QueryEnginePostgresListPageTest.ast +0 -0
- package/tests/PostgresIntegrationTest.res.mjs +0 -377
- package/tests/QueryEnginePostgresListPageTest.res.mjs +0 -461
|
@@ -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
|
|
@@ -28,7 +28,12 @@ let param = (b: paramBuilder, v: JSON.t): string => {
|
|
|
28
28
|
let col = (field: string): string => `(item->>'${field->String.replaceAll("'", "''")}')`
|
|
29
29
|
|
|
30
30
|
// One Filter.config → SQL predicate, appending any bound params.
|
|
31
|
-
let filterSql = (
|
|
31
|
+
let filterSql = (
|
|
32
|
+
b: paramBuilder,
|
|
33
|
+
field: string,
|
|
34
|
+
cmp: QueryEngine.Filter.comparator,
|
|
35
|
+
v: QueryEngine.value,
|
|
36
|
+
): string => {
|
|
32
37
|
let c = col(field)
|
|
33
38
|
let bind = () => b->param(JSON.Encode.string(valueToText(v)))
|
|
34
39
|
let cmpSql = op =>
|
|
@@ -48,10 +53,16 @@ let filterSql = (b: paramBuilder, field: string, cmp: QueryEngine.Filter.compara
|
|
|
48
53
|
}
|
|
49
54
|
}
|
|
50
55
|
|
|
51
|
-
let subIdSql = (
|
|
56
|
+
let subIdSql = (
|
|
57
|
+
b: paramBuilder,
|
|
58
|
+
field: string,
|
|
59
|
+
cmp: QueryEngine.SubId.comparator,
|
|
60
|
+
v: QueryEngine.value,
|
|
61
|
+
): string => {
|
|
52
62
|
let c = col(field)
|
|
53
63
|
let bind = () => b->param(JSON.Encode.string(valueToText(v)))
|
|
54
|
-
let cmpSql = op =>
|
|
64
|
+
let cmpSql = op =>
|
|
65
|
+
isNumeric(v) ? `${c}::numeric ${op} ${bind()}::numeric` : `${c} ${op} ${bind()}`
|
|
55
66
|
switch cmp {
|
|
56
67
|
| QueryEngine.SubId.Equal => cmpSql("=")
|
|
57
68
|
| Unequal => cmpSql("<>")
|
|
@@ -63,24 +74,28 @@ let subIdSql = (b: paramBuilder, field: string, cmp: QueryEngine.SubId.comparato
|
|
|
63
74
|
}
|
|
64
75
|
}
|
|
65
76
|
|
|
66
|
-
let notExpired =
|
|
77
|
+
let notExpired = QueryDbStorage_Postgres_Ops.notExpiredClause
|
|
67
78
|
|
|
68
79
|
// Add `id` (from partition_key) when absent — matches allRows/scan in the SQLite
|
|
69
80
|
// engine; partition-key queries return items as stored.
|
|
70
81
|
let decodeRow = (~withId, row: dict<JSON.t>): JSON.t => {
|
|
71
|
-
let item =
|
|
82
|
+
let item = QueryDbStorage_Postgres_Ops.decodeItem(row)
|
|
72
83
|
if withId {
|
|
73
84
|
let pk = switch row->Dict.get("partition_key") {
|
|
74
85
|
| Some(JSON.String(s)) => s
|
|
75
86
|
| _ => ""
|
|
76
87
|
}
|
|
77
|
-
|
|
88
|
+
QueryDbStorage_Postgres_Ops.withId(item, pk)
|
|
78
89
|
} else {
|
|
79
90
|
item
|
|
80
91
|
}
|
|
81
92
|
}
|
|
82
93
|
|
|
83
|
-
module Make = (
|
|
94
|
+
module Make = (
|
|
95
|
+
P: {
|
|
96
|
+
let pool: PgDriver.pool
|
|
97
|
+
},
|
|
98
|
+
) => {
|
|
84
99
|
let query = async (
|
|
85
100
|
~readModelName,
|
|
86
101
|
~key=?,
|
|
@@ -90,7 +105,7 @@ module Make = (P: {let pool: PgDriver.pool}) => {
|
|
|
90
105
|
~ascending=?,
|
|
91
106
|
~limit=?,
|
|
92
107
|
): array<JSON.t> => {
|
|
93
|
-
let table =
|
|
108
|
+
let table = QueryDbStorage_Postgres_Ops.tableName(readModelName)
|
|
94
109
|
let keyStr = switch key {
|
|
95
110
|
| Some(k) => k
|
|
96
111
|
| None => valueToText(id)
|
|
@@ -109,16 +124,22 @@ module Make = (P: {let pool: PgDriver.pool}) => {
|
|
|
109
124
|
| Some(n) => ` LIMIT ${b->param(JSON.Encode.int(n))}`
|
|
110
125
|
| None => ""
|
|
111
126
|
}
|
|
112
|
-
let sql = `SELECT partition_key, item FROM ${table} WHERE ${where->Array.join(
|
|
127
|
+
let sql = `SELECT partition_key, item FROM ${table} WHERE ${where->Array.join(
|
|
128
|
+
" AND ",
|
|
129
|
+
)} ORDER BY sub_key ${dir}${limitClause}`
|
|
113
130
|
(await P.pool->PgDriver.query(sql, b.params))->Array.map(decodeRow(~withId=false, ...))
|
|
114
131
|
}
|
|
115
132
|
|
|
116
133
|
let scan = async (~readModelName, ~filterConfigs, ~limit): array<JSON.t> => {
|
|
117
|
-
let table =
|
|
134
|
+
let table = QueryDbStorage_Postgres_Ops.tableName(readModelName)
|
|
118
135
|
let b = {params: []}
|
|
119
136
|
let where = [notExpired]
|
|
120
|
-
filterConfigs->Array.forEach(((field, cmp, v)) =>
|
|
121
|
-
|
|
137
|
+
filterConfigs->Array.forEach(((field, cmp, v)) =>
|
|
138
|
+
where->Array.push(filterSql(b, field, cmp, v))
|
|
139
|
+
)
|
|
140
|
+
let sql = `SELECT partition_key, item FROM ${table} WHERE ${where->Array.join(
|
|
141
|
+
" AND ",
|
|
142
|
+
)} ORDER BY partition_key, sub_key LIMIT ${b->param(JSON.Encode.int(limit))}`
|
|
122
143
|
(await P.pool->PgDriver.query(sql, b.params))->Array.map(decodeRow(~withId=true, ...))
|
|
123
144
|
}
|
|
124
145
|
|
|
@@ -138,7 +159,7 @@ module Make = (P: {let pool: PgDriver.pool}) => {
|
|
|
138
159
|
// (`ensureIndexes`) instead of scanning. String comparison mirrors the
|
|
139
160
|
// resolver's JS path (which only matched string-typed fields).
|
|
140
161
|
let indexLookup = async (~readModelName, field: string, value: string): array<JSON.t> => {
|
|
141
|
-
let table =
|
|
162
|
+
let table = QueryDbStorage_Postgres_Ops.tableName(readModelName)
|
|
142
163
|
let b = {params: []}
|
|
143
164
|
let sql = `SELECT partition_key, item FROM ${table} WHERE ${col(field)} = ${b->param(
|
|
144
165
|
JSON.Encode.string(value),
|
|
@@ -154,7 +175,7 @@ module Make = (P: {let pool: PgDriver.pool}) => {
|
|
|
154
175
|
if ids->Array.length == 0 {
|
|
155
176
|
[]
|
|
156
177
|
} else {
|
|
157
|
-
let table =
|
|
178
|
+
let table = QueryDbStorage_Postgres_Ops.tableName(readModelName)
|
|
158
179
|
let b = {params: []}
|
|
159
180
|
let idsJson = JSON.Encode.array(ids->Array.map(JSON.Encode.string))
|
|
160
181
|
let sql = `SELECT partition_key, item FROM ${table} WHERE partition_key = ANY(${b->param(
|
|
@@ -168,6 +189,39 @@ module Make = (P: {let pool: PgDriver.pool}) => {
|
|
|
168
189
|
let idExprC = `COALESCE(item->>'id', partition_key) COLLATE "C"`
|
|
169
190
|
let jsonTextC = (field: string): string => `${col(field)} COLLATE "C"`
|
|
170
191
|
|
|
192
|
+
// "not retired", as jsonb rather than text. `->` keeps the value typed, so a
|
|
193
|
+
// JSON boolean true is distinguishable from the JSON string "true"; `->>`
|
|
194
|
+
// would render both as 'true' and hide a row the shared spec keeps, since that
|
|
195
|
+
// spec decodes strictly and reads a non-boolean as "no flag". `IS DISTINCT
|
|
196
|
+
// FROM` rather than `<>` so an absent key — NULL — keeps the row instead of
|
|
197
|
+
// making the whole predicate NULL and dropping it.
|
|
198
|
+
//
|
|
199
|
+
// The state form compares against JSON strings instead of `true`, one
|
|
200
|
+
// `IS DISTINCT FROM` per state, ANDed. Not `NOT IN`: that is ordinary
|
|
201
|
+
// equality, so an absent key makes the predicate NULL and drops a row this
|
|
202
|
+
// spec keeps. Same shape and same index behaviour as the single-state form —
|
|
203
|
+
// a handful of equality predicates over an enum-valued attribute cost what one
|
|
204
|
+
// over a boolean does — so the `@index` guidance carries over unchanged.
|
|
205
|
+
//
|
|
206
|
+
// A set naming no states withdraws nothing, so the predicate is vacuously
|
|
207
|
+
// true, matching what `isRetiredValue` answers for it.
|
|
208
|
+
let notRetiredC = (scope: Reventless.OwnerScope.retiredScope): string => {
|
|
209
|
+
let key = scope.field->String.replaceAll("'", "''")
|
|
210
|
+
let distinctFrom = lit => `(item->'${key}') IS DISTINCT FROM ${lit}`
|
|
211
|
+
switch scope.values {
|
|
212
|
+
| None => distinctFrom("'true'::jsonb")
|
|
213
|
+
| Some([]) => "TRUE"
|
|
214
|
+
| Some(states) =>
|
|
215
|
+
// Parenthesised though every caller ANDs these together already: a clause
|
|
216
|
+
// that is safe only because of where it is spliced is a clause that breaks
|
|
217
|
+
// the first time it is spliced somewhere else.
|
|
218
|
+
"(" ++
|
|
219
|
+
states
|
|
220
|
+
->Array.map(state => distinctFrom(`'"${state->String.replaceAll("'", "''")}"'::jsonb`))
|
|
221
|
+
->Array.join(" AND ") ++ ")"
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
171
225
|
// Connection-list push-down. Builds `item->>'field'` predicates + keyset
|
|
172
226
|
// WHERE + `ORDER BY … LIMIT pageSize+1` so a page reads only what it returns
|
|
173
227
|
// rather than the whole read model. Reproduces `QueryDbListQuery` semantics
|
|
@@ -183,8 +237,10 @@ module Make = (P: {let pool: PgDriver.pool}) => {
|
|
|
183
237
|
~argsDict: dict<JSON.t>,
|
|
184
238
|
~capability: GraphQL_FragmentGenerator.serverCapability,
|
|
185
239
|
~labelField as _,
|
|
240
|
+
~ownerScope: option<(string, string)>=?,
|
|
241
|
+
~retiredScope: option<Reventless.OwnerScope.retiredScope>=?,
|
|
186
242
|
): option<JSON.t> => {
|
|
187
|
-
let table =
|
|
243
|
+
let table = QueryDbStorage_Postgres_Ops.tableName(readModelName)
|
|
188
244
|
let filterDict =
|
|
189
245
|
argsDict->Dict.get("filter")->Option.flatMap(JSON.Decode.object)->Option.getOr(Dict.make())
|
|
190
246
|
let strNonEmpty = k =>
|
|
@@ -210,6 +266,19 @@ module Make = (P: {let pool: PgDriver.pool}) => {
|
|
|
210
266
|
} else {
|
|
211
267
|
let b = {params: []}
|
|
212
268
|
let whereParts = [notExpired]
|
|
269
|
+
// Into the SQL, before the LIMIT — narrowing the returned page instead
|
|
270
|
+
// would hand back fewer rows than asked for while still claiming a next
|
|
271
|
+
// page. Mirrors the SQLite push-down; the parity harness holds the two
|
|
272
|
+
// and the shared spec to the same answer.
|
|
273
|
+
switch ownerScope {
|
|
274
|
+
| Some((field, required)) =>
|
|
275
|
+
whereParts->Array.push(`${jsonTextC(field)} = ${b->param(JSON.Encode.string(required))}`)
|
|
276
|
+
| None => ()
|
|
277
|
+
}
|
|
278
|
+
switch retiredScope {
|
|
279
|
+
| Some(scope) => whereParts->Array.push(notRetiredC(scope))
|
|
280
|
+
| None => ()
|
|
281
|
+
}
|
|
213
282
|
let valString = v =>
|
|
214
283
|
switch v->JSON.Decode.string {
|
|
215
284
|
| Some(s) => Some(s)
|
|
@@ -217,15 +286,17 @@ module Make = (P: {let pool: PgDriver.pool}) => {
|
|
|
217
286
|
}
|
|
218
287
|
capability.filterFields->Array.forEach(f => {
|
|
219
288
|
switch filterDict->Dict.get(f.name ++ "Eq") {
|
|
220
|
-
| Some(v)
|
|
289
|
+
| Some(v) if v != JSON.Encode.null =>
|
|
221
290
|
whereParts->Array.push(
|
|
222
|
-
`${jsonTextC(f.name)} = ${b->param(
|
|
291
|
+
`${jsonTextC(f.name)} = ${b->param(
|
|
292
|
+
JSON.Encode.string(valString(v)->Option.getOr("")),
|
|
293
|
+
)}`,
|
|
223
294
|
)
|
|
224
295
|
| _ => ()
|
|
225
296
|
}
|
|
226
297
|
if f.range {
|
|
227
298
|
switch filterDict->Dict.get(f.name ++ "From") {
|
|
228
|
-
| Some(v)
|
|
299
|
+
| Some(v) if v != JSON.Encode.null =>
|
|
229
300
|
whereParts->Array.push(
|
|
230
301
|
`${jsonTextC(f.name)} >= ${b->param(
|
|
231
302
|
JSON.Encode.string(valString(v)->Option.getOr("")),
|
|
@@ -234,7 +305,7 @@ module Make = (P: {let pool: PgDriver.pool}) => {
|
|
|
234
305
|
| _ => ()
|
|
235
306
|
}
|
|
236
307
|
switch filterDict->Dict.get(f.name ++ "To") {
|
|
237
|
-
| Some(v)
|
|
308
|
+
| Some(v) if v != JSON.Encode.null =>
|
|
238
309
|
whereParts->Array.push(
|
|
239
310
|
`${jsonTextC(f.name)} <= ${b->param(
|
|
240
311
|
JSON.Encode.string(valString(v)->Option.getOr("")),
|
|
@@ -256,7 +327,8 @@ module Make = (P: {let pool: PgDriver.pool}) => {
|
|
|
256
327
|
| Some(f) => jsonTextC(f)
|
|
257
328
|
| None => idExprC
|
|
258
329
|
}
|
|
259
|
-
|
|
330
|
+
let afterCursor = argsDict->Dict.get("after")->Option.flatMap(JSON.Decode.string)
|
|
331
|
+
switch afterCursor {
|
|
260
332
|
| Some(c) =>
|
|
261
333
|
whereParts->Array.push(
|
|
262
334
|
`${cursorExpr} ${isDesc ? "<" : ">"} ${b->param(
|
|
@@ -278,7 +350,8 @@ module Make = (P: {let pool: PgDriver.pool}) => {
|
|
|
278
350
|
let sql = `SELECT partition_key, item FROM ${table} WHERE ${whereParts->Array.join(
|
|
279
351
|
" AND ",
|
|
280
352
|
)} ORDER BY ${orderClause} LIMIT ${b->param(JSON.Encode.int(pageSize + 1))}`
|
|
281
|
-
let rows =
|
|
353
|
+
let rows =
|
|
354
|
+
(await P.pool->PgDriver.query(sql, b.params))->Array.map(decodeRow(~withId=true, ...))
|
|
282
355
|
let hasMore = rows->Array.length > pageSize
|
|
283
356
|
let pageItems = rows->Array.slice(~start=0, ~end=pageSize)
|
|
284
357
|
let cursorField = orderByField->Option.getOr("id")
|
|
@@ -290,7 +363,10 @@ module Make = (P: {let pool: PgDriver.pool}) => {
|
|
|
290
363
|
ReventlessCore.QueryDbListQuery.buildConnection(
|
|
291
364
|
~pageItems,
|
|
292
365
|
~hasNextPage=hasMore,
|
|
293
|
-
|
|
366
|
+
// Same rule the spec uses, because the parity suite compares them: a
|
|
367
|
+
// forward page reached by a cursor has one behind it. Hardcoding false
|
|
368
|
+
// made Prev disappear on exactly the pages that have a previous one.
|
|
369
|
+
~hasPreviousPage=afterCursor->Option.isSome,
|
|
294
370
|
~cursorValueOf,
|
|
295
371
|
),
|
|
296
372
|
)
|
|
@@ -309,14 +385,18 @@ module Make = (P: {let pool: PgDriver.pool}) => {
|
|
|
309
385
|
~subIdField as _: string,
|
|
310
386
|
~id: string,
|
|
311
387
|
~argsDict: dict<JSON.t>,
|
|
388
|
+
~ownerScope: option<(string, string)>=?,
|
|
389
|
+
~retiredScope: option<Reventless.OwnerScope.retiredScope>=?,
|
|
312
390
|
): JSON.t => {
|
|
313
|
-
let table =
|
|
391
|
+
let table = QueryDbStorage_Postgres_Ops.tableName(readModelName)
|
|
314
392
|
let filterDict =
|
|
315
393
|
argsDict->Dict.get("filter")->Option.flatMap(JSON.Decode.object)->Option.getOr(Dict.make())
|
|
316
394
|
let fstr = k => filterDict->Dict.get(k)->Option.flatMap(JSON.Decode.string)
|
|
317
395
|
let orderDesc = fstr("order")->Option.mapOr(false, o => o == "DESC")
|
|
318
|
-
let last =
|
|
319
|
-
|
|
396
|
+
let last =
|
|
397
|
+
argsDict->Dict.get("last")->Option.flatMap(JSON.Decode.float)->Option.map(Float.toInt)
|
|
398
|
+
let first =
|
|
399
|
+
argsDict->Dict.get("first")->Option.flatMap(JSON.Decode.float)->Option.map(Float.toInt)
|
|
320
400
|
let after = argsDict->Dict.get("after")->Option.flatMap(JSON.Decode.string)
|
|
321
401
|
let before = argsDict->Dict.get("before")->Option.flatMap(JSON.Decode.string)
|
|
322
402
|
let isBackward = last->Option.isSome
|
|
@@ -324,28 +404,49 @@ module Make = (P: {let pool: PgDriver.pool}) => {
|
|
|
324
404
|
let sk = `sub_key COLLATE "C"`
|
|
325
405
|
let b = {params: []}
|
|
326
406
|
let where = [`partition_key = ${b->param(JSON.Encode.string(id))}`, notExpired]
|
|
407
|
+
// Before the keyset window and the LIMIT, for the same reason as `listPage`.
|
|
408
|
+
switch ownerScope {
|
|
409
|
+
| Some((field, required)) =>
|
|
410
|
+
where->Array.push(`${jsonTextC(field)} = ${b->param(JSON.Encode.string(required))}`)
|
|
411
|
+
| None => ()
|
|
412
|
+
}
|
|
413
|
+
switch retiredScope {
|
|
414
|
+
| Some(scope) => where->Array.push(notRetiredC(scope))
|
|
415
|
+
| None => ()
|
|
416
|
+
}
|
|
327
417
|
fstr("prefix")->Option.forEach(p =>
|
|
328
418
|
where->Array.push(`starts_with(sub_key, ${b->param(JSON.Encode.string(p))})`)
|
|
329
419
|
)
|
|
330
|
-
fstr("from")->Option.forEach(f =>
|
|
331
|
-
|
|
420
|
+
fstr("from")->Option.forEach(f =>
|
|
421
|
+
where->Array.push(`${sk} >= ${b->param(JSON.Encode.string(f))}`)
|
|
422
|
+
)
|
|
423
|
+
fstr("to")->Option.forEach(t =>
|
|
424
|
+
where->Array.push(`${sk} <= ${b->param(JSON.Encode.string(t))}`)
|
|
425
|
+
)
|
|
332
426
|
fstr("eq")->Option.forEach(e => where->Array.push(`${sk} = ${b->param(JSON.Encode.string(e))}`))
|
|
333
427
|
// Keyset: forward excludes ≤ after; backward excludes ≥ before.
|
|
334
428
|
switch (isBackward, after, before) {
|
|
335
429
|
| (false, Some(c), _) =>
|
|
336
|
-
where->Array.push(
|
|
430
|
+
where->Array.push(
|
|
431
|
+
`${sk} > ${b->param(JSON.Encode.string(ReventlessCore.QueryDbListQuery.decodeCursor(c)))}`,
|
|
432
|
+
)
|
|
337
433
|
| (true, _, Some(c)) =>
|
|
338
|
-
where->Array.push(
|
|
434
|
+
where->Array.push(
|
|
435
|
+
`${sk} < ${b->param(JSON.Encode.string(ReventlessCore.QueryDbListQuery.decodeCursor(c)))}`,
|
|
436
|
+
)
|
|
339
437
|
| _ => ()
|
|
340
438
|
}
|
|
341
439
|
// Page-fetch direction is the LOGICAL order forward, and its OPPOSITE
|
|
342
440
|
// backward (take the N nearest `before`, then reverse to logical) — mirrors
|
|
343
441
|
// the resolver's `reverse = isBackward ? !orderDesc : orderDesc`.
|
|
344
442
|
let fetchDesc = isBackward ? !orderDesc : orderDesc
|
|
345
|
-
let pageSize =
|
|
443
|
+
let pageSize =
|
|
444
|
+
(isBackward ? last : first)->Option.getOr(ReventlessCore.QueryDbListQuery.defaultListPageSize)
|
|
346
445
|
let sql = `SELECT partition_key, sub_key, item FROM ${table} WHERE ${where->Array.join(
|
|
347
446
|
" AND ",
|
|
348
|
-
)} ORDER BY ${sk} ${fetchDesc ? "DESC" : "ASC"} LIMIT ${b->param(
|
|
447
|
+
)} ORDER BY ${sk} ${fetchDesc ? "DESC" : "ASC"} LIMIT ${b->param(
|
|
448
|
+
JSON.Encode.int(pageSize + 1),
|
|
449
|
+
)}`
|
|
349
450
|
let rows = await P.pool->PgDriver.query(sql, b.params)
|
|
350
451
|
let hasMore = rows->Array.length > pageSize
|
|
351
452
|
let taken = rows->Array.slice(~start=0, ~end=pageSize)
|
|
@@ -359,17 +460,18 @@ module Make = (P: {let pool: PgDriver.pool}) => {
|
|
|
359
460
|
| _ => ""
|
|
360
461
|
}
|
|
361
462
|
let cursors = logical->Array.map(subKeyOf)
|
|
362
|
-
let edges =
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
)
|
|
463
|
+
let edges = pageItems->Array.mapWithIndex((item, i) =>
|
|
464
|
+
Obj.magic({
|
|
465
|
+
"node": item,
|
|
466
|
+
"cursor": ReventlessCore.QueryDbListQuery.encodeCursor(cursors->Array.getUnsafe(i)),
|
|
467
|
+
})
|
|
468
|
+
)
|
|
369
469
|
let startCursor =
|
|
370
470
|
cursors->Array.get(0)->Option.map(ReventlessCore.QueryDbListQuery.encodeCursor)
|
|
371
471
|
let endCursor =
|
|
372
|
-
cursors
|
|
472
|
+
cursors
|
|
473
|
+
->Array.get(cursors->Array.length - 1)
|
|
474
|
+
->Option.map(ReventlessCore.QueryDbListQuery.encodeCursor)
|
|
373
475
|
Obj.magic({
|
|
374
476
|
"edges": edges,
|
|
375
477
|
"pageInfo": {
|
|
@@ -380,7 +482,4 @@ module Make = (P: {let pool: PgDriver.pool}) => {
|
|
|
380
482
|
},
|
|
381
483
|
})
|
|
382
484
|
}
|
|
383
|
-
|
|
384
|
-
let make: QueryDb_Adapter.queryEngineMaker = _allQueryDbs =>
|
|
385
|
-
Pulumi.Output.make({QueryEngine.scan, query})
|
|
386
485
|
}
|