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.
Files changed (181) hide show
  1. package/README.md +32 -16
  2. package/dist/adapters/capabilities.d.ts +34 -0
  3. package/dist/adapters/pg/bunsql-extra.d.ts +17 -0
  4. package/dist/adapters/pg/bunsql.d.ts +60 -0
  5. package/dist/adapters/pg/call.d.ts +2 -12
  6. package/dist/adapters/pg/nodepostgres-extra.d.ts +18 -0
  7. package/dist/adapters/pg/nodepostgres.d.ts +57 -0
  8. package/dist/adapters/pg/postgres-extra.d.ts +50 -1
  9. package/dist/adapters/pg/postgresjs.d.ts +0 -4
  10. package/dist/adapters/pg/session.d.ts +94 -0
  11. package/dist/adapters/pg/watch.d.ts +18 -0
  12. package/dist/contracts/catalog/object.d.ts +1 -0
  13. package/dist/contracts/catalog/types.d.ts +15 -8
  14. package/dist/contracts/connection.d.ts +13 -0
  15. package/dist/contracts/driver.d.ts +18 -5
  16. package/dist/contracts/error.d.ts +17 -1
  17. package/dist/contracts/generator.d.ts +63 -0
  18. package/dist/contracts/index.d.ts +2 -2
  19. package/dist/contracts/index.js +1 -1
  20. package/dist/contracts/internal.js +11 -9
  21. package/dist/contracts/location.d.ts +24 -0
  22. package/dist/contracts/rows.d.ts +15 -0
  23. package/dist/dialects/pg/archive-bind.d.ts +22 -0
  24. package/dist/dialects/pg/column.d.ts +126 -9
  25. package/dist/dialects/pg/compile.d.ts +1 -0
  26. package/dist/dialects/pg/custom.d.ts +6 -0
  27. package/dist/dialects/pg/filter-parse.d.ts +19 -0
  28. package/dist/dialects/pg/filters.d.ts +19 -0
  29. package/dist/dialects/pg/index.d.ts +11 -2
  30. package/dist/dialects/pg/index.js +159 -1063
  31. package/dist/dialects/pg/keys.d.ts +19 -3
  32. package/dist/dialects/pg/model.d.ts +223 -2
  33. package/dist/dialects/pg/operator-fit.d.ts +86 -0
  34. package/dist/dialects/pg/operators.d.ts +39 -1
  35. package/dist/dialects/pg/ops/arr-append.d.ts +9 -0
  36. package/dist/dialects/pg/ops/arr-ns.d.ts +7 -0
  37. package/dist/dialects/pg/ops/arr-remove.d.ts +9 -0
  38. package/dist/dialects/pg/ops/containedBy.d.ts +9 -0
  39. package/dist/dialects/pg/ops/contains.d.ts +8 -4
  40. package/dist/dialects/pg/ops/hasAnyKey.d.ts +8 -0
  41. package/dist/dialects/pg/ops/hasKey.d.ts +8 -0
  42. package/dist/dialects/pg/ops/json-ns.d.ts +6 -0
  43. package/dist/dialects/pg/ops/json-set.d.ts +12 -0
  44. package/dist/dialects/pg/ops/matches.d.ts +16 -0
  45. package/dist/dialects/pg/ops/overlaps.d.ts +9 -0
  46. package/dist/dialects/pg/ops/path.d.ts +13 -0
  47. package/dist/dialects/pg/preset.d.ts +76 -0
  48. package/dist/dialects/pg/relations.d.ts +59 -2
  49. package/dist/dialects/pg/schema.d.ts +109 -5
  50. package/dist/dialects/pg/table.d.ts +130 -11
  51. package/dist/dialects/pg/temporal.d.ts +9 -7
  52. package/dist/dialects/pg/tenancy.d.ts +94 -0
  53. package/dist/dialects/pg/through.d.ts +21 -0
  54. package/dist/dialects/pg/trait.d.ts +141 -0
  55. package/dist/dialects/pg/where.d.ts +37 -0
  56. package/dist/runtime/aggregate.d.ts +25 -0
  57. package/dist/runtime/archive-handle.d.ts +18 -0
  58. package/dist/runtime/archive-rules.d.ts +12 -0
  59. package/dist/runtime/archive.d.ts +73 -0
  60. package/dist/runtime/batch.d.ts +20 -0
  61. package/dist/runtime/client.d.ts +114 -5
  62. package/dist/runtime/conflict.d.ts +2 -1
  63. package/dist/runtime/exposure.d.ts +27 -0
  64. package/dist/runtime/hookm.d.ts +39 -0
  65. package/dist/runtime/ids/index.d.ts +11 -0
  66. package/dist/runtime/ids/index.js +193 -0
  67. package/dist/runtime/ids/okid-extended.d.ts +19 -0
  68. package/dist/runtime/ids/okid-generator.d.ts +21 -0
  69. package/dist/runtime/ids/okid-shared.d.ts +89 -0
  70. package/dist/runtime/ids/okid.d.ts +24 -0
  71. package/dist/runtime/ids/uuid.d.ts +21 -0
  72. package/dist/runtime/lock.d.ts +22 -0
  73. package/dist/runtime/operator-sql.d.ts +8 -0
  74. package/dist/runtime/page.d.ts +25 -0
  75. package/dist/runtime/pg/bun.d.ts +30 -0
  76. package/dist/runtime/pg/bun.js +52 -0
  77. package/dist/runtime/pg/pg.d.ts +29 -0
  78. package/dist/runtime/pg/pg.js +52 -0
  79. package/dist/runtime/pg/pglite.js +16 -17
  80. package/dist/runtime/pg/postgresjs.js +14 -24
  81. package/dist/runtime/plan.d.ts +146 -7
  82. package/dist/runtime/preset-stack.d.ts +17 -0
  83. package/dist/runtime/presets.d.ts +48 -0
  84. package/dist/runtime/read-stream.d.ts +26 -0
  85. package/dist/runtime/safety/index.d.ts +159 -0
  86. package/dist/runtime/safety/index.js +294 -0
  87. package/dist/runtime/safety-hook.d.ts +42 -0
  88. package/dist/runtime/server-check.d.ts +34 -0
  89. package/dist/runtime/tenancy/index.d.ts +37 -0
  90. package/dist/runtime/tenancy/index.js +365 -0
  91. package/dist/runtime/trait-read.d.ts +30 -0
  92. package/dist/runtime/traits/index.d.ts +128 -0
  93. package/dist/runtime/traits/index.js +509 -0
  94. package/dist/runtime/transaction.d.ts +19 -0
  95. package/dist/runtime/tx.d.ts +22 -7
  96. package/dist/runtime/types.d.ts +376 -27
  97. package/dist/runtime/validate/bind.d.ts +11 -0
  98. package/dist/runtime/validate/closed.d.ts +16 -0
  99. package/dist/runtime/validate/engine.d.ts +41 -0
  100. package/dist/runtime/validate/hook.d.ts +17 -0
  101. package/dist/runtime/validate/index.d.ts +21 -0
  102. package/dist/runtime/validate/index.js +175 -0
  103. package/dist/runtime/validate/load.d.ts +13 -0
  104. package/dist/runtime/validate/places.d.ts +41 -0
  105. package/dist/runtime/validate/rules.d.ts +108 -0
  106. package/dist/runtime/validate/surface.d.ts +102 -0
  107. package/dist/runtime/write.d.ts +30 -5
  108. package/dist/shared/{fgqgsb5z.js → 0qkxbm1f.js} +2 -2
  109. package/dist/shared/0v1g424p.js +26 -0
  110. package/dist/shared/10tjenmd.js +210 -0
  111. package/dist/shared/{xqbhg6cn.js → 1dp5knt7.js} +95 -10
  112. package/dist/shared/38crxbgn.js +229 -0
  113. package/dist/shared/3a5ne750.js +158 -0
  114. package/dist/shared/4b9da9d7.js +138 -0
  115. package/dist/shared/4e4c6vq2.js +62 -0
  116. package/dist/shared/{x5x59srr.js → 4jzzghr6.js} +416 -83
  117. package/dist/shared/{ycwz42az.js → 52szzakm.js} +6 -26
  118. package/dist/shared/{05w44d8g.js → 5z4q2zda.js} +567 -18
  119. package/dist/shared/672t5vym.js +12 -0
  120. package/dist/shared/7m88x3e9.js +45 -0
  121. package/dist/shared/7mht49yd.js +1091 -0
  122. package/dist/shared/{mtkwv8wx.js → 7p2wbz7f.js} +1 -1
  123. package/dist/shared/83eaefwr.js +10 -0
  124. package/dist/shared/96ps4g1z.js +32 -0
  125. package/dist/shared/97g2jnrw.js +76 -0
  126. package/dist/shared/{xy79j5hq.js → 9k6vetvf.js} +25 -36
  127. package/dist/shared/{w8samvk1.js → a1gdp7j9.js} +1 -1
  128. package/dist/shared/a5sxz52y.js +13 -0
  129. package/dist/shared/{dynq16vb.js → b066g80h.js} +8 -6
  130. package/dist/shared/cstgkzdx.js +154 -0
  131. package/dist/shared/{523ft3e8.js → d04j53v8.js} +6 -5
  132. package/dist/shared/{y9m15dfv.js → d2whjz4k.js} +15 -9
  133. package/dist/shared/dd4f8mnx.js +307 -0
  134. package/dist/shared/e6wg00wd.js +164 -0
  135. package/dist/shared/e7a268fy.js +195 -0
  136. package/dist/shared/{kprcwaha.js → eaxwrgby.js} +4 -3
  137. package/dist/shared/{dkn3y60a.js → ecdzej21.js} +76 -112
  138. package/dist/shared/{hadrfb9h.js → edwqqryd.js} +17 -14
  139. package/dist/shared/{m3ypagh5.js → eqmtn1bj.js} +57 -5
  140. package/dist/shared/g1b5zzj9.js +75 -0
  141. package/dist/shared/g9f0zqce.js +376 -0
  142. package/dist/shared/ge03bfca.js +64 -0
  143. package/dist/shared/{yd1kzwn2.js → jns2wx4t.js} +55 -12
  144. package/dist/shared/k3ted12y.js +33 -0
  145. package/dist/shared/k7dwkhvq.js +31 -0
  146. package/dist/shared/{hd293j9g.js → kyxb62mc.js} +14 -2
  147. package/dist/shared/m9tv7xn7.js +404 -0
  148. package/dist/shared/mrq77sgw.js +55 -0
  149. package/dist/shared/mxmcdsh5.js +450 -0
  150. package/dist/shared/n442h54c.js +14 -0
  151. package/dist/shared/n47rd821.js +442 -0
  152. package/dist/shared/n4jqhpqz.js +27 -0
  153. package/dist/shared/{mkjmqjv4.js → nfzc5z0w.js} +2 -104
  154. package/dist/shared/nsva2a6b.js +42 -0
  155. package/dist/shared/ps43134h.js +69 -0
  156. package/dist/shared/{8b04d28c.js → py3nh7jy.js} +2 -2
  157. package/dist/shared/qa5dsc7z.js +86 -0
  158. package/dist/shared/{g5fhqmwe.js → qxa0yb9j.js} +6 -4
  159. package/dist/shared/r6nwkxnt.js +86 -0
  160. package/dist/shared/{jjb7yht9.js → rtnaqre0.js} +3 -3
  161. package/dist/shared/rwmrejjz.js +26 -0
  162. package/dist/shared/{zhc5vmgf.js → s6gqfn5j.js} +2 -2
  163. package/dist/shared/sacqha1y.js +70 -0
  164. package/dist/shared/{qk3hxfja.js → sfh4pk75.js} +5 -5
  165. package/dist/shared/{j5cn2963.js → t4tbsy9h.js} +13 -64
  166. package/dist/shared/{ngk8gj61.js → tbszx8mz.js} +15 -1
  167. package/dist/shared/v18jva5b.js +90 -0
  168. package/dist/shared/x9v1qgeb.js +201 -0
  169. package/dist/shared/xatfg0gn.js +193 -0
  170. package/dist/shared/xkz8g6by.js +225 -0
  171. package/dist/shared/{rgs0st28.js → xrfnq5k8.js} +237 -434
  172. package/dist/shared/ytg3xf9p.js +149 -0
  173. package/dist/shared/yw5c610b.js +48 -0
  174. package/dist/shared/zd93g3rq.js +31 -0
  175. package/dist/shared/zt8y0wzb.js +132 -0
  176. package/dist/shared/{p0cbr3g2.js → zwmqxb0g.js} +1 -1
  177. package/dist/tooling/cli.js +14 -10
  178. package/dist/tooling/migrate/commands.d.ts +10 -0
  179. package/dist/tooling/migrate/engine.d.ts +36 -0
  180. package/package.json +41 -4
  181. package/dist/shared/gk7cs7sp.js +0 -566
