okmodel 0.1.1 → 0.2.1
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/README.md +32 -16
- package/dist/adapters/capabilities.d.ts +34 -0
- package/dist/adapters/pg/bunsql-extra.d.ts +17 -0
- package/dist/adapters/pg/bunsql.d.ts +60 -0
- package/dist/adapters/pg/call.d.ts +2 -12
- package/dist/adapters/pg/nodepostgres-extra.d.ts +18 -0
- package/dist/adapters/pg/nodepostgres.d.ts +57 -0
- package/dist/adapters/pg/postgres-extra.d.ts +50 -1
- package/dist/adapters/pg/postgresjs.d.ts +0 -4
- package/dist/adapters/pg/session.d.ts +94 -0
- package/dist/adapters/pg/watch.d.ts +18 -0
- package/dist/contracts/catalog/object.d.ts +1 -0
- package/dist/contracts/catalog/types.d.ts +15 -8
- package/dist/contracts/connection.d.ts +13 -0
- package/dist/contracts/driver.d.ts +18 -5
- package/dist/contracts/error.d.ts +17 -1
- package/dist/contracts/generator.d.ts +63 -0
- package/dist/contracts/index.d.ts +2 -2
- package/dist/contracts/index.js +1 -1
- package/dist/contracts/internal.js +11 -9
- package/dist/contracts/location.d.ts +24 -0
- package/dist/contracts/rows.d.ts +15 -0
- package/dist/dialects/pg/archive-bind.d.ts +22 -0
- package/dist/dialects/pg/column.d.ts +126 -9
- package/dist/dialects/pg/compile.d.ts +1 -0
- package/dist/dialects/pg/custom.d.ts +6 -0
- package/dist/dialects/pg/filter-parse.d.ts +19 -0
- package/dist/dialects/pg/filters.d.ts +19 -0
- package/dist/dialects/pg/index.d.ts +11 -2
- package/dist/dialects/pg/index.js +159 -1063
- package/dist/dialects/pg/keys.d.ts +19 -3
- package/dist/dialects/pg/model.d.ts +223 -2
- package/dist/dialects/pg/operator-fit.d.ts +86 -0
- package/dist/dialects/pg/operators.d.ts +39 -1
- package/dist/dialects/pg/ops/arr-append.d.ts +9 -0
- package/dist/dialects/pg/ops/arr-ns.d.ts +7 -0
- package/dist/dialects/pg/ops/arr-remove.d.ts +9 -0
- package/dist/dialects/pg/ops/containedBy.d.ts +9 -0
- package/dist/dialects/pg/ops/contains.d.ts +8 -4
- package/dist/dialects/pg/ops/hasAnyKey.d.ts +8 -0
- package/dist/dialects/pg/ops/hasKey.d.ts +8 -0
- package/dist/dialects/pg/ops/json-ns.d.ts +6 -0
- package/dist/dialects/pg/ops/json-set.d.ts +12 -0
- package/dist/dialects/pg/ops/matches.d.ts +16 -0
- package/dist/dialects/pg/ops/overlaps.d.ts +9 -0
- package/dist/dialects/pg/ops/path.d.ts +13 -0
- package/dist/dialects/pg/preset.d.ts +76 -0
- package/dist/dialects/pg/relations.d.ts +59 -2
- package/dist/dialects/pg/schema.d.ts +109 -5
- package/dist/dialects/pg/table.d.ts +130 -11
- package/dist/dialects/pg/temporal.d.ts +9 -7
- package/dist/dialects/pg/tenancy.d.ts +94 -0
- package/dist/dialects/pg/through.d.ts +21 -0
- package/dist/dialects/pg/trait.d.ts +141 -0
- package/dist/dialects/pg/where.d.ts +37 -0
- package/dist/runtime/aggregate.d.ts +25 -0
- package/dist/runtime/archive-handle.d.ts +18 -0
- package/dist/runtime/archive-rules.d.ts +12 -0
- package/dist/runtime/archive.d.ts +73 -0
- package/dist/runtime/batch.d.ts +20 -0
- package/dist/runtime/client.d.ts +114 -5
- package/dist/runtime/conflict.d.ts +2 -1
- package/dist/runtime/exposure.d.ts +27 -0
- package/dist/runtime/hookm.d.ts +39 -0
- package/dist/runtime/ids/index.d.ts +11 -0
- package/dist/runtime/ids/index.js +193 -0
- package/dist/runtime/ids/okid-extended.d.ts +19 -0
- package/dist/runtime/ids/okid-generator.d.ts +21 -0
- package/dist/runtime/ids/okid-shared.d.ts +89 -0
- package/dist/runtime/ids/okid.d.ts +24 -0
- package/dist/runtime/ids/uuid.d.ts +21 -0
- package/dist/runtime/lock.d.ts +22 -0
- package/dist/runtime/operator-sql.d.ts +8 -0
- package/dist/runtime/page.d.ts +25 -0
- package/dist/runtime/pg/bun.d.ts +30 -0
- package/dist/runtime/pg/bun.js +52 -0
- package/dist/runtime/pg/pg.d.ts +29 -0
- package/dist/runtime/pg/pg.js +52 -0
- package/dist/runtime/pg/pglite.js +16 -17
- package/dist/runtime/pg/postgresjs.js +14 -24
- package/dist/runtime/plan.d.ts +146 -7
- package/dist/runtime/preset-stack.d.ts +17 -0
- package/dist/runtime/presets.d.ts +48 -0
- package/dist/runtime/read-stream.d.ts +26 -0
- package/dist/runtime/safety/index.d.ts +159 -0
- package/dist/runtime/safety/index.js +294 -0
- package/dist/runtime/safety-hook.d.ts +42 -0
- package/dist/runtime/server-check.d.ts +34 -0
- package/dist/runtime/tenancy/index.d.ts +37 -0
- package/dist/runtime/tenancy/index.js +365 -0
- package/dist/runtime/trait-read.d.ts +30 -0
- package/dist/runtime/traits/index.d.ts +128 -0
- package/dist/runtime/traits/index.js +509 -0
- package/dist/runtime/transaction.d.ts +19 -0
- package/dist/runtime/tx.d.ts +22 -7
- package/dist/runtime/types.d.ts +376 -27
- package/dist/runtime/validate/bind.d.ts +11 -0
- package/dist/runtime/validate/closed.d.ts +16 -0
- package/dist/runtime/validate/engine.d.ts +41 -0
- package/dist/runtime/validate/hook.d.ts +17 -0
- package/dist/runtime/validate/index.d.ts +21 -0
- package/dist/runtime/validate/index.js +175 -0
- package/dist/runtime/validate/load.d.ts +13 -0
- package/dist/runtime/validate/places.d.ts +41 -0
- package/dist/runtime/validate/rules.d.ts +108 -0
- package/dist/runtime/validate/surface.d.ts +102 -0
- package/dist/runtime/write.d.ts +30 -5
- package/dist/shared/{fgqgsb5z.js → 0qkxbm1f.js} +2 -2
- package/dist/shared/0v1g424p.js +26 -0
- package/dist/shared/10tjenmd.js +210 -0
- package/dist/shared/{xqbhg6cn.js → 1dp5knt7.js} +95 -10
- package/dist/shared/38crxbgn.js +229 -0
- package/dist/shared/3a5ne750.js +158 -0
- package/dist/shared/4b9da9d7.js +138 -0
- package/dist/shared/4e4c6vq2.js +62 -0
- package/dist/shared/{x5x59srr.js → 4jzzghr6.js} +416 -83
- package/dist/shared/{ycwz42az.js → 52szzakm.js} +6 -26
- package/dist/shared/{05w44d8g.js → 5z4q2zda.js} +567 -18
- package/dist/shared/672t5vym.js +12 -0
- package/dist/shared/7m88x3e9.js +45 -0
- package/dist/shared/7mht49yd.js +1091 -0
- package/dist/shared/{mtkwv8wx.js → 7p2wbz7f.js} +1 -1
- package/dist/shared/83eaefwr.js +10 -0
- package/dist/shared/96ps4g1z.js +32 -0
- package/dist/shared/97g2jnrw.js +76 -0
- package/dist/shared/{xy79j5hq.js → 9k6vetvf.js} +25 -36
- package/dist/shared/{w8samvk1.js → a1gdp7j9.js} +1 -1
- package/dist/shared/a5sxz52y.js +13 -0
- package/dist/shared/{dynq16vb.js → b066g80h.js} +8 -6
- package/dist/shared/cstgkzdx.js +154 -0
- package/dist/shared/{523ft3e8.js → d04j53v8.js} +6 -5
- package/dist/shared/{y9m15dfv.js → d2whjz4k.js} +15 -9
- package/dist/shared/dd4f8mnx.js +307 -0
- package/dist/shared/e6wg00wd.js +164 -0
- package/dist/shared/e7a268fy.js +195 -0
- package/dist/shared/{kprcwaha.js → eaxwrgby.js} +4 -3
- package/dist/shared/{dkn3y60a.js → ecdzej21.js} +76 -112
- package/dist/shared/{hadrfb9h.js → edwqqryd.js} +17 -14
- package/dist/shared/{m3ypagh5.js → eqmtn1bj.js} +57 -5
- package/dist/shared/g1b5zzj9.js +75 -0
- package/dist/shared/g9f0zqce.js +376 -0
- package/dist/shared/ge03bfca.js +64 -0
- package/dist/shared/{yd1kzwn2.js → jns2wx4t.js} +55 -12
- package/dist/shared/k3ted12y.js +33 -0
- package/dist/shared/k7dwkhvq.js +31 -0
- package/dist/shared/{hd293j9g.js → kyxb62mc.js} +14 -2
- package/dist/shared/m9tv7xn7.js +404 -0
- package/dist/shared/mrq77sgw.js +55 -0
- package/dist/shared/mxmcdsh5.js +450 -0
- package/dist/shared/n442h54c.js +14 -0
- package/dist/shared/n47rd821.js +442 -0
- package/dist/shared/n4jqhpqz.js +27 -0
- package/dist/shared/{mkjmqjv4.js → nfzc5z0w.js} +2 -104
- package/dist/shared/nsva2a6b.js +42 -0
- package/dist/shared/ps43134h.js +69 -0
- package/dist/shared/{8b04d28c.js → py3nh7jy.js} +2 -2
- package/dist/shared/qa5dsc7z.js +86 -0
- package/dist/shared/{g5fhqmwe.js → qxa0yb9j.js} +6 -4
- package/dist/shared/r6nwkxnt.js +86 -0
- package/dist/shared/{jjb7yht9.js → rtnaqre0.js} +3 -3
- package/dist/shared/rwmrejjz.js +26 -0
- package/dist/shared/{zhc5vmgf.js → s6gqfn5j.js} +2 -2
- package/dist/shared/sacqha1y.js +70 -0
- package/dist/shared/{qk3hxfja.js → sfh4pk75.js} +5 -5
- package/dist/shared/{j5cn2963.js → t4tbsy9h.js} +13 -64
- package/dist/shared/{ngk8gj61.js → tbszx8mz.js} +15 -1
- package/dist/shared/v18jva5b.js +90 -0
- package/dist/shared/x9v1qgeb.js +201 -0
- package/dist/shared/xatfg0gn.js +193 -0
- package/dist/shared/xkz8g6by.js +225 -0
- package/dist/shared/{rgs0st28.js → xrfnq5k8.js} +237 -434
- package/dist/shared/ytg3xf9p.js +149 -0
- package/dist/shared/yw5c610b.js +48 -0
- package/dist/shared/zd93g3rq.js +31 -0
- package/dist/shared/zt8y0wzb.js +132 -0
- package/dist/shared/{p0cbr3g2.js → zwmqxb0g.js} +1 -1
- package/dist/tooling/cli.js +14 -10
- package/dist/tooling/migrate/commands.d.ts +10 -0
- package/dist/tooling/migrate/engine.d.ts +36 -0
- package/package.json +41 -4
- package/dist/shared/gk7cs7sp.js +0 -566
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
import {
|
|
2
|
+
safetyInstalled,
|
|
3
|
+
runSafety
|
|
4
|
+
} from "./a5sxz52y.js";
|
|
1
5
|
import {
|
|
2
6
|
isOperator,
|
|
3
7
|
operatorName,
|
|
@@ -5,25 +9,131 @@ import {
|
|
|
5
9
|
} from "./cn0n68ft.js";
|
|
6
10
|
import {
|
|
7
11
|
assertIdentifier2
|
|
8
|
-
} from "./
|
|
12
|
+
} from "./nfzc5z0w.js";
|
|
9
13
|
import {
|
|
10
14
|
OkmError2,
|
|
11
|
-
safe2,
|
|
12
15
|
throwNamed2
|
|
13
|
-
} from "./
|
|
16
|
+
} from "./52szzakm.js";
|
|
14
17
|
|
|
15
18
|
// src/runtime/plan.ts
|
|
16
|
-
var
|
|
17
|
-
var
|
|
18
|
-
|
|
19
|
+
var activeScope;
|
|
20
|
+
var activeView;
|
|
21
|
+
function withRowFilters(scope, view, fn) {
|
|
22
|
+
const previousScope = activeScope;
|
|
23
|
+
const previousView = activeView;
|
|
24
|
+
activeScope = scope;
|
|
25
|
+
activeView = view;
|
|
26
|
+
try {
|
|
27
|
+
return fn();
|
|
28
|
+
} finally {
|
|
29
|
+
activeScope = previousScope;
|
|
30
|
+
activeView = previousView;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
var archiveInspect;
|
|
34
|
+
function installArchiveInspect(fn) {
|
|
35
|
+
archiveInspect = fn;
|
|
36
|
+
}
|
|
37
|
+
function archiveRules(model, view) {
|
|
38
|
+
return archiveInspect?.(model, view);
|
|
39
|
+
}
|
|
40
|
+
var FIND_OPTIONS = [
|
|
41
|
+
"include",
|
|
42
|
+
"limit",
|
|
43
|
+
"orderBy",
|
|
44
|
+
"select",
|
|
45
|
+
"signal",
|
|
46
|
+
"timeout",
|
|
47
|
+
"where"
|
|
48
|
+
];
|
|
49
|
+
var ONE_OPTIONS = ["include", "orderBy", "select", "signal", "timeout", "where"];
|
|
50
|
+
var FILTER_OPTIONS = ["signal", "timeout", "where"];
|
|
51
|
+
function readOptionNames(op) {
|
|
52
|
+
if (op === "find")
|
|
53
|
+
return FIND_OPTIONS;
|
|
54
|
+
if (op === "one")
|
|
55
|
+
return ONE_OPTIONS;
|
|
56
|
+
return FILTER_OPTIONS;
|
|
57
|
+
}
|
|
19
58
|
var BASE_RULES = [
|
|
20
59
|
{ rule: "parameterised", contribution: "planner", provenance: "planner" },
|
|
21
60
|
{ rule: "allowlisted", contribution: "catalog", provenance: "catalog" },
|
|
22
61
|
{ rule: "identifier", contribution: "catalog", provenance: "catalog" }
|
|
23
62
|
];
|
|
24
63
|
var indexesOf = new WeakMap;
|
|
25
|
-
|
|
64
|
+
var operatorEmit;
|
|
65
|
+
function installOperatorSql(emit) {
|
|
66
|
+
operatorEmit = emit;
|
|
67
|
+
}
|
|
68
|
+
function readNeedsOperatorSql(schema, call) {
|
|
69
|
+
if (operatorEmit !== undefined)
|
|
70
|
+
return false;
|
|
71
|
+
const table = indexes(schema).get(call.table);
|
|
72
|
+
return table !== undefined && needsOperatorSql(table, call.where);
|
|
73
|
+
}
|
|
74
|
+
function emitRelationFrom(schema, relation, child, sink, parent, alias, depth) {
|
|
75
|
+
if (relation.through !== undefined) {
|
|
76
|
+
const planner = { join: emitJoin, where: emitWhere, indexes, quote };
|
|
77
|
+
relation.through.emit(planner, schema, relation, child, sink, parent, alias, depth);
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
sink.text(quote(child.model.sql));
|
|
81
|
+
sink.text(" ");
|
|
82
|
+
sink.text(alias);
|
|
83
|
+
sink.text(" where ");
|
|
84
|
+
emitJoin(sink, parent, alias, relation);
|
|
85
|
+
}
|
|
86
|
+
var SYNC_OPERATORS = ",lt,lte,gt,gte,between,inList,notIn,";
|
|
87
|
+
var TEXT_OPERATORS = ",startsWith,endsWith,contains,like,ilike,";
|
|
88
|
+
function needsOperatorSql(table, value, key) {
|
|
89
|
+
if (isOperator(value)) {
|
|
90
|
+
const name = operatorName(value);
|
|
91
|
+
if (name === "or" || name === "and") {
|
|
92
|
+
const branches = operatorValue(value);
|
|
93
|
+
return Array.isArray(branches) && branches.some((branch) => needsOperatorSql(table, branch));
|
|
94
|
+
}
|
|
95
|
+
if (name === "not" || name === "eq")
|
|
96
|
+
return needsOperatorSql(table, operatorValue(value), key);
|
|
97
|
+
if (TEXT_OPERATORS.includes(`,${name},`)) {
|
|
98
|
+
const column = key === undefined ? undefined : table.columns.get(key);
|
|
99
|
+
return column !== undefined && !isTextColumn(column.dataType);
|
|
100
|
+
}
|
|
101
|
+
return !SYNC_OPERATORS.includes(`,${name},`);
|
|
102
|
+
}
|
|
103
|
+
if (Array.isArray(value))
|
|
104
|
+
return value.some((item) => needsOperatorSql(table, item, key));
|
|
105
|
+
if (!isRecord(value))
|
|
106
|
+
return false;
|
|
107
|
+
for (const field of Object.keys(value)) {
|
|
108
|
+
if (needsOperatorSql(table, value[field], field))
|
|
109
|
+
return true;
|
|
110
|
+
}
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
function isTextColumn(dataType) {
|
|
114
|
+
return dataType === "text" || dataType === "citext" || dataType.startsWith("varchar(") || dataType.startsWith("char(") || dataType.startsWith("character");
|
|
115
|
+
}
|
|
116
|
+
function emitLazy(column, ref, name, value, sink) {
|
|
117
|
+
const emit = operatorEmit;
|
|
118
|
+
if (emit === undefined) {
|
|
119
|
+
fail("OKM1121", `Operator ${name} on ${column.field} is not loaded.`);
|
|
120
|
+
}
|
|
121
|
+
emit(column, ref, name, value, sink);
|
|
122
|
+
}
|
|
123
|
+
function verifyRead(schema, call) {
|
|
124
|
+
if (!safetyInstalled())
|
|
125
|
+
return;
|
|
126
|
+
const source = schema.model[call.table]?.source;
|
|
127
|
+
const hatches = [];
|
|
128
|
+
if (call.all !== undefined)
|
|
129
|
+
hatches.push({ name: "all", reason: call.all });
|
|
130
|
+
if (call.scope !== undefined && "unscoped" in call.scope) {
|
|
131
|
+
hatches.push({ name: "unscoped", reason: call.scope.unscoped });
|
|
132
|
+
}
|
|
133
|
+
runSafety(appliedRules(call, source), hatches.length === 0 ? undefined : hatches);
|
|
134
|
+
}
|
|
26
135
|
function bindCall(schema, call, hooks) {
|
|
136
|
+
verifyRead(schema, call);
|
|
27
137
|
const params = [];
|
|
28
138
|
const marks = [];
|
|
29
139
|
const sink = {
|
|
@@ -36,7 +146,6 @@ function bindCall(schema, call, hooks) {
|
|
|
36
146
|
}
|
|
37
147
|
};
|
|
38
148
|
emit(schema, call, sink, undefined, hooks);
|
|
39
|
-
verifyRead(BASE_RULES);
|
|
40
149
|
return { key: marks.join(""), params };
|
|
41
150
|
}
|
|
42
151
|
function compileCall(schema, call, key, hooks) {
|
|
@@ -96,8 +205,15 @@ function logicalIntent(call) {
|
|
|
96
205
|
all: call.all
|
|
97
206
|
};
|
|
98
207
|
}
|
|
99
|
-
function appliedRules(call) {
|
|
100
|
-
const rules = [
|
|
208
|
+
function appliedRules(call, source) {
|
|
209
|
+
const rules = [];
|
|
210
|
+
for (const rule of BASE_RULES) {
|
|
211
|
+
if (source !== undefined && rule.contribution === "catalog") {
|
|
212
|
+
rules.push({ ...rule, source });
|
|
213
|
+
} else {
|
|
214
|
+
rules.push(rule);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
101
217
|
if (call.where !== undefined) {
|
|
102
218
|
rules.push({ rule: "filter", contribution: "caller", provenance: "caller" });
|
|
103
219
|
}
|
|
@@ -108,13 +224,37 @@ function appliedRules(call) {
|
|
|
108
224
|
provenance: "caller"
|
|
109
225
|
});
|
|
110
226
|
}
|
|
227
|
+
if (call.tenancyRules !== undefined) {
|
|
228
|
+
for (const rule of call.tenancyRules)
|
|
229
|
+
rules.push(rule);
|
|
230
|
+
}
|
|
231
|
+
if (call.ruleLines !== undefined) {
|
|
232
|
+
for (const rule of call.ruleLines)
|
|
233
|
+
rules.push(rule);
|
|
234
|
+
}
|
|
111
235
|
return rules;
|
|
112
236
|
}
|
|
113
237
|
function emit(schema, call, sink, outputs, hooks) {
|
|
238
|
+
const previousScope = activeScope;
|
|
239
|
+
const previousView = activeView;
|
|
240
|
+
activeScope = call.scope;
|
|
241
|
+
activeView = call.archive;
|
|
242
|
+
try {
|
|
243
|
+
emitRead(schema, call, sink, outputs, hooks);
|
|
244
|
+
} finally {
|
|
245
|
+
activeScope = previousScope;
|
|
246
|
+
activeView = previousView;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
function emitRead(schema, call, sink, outputs, hooks) {
|
|
114
250
|
const table = indexes(schema).get(call.table);
|
|
115
251
|
if (table === undefined) {
|
|
116
252
|
throwNamed2("OKM1120", call.table, [...indexes(schema).keys()], `Table ${call.table} is not in the schema. Accepted names: ${list([...indexes(schema).keys()])}.`);
|
|
117
253
|
}
|
|
254
|
+
if (call.build !== undefined) {
|
|
255
|
+
call.build(schema, table, sink, outputs, call);
|
|
256
|
+
return;
|
|
257
|
+
}
|
|
118
258
|
rejectOptions(call);
|
|
119
259
|
const selected = selectedColumns(table, call.select);
|
|
120
260
|
sink.mark(`${call.op}|${call.table}|`);
|
|
@@ -169,9 +309,10 @@ function emit(schema, call, sink, outputs, hooks) {
|
|
|
169
309
|
continue;
|
|
170
310
|
hooks?.join(schema, include, sink, "t", `i${String(index)}`, 0);
|
|
171
311
|
}
|
|
172
|
-
emitWhere(schema, table, call.where, sink, "t", 0);
|
|
312
|
+
emitWhere(schema, table, call.where, sink, "t", 0, false, call.after);
|
|
173
313
|
emitOrder(table, call.orderBy, sink, "t");
|
|
174
314
|
emitLimit(call, sink);
|
|
315
|
+
call.tail?.(sink);
|
|
175
316
|
}
|
|
176
317
|
function emitJoin(sink, parent, child, relation) {
|
|
177
318
|
for (let index = 0;index < relation.local.length; index += 1) {
|
|
@@ -190,26 +331,56 @@ function emitJoin(sink, parent, child, relation) {
|
|
|
190
331
|
sink.text(quote(local));
|
|
191
332
|
}
|
|
192
333
|
}
|
|
193
|
-
function
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
334
|
+
function holdArchive(table, sink, alias, appended) {
|
|
335
|
+
const archive = table.model.archive;
|
|
336
|
+
if (archive === undefined || activeView === "with")
|
|
337
|
+
return false;
|
|
338
|
+
sink.text(appended ? " and " : " where ");
|
|
339
|
+
sink.text(alias);
|
|
340
|
+
sink.text(".");
|
|
341
|
+
sink.text(quote(archive.at));
|
|
342
|
+
sink.text(activeView === "only" ? " is not null" : " is null");
|
|
343
|
+
sink.mark(activeView === "only" ? "archived|" : "active|");
|
|
344
|
+
return true;
|
|
345
|
+
}
|
|
346
|
+
function emitWhere(schema, table, where, sink, alias, depth, appended = false, extra) {
|
|
347
|
+
const held = schema.tenancy?.predicate({
|
|
348
|
+
table: table.model.name,
|
|
349
|
+
fieldSql: (field) => table.columns.get(field)?.sql,
|
|
350
|
+
encode: table.columns.get(schema.tenancy?.key ?? "")?.encode,
|
|
351
|
+
alias,
|
|
352
|
+
appended,
|
|
353
|
+
scope: activeScope,
|
|
354
|
+
sink
|
|
355
|
+
}) === true;
|
|
356
|
+
const archived = holdArchive(table, sink, alias, appended || held);
|
|
357
|
+
let started = appended || held || archived;
|
|
358
|
+
if (extra !== undefined) {
|
|
359
|
+
sink.text(started ? " and (" : " where (");
|
|
360
|
+
extra(sink, alias);
|
|
361
|
+
sink.text(")");
|
|
362
|
+
started = true;
|
|
200
363
|
}
|
|
201
|
-
|
|
202
|
-
|
|
364
|
+
for (const part of isOperator(where) && operatorName(where) === "and" ? operatorValue(where) : [where]) {
|
|
365
|
+
if (part === undefined)
|
|
366
|
+
continue;
|
|
367
|
+
const joined = isOperator(part);
|
|
368
|
+
if (!joined && !isRecord(part))
|
|
369
|
+
fail("OKM1121", WHERE_FIELDS);
|
|
370
|
+
if (!joined && Object.keys(part).length === 0)
|
|
371
|
+
continue;
|
|
372
|
+
sink.text(started ? " and " : " where ");
|
|
373
|
+
started = true;
|
|
374
|
+
if (joined && operatorName(part) === "or")
|
|
375
|
+
emitOr(schema, table, operatorValue(part), sink, alias, depth);
|
|
376
|
+
else
|
|
377
|
+
emitAnd(schema, table, part, sink, alias, depth);
|
|
203
378
|
}
|
|
204
|
-
const keys = Object.keys(where);
|
|
205
|
-
if (keys.length === 0)
|
|
206
|
-
return;
|
|
207
|
-
sink.text(appended ? " and " : " where ");
|
|
208
|
-
emitAnd(schema, table, where, sink, alias, depth);
|
|
209
379
|
}
|
|
380
|
+
var WHERE_FIELDS = "where must be an object of fields. Wrap an object value with eq.";
|
|
210
381
|
function emitAnd(schema, table, where, sink, alias, depth) {
|
|
211
382
|
if (!isRecord(where)) {
|
|
212
|
-
fail("OKM1121",
|
|
383
|
+
fail("OKM1121", WHERE_FIELDS);
|
|
213
384
|
}
|
|
214
385
|
const keys = Object.keys(where);
|
|
215
386
|
let wrote = false;
|
|
@@ -231,6 +402,7 @@ function emitPredicate(schema, table, key, value, sink, alias, depth) {
|
|
|
231
402
|
emitRelationFilter(schema, relation, value, sink, alias, depth);
|
|
232
403
|
return;
|
|
233
404
|
}
|
|
405
|
+
schema.tenancy?.guard(table.model.name, key, "where");
|
|
234
406
|
const column = table.columns.get(key);
|
|
235
407
|
if (column === undefined) {
|
|
236
408
|
throwNamed2("OKM1120", key, table.names, `Field ${key} is not on ${table.model.name}. Accepted names: ${list(table.names)}.`);
|
|
@@ -238,7 +410,7 @@ function emitPredicate(schema, table, key, value, sink, alias, depth) {
|
|
|
238
410
|
sink.mark(`w:${key}`);
|
|
239
411
|
emitOperand(schema, table, column, value, sink, alias, depth);
|
|
240
412
|
}
|
|
241
|
-
function emitOperand(schema, table, column, value, sink, alias, depth) {
|
|
413
|
+
function emitOperand(schema, table, column, value, sink, alias, depth, wrapped = false) {
|
|
242
414
|
const ref = `${alias}.${quote(column.sql)}`;
|
|
243
415
|
if (isOperator(value)) {
|
|
244
416
|
emitOperator(schema, table, column, ref, operatorName(value), operatorValue(value), sink, alias, depth);
|
|
@@ -250,9 +422,8 @@ function emitOperand(schema, table, column, value, sink, alias, depth) {
|
|
|
250
422
|
sink.text(" is null");
|
|
251
423
|
return;
|
|
252
424
|
}
|
|
253
|
-
if (typeof value === "object")
|
|
254
|
-
|
|
255
|
-
}
|
|
425
|
+
if (typeof value === "object")
|
|
426
|
+
takeObject(column, value, !wrapped);
|
|
256
427
|
sink.mark(":eq");
|
|
257
428
|
sink.text(ref);
|
|
258
429
|
sink.text(" = ");
|
|
@@ -272,7 +443,7 @@ function emitOperator(schema, table, column, ref, name, value, sink, alias, dept
|
|
|
272
443
|
return;
|
|
273
444
|
}
|
|
274
445
|
if (name === "eq") {
|
|
275
|
-
emitOperand(schema, table, column, value, sink, alias, depth);
|
|
446
|
+
emitOperand(schema, table, column, value, sink, alias, depth, true);
|
|
276
447
|
return;
|
|
277
448
|
}
|
|
278
449
|
if (name === "lt" || name === "lte" || name === "gt" || name === "gte") {
|
|
@@ -296,26 +467,25 @@ function emitOperator(schema, table, column, ref, name, value, sink, alias, dept
|
|
|
296
467
|
emitIn(column, ref, name, value, sink);
|
|
297
468
|
return;
|
|
298
469
|
}
|
|
299
|
-
if (name
|
|
470
|
+
if (TEXT_OPERATORS.includes(`,${name},`) && isTextColumn(column.dataType)) {
|
|
300
471
|
if (typeof value !== "string") {
|
|
301
472
|
fail("OKM1121", `${name} on ${column.field} needs a string.`);
|
|
302
473
|
}
|
|
303
474
|
sink.text(ref);
|
|
475
|
+
if (name === "like" || name === "ilike") {
|
|
476
|
+
sink.text(name === "like" ? " like " : " ilike ");
|
|
477
|
+
sink.param(value);
|
|
478
|
+
return;
|
|
479
|
+
}
|
|
304
480
|
sink.text(" like ");
|
|
305
|
-
|
|
306
|
-
sink.text(" escape E'\\\\'");
|
|
307
|
-
return;
|
|
308
|
-
}
|
|
309
|
-
if (name === "like" || name === "ilike") {
|
|
310
|
-
if (typeof value !== "string") {
|
|
481
|
+
if (name !== "startsWith" && name !== "endsWith" && name !== "contains") {
|
|
311
482
|
fail("OKM1121", `${name} on ${column.field} needs a string.`);
|
|
312
483
|
}
|
|
313
|
-
sink.
|
|
314
|
-
sink.text(
|
|
315
|
-
sink.param(value);
|
|
484
|
+
sink.param(literalPattern(value, name));
|
|
485
|
+
sink.text(" escape E'\\\\'");
|
|
316
486
|
return;
|
|
317
487
|
}
|
|
318
|
-
|
|
488
|
+
emitLazy(column, ref, name, value, sink);
|
|
319
489
|
}
|
|
320
490
|
function emitNot(schema, table, column, ref, value, sink, alias, depth) {
|
|
321
491
|
if (value === null) {
|
|
@@ -332,7 +502,7 @@ function emitNot(schema, table, column, ref, value, sink, alias, depth) {
|
|
|
332
502
|
}
|
|
333
503
|
}
|
|
334
504
|
sink.text("not (");
|
|
335
|
-
emitOperand(schema, table, column, value, sink, alias, depth);
|
|
505
|
+
emitOperand(schema, table, column, value, sink, alias, depth, true);
|
|
336
506
|
sink.text(")");
|
|
337
507
|
}
|
|
338
508
|
function emitIn(column, ref, name, value, sink) {
|
|
@@ -402,13 +572,10 @@ function emitRelationFilter(schema, relation, value, sink, alias, depth) {
|
|
|
402
572
|
if (name === "none" || name === "every")
|
|
403
573
|
sink.text("not ");
|
|
404
574
|
sink.text("exists (select 1 from ");
|
|
405
|
-
|
|
406
|
-
sink.text(" ");
|
|
407
|
-
sink.text(childAlias);
|
|
408
|
-
sink.text(" where ");
|
|
409
|
-
emitJoin(sink, alias, childAlias, relation);
|
|
575
|
+
emitRelationFrom(schema, relation, child, sink, alias, childAlias, depth);
|
|
410
576
|
const inner = operatorValue(value);
|
|
411
577
|
if (name === "every") {
|
|
578
|
+
emitWhere(schema, child, undefined, sink, childAlias, depth + 1, true);
|
|
412
579
|
sink.text(" and not (");
|
|
413
580
|
emitAnd(schema, child, inner ?? {}, sink, childAlias, depth + 1);
|
|
414
581
|
sink.text(")");
|
|
@@ -540,8 +707,7 @@ function parseOrder(field, value) {
|
|
|
540
707
|
fail("OKM1120", `orderBy ${field} must be asc or desc.`);
|
|
541
708
|
}
|
|
542
709
|
function rejectOptions(call) {
|
|
543
|
-
const
|
|
544
|
-
const accepted = call.op === "find" ? FIND_OPTIONS : call.op === "one" ? ONE_OPTIONS : FILTER_OPTIONS;
|
|
710
|
+
const accepted = readOptionNames(call.op);
|
|
545
711
|
const present = {};
|
|
546
712
|
if (call.where !== undefined)
|
|
547
713
|
present.where = call.where;
|
|
@@ -569,11 +735,18 @@ function readLimit(value) {
|
|
|
569
735
|
return value;
|
|
570
736
|
}
|
|
571
737
|
function requireValue(column, value) {
|
|
572
|
-
if (value === undefined || value === null
|
|
573
|
-
fail("OKM1121", `Field ${column.field} needs a value
|
|
738
|
+
if (value === undefined || value === null) {
|
|
739
|
+
fail("OKM1121", `Field ${column.field} needs a value.`);
|
|
574
740
|
}
|
|
741
|
+
if (typeof value === "object")
|
|
742
|
+
takeObject(column, value);
|
|
575
743
|
return value;
|
|
576
744
|
}
|
|
745
|
+
function takeObject(column, value, bare = false) {
|
|
746
|
+
if (bare || !column.accepts?.includes({}.toString.call(value).slice(8, -1))) {
|
|
747
|
+
fail("OKM1121", `Field ${column.field} rejects that object.`);
|
|
748
|
+
}
|
|
749
|
+
}
|
|
577
750
|
function projectExpr(sql, dataType) {
|
|
578
751
|
if (dataType === "timestamptz" || dataType === "timestamp")
|
|
579
752
|
return jsonExpr(sql, dataType);
|
|
@@ -630,8 +803,9 @@ function indexes(schema) {
|
|
|
630
803
|
}
|
|
631
804
|
names.sort();
|
|
632
805
|
const relations = new Map;
|
|
633
|
-
for (const relation of model.relations)
|
|
806
|
+
for (const relation of model.relations) {
|
|
634
807
|
relations.set(relation.name, relation);
|
|
808
|
+
}
|
|
635
809
|
built.set(model.name, { model, columns, relations, names });
|
|
636
810
|
}
|
|
637
811
|
indexesOf.set(schema, built);
|
|
@@ -661,9 +835,16 @@ function list(names) {
|
|
|
661
835
|
function isRecord(value) {
|
|
662
836
|
return typeof value === "object" && value !== null && !Array.isArray(value) && !isOperator(value);
|
|
663
837
|
}
|
|
838
|
+
var FAIL_FIX = {
|
|
839
|
+
OKM1121: "Use eq for an object value, or has, none, or every for a relation.",
|
|
840
|
+
OKM1101: "Pass limit, or call .all(reason)."
|
|
841
|
+
};
|
|
842
|
+
function registerFailFix(code, summary) {
|
|
843
|
+
FAIL_FIX[code] = summary;
|
|
844
|
+
}
|
|
664
845
|
function fail(code, message) {
|
|
665
|
-
const
|
|
666
|
-
throw new OkmError2(code, message,
|
|
846
|
+
const summary = FAIL_FIX[code];
|
|
847
|
+
throw new OkmError2(code, message, summary === undefined ? undefined : { fix: { summary } });
|
|
667
848
|
}
|
|
668
849
|
function plain(value) {
|
|
669
850
|
if (isOperator(value)) {
|
|
@@ -685,382 +866,4 @@ function hasMany(include) {
|
|
|
685
866
|
return Object.keys(include).length > 0;
|
|
686
867
|
}
|
|
687
868
|
|
|
688
|
-
|
|
689
|
-
var PLAN_LIMIT = 64;
|
|
690
|
-
var SINGLE_ENDPOINT = {
|
|
691
|
-
endpoint: "primary",
|
|
692
|
-
role: "primary",
|
|
693
|
-
reason: "single-endpoint"
|
|
694
|
-
};
|
|
695
|
-
function createClient(schema, pool, options) {
|
|
696
|
-
const connected = checkServer(pool, schema, options.http, options.requireMeta === true, callOptions(options), {
|
|
697
|
-
...options.catalog !== undefined ? { catalog: options.catalog } : {},
|
|
698
|
-
...options.catalogDir !== undefined ? { catalogDir: options.catalogDir } : {}
|
|
699
|
-
});
|
|
700
|
-
const session = {
|
|
701
|
-
schema,
|
|
702
|
-
pool,
|
|
703
|
-
ownsPool: options.ownsPool,
|
|
704
|
-
http: options.http,
|
|
705
|
-
includeValues: options.includeValues === true,
|
|
706
|
-
logger: options.logger,
|
|
707
|
-
connected,
|
|
708
|
-
cache: new Map
|
|
709
|
-
};
|
|
710
|
-
const tables = {};
|
|
711
|
-
for (const name of Object.keys(schema.model)) {
|
|
712
|
-
tables[name] = tableApi(session, name);
|
|
713
|
-
}
|
|
714
|
-
let closing;
|
|
715
|
-
const client = {
|
|
716
|
-
...tables,
|
|
717
|
-
table(name) {
|
|
718
|
-
const found = tables[name];
|
|
719
|
-
if (found !== undefined)
|
|
720
|
-
return found;
|
|
721
|
-
try {
|
|
722
|
-
throwNamed2("OKM1120", name, Object.keys(tables), `Table ${name} is not in the schema. Accepted names: ${Object.keys(tables).join(", ")}.`);
|
|
723
|
-
} catch (error) {
|
|
724
|
-
if (error instanceof OkmError2)
|
|
725
|
-
throw attachHttp(session.http, error);
|
|
726
|
-
throw error;
|
|
727
|
-
}
|
|
728
|
-
},
|
|
729
|
-
close() {
|
|
730
|
-
if (!options.ownsPool)
|
|
731
|
-
return Promise.resolve();
|
|
732
|
-
closing ??= pool.close();
|
|
733
|
-
return closing;
|
|
734
|
-
},
|
|
735
|
-
connected
|
|
736
|
-
};
|
|
737
|
-
attachAsyncDispose(client, () => client.close());
|
|
738
|
-
return client;
|
|
739
|
-
}
|
|
740
|
-
function attachAsyncDispose(client, close) {
|
|
741
|
-
const symbol = Symbol.asyncDispose;
|
|
742
|
-
if (typeof symbol !== "symbol")
|
|
743
|
-
return;
|
|
744
|
-
Object.defineProperty(client, symbol, {
|
|
745
|
-
configurable: true,
|
|
746
|
-
enumerable: false,
|
|
747
|
-
writable: true,
|
|
748
|
-
value: () => close()
|
|
749
|
-
});
|
|
750
|
-
}
|
|
751
|
-
function tableApi(session, table) {
|
|
752
|
-
return {
|
|
753
|
-
find(options = {}) {
|
|
754
|
-
return start(session, "find", table, options, {});
|
|
755
|
-
},
|
|
756
|
-
one(options = {}) {
|
|
757
|
-
return start(session, "one", table, options, {});
|
|
758
|
-
},
|
|
759
|
-
count(options = {}) {
|
|
760
|
-
return start(session, "count", table, options, {});
|
|
761
|
-
},
|
|
762
|
-
exists(options = {}) {
|
|
763
|
-
return start(session, "exists", table, options, {});
|
|
764
|
-
},
|
|
765
|
-
insert(data, options = {}) {
|
|
766
|
-
return writeHandle(session, "insert", table, data, options, {});
|
|
767
|
-
},
|
|
768
|
-
update(target, options = {}) {
|
|
769
|
-
return writeHandle(session, "update", table, target, options, {});
|
|
770
|
-
},
|
|
771
|
-
delete(target, options = {}) {
|
|
772
|
-
return writeHandle(session, "delete", table, target, options, {});
|
|
773
|
-
}
|
|
774
|
-
};
|
|
775
|
-
}
|
|
776
|
-
var writers;
|
|
777
|
-
function loadWrite() {
|
|
778
|
-
writers ??= import("./x5x59srr.js");
|
|
779
|
-
return writers;
|
|
780
|
-
}
|
|
781
|
-
function writeHandle(session, op, table, input, options, mods) {
|
|
782
|
-
let pending;
|
|
783
|
-
const run = () => {
|
|
784
|
-
pending ??= runWrite(session, op, table, input, options, mods);
|
|
785
|
-
return pending;
|
|
786
|
-
};
|
|
787
|
-
const self = {
|
|
788
|
-
then(onFulfilled, onRejected) {
|
|
789
|
-
return run().then(onFulfilled, onRejected);
|
|
790
|
-
},
|
|
791
|
-
catch(onRejected) {
|
|
792
|
-
return self.then(undefined, onRejected);
|
|
793
|
-
},
|
|
794
|
-
finally(fn) {
|
|
795
|
-
return self.then((value) => {
|
|
796
|
-
fn();
|
|
797
|
-
return value;
|
|
798
|
-
}, (error) => {
|
|
799
|
-
fn();
|
|
800
|
-
throw error;
|
|
801
|
-
});
|
|
802
|
-
},
|
|
803
|
-
safe() {
|
|
804
|
-
return safe2(run());
|
|
805
|
-
},
|
|
806
|
-
sql() {
|
|
807
|
-
return loadWrite().then((mod) => mod.explainWrite(session.schema, op, table, input, options, mods));
|
|
808
|
-
},
|
|
809
|
-
expect(count) {
|
|
810
|
-
return writeHandle(session, op, table, input, options, { ...mods, expect: count });
|
|
811
|
-
},
|
|
812
|
-
all(reason) {
|
|
813
|
-
return writeHandle(session, op, table, input, options, { ...mods, all: reason });
|
|
814
|
-
}
|
|
815
|
-
};
|
|
816
|
-
return self;
|
|
817
|
-
}
|
|
818
|
-
async function runWrite(session, op, table, input, options, mods) {
|
|
819
|
-
await session.connected;
|
|
820
|
-
const mod = await loadWrite();
|
|
821
|
-
try {
|
|
822
|
-
return await mod.executeWrite({ schema: session.schema, pool: session.pool }, op, table, input, options, mods);
|
|
823
|
-
} catch (error) {
|
|
824
|
-
throw logged(session, await mapError(session, error));
|
|
825
|
-
}
|
|
826
|
-
}
|
|
827
|
-
function start(session, op, table, options, mods) {
|
|
828
|
-
const call = readCall(op, table, options, mods.all);
|
|
829
|
-
let prepared;
|
|
830
|
-
const prepare = async () => {
|
|
831
|
-
if (prepared !== undefined)
|
|
832
|
-
return prepared;
|
|
833
|
-
const hooks = await hooksFor(call);
|
|
834
|
-
const bound = bindCall(session.schema, call, hooks);
|
|
835
|
-
let plan = session.cache.get(bound.key);
|
|
836
|
-
if (plan === undefined) {
|
|
837
|
-
plan = compileCall(session.schema, call, bound.key, hooks);
|
|
838
|
-
if (session.cache.size >= PLAN_LIMIT) {
|
|
839
|
-
const oldest = session.cache.keys().next().value;
|
|
840
|
-
if (oldest !== undefined)
|
|
841
|
-
session.cache.delete(oldest);
|
|
842
|
-
}
|
|
843
|
-
session.cache.set(bound.key, plan);
|
|
844
|
-
}
|
|
845
|
-
prepared = { plan, params: bound.params };
|
|
846
|
-
return prepared;
|
|
847
|
-
};
|
|
848
|
-
const run = async () => {
|
|
849
|
-
await session.connected;
|
|
850
|
-
const { plan, params } = await prepare();
|
|
851
|
-
try {
|
|
852
|
-
const result = await session.pool.execute(plan.text, params, callOptions(mods));
|
|
853
|
-
const value = plan.outputs.includes.length === 0 ? decodeResult(plan, result.rows, table) : (await import("./hadrfb9h.js")).decodeIncluded(plan, result.rows, table);
|
|
854
|
-
if (mods.required === true && value === null) {
|
|
855
|
-
throw new OkmError2("not_found", `one() on ${table} matched no row.`, withHttp(session.http, { kind: "not_found", table }));
|
|
856
|
-
}
|
|
857
|
-
return value;
|
|
858
|
-
} catch (error) {
|
|
859
|
-
throw logged(session, await mapError(session, error));
|
|
860
|
-
}
|
|
861
|
-
};
|
|
862
|
-
let pending;
|
|
863
|
-
const once = () => {
|
|
864
|
-
pending ??= run();
|
|
865
|
-
return pending;
|
|
866
|
-
};
|
|
867
|
-
const self = {
|
|
868
|
-
then(onFulfilled, onRejected) {
|
|
869
|
-
return once().then(onFulfilled, onRejected);
|
|
870
|
-
},
|
|
871
|
-
catch(onRejected) {
|
|
872
|
-
return this.then(undefined, onRejected);
|
|
873
|
-
},
|
|
874
|
-
finally(fn) {
|
|
875
|
-
return this.then((value) => {
|
|
876
|
-
fn();
|
|
877
|
-
return value;
|
|
878
|
-
}, (error) => {
|
|
879
|
-
fn();
|
|
880
|
-
throw error;
|
|
881
|
-
});
|
|
882
|
-
},
|
|
883
|
-
inspect() {
|
|
884
|
-
if (call.include === undefined && prepared !== undefined) {
|
|
885
|
-
return inspection(prepared.plan, prepared.params, call);
|
|
886
|
-
}
|
|
887
|
-
if (call.include === undefined) {
|
|
888
|
-
const bound = bindNow(session.schema, call);
|
|
889
|
-
prepared = bound;
|
|
890
|
-
return inspection(bound.plan, bound.params, call);
|
|
891
|
-
}
|
|
892
|
-
return prepare().then(({ plan, params }) => inspection(plan, params, call));
|
|
893
|
-
},
|
|
894
|
-
sql() {
|
|
895
|
-
if (call.include === undefined && prepared !== undefined) {
|
|
896
|
-
return { text: prepared.plan.text, params: prepared.params };
|
|
897
|
-
}
|
|
898
|
-
if (call.include === undefined) {
|
|
899
|
-
const { plan, params } = bindNow(session.schema, call);
|
|
900
|
-
prepared = { plan, params };
|
|
901
|
-
return { text: plan.text, params };
|
|
902
|
-
}
|
|
903
|
-
return prepare().then(({ plan, params }) => ({ text: plan.text, params }));
|
|
904
|
-
},
|
|
905
|
-
safe() {
|
|
906
|
-
return safe2(once());
|
|
907
|
-
},
|
|
908
|
-
all(reason) {
|
|
909
|
-
return start(session, op, table, options, { ...mods, all: reason });
|
|
910
|
-
},
|
|
911
|
-
required() {
|
|
912
|
-
return start(session, op, table, options, { ...mods, required: true });
|
|
913
|
-
},
|
|
914
|
-
stream() {
|
|
915
|
-
return streamRows(session, call, prepare);
|
|
916
|
-
}
|
|
917
|
-
};
|
|
918
|
-
return self;
|
|
919
|
-
}
|
|
920
|
-
async function* streamRows(session, call, prepare) {
|
|
921
|
-
await session.connected;
|
|
922
|
-
if (session.pool.capabilities.stream !== true || session.pool.stream === undefined) {
|
|
923
|
-
throw logged(session, new OkmError2("OKM1111", "stream needs a driver with stream: true. This driver does not have it.", withHttp(session.http, {
|
|
924
|
-
fix: { summary: "Use a driver that sets stream, or read with find." }
|
|
925
|
-
})));
|
|
926
|
-
}
|
|
927
|
-
const { plan, params } = await prepare();
|
|
928
|
-
const decode = plan.outputs.includes.length === 0 ? decodeResult : (await import("./hadrfb9h.js")).decodeIncluded;
|
|
929
|
-
for await (const chunk of session.pool.stream(plan.text, params)) {
|
|
930
|
-
const decoded = decode(plan, chunk, call.table);
|
|
931
|
-
if (!Array.isArray(decoded))
|
|
932
|
-
continue;
|
|
933
|
-
for (const row of decoded)
|
|
934
|
-
yield row;
|
|
935
|
-
}
|
|
936
|
-
}
|
|
937
|
-
function readCall(op, table, options, all) {
|
|
938
|
-
const record = options;
|
|
939
|
-
const accepted = op === "find" ? ["include", "limit", "orderBy", "select", "where"] : op === "one" ? ["include", "orderBy", "select", "where"] : ["where"];
|
|
940
|
-
for (const key of Object.keys(record)) {
|
|
941
|
-
if (!accepted.includes(key)) {
|
|
942
|
-
throwNamed2("OKM1120", key, accepted, `Option ${key} is not accepted by ${op}. Accepted options: ${accepted.join(", ")}.`);
|
|
943
|
-
}
|
|
944
|
-
}
|
|
945
|
-
return {
|
|
946
|
-
op,
|
|
947
|
-
table,
|
|
948
|
-
...record.where !== undefined ? { where: record.where } : {},
|
|
949
|
-
...record.select !== undefined ? { select: record.select } : {},
|
|
950
|
-
...record.orderBy !== undefined ? { orderBy: record.orderBy } : {},
|
|
951
|
-
...record.limit !== undefined ? { limit: record.limit } : {},
|
|
952
|
-
...record.include !== undefined ? { include: record.include } : {},
|
|
953
|
-
...all !== undefined ? { all } : {}
|
|
954
|
-
};
|
|
955
|
-
}
|
|
956
|
-
async function checkServer(pool, schema, http, requireMeta, options, source) {
|
|
957
|
-
let rows;
|
|
958
|
-
try {
|
|
959
|
-
const result = await pool.execute(SERVER_CHECK, undefined, options);
|
|
960
|
-
rows = result.rows;
|
|
961
|
-
} catch (error) {
|
|
962
|
-
const { mapPostgresError } = await import("./zhc5vmgf.js");
|
|
963
|
-
throw mapPostgresError(error, mapOptions(http, false));
|
|
964
|
-
}
|
|
965
|
-
const version = rows[0]?.[1];
|
|
966
|
-
if (version === null || version === undefined || !version.startsWith("PostgreSQL")) {
|
|
967
|
-
throw new OkmError2("OKM1801", "The server is not PostgreSQL. The schema dialect is postgres.", withHttp(http, {
|
|
968
|
-
fix: { summary: "Open the schema with the Postgres driver that matches it." }
|
|
969
|
-
}));
|
|
970
|
-
}
|
|
971
|
-
const recorded = rows[0]?.[2];
|
|
972
|
-
if (recorded === null || recorded === undefined || recorded.length === 0) {
|
|
973
|
-
if (requireMeta) {
|
|
974
|
-
throw new OkmError2("OKM1520", "okm_meta has no catalog hash.", withHttp(http, {
|
|
975
|
-
fix: {
|
|
976
|
-
summary: "Apply migrations so okm_meta records the catalog, or omit requireMeta to adopt this database."
|
|
977
|
-
}
|
|
978
|
-
}));
|
|
979
|
-
}
|
|
980
|
-
} else {
|
|
981
|
-
const { assertCompatible } = await import("./qk3hxfja.js");
|
|
982
|
-
try {
|
|
983
|
-
await assertCompatible(pool, schema, recorded, source, options);
|
|
984
|
-
} catch (error) {
|
|
985
|
-
if (error instanceof OkmError2)
|
|
986
|
-
throw attachHttp(http, error);
|
|
987
|
-
throw error;
|
|
988
|
-
}
|
|
989
|
-
}
|
|
990
|
-
const requires = schema.requires?.postgres;
|
|
991
|
-
if (requires === undefined)
|
|
992
|
-
return;
|
|
993
|
-
const major = Math.floor(Number(rows[0]?.[0] ?? "0") / 1e4);
|
|
994
|
-
const match = /^>=(\d+)$/.exec(requires.trim());
|
|
995
|
-
const need = match?.[1] === undefined ? undefined : Number(match[1]);
|
|
996
|
-
if (need === undefined || major < need) {
|
|
997
|
-
throw new OkmError2("OKM1802", `The server is PostgreSQL ${String(major)}. schema({ requires }) asks for ${requires}.`, withHttp(http, {
|
|
998
|
-
fix: { summary: "Upgrade the server, or lower requires to a version the server meets." }
|
|
999
|
-
}));
|
|
1000
|
-
}
|
|
1001
|
-
}
|
|
1002
|
-
async function mapError(session, error) {
|
|
1003
|
-
if (error instanceof OkmError2) {
|
|
1004
|
-
return session.http === undefined ? error : new OkmError2(error.code, error.message, { ...errorFields(error), http: session.http });
|
|
1005
|
-
}
|
|
1006
|
-
const { mapPostgresError } = await import("./zhc5vmgf.js");
|
|
1007
|
-
return mapPostgresError(error, mapOptions(session.http, session.includeValues));
|
|
1008
|
-
}
|
|
1009
|
-
function errorFields(error) {
|
|
1010
|
-
return {
|
|
1011
|
-
kind: error.kind,
|
|
1012
|
-
...error.table !== undefined ? { table: error.table } : {},
|
|
1013
|
-
...error.columns.length > 0 ? { columns: error.columns } : {},
|
|
1014
|
-
fix: error.fix
|
|
1015
|
-
};
|
|
1016
|
-
}
|
|
1017
|
-
var includeHooks;
|
|
1018
|
-
function hooksFor(call) {
|
|
1019
|
-
if (call.include === undefined)
|
|
1020
|
-
return Promise.resolve(undefined);
|
|
1021
|
-
includeHooks ??= import("./hadrfb9h.js").then((mod) => mod.includeHooks);
|
|
1022
|
-
return includeHooks;
|
|
1023
|
-
}
|
|
1024
|
-
function bindNow(schema, call) {
|
|
1025
|
-
const bound = bindCall(schema, call);
|
|
1026
|
-
const plan = compileCall(schema, call, bound.key);
|
|
1027
|
-
return { plan, params: bound.params };
|
|
1028
|
-
}
|
|
1029
|
-
function inspection(plan, params, call) {
|
|
1030
|
-
return {
|
|
1031
|
-
intent: logicalIntent(call),
|
|
1032
|
-
rules: appliedRules(call),
|
|
1033
|
-
plan: { strategy: "postgres:single-statement", statements: 1, fingerprint: plan.fingerprint },
|
|
1034
|
-
sql: { text: plan.text, params },
|
|
1035
|
-
routing: SINGLE_ENDPOINT
|
|
1036
|
-
};
|
|
1037
|
-
}
|
|
1038
|
-
function withHttp(http, options) {
|
|
1039
|
-
return http === undefined ? options : { ...options, http };
|
|
1040
|
-
}
|
|
1041
|
-
function attachHttp(http, error) {
|
|
1042
|
-
if (http === undefined)
|
|
1043
|
-
return error;
|
|
1044
|
-
return new OkmError2(error.code, error.message, { ...errorFields(error), http });
|
|
1045
|
-
}
|
|
1046
|
-
function callOptions(input) {
|
|
1047
|
-
if (input.signal === undefined && input.timeout === undefined)
|
|
1048
|
-
return;
|
|
1049
|
-
return {
|
|
1050
|
-
...input.signal !== undefined ? { signal: input.signal } : {},
|
|
1051
|
-
...input.timeout !== undefined ? { timeout: input.timeout } : {}
|
|
1052
|
-
};
|
|
1053
|
-
}
|
|
1054
|
-
var SERVER_CHECK = "select current_setting('server_version_num'), version(), (select (xpath('//catalog_hash/text()', query_to_xml('select catalog_hash from okm_meta where id = ''head''', true, false, '')))[1]::text where to_regclass('okm_meta') is not null)";
|
|
1055
|
-
function mapOptions(http, includeValues) {
|
|
1056
|
-
return {
|
|
1057
|
-
...http !== undefined ? { http } : {},
|
|
1058
|
-
...includeValues ? { includeValues: true } : {}
|
|
1059
|
-
};
|
|
1060
|
-
}
|
|
1061
|
-
function logged(session, error) {
|
|
1062
|
-
session.logger?.error?.({ code: error.code, summary: error.summary });
|
|
1063
|
-
return error;
|
|
1064
|
-
}
|
|
1065
|
-
|
|
1066
|
-
export { decodeResult, emitJoin, emitWhere, emitOrder, emitPrimaryOrder, selectedColumns, rejectKeys, readLimit, projectExpr, jsonExpr, decodeRow, cell, indexes, quote, list, isRecord, fail, createClient };
|
|
869
|
+
export { withRowFilters, installArchiveInspect, archiveRules, readOptionNames, installOperatorSql, readNeedsOperatorSql, emitRelationFrom, bindCall, compileCall, decodeResult, logicalIntent, appliedRules, emitJoin, emitWhere, emitOrder, emitPrimaryOrder, selectedColumns, parseOrder, rejectKeys, readLimit, takeObject, projectExpr, jsonExpr, decodeRow, cell, indexes, quote, list, isRecord, registerFailFix, fail };
|