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,442 @@
1
+ import {
2
+ validationGate
3
+ } from "./3a5ne750.js";
4
+ import {
5
+ isOperator
6
+ } from "./cn0n68ft.js";
7
+ import {
8
+ OkmError2
9
+ } from "./52szzakm.js";
10
+
11
+ // src/runtime/validate/engine.ts
12
+ var BRAND = Symbol("okmodel.validated");
13
+ var EMAIL = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
14
+ var UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
15
+ var INTEGER_TEXT = /^-?\d+$/;
16
+ var INTEGER_RANGE = {
17
+ smallint: [-32768n, 32767n],
18
+ integer: [-2147483648n, 2147483647n],
19
+ bigint: [-9223372036854775808n, 9223372036854775807n]
20
+ };
21
+ async function prepare(model, source, schemaFlag, op, input) {
22
+ engage(model, source, schemaFlag);
23
+ if (op === "update")
24
+ return finishUpdate(model, input);
25
+ return finish(model, "insert", input, false, false, undefined, undefined);
26
+ }
27
+ async function stand(model, source, schemaFlag, op, body, mode, pick, omit) {
28
+ engage(model, source, schemaFlag);
29
+ const input = op === "update" ? patchOf(body) : body;
30
+ if (mode === "check")
31
+ return issuesOf(model, op, input, pick, omit);
32
+ return finish(model, op, input, true, true, pick, omit);
33
+ }
34
+ function engage(model, source, schemaFlag) {
35
+ const gate = validationGate(schemaFlag, source);
36
+ if (gate !== undefined)
37
+ model.validation = gate;
38
+ }
39
+ async function finishUpdate(model, input) {
40
+ if (Array.isArray(input)) {
41
+ const items = [];
42
+ const issues = [];
43
+ for (let index = 0;index < input.length; index += 1) {
44
+ const item = input[index];
45
+ if (!isRecord(item))
46
+ failShape("update expects { where, set } or a list of rows.");
47
+ const set = await oneValue(model, "update", item.set, false, false, undefined, undefined);
48
+ pushIndexed(issues, index, set.issues);
49
+ items.push({ ...item, set: set.row });
50
+ }
51
+ if (issues.length > 0)
52
+ throwIssues(issues);
53
+ return items;
54
+ }
55
+ if (!isRecord(input) || !isRecord(input.set))
56
+ return input;
57
+ const set = await oneValue(model, "update", input.set, false, false, undefined, undefined);
58
+ if (set.issues.length > 0)
59
+ throwIssues(set.issues);
60
+ return { ...input, set: set.row };
61
+ }
62
+ async function finish(model, op, input, seal, strict, pick, omit) {
63
+ if (Array.isArray(input)) {
64
+ const rows = [];
65
+ const issues = [];
66
+ for (let index = 0;index < input.length; index += 1) {
67
+ const one = await oneValue(model, op, input[index], seal, strict, pick, omit);
68
+ pushIndexed(issues, index, one.issues);
69
+ rows.push(one.row);
70
+ }
71
+ if (issues.length > 0)
72
+ throwIssues(issues);
73
+ return seal ? Object.freeze(rows) : rows;
74
+ }
75
+ const one = await oneValue(model, op, input, seal, strict, pick, omit);
76
+ if (one.issues.length > 0)
77
+ throwIssues(one.issues);
78
+ return one.row;
79
+ }
80
+ async function issuesOf(model, op, input, pick, omit) {
81
+ if (Array.isArray(input)) {
82
+ const issues = [];
83
+ for (let index = 0;index < input.length; index += 1) {
84
+ const one = await oneValue(model, op, input[index], false, true, pick, omit);
85
+ pushIndexed(issues, index, one.issues);
86
+ }
87
+ return issues;
88
+ }
89
+ const one = await oneValue(model, op, input, false, true, pick, omit);
90
+ return one.issues;
91
+ }
92
+ function pushIndexed(issues, index, found) {
93
+ for (const issue of found)
94
+ issues.push({ path: [index, ...issue.path], message: issue.message });
95
+ }
96
+ async function oneValue(model, op, input, seal, strict, pick, omit) {
97
+ if (!isRecord(input))
98
+ failShape("insert expects an object or a list of objects.");
99
+ if (isBranded(input)) {
100
+ const row = copyRow(input);
101
+ keep(row, pick, omit);
102
+ return { row: seal ? sealRow(row) : row, issues: [] };
103
+ }
104
+ const row = acceptKeys(model, op, input, strict);
105
+ keep(row, pick, omit);
106
+ const gate = model.validation;
107
+ const issues = [];
108
+ if (gate === undefined)
109
+ return { row, issues };
110
+ for (const column of model.columns) {
111
+ if (!column.writable || op === "update" && column.guardUpdate)
112
+ continue;
113
+ if (!Object.hasOwn(row, column.field)) {
114
+ if (op === "insert" && gate.required.includes(column.field) && selected(column.field, pick, omit)) {
115
+ issues.push({ path: [column.field], message: "required" });
116
+ }
117
+ continue;
118
+ }
119
+ if (!selected(column.field, pick, omit))
120
+ continue;
121
+ let value = row[column.field];
122
+ if (isOperator(value))
123
+ continue;
124
+ if (value === null) {
125
+ if (gate.notNull.includes(column.field))
126
+ issues.push({ path: [column.field], message: "required" });
127
+ continue;
128
+ }
129
+ value = await applyTransforms(gate.fields[column.field], op, value);
130
+ row[column.field] = value;
131
+ issues.push(...derivedIssues(column, gate, value));
132
+ const checked = await checkRules(gate.fields[column.field], op, value, column.field);
133
+ row[column.field] = checked.value;
134
+ issues.push(...checked.issues);
135
+ }
136
+ const rowChecked = await checkRules(gate.row, op, row, undefined);
137
+ issues.push(...rowChecked.issues);
138
+ if (seal && issues.length === 0)
139
+ return { row: sealRow(row), issues };
140
+ return { row, issues };
141
+ }
142
+ function acceptKeys(model, op, input, strict) {
143
+ const row = {};
144
+ for (const key of Object.keys(input)) {
145
+ const column = findColumn(model, key);
146
+ if (column === undefined)
147
+ continue;
148
+ const blocked = op === "update" && column.guardUpdate || !column.writable;
149
+ if (blocked) {
150
+ if (strict) {
151
+ const reason = op === "update" && column.guardUpdate ? "is a primary key" : column.guarded ? "is guarded" : "cannot be written";
152
+ refuse(model.name, key, reason);
153
+ }
154
+ if (input[key] !== undefined)
155
+ row[key] = input[key];
156
+ continue;
157
+ }
158
+ if (input[key] !== undefined)
159
+ row[key] = input[key];
160
+ }
161
+ return row;
162
+ }
163
+ function refuse(table, field, reason) {
164
+ throw new OkmError2("OKM1190", `Field ${table}.${field} ${reason}. Input cannot set it.`, {
165
+ kind: "invalid",
166
+ fix: { summary: "Remove the guarded field. Input cannot set it." }
167
+ });
168
+ }
169
+ function keep(row, pick, omit) {
170
+ if (pick === undefined && omit === undefined)
171
+ return;
172
+ for (const key of Object.keys(row)) {
173
+ if (!selected(key, pick, omit))
174
+ delete row[key];
175
+ }
176
+ }
177
+ function selected(field, pick, omit) {
178
+ if (pick !== undefined && !pick.includes(field))
179
+ return false;
180
+ if (omit !== undefined && omit.includes(field))
181
+ return false;
182
+ return true;
183
+ }
184
+ async function applyTransforms(rules, op, value) {
185
+ if (rules === undefined)
186
+ return value;
187
+ let phase;
188
+ let current = value;
189
+ for (const item of rules) {
190
+ const step = active(item, phase);
191
+ phase = step.phase;
192
+ if (step.rule === undefined || step.limit !== undefined && step.limit !== op)
193
+ continue;
194
+ if (isTransform(step.rule))
195
+ current = transformValue(step.rule, current);
196
+ }
197
+ return current;
198
+ }
199
+ async function checkRules(rules, op, value, field) {
200
+ if (rules === undefined)
201
+ return { value, issues: [] };
202
+ const issues = [];
203
+ let phase;
204
+ let current = value;
205
+ for (const item of rules) {
206
+ const step = active(item, phase);
207
+ phase = step.phase;
208
+ if (step.rule === undefined || step.limit !== undefined && step.limit !== op)
209
+ continue;
210
+ const rule = step.rule;
211
+ if (isTransform(rule))
212
+ continue;
213
+ if (isStandard(rule)) {
214
+ const result = await rule["~standard"].validate(current);
215
+ if (result.issues === undefined) {
216
+ current = result.value;
217
+ continue;
218
+ }
219
+ for (const issue of result.issues) {
220
+ issues.push({ path: issuePath(field, issue.path), message: issue.message });
221
+ }
222
+ continue;
223
+ }
224
+ if (rule.k === "email" && typeof rule.key === "string") {
225
+ if (typeof current !== "string" || !EMAIL.test(current))
226
+ issues.push(fieldIssue(field, rule.key));
227
+ continue;
228
+ }
229
+ if (rule.k === "min" && typeof rule.n === "number" && typeof rule.key === "string") {
230
+ const ok = typeof current === "string" ? textLength(current) >= rule.n : typeof current === "number" && current >= rule.n;
231
+ if (!ok)
232
+ issues.push(fieldIssue(field, rule.key));
233
+ continue;
234
+ }
235
+ if (rule.k === "rule" && typeof rule.fn === "function" && typeof rule.path === "string" && typeof rule.key === "string" && !rule.fn(current)) {
236
+ issues.push({ path: [rule.path], message: rule.key });
237
+ }
238
+ }
239
+ return { value: current, issues };
240
+ }
241
+ function fieldIssue(field, message) {
242
+ return { path: field === undefined ? [] : [field], message };
243
+ }
244
+ function derivedIssues(column, gate, value) {
245
+ const issues = [];
246
+ const type = column.dataType.replace(/\[\]/g, "");
247
+ const length = /^(?:varchar|character varying|char|character)\((\d+)\)$/.exec(type);
248
+ if (length !== null && typeof value === "string" && textLength(value) > Number(length[1])) {
249
+ issues.push({ path: [column.field], message: "too_long" });
250
+ }
251
+ const list = gate.pick[column.field];
252
+ if (list !== undefined && (typeof value !== "string" || !list.includes(value))) {
253
+ issues.push({ path: [column.field], message: "picklist" });
254
+ }
255
+ const range = INTEGER_RANGE[type];
256
+ if (range !== undefined && !integerInRange(value, range[0], range[1])) {
257
+ issues.push({ path: [column.field], message: "integer_range" });
258
+ }
259
+ if (type === "numeric" || type.startsWith("numeric(") || type === "real" || type === "double precision") {
260
+ if (!precisionOk(type, value))
261
+ issues.push({ path: [column.field], message: "precision" });
262
+ }
263
+ if (type === "uuid" && (typeof value !== "string" || !UUID.test(value))) {
264
+ issues.push({ path: [column.field], message: "uuid" });
265
+ }
266
+ if ((type === "json" || type === "jsonb") && !jsonOk(value)) {
267
+ issues.push({ path: [column.field], message: "json" });
268
+ }
269
+ return issues;
270
+ }
271
+ function textLength(value) {
272
+ let count = 0;
273
+ for (let index = 0;index < value.length; ) {
274
+ const code = value.codePointAt(index) ?? 0;
275
+ index += code > 65535 ? 2 : 1;
276
+ count += 1;
277
+ }
278
+ return count;
279
+ }
280
+ function integerInRange(value, low, high) {
281
+ const parsed = integerValue(value);
282
+ if (parsed === undefined)
283
+ return false;
284
+ return parsed >= low && parsed <= high;
285
+ }
286
+ function integerValue(value) {
287
+ if (typeof value === "bigint")
288
+ return value;
289
+ if (typeof value === "number" && Number.isSafeInteger(value))
290
+ return BigInt(value);
291
+ if (typeof value === "string" && INTEGER_TEXT.test(value)) {
292
+ try {
293
+ return BigInt(value);
294
+ } catch {
295
+ return;
296
+ }
297
+ }
298
+ return;
299
+ }
300
+ function precisionOk(type, value) {
301
+ if (type === "real" || type === "double precision") {
302
+ return typeof value === "number" && Number.isFinite(value);
303
+ }
304
+ const text = typeof value === "number" ? Number.isFinite(value) ? String(value) : "" : typeof value === "string" ? value : "";
305
+ const plain = plainDecimal(text);
306
+ if (plain === undefined)
307
+ return false;
308
+ const match = /^numeric\((\d+)(?:,(\d+))?\)$/.exec(type);
309
+ if (match === null)
310
+ return true;
311
+ const precision = Number(match[1]);
312
+ const scale = match[2] === undefined ? 0 : Number(match[2]);
313
+ const [whole, fraction = ""] = plain.replace(/^-/, "").split(".");
314
+ const digits = `${whole === "0" ? "" : whole}${fraction}`.replace(/^0+/, "");
315
+ return fraction.length <= scale && (whole ?? "").replace(/^0+/, "").length <= precision - scale && digits.length <= precision;
316
+ }
317
+ function plainDecimal(text) {
318
+ if (!/^-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?$/.test(text))
319
+ return;
320
+ const match = /^(-?)(\d+)(?:\.(\d+))?(?:[eE]([+-]?\d+))?$/.exec(text);
321
+ if (match === null)
322
+ return;
323
+ const whole = match[2] ?? "0";
324
+ const fraction = match[3] ?? "";
325
+ const exp = match[4] === undefined ? 0 : Number(match[4]);
326
+ if (!Number.isSafeInteger(exp))
327
+ return;
328
+ const digits = `${whole}${fraction}`;
329
+ const point = whole.length + exp;
330
+ let plain;
331
+ if (point >= digits.length)
332
+ plain = digits + "0".repeat(point - digits.length);
333
+ else if (point > 0)
334
+ plain = `${digits.slice(0, point)}.${digits.slice(point)}`;
335
+ else
336
+ plain = `0.${"0".repeat(-point)}${digits}`;
337
+ return `${match[1] ?? ""}${plain}`;
338
+ }
339
+ function jsonOk(value) {
340
+ try {
341
+ return JSON.stringify(value) !== undefined;
342
+ } catch {
343
+ return false;
344
+ }
345
+ }
346
+ function isStandard(value) {
347
+ if (typeof value !== "object" || value === null || !("~standard" in value))
348
+ return false;
349
+ const standard = value["~standard"];
350
+ return typeof standard === "object" && standard !== null && standard.version === 1 && typeof standard.validate === "function";
351
+ }
352
+ function active(item, phase) {
353
+ if (isStandard(item))
354
+ return { phase, limit: undefined, rule: item };
355
+ if (!isRule(item))
356
+ return { phase, limit: undefined, rule: undefined };
357
+ if (item.k === "phase" && (item.op === "insert" || item.op === "update")) {
358
+ return { phase: item.op, limit: undefined, rule: undefined };
359
+ }
360
+ if (item.k === "when" && (item.op === "insert" || item.op === "update")) {
361
+ const inner = item.rule;
362
+ if (isStandard(inner) || isRule(inner))
363
+ return { phase, limit: item.op, rule: inner };
364
+ }
365
+ return { phase, limit: phase, rule: item };
366
+ }
367
+ function isRule(value) {
368
+ return typeof value === "object" && value !== null && "k" in value && typeof value.k === "string";
369
+ }
370
+ function isTransform(rule) {
371
+ return !isStandard(rule) && (rule.k === "trim" || rule.k === "lower");
372
+ }
373
+ function transformValue(rule, value) {
374
+ if (typeof value !== "string")
375
+ return value;
376
+ if (rule.k === "trim")
377
+ return value.trim();
378
+ if (rule.k === "lower")
379
+ return value.toLowerCase();
380
+ return value;
381
+ }
382
+ function issuePath(field, path) {
383
+ const segments = [];
384
+ if (field !== undefined)
385
+ segments.push(field);
386
+ if (path === undefined)
387
+ return segments;
388
+ for (const part of path) {
389
+ const key = typeof part === "object" && part !== null && "key" in part ? part.key : part;
390
+ if (typeof key === "string" || typeof key === "number")
391
+ segments.push(key);
392
+ }
393
+ return segments;
394
+ }
395
+ function patchOf(body) {
396
+ if (isRecord(body) && isRecord(body.set))
397
+ return body.set;
398
+ return body;
399
+ }
400
+ function copyRow(value) {
401
+ const row = {};
402
+ for (const key of Object.keys(value))
403
+ row[key] = value[key];
404
+ return row;
405
+ }
406
+ function sealRow(row) {
407
+ Object.defineProperty(row, BRAND, { value: true });
408
+ return Object.freeze(row);
409
+ }
410
+ function isBranded(value) {
411
+ return Object.hasOwn(value, BRAND) && value[BRAND] === true;
412
+ }
413
+ function findColumn(model, field) {
414
+ for (const column of model.columns)
415
+ if (column.field === field)
416
+ return column;
417
+ return;
418
+ }
419
+ function isRecord(value) {
420
+ return typeof value === "object" && value !== null && !Array.isArray(value);
421
+ }
422
+ function failShape(message) {
423
+ throw new OkmError2("OKM1121", message, { kind: "invalid" });
424
+ }
425
+ function throwIssues(issues) {
426
+ const columns = [];
427
+ for (const issue of issues) {
428
+ const field = [...issue.path].reverse().find((part) => typeof part === "string");
429
+ if (typeof field === "string" && !columns.includes(field))
430
+ columns.push(field);
431
+ }
432
+ throw new OkmError2("OKM1200", "Validation failed.", {
433
+ kind: "invalid",
434
+ ...columns.length > 0 ? { columns } : {},
435
+ issues,
436
+ fix: { summary: "Fix the fields named in issues. Each issue message is a key." }
437
+ });
438
+ }
439
+ export {
440
+ prepare,
441
+ stand
442
+ };
@@ -0,0 +1,27 @@
1
+ // src/runtime/tx.ts
2
+ async function runWrite(host, statements, options, atomic) {
3
+ if (statements.length === 0)
4
+ return [];
5
+ const first = statements[0];
6
+ const limit = options?.timeout ?? host.timeouts?.statement;
7
+ const call = limit === undefined || limit === options?.timeout ? options : { ...options, timeout: limit };
8
+ const single = statements.length === 1 && first !== undefined && atomic === undefined;
9
+ const watched = host.hookm !== undefined && !single && host.tx === undefined ? await import("./96ps4g1z.js") : undefined;
10
+ watched?.phase(host.hookm, "start", 0);
11
+ let results;
12
+ try {
13
+ results = single ? [await host.pool.execute(first.text, first.params, call)] : await host.pool.batch(statements, call);
14
+ } catch (error) {
15
+ watched?.phase(host.hookm, "rollback", 0);
16
+ throw error;
17
+ }
18
+ watched?.phase(host.hookm, "commit", 0);
19
+ if (host.hookm !== undefined) {
20
+ const heard = results.flatMap((result) => result.notices);
21
+ if (heard.length > 0)
22
+ (await import("./96ps4g1z.js")).notices(host.hookm, heard);
23
+ }
24
+ return results;
25
+ }
26
+
27
+ export { runWrite };
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  catalogError2
3
- } from "./ycwz42az.js";
3
+ } from "./52szzakm.js";
4
4
 
5
5
  // src/contracts/utf8.ts
6
6
  function utf8ByteLength2(value) {
@@ -273,109 +273,7 @@ function toHex(bytes) {
273
273
  // src/contracts/catalog/words.ts
274
274
  var words;
275
275
  function reservedWords() {
276
- words ??= new Set([
277
- "all",
278
- "analyse",
279
- "analyze",
280
- "and",
281
- "any",
282
- "array",
283
- "as",
284
- "asc",
285
- "asymmetric",
286
- "authorization",
287
- "binary",
288
- "both",
289
- "case",
290
- "cast",
291
- "check",
292
- "collate",
293
- "collation",
294
- "column",
295
- "concurrently",
296
- "constraint",
297
- "create",
298
- "cross",
299
- "current_catalog",
300
- "current_date",
301
- "current_role",
302
- "current_schema",
303
- "current_time",
304
- "current_timestamp",
305
- "current_user",
306
- "default",
307
- "deferrable",
308
- "desc",
309
- "distinct",
310
- "do",
311
- "else",
312
- "end",
313
- "except",
314
- "false",
315
- "fetch",
316
- "for",
317
- "foreign",
318
- "freeze",
319
- "from",
320
- "full",
321
- "grant",
322
- "group",
323
- "having",
324
- "ilike",
325
- "in",
326
- "initially",
327
- "inner",
328
- "intersect",
329
- "into",
330
- "is",
331
- "isnull",
332
- "join",
333
- "lateral",
334
- "leading",
335
- "left",
336
- "like",
337
- "limit",
338
- "localtime",
339
- "localtimestamp",
340
- "natural",
341
- "not",
342
- "notnull",
343
- "null",
344
- "offset",
345
- "on",
346
- "only",
347
- "or",
348
- "order",
349
- "outer",
350
- "overlaps",
351
- "placing",
352
- "primary",
353
- "references",
354
- "returning",
355
- "right",
356
- "select",
357
- "session_user",
358
- "similar",
359
- "some",
360
- "symmetric",
361
- "system_user",
362
- "table",
363
- "tablesample",
364
- "then",
365
- "to",
366
- "trailing",
367
- "true",
368
- "union",
369
- "unique",
370
- "user",
371
- "using",
372
- "variadic",
373
- "verbose",
374
- "when",
375
- "where",
376
- "window",
377
- "with"
378
- ]);
276
+ words ??= new Set("all analyse analyze and any array as asc asymmetric authorization binary both case cast check collate collation column concurrently constraint create cross current_catalog current_date current_role current_schema current_time current_timestamp current_user default deferrable desc distinct do else end except false fetch for foreign freeze from full grant group having ilike in initially inner intersect into is isnull join lateral leading left like limit localtime localtimestamp natural not notnull null offset on only or order outer overlaps placing primary references returning right select session_user similar some symmetric system_user table tablesample then to trailing true union unique user using variadic verbose when where window with".split(" "));
379
277
  return words;
380
278
  }
381
279
 
@@ -0,0 +1,42 @@
1
+ import {
2
+ OkmError2
3
+ } from "./52szzakm.js";
4
+
5
+ // src/dialects/pg/preset.ts
6
+ var RESERVED_PRESETS = new Set([
7
+ "find",
8
+ "one",
9
+ "page",
10
+ "aggregate",
11
+ "count",
12
+ "exists",
13
+ "insert",
14
+ "update",
15
+ "delete",
16
+ "archive",
17
+ "restore",
18
+ "withArchived",
19
+ "onlyArchived",
20
+ "then",
21
+ "lock",
22
+ "watch",
23
+ "subscribe",
24
+ "stream",
25
+ "inspect",
26
+ "explain",
27
+ "with",
28
+ "for",
29
+ "as"
30
+ ]);
31
+ function checkPresetNames(names, where) {
32
+ for (const name of names) {
33
+ if (!RESERVED_PRESETS.has(name))
34
+ continue;
35
+ throw new OkmError2("OKM1040", `Preset ${name} on ${where} is a client method or a reserved name. Reserved: ${[...RESERVED_PRESETS].join(", ")}.`, { fix: { summary: `Rename the preset ${name}.` } });
36
+ }
37
+ }
38
+ function duplicatePreset(name, table, first, second) {
39
+ throw new OkmError2("OKM1040", `Preset ${name} on ${table} is defined by ${first} and by ${second}.`, { fix: { summary: `Rename the preset in ${first} or in ${second}, or drop one of them.` } });
40
+ }
41
+
42
+ export { checkPresetNames, duplicatePreset };
@@ -0,0 +1,69 @@
1
+ import {
2
+ encodeText,
3
+ decodeText
4
+ } from "./sacqha1y.js";
5
+ import {
6
+ required,
7
+ openColumn
8
+ } from "./dd4f8mnx.js";
9
+ import {
10
+ definition,
11
+ rejected
12
+ } from "./xatfg0gn.js";
13
+ import {
14
+ readClientGenerator
15
+ } from "./rwmrejjz.js";
16
+
17
+ // src/dialects/pg/keys.ts
18
+ function id2(options) {
19
+ const chosen = options?.default;
20
+ const client = typeof chosen === "function" ? readClientGenerator(chosen) : undefined;
21
+ const okid = client?.name === "okid";
22
+ const supplied = chosen === "none";
23
+ if (client === undefined && chosen !== undefined && chosen !== "uuidv7" && chosen !== "uuidv4" && chosen !== "none") {
24
+ definition(`id() default ${String(chosen)} must be uuidv7, uuidv4, none, or a client generator.`);
25
+ }
26
+ return openColumn({
27
+ baseType: okid ? "text" : "uuid",
28
+ nullable: false,
29
+ hasDefault: client !== undefined || !supplied,
30
+ generated: false,
31
+ guarded: client !== undefined || !supplied,
32
+ hidden: false,
33
+ omitWrite: client !== undefined || !supplied,
34
+ omitUpdate: true,
35
+ ...client !== undefined ? { clientDefault: client } : {},
36
+ ...okid ? { collation: "C" } : {},
37
+ idSource: chosen === undefined ? "implicit" : "column",
38
+ ...client === undefined && !supplied ? { defaultSql: chosen === "uuidv4" ? "gen_random_uuid()" : "uuidv7()" } : {},
39
+ primaryKey: true,
40
+ encode: okid ? encodeText : encodeUuid,
41
+ decode: okid ? decodeText : decodeUuid,
42
+ sqlForm: "quote"
43
+ });
44
+ }
45
+ function uuid2() {
46
+ return required({
47
+ baseType: "uuid",
48
+ encode: encodeUuid,
49
+ decode: decodeUuid,
50
+ sqlForm: "quote"
51
+ });
52
+ }
53
+ function encodeUuid(value) {
54
+ const text = value.toLowerCase();
55
+ if (!uuidText().test(text)) {
56
+ rejected(`uuid ${value} must be 8-4-4-4-12 hexadecimal digits, for example 01890c5a-8f0e-7c3a-9b2d-6e4f1a0b9c8d.`);
57
+ }
58
+ return text;
59
+ }
60
+ function decodeUuid(wire) {
61
+ return encodeUuid(wire);
62
+ }
63
+ function uuidText() {
64
+ uuidPattern ??= /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
65
+ return uuidPattern;
66
+ }
67
+ var uuidPattern;
68
+
69
+ export { id2, uuid2 };