@@ -0,0 +1,450 @@
1
+ import {
2
+ archiveRules,
3
+ readOptionNames,
4
+ readNeedsOperatorSql,
5
+ bindCall,
6
+ compileCall,
7
+ decodeResult,
8
+ logicalIntent,
9
+ appliedRules
10
+ } from "./xrfnq5k8.js";
11
+ import {
12
+ OkmError2,
13
+ safe2,
14
+ throwNamed2
15
+ } from "./52szzakm.js";
16
+
17
+ // src/runtime/client.ts
18
+ var PLAN_LIMIT = 64;
19
+ var SINGLE_ENDPOINT = {
20
+ endpoint: "primary",
21
+ role: "primary",
22
+ reason: "single-endpoint"
23
+ };
24
+ function createClient(schema, pool, options) {
25
+ const connected = checkServer(pool, schema, options.http, options.requireMeta === true, callOptions(options), {
26
+ ...options.catalog !== undefined ? { catalog: options.catalog } : {},
27
+ ...options.catalogDir !== undefined ? { catalogDir: options.catalogDir } : {}
28
+ });
29
+ const session = {
30
+ schema,
31
+ pool,
32
+ ownsPool: options.ownsPool,
33
+ http: options.http,
34
+ includeValues: options.includeValues === true,
35
+ logger: options.logger,
36
+ connected,
37
+ cache: new Map,
38
+ generators: options.generators,
39
+ timeouts: options.timeouts,
40
+ hookm: options.hookm,
41
+ closing: { current: undefined }
42
+ };
43
+ return openClient(session);
44
+ }
45
+ function openClient(session) {
46
+ const names = Object.keys(session.schema.model);
47
+ const tables = {};
48
+ for (const name of names)
49
+ tables[name] = tableApi(session, name);
50
+ const close = () => {
51
+ if (!session.ownsPool)
52
+ return Promise.resolve();
53
+ session.closing.current ??= session.pool.close();
54
+ return session.closing.current;
55
+ };
56
+ const client = {
57
+ ...tables,
58
+ table(name) {
59
+ const found = tables[name];
60
+ if (found !== undefined)
61
+ return found;
62
+ try {
63
+ if (session.schema.model[name] !== undefined && session.schema.tenancy !== undefined && session.scope === undefined) {
64
+ session.schema.tenancy.missing(name);
65
+ }
66
+ throwNamed2("OKM1120", name, names, `Table ${name} is not in the schema. Accepted names: ${names.join(", ")}.`);
67
+ } catch (error) {
68
+ if (error instanceof OkmError2)
69
+ throw attachHttp(session.http, error);
70
+ throw error;
71
+ }
72
+ },
73
+ close,
74
+ connected: session.connected,
75
+ tx: (...args) => import("./g9f0zqce.js").then((mod) => mod.tx(session, args)),
76
+ batch: (ops, options) => batchHandle(session, ops, options),
77
+ ...session.tx?.extras
78
+ };
79
+ const hooks = session.schema.hooks;
80
+ if (hooks !== undefined) {
81
+ for (const hook of hooks) {
82
+ hook(client, {
83
+ names,
84
+ tables,
85
+ scoped: session.scope !== undefined,
86
+ open: (scope) => openClient({ ...session, scope })
87
+ });
88
+ }
89
+ }
90
+ attachAsyncDispose(client, close);
91
+ return client;
92
+ }
93
+ function attachAsyncDispose(client, close) {
94
+ const symbol = Symbol.asyncDispose;
95
+ if (typeof symbol !== "symbol")
96
+ return;
97
+ Object.defineProperty(client, symbol, {
98
+ configurable: true,
99
+ enumerable: false,
100
+ writable: true,
101
+ value: () => close()
102
+ });
103
+ }
104
+ function tableApi(session, table, view, uses) {
105
+ const base = { uses };
106
+ const mods = { archive: view, uses };
107
+ const api = {
108
+ find(options = {}) {
109
+ if ("lock" in options) {
110
+ return lazyRead(session, () => import("./mrq77sgw.js"), table, options, base, view);
111
+ }
112
+ return start(session, "find", table, options, base, view);
113
+ },
114
+ one(options = {}) {
115
+ return start(session, "one", table, options, base, view);
116
+ },
117
+ count(options = {}) {
118
+ return start(session, "count", table, options, base, view);
119
+ },
120
+ exists(options = {}) {
121
+ return start(session, "exists", table, options, base, view);
122
+ },
123
+ page(options) {
124
+ return lazyRead(session, () => import("./e7a268fy.js"), table, options, base, view);
125
+ },
126
+ aggregate(options = {}) {
127
+ return lazyRead(session, () => import("./10tjenmd.js"), table, options, base, view);
128
+ },
129
+ insert(data, options = {}) {
130
+ return writeHandle(session, "insert", table, data, options, mods);
131
+ },
132
+ update(target, options = {}) {
133
+ return writeHandle(session, "update", table, target, options, mods);
134
+ },
135
+ delete(target, options = {}) {
136
+ return writeHandle(session, "delete", table, target, options, mods);
137
+ }
138
+ };
139
+ const hooks = session.schema.hooks;
140
+ if (hooks !== undefined) {
141
+ for (const hook of hooks) {
142
+ hook(api, {
143
+ table,
144
+ ...view !== undefined ? { view } : {},
145
+ uses,
146
+ reopen: (next) => tableApi(session, table, next, uses),
147
+ session
148
+ });
149
+ }
150
+ }
151
+ for (const name in session.schema.model[table]?.presets) {
152
+ api[name] = (...args) => tableApi(session, table, view, [uses, name, args]);
153
+ }
154
+ return api;
155
+ }
156
+ function batchHandle(session, ops, options, replica) {
157
+ return queryHandle(() => import("./v18jva5b.js").then((mod) => mod.batch(session, ops, options, replica)), {
158
+ replica: () => batchHandle(session, ops, options, true)
159
+ });
160
+ }
161
+ var writers;
162
+ function loadWrite() {
163
+ writers ??= import("./4jzzghr6.js");
164
+ return writers;
165
+ }
166
+ function queryHandle(run, extra) {
167
+ let pending;
168
+ const once = () => {
169
+ pending ??= run();
170
+ return pending;
171
+ };
172
+ const self = {
173
+ then(onFulfilled, onRejected) {
174
+ return once().then(onFulfilled, onRejected);
175
+ },
176
+ catch(onRejected) {
177
+ return self.then(undefined, onRejected);
178
+ },
179
+ finally(fn) {
180
+ return self.then((value) => {
181
+ fn();
182
+ return value;
183
+ }, (error) => {
184
+ fn();
185
+ throw error;
186
+ });
187
+ },
188
+ safe() {
189
+ return safe2(once());
190
+ },
191
+ ...extra
192
+ };
193
+ return self;
194
+ }
195
+ function writeHandle(session, op, table, input, options, mods) {
196
+ return queryHandle(() => runWrite(session, op, table, input, options, mods), {
197
+ "~plan": () => runWrite(session, op, table, input, options, mods, true),
198
+ sql() {
199
+ return loadWrite().then((mod) => mod.explainWrite(session.schema, op, table, input, options, mods, session.generators, session.scope));
200
+ },
201
+ expect(count) {
202
+ return writeHandle(session, op, table, input, options, { ...mods, expect: count });
203
+ },
204
+ all(reason) {
205
+ return writeHandle(session, op, table, input, options, { ...mods, all: reason });
206
+ }
207
+ });
208
+ }
209
+ async function runWrite(session, op, table, input, options, mods, plan) {
210
+ await session.connected;
211
+ const mod = await loadWrite();
212
+ try {
213
+ return await mod.executeWrite(session, op, table, input, options, mods, session.scope, plan);
214
+ } catch (error) {
215
+ throw logged(session, await mapError(session, error));
216
+ }
217
+ }
218
+ function start(session, op, table, options, mods, view) {
219
+ const model = session.schema.model[table];
220
+ const call = readCall(op, table, options, mods.all, session.scope, model, session.schema, view);
221
+ return readHandle(session, table, call, mods, async (plan, rows) => {
222
+ const value = await decodeRows(plan, rows, table);
223
+ if (mods.required === true && value === null) {
224
+ throw new OkmError2("not_found", `one() on ${table} matched no row.`, withHttp(session.http, { kind: "not_found", table }));
225
+ }
226
+ return value;
227
+ }, (prepare) => ({
228
+ all(reason) {
229
+ return start(session, op, table, options, { ...mods, all: reason }, view);
230
+ },
231
+ required() {
232
+ return start(session, op, table, options, { ...mods, required: true }, view);
233
+ },
234
+ stream() {
235
+ return streamLazy(session, call, prepare);
236
+ }
237
+ }));
238
+ }
239
+ function lazyRead(session, load, table, options, mods, view) {
240
+ const read = () => load().then((mod) => ({ it: mod.build(session, table, options, mods, view) }));
241
+ return queryHandle(() => read().then((box) => box.it), {
242
+ inspect: () => read().then((box) => box.it.inspect()),
243
+ sql: () => read().then((box) => box.it.sql()),
244
+ all: (reason) => lazyRead(session, load, table, options, { ...mods, all: reason }, view)
245
+ });
246
+ }
247
+ async function decodeRows(plan, rows, table) {
248
+ return plan.outputs.includes.length === 0 ? decodeResult(plan, rows, table) : (await import("./edwqqryd.js")).decodeIncluded(plan, rows, table);
249
+ }
250
+ function readHandle(session, table, call, mods, decode, extra) {
251
+ let prepared;
252
+ const origin = call;
253
+ const prepare = async () => {
254
+ if (prepared !== undefined)
255
+ return prepared;
256
+ if (mods.uses !== undefined) {
257
+ call = (await import("./r6nwkxnt.js")).refine(session.schema, origin, mods.uses);
258
+ }
259
+ if (readNeedsOperatorSql(session.schema, call))
260
+ await loadOperatorSql();
261
+ const hooks = await hooksFor(call);
262
+ const bound = bindCall(session.schema, call, hooks);
263
+ let plan = session.cache.get(bound.key);
264
+ if (plan === undefined) {
265
+ plan = compileCall(session.schema, call, bound.key, hooks);
266
+ if (session.cache.size >= PLAN_LIMIT) {
267
+ const oldest = session.cache.keys().next().value;
268
+ if (oldest !== undefined)
269
+ session.cache.delete(oldest);
270
+ }
271
+ session.cache.set(bound.key, plan);
272
+ }
273
+ prepared = { plan, params: bound.params };
274
+ return prepared;
275
+ };
276
+ const sync = () => call.include === undefined && mods.uses === undefined && !readNeedsOperatorSql(session.schema, call);
277
+ const run = async () => {
278
+ await session.connected;
279
+ const { plan, params } = await prepare();
280
+ try {
281
+ const result = await session.pool.execute(plan.text, params, callOptions(call, session.timeouts?.statement));
282
+ if (session.hookm && result.notices.length) {
283
+ import("./96ps4g1z.js").then((mod) => mod.notices(session.hookm, result.notices));
284
+ }
285
+ return await decode(plan, result.rows);
286
+ } catch (error) {
287
+ const mapped = await mapError(session, error);
288
+ if (session.schema.model[table]?.conceal !== true)
289
+ throw logged(session, mapped);
290
+ const { scrubCall } = await import("./e6wg00wd.js");
291
+ throw logged(session, scrubCall(mapped, session.schema, call));
292
+ }
293
+ };
294
+ return queryHandle(run, {
295
+ inspect() {
296
+ const source = session.schema.model[table]?.source;
297
+ if (call.include === undefined && prepared !== undefined) {
298
+ return finish(session, inspection(prepared.plan, prepared.params, call, source), call);
299
+ }
300
+ if (sync()) {
301
+ const bound = bindNow(session.schema, call);
302
+ prepared = bound;
303
+ return finish(session, inspection(bound.plan, bound.params, call, source), call);
304
+ }
305
+ return prepare().then(({ plan, params }) => finish(session, inspection(plan, params, call, source), call));
306
+ },
307
+ sql() {
308
+ if (call.include === undefined && prepared !== undefined) {
309
+ return { text: prepared.plan.text, params: prepared.params };
310
+ }
311
+ if (sync()) {
312
+ const { plan, params } = bindNow(session.schema, call);
313
+ prepared = { plan, params };
314
+ return { text: plan.text, params };
315
+ }
316
+ return prepare().then(({ plan, params }) => ({ text: plan.text, params }));
317
+ },
318
+ ...extra?.(prepare)
319
+ });
320
+ }
321
+ function streamLazy(session, call, prepare) {
322
+ return async function* () {
323
+ const { streamRows } = await import("./zd93g3rq.js");
324
+ yield* streamRows(session, call, prepare, (error) => logged(session, error));
325
+ }();
326
+ }
327
+ function archiveRecord(model, view) {
328
+ if (model?.archive === undefined)
329
+ return;
330
+ const rules = archiveRules(model, view);
331
+ if (rules === undefined)
332
+ return;
333
+ return { ruleLines: rules };
334
+ }
335
+ function readCall(op, table, options, all, scope, model, schema, view) {
336
+ const record = options;
337
+ const accepted = readOptionNames(op);
338
+ for (const key of Object.keys(record)) {
339
+ if (!accepted.includes(key)) {
340
+ throwNamed2("OKM1120", key, accepted, `Option ${key} is not accepted by ${op}. Accepted options: ${accepted.join(", ")}.`);
341
+ }
342
+ }
343
+ return {
344
+ op,
345
+ table,
346
+ ...record.where !== undefined ? { where: record.where } : {},
347
+ ...record.select !== undefined ? { select: record.select } : {},
348
+ ...record.orderBy !== undefined ? { orderBy: record.orderBy } : {},
349
+ ...record.limit !== undefined ? { limit: record.limit } : {},
350
+ ...record.include !== undefined ? { include: record.include } : {},
351
+ ...callOptions(record),
352
+ ...all !== undefined ? { all } : {},
353
+ ...scope !== undefined ? { scope } : {},
354
+ ...schema.tenancy !== undefined && model !== undefined ? { tenancyRules: schema.tenancy.rules(table, model.source, scope) } : {},
355
+ ...view !== undefined ? { archive: view } : {},
356
+ ...archiveRecord(model, view)
357
+ };
358
+ }
359
+ async function checkServer(pool, schema, http, requireMeta, options, source) {
360
+ const server = await import("./ge03bfca.js");
361
+ let rows;
362
+ try {
363
+ const result = await pool.execute(server.versionQuery, undefined, options);
364
+ rows = result.rows;
365
+ } catch (error) {
366
+ const { mapPostgresError } = await import("./s6gqfn5j.js");
367
+ throw mapPostgresError(error, mapOptions(http, false));
368
+ }
369
+ await server.acceptServer(pool, schema, rows, http, requireMeta, options, source);
370
+ }
371
+ async function mapError(session, error) {
372
+ if (error instanceof OkmError2) {
373
+ return session.http === undefined ? error : new OkmError2(error.code, error.message, { ...errorFields(error), http: session.http });
374
+ }
375
+ const { mapPostgresError } = await import("./s6gqfn5j.js");
376
+ return mapPostgresError(error, mapOptions(session.http, session.includeValues));
377
+ }
378
+ function errorFields(error) {
379
+ return {
380
+ kind: error.kind,
381
+ ...error.table !== undefined ? { table: error.table } : {},
382
+ ...error.columns.length > 0 ? { columns: error.columns } : {},
383
+ fix: error.fix
384
+ };
385
+ }
386
+ var includeHooks;
387
+ var operatorSql;
388
+ function loadOperatorSql() {
389
+ operatorSql ??= import("./4b9da9d7.js").then(() => {
390
+ return;
391
+ });
392
+ return operatorSql;
393
+ }
394
+ function hooksFor(call) {
395
+ if (call.include === undefined)
396
+ return Promise.resolve(undefined);
397
+ includeHooks ??= import("./edwqqryd.js").then((mod) => mod.includeHooks);
398
+ return includeHooks;
399
+ }
400
+ function bindNow(schema, call) {
401
+ const bound = bindCall(schema, call);
402
+ const plan = compileCall(schema, call, bound.key);
403
+ return { plan, params: bound.params };
404
+ }
405
+ function finish(session, view, call) {
406
+ if (session.schema.model[call.table]?.conceal !== true)
407
+ return view;
408
+ return import("./e6wg00wd.js").then((mod) => mod.redactView(session.schema, view, call));
409
+ }
410
+ function inspection(plan, params, call, source) {
411
+ return {
412
+ intent: logicalIntent(call),
413
+ rules: appliedRules(call, source),
414
+ plan: { strategy: "postgres:single-statement", statements: 1, fingerprint: plan.fingerprint },
415
+ sql: { text: plan.text, params },
416
+ routing: SINGLE_ENDPOINT
417
+ };
418
+ }
419
+ function withHttp(http, options) {
420
+ return http === undefined ? options : { ...options, http };
421
+ }
422
+ function attachHttp(http, error) {
423
+ if (http === undefined)
424
+ return error;
425
+ return new OkmError2(error.code, error.message, { ...errorFields(error), http });
426
+ }
427
+ function callOptions(input, statement) {
428
+ const timeout = input.timeout ?? statement;
429
+ if (input.signal === undefined && timeout === undefined)
430
+ return;
431
+ return {
432
+ ...input.signal !== undefined ? { signal: input.signal } : {},
433
+ ...timeout !== undefined ? { timeout } : {}
434
+ };
435
+ }
436
+ function mapOptions(http, includeValues) {
437
+ return {
438
+ ...http !== undefined ? { http } : {},
439
+ ...includeValues ? { includeValues: true } : {}
440
+ };
441
+ }
442
+ async function settleCall(session, error) {
443
+ throw logged(session, await mapError(session, error));
444
+ }
445
+ function logged(session, error) {
446
+ session.logger?.error?.({ code: error.code, summary: error.summary });
447
+ return error;
448
+ }
449
+
450
+ export { createClient, openClient, queryHandle, decodeRows, readHandle, readCall, withHttp, attachHttp, settleCall };
@@ -0,0 +1,14 @@
1
+ import {
2
+ OkmError2
3
+ } from "./52szzakm.js";
4
+
5
+ // src/runtime/validate/closed.ts
6
+ var VALIDATION_NOT_IMPORTED2 = "Validation is enabled but `okmodel/validate` was not imported.";
7
+ var VALIDATION_NOT_IMPORTED_FIX2 = 'Import "okmodel/validate" before schema().';
8
+ function refuseMissingValidation2() {
9
+ throw new OkmError2("OKM1201", VALIDATION_NOT_IMPORTED2, {
10
+ fix: { summary: VALIDATION_NOT_IMPORTED_FIX2 }
11
+ });
12
+ }
13
+
14
+ export { VALIDATION_NOT_IMPORTED2, VALIDATION_NOT_IMPORTED_FIX2, refuseMissingValidation2 };