okmodel 0.1.1 → 0.3.0

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 (228) hide show
  1. package/README.md +727 -37
  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/enum.d.ts +31 -4
  13. package/dist/contracts/catalog/extension.d.ts +61 -0
  14. package/dist/contracts/catalog/object.d.ts +1 -0
  15. package/dist/contracts/catalog/privilege.d.ts +64 -0
  16. package/dist/contracts/catalog/routine.d.ts +56 -0
  17. package/dist/contracts/catalog/types.d.ts +179 -18
  18. package/dist/contracts/catalog/view.d.ts +73 -0
  19. package/dist/contracts/connection.d.ts +13 -0
  20. package/dist/contracts/driver.d.ts +18 -5
  21. package/dist/contracts/error.d.ts +17 -1
  22. package/dist/contracts/generator.d.ts +63 -0
  23. package/dist/contracts/index.d.ts +2 -2
  24. package/dist/contracts/index.js +1 -1
  25. package/dist/contracts/internal.js +11 -9
  26. package/dist/contracts/location.d.ts +24 -0
  27. package/dist/contracts/rows.d.ts +15 -0
  28. package/dist/dialects/pg/archive-bind.d.ts +22 -0
  29. package/dist/dialects/pg/column.d.ts +129 -10
  30. package/dist/dialects/pg/compile.d.ts +1 -0
  31. package/dist/dialects/pg/custom.d.ts +6 -0
  32. package/dist/dialects/pg/ddl.d.ts +65 -1
  33. package/dist/dialects/pg/enum.d.ts +10 -8
  34. package/dist/dialects/pg/ext/citext.d.ts +14 -0
  35. package/dist/dialects/pg/ext/citext.js +11 -0
  36. package/dist/dialects/pg/ext/index.d.ts +52 -0
  37. package/dist/dialects/pg/ext/index.js +6 -0
  38. package/dist/dialects/pg/ext/pg-trgm.d.ts +53 -0
  39. package/dist/dialects/pg/ext/pg-trgm.js +42 -0
  40. package/dist/dialects/pg/filter-parse.d.ts +19 -0
  41. package/dist/dialects/pg/filters.d.ts +19 -0
  42. package/dist/dialects/pg/fn/index.d.ts +107 -0
  43. package/dist/dialects/pg/fn/index.js +175 -0
  44. package/dist/dialects/pg/fn/touch.d.ts +14 -0
  45. package/dist/dialects/pg/index.d.ts +11 -2
  46. package/dist/dialects/pg/index.js +243 -1062
  47. package/dist/dialects/pg/introspect.d.ts +10 -1
  48. package/dist/dialects/pg/keys.d.ts +19 -3
  49. package/dist/dialects/pg/model.d.ts +223 -2
  50. package/dist/dialects/pg/operator-fit.d.ts +86 -0
  51. package/dist/dialects/pg/operators.d.ts +46 -1
  52. package/dist/dialects/pg/ops/arr-append.d.ts +9 -0
  53. package/dist/dialects/pg/ops/arr-ns.d.ts +7 -0
  54. package/dist/dialects/pg/ops/arr-remove.d.ts +9 -0
  55. package/dist/dialects/pg/ops/containedBy.d.ts +9 -0
  56. package/dist/dialects/pg/ops/contains.d.ts +8 -4
  57. package/dist/dialects/pg/ops/hasAnyKey.d.ts +8 -0
  58. package/dist/dialects/pg/ops/hasKey.d.ts +8 -0
  59. package/dist/dialects/pg/ops/json-ns.d.ts +6 -0
  60. package/dist/dialects/pg/ops/json-set.d.ts +12 -0
  61. package/dist/dialects/pg/ops/matches.d.ts +16 -0
  62. package/dist/dialects/pg/ops/overlaps.d.ts +9 -0
  63. package/dist/dialects/pg/ops/path.d.ts +13 -0
  64. package/dist/dialects/pg/preset.d.ts +76 -0
  65. package/dist/dialects/pg/relations.d.ts +59 -2
  66. package/dist/dialects/pg/role/check.d.ts +45 -0
  67. package/dist/dialects/pg/role/index.d.ts +50 -0
  68. package/dist/dialects/pg/role/sql.d.ts +44 -0
  69. package/dist/dialects/pg/schema.d.ts +109 -5
  70. package/dist/dialects/pg/table.d.ts +137 -11
  71. package/dist/dialects/pg/temporal.d.ts +9 -7
  72. package/dist/dialects/pg/tenancy.d.ts +103 -0
  73. package/dist/dialects/pg/through.d.ts +21 -0
  74. package/dist/dialects/pg/trait.d.ts +141 -0
  75. package/dist/dialects/pg/view/client.d.ts +30 -0
  76. package/dist/dialects/pg/view/depend.d.ts +35 -0
  77. package/dist/dialects/pg/view/index.d.ts +114 -0
  78. package/dist/dialects/pg/view/index.js +289 -0
  79. package/dist/dialects/pg/view/scratch.d.ts +21 -0
  80. package/dist/dialects/pg/where.d.ts +37 -0
  81. package/dist/runtime/aggregate.d.ts +25 -0
  82. package/dist/runtime/archive-handle.d.ts +18 -0
  83. package/dist/runtime/archive-rules.d.ts +12 -0
  84. package/dist/runtime/archive.d.ts +73 -0
  85. package/dist/runtime/batch.d.ts +20 -0
  86. package/dist/runtime/client.d.ts +114 -5
  87. package/dist/runtime/conflict.d.ts +2 -1
  88. package/dist/runtime/exposure.d.ts +27 -0
  89. package/dist/runtime/hookm.d.ts +39 -0
  90. package/dist/runtime/ids/index.d.ts +11 -0
  91. package/dist/runtime/ids/index.js +193 -0
  92. package/dist/runtime/ids/okid-extended.d.ts +19 -0
  93. package/dist/runtime/ids/okid-generator.d.ts +21 -0
  94. package/dist/runtime/ids/okid-shared.d.ts +89 -0
  95. package/dist/runtime/ids/okid.d.ts +24 -0
  96. package/dist/runtime/ids/uuid.d.ts +21 -0
  97. package/dist/runtime/lock.d.ts +22 -0
  98. package/dist/runtime/operator-sql.d.ts +8 -0
  99. package/dist/runtime/page.d.ts +25 -0
  100. package/dist/runtime/pg/bun.d.ts +30 -0
  101. package/dist/runtime/pg/bun.js +52 -0
  102. package/dist/runtime/pg/pg.d.ts +29 -0
  103. package/dist/runtime/pg/pg.js +52 -0
  104. package/dist/runtime/pg/pglite.js +16 -17
  105. package/dist/runtime/pg/postgresjs.js +14 -24
  106. package/dist/runtime/plan.d.ts +146 -7
  107. package/dist/runtime/preset-stack.d.ts +17 -0
  108. package/dist/runtime/presets.d.ts +48 -0
  109. package/dist/runtime/read-stream.d.ts +26 -0
  110. package/dist/runtime/safety/index.d.ts +159 -0
  111. package/dist/runtime/safety/index.js +294 -0
  112. package/dist/runtime/safety-hook.d.ts +42 -0
  113. package/dist/runtime/server-check.d.ts +34 -0
  114. package/dist/runtime/tenancy/index.d.ts +37 -0
  115. package/dist/runtime/tenancy/index.js +368 -0
  116. package/dist/runtime/trait-read.d.ts +30 -0
  117. package/dist/runtime/traits/index.d.ts +133 -0
  118. package/dist/runtime/traits/index.js +646 -0
  119. package/dist/runtime/transaction.d.ts +19 -0
  120. package/dist/runtime/tx.d.ts +22 -7
  121. package/dist/runtime/types.d.ts +376 -27
  122. package/dist/runtime/validate/bind.d.ts +11 -0
  123. package/dist/runtime/validate/closed.d.ts +16 -0
  124. package/dist/runtime/validate/engine.d.ts +41 -0
  125. package/dist/runtime/validate/hook.d.ts +17 -0
  126. package/dist/runtime/validate/index.d.ts +21 -0
  127. package/dist/runtime/validate/index.js +175 -0
  128. package/dist/runtime/validate/load.d.ts +13 -0
  129. package/dist/runtime/validate/places.d.ts +41 -0
  130. package/dist/runtime/validate/rules.d.ts +108 -0
  131. package/dist/runtime/validate/surface.d.ts +102 -0
  132. package/dist/runtime/write.d.ts +30 -5
  133. package/dist/shared/01yr20s5.js +149 -0
  134. package/dist/shared/0jc3ffzf.js +154 -0
  135. package/dist/shared/0v1g424p.js +26 -0
  136. package/dist/shared/1g0drakv.js +225 -0
  137. package/dist/shared/{kprcwaha.js → 1tfdfvhh.js} +5 -3
  138. package/dist/shared/{05w44d8g.js → 2ssqrv66.js} +767 -715
  139. package/dist/shared/3ekj9x92.js +210 -0
  140. package/dist/shared/49ypnfwt.js +1122 -0
  141. package/dist/shared/4e4c6vq2.js +62 -0
  142. package/dist/shared/4m9fn52z.js +70 -0
  143. package/dist/shared/{ycwz42az.js → 52szzakm.js} +6 -26
  144. package/dist/shared/5nmdenb6.js +1229 -0
  145. package/dist/shared/672t5vym.js +12 -0
  146. package/dist/shared/6bazvqwm.js +86 -0
  147. package/dist/shared/6w5j844a.js +201 -0
  148. package/dist/shared/{fgqgsb5z.js → 75xkjv0t.js} +2 -2
  149. package/dist/shared/7fvtrkwr.js +84 -0
  150. package/dist/shared/7m88x3e9.js +45 -0
  151. package/dist/shared/{mtkwv8wx.js → 7p2wbz7f.js} +1 -1
  152. package/dist/shared/83eaefwr.js +10 -0
  153. package/dist/shared/94ez8kzh.js +19 -0
  154. package/dist/shared/96ps4g1z.js +32 -0
  155. package/dist/shared/{xqbhg6cn.js → 999ade2q.js} +95 -10
  156. package/dist/shared/9m80mkac.js +158 -0
  157. package/dist/shared/9x25fxhc.js +150 -0
  158. package/dist/shared/a5sxz52y.js +13 -0
  159. package/dist/shared/axhk06q1.js +156 -0
  160. package/dist/shared/{dynq16vb.js → b066g80h.js} +8 -6
  161. package/dist/shared/bqha5cg6.js +229 -0
  162. package/dist/shared/bryqk9k9.js +31 -0
  163. package/dist/shared/c8aghh05.js +442 -0
  164. package/dist/shared/c9z5g0fm.js +309 -0
  165. package/dist/shared/{m3ypagh5.js → dbjej1tw.js} +124 -15
  166. package/dist/shared/dyj86cmj.js +69 -0
  167. package/dist/shared/e6wg00wd.js +164 -0
  168. package/dist/shared/{hadrfb9h.js → edwqqryd.js} +17 -14
  169. package/dist/shared/{qk3hxfja.js → fptpmvxp.js} +5 -5
  170. package/dist/shared/fs0ex00p.js +76 -0
  171. package/dist/shared/{jjb7yht9.js → g4f5kcx9.js} +3 -3
  172. package/dist/shared/gjthds9j.js +48 -0
  173. package/dist/shared/hcmdtwgh.js +102 -0
  174. package/dist/shared/{g5fhqmwe.js → jkc6ws1p.js} +6 -56
  175. package/dist/shared/{yd1kzwn2.js → jns2wx4t.js} +55 -12
  176. package/dist/shared/k34e0xf2.js +450 -0
  177. package/dist/shared/k3ted12y.js +33 -0
  178. package/dist/shared/k7dwkhvq.js +31 -0
  179. package/dist/shared/{8b04d28c.js → kgnc8grt.js} +2 -2
  180. package/dist/shared/{hd293j9g.js → kyxb62mc.js} +14 -2
  181. package/dist/shared/m78s7898.js +376 -0
  182. package/dist/shared/m9tv7xn7.js +404 -0
  183. package/dist/shared/meck53jv.js +99 -0
  184. package/dist/shared/mt5wswjz.js +90 -0
  185. package/dist/shared/n0dgs0pb.js +75 -0
  186. package/dist/shared/n442h54c.js +14 -0
  187. package/dist/shared/n4jqhpqz.js +27 -0
  188. package/dist/shared/n7mcc1gk.js +132 -0
  189. package/dist/shared/ne02ng5c.js +68 -0
  190. package/dist/shared/{mkjmqjv4.js → nfzc5z0w.js} +2 -104
  191. package/dist/shared/nsva2a6b.js +42 -0
  192. package/dist/shared/p24x2334.js +195 -0
  193. package/dist/shared/qbxzvwwc.js +88 -0
  194. package/dist/shared/{w8samvk1.js → qybh48nd.js} +1 -1
  195. package/dist/shared/r6nwkxnt.js +86 -0
  196. package/dist/shared/{xy79j5hq.js → rrstp3kb.js} +25 -36
  197. package/dist/shared/rwmrejjz.js +26 -0
  198. package/dist/shared/{zhc5vmgf.js → s6gqfn5j.js} +2 -2
  199. package/dist/shared/{523ft3e8.js → scwxrpx2.js} +7 -5
  200. package/dist/shared/{j5cn2963.js → t4tbsy9h.js} +13 -64
  201. package/dist/shared/{y9m15dfv.js → t616ynfn.js} +544 -32
  202. package/dist/shared/{ngk8gj61.js → tbszx8mz.js} +15 -1
  203. package/dist/shared/th14ygv2.js +69 -0
  204. package/dist/shared/tmbdywx0.js +178 -0
  205. package/dist/shared/ty3v3hfk.js +64 -0
  206. package/dist/shared/wekze34d.js +246 -0
  207. package/dist/shared/x6wysh83.js +1132 -0
  208. package/dist/shared/{rgs0st28.js → xrfnq5k8.js} +237 -434
  209. package/dist/shared/{x5x59srr.js → xva8v7qk.js} +416 -83
  210. package/dist/shared/{dkn3y60a.js → xw1d69vd.js} +76 -112
  211. package/dist/shared/y005b761.js +112 -0
  212. package/dist/shared/ydaz1sv3.js +55 -0
  213. package/dist/shared/{p0cbr3g2.js → zwmqxb0g.js} +1 -1
  214. package/dist/tooling/cli.js +40 -12
  215. package/dist/tooling/migrate/apply.d.ts +16 -0
  216. package/dist/tooling/migrate/commands.d.ts +12 -1
  217. package/dist/tooling/migrate/config.d.ts +8 -0
  218. package/dist/tooling/migrate/doctor.d.ts +25 -0
  219. package/dist/tooling/migrate/drift.d.ts +19 -0
  220. package/dist/tooling/migrate/engine.d.ts +36 -0
  221. package/dist/tooling/migrate/ext.d.ts +17 -0
  222. package/dist/tooling/migrate/extensions.d.ts +51 -0
  223. package/dist/tooling/migrate/managed.d.ts +17 -0
  224. package/dist/tooling/migrate/plan.d.ts +14 -1
  225. package/dist/tooling/migrate/project.d.ts +25 -1
  226. package/dist/tooling/migrate/type-name.d.ts +19 -0
  227. package/package.json +61 -4
  228. package/dist/shared/gk7cs7sp.js +0 -566
@@ -0,0 +1,229 @@
1
+ import {
2
+ quoteIdent2,
3
+ noticeBuffer,
4
+ WireSession,
5
+ cursorStream
6
+ } from "./0jc3ffzf.js";
7
+ import {
8
+ NODE_POSTGRES_CAPABILITIES,
9
+ driverErrors,
10
+ rejectClosed,
11
+ mapFailure,
12
+ runCall
13
+ } from "./rrstp3kb.js";
14
+
15
+ // src/adapters/pg/nodepostgres.ts
16
+ import net from "node:net";
17
+ import pg from "pg";
18
+ var CANCEL_CODE = 80877102;
19
+ var identity = (value) => value;
20
+ var WIRE = {
21
+ getTypeParser() {
22
+ return identity;
23
+ }
24
+ };
25
+ function open4(config) {
26
+ const max = config.max ?? 10;
27
+ const acquireMs = config.timeouts?.acquire;
28
+ const counters = { reserved: 0, busy: 0, reservedBusy: 0, waiting: 0 };
29
+ const inflight = new Set;
30
+ const notices = noticeBuffer();
31
+ let closed = false;
32
+ const isClosed = () => closed;
33
+ const named = config.prepared === "named";
34
+ const pool = new pg.Pool({
35
+ connectionString: config.url,
36
+ max,
37
+ idleTimeoutMillis: 0,
38
+ allowExitOnIdle: true,
39
+ connectionTimeoutMillis: 5000,
40
+ application_name: "okmodel",
41
+ types: WIRE,
42
+ ...config.ssl !== undefined ? { ssl: config.ssl } : {},
43
+ ...config.searchPath !== undefined ? { options: `-c search_path=${config.searchPath}` } : {}
44
+ });
45
+ pool.on("connect", (client) => {
46
+ client.on("notice", (notice) => {
47
+ notices.push(notice);
48
+ });
49
+ });
50
+ const root = new WireSession((text, params) => send(pool, text, params, named, undefined), notices, isClosed, true, () => {
51
+ counters.busy += 1;
52
+ }, () => {
53
+ counters.busy = Math.max(0, counters.busy - 1);
54
+ }, inflight);
55
+ const held = { pool, acquireMs, counters, notices, named, isClosed };
56
+ const checkout = () => closed ? rejectClosed() : import("./6bazvqwm.js").then((mod) => mod.checkout(held));
57
+ return {
58
+ capabilities: NODE_POSTGRES_CAPABILITIES,
59
+ execute(text, params, options) {
60
+ return runCall(closed, options, async (watch) => {
61
+ if (watch === undefined)
62
+ return root.query(text, params, undefined);
63
+ const client = await connectClient(pool, acquireMs, counters);
64
+ counters.busy += 1;
65
+ const session = new WireSession((statement, values) => send(client, statement, values, named, client), notices, isClosed, true, () => {
66
+ return;
67
+ }, () => {
68
+ return;
69
+ }, inflight);
70
+ try {
71
+ return await session.query(text, params, watch);
72
+ } finally {
73
+ counters.busy = Math.max(0, counters.busy - 1);
74
+ client.release();
75
+ }
76
+ });
77
+ },
78
+ async batch(statements, options) {
79
+ const connection = await checkout();
80
+ try {
81
+ return await connection.batch(statements, options);
82
+ } finally {
83
+ await connection.release();
84
+ }
85
+ },
86
+ reserve: () => checkout(),
87
+ stream(text, params) {
88
+ return stream(text, params);
89
+ },
90
+ listen(channel, onNotify) {
91
+ return listen(channel, onNotify);
92
+ },
93
+ cancel() {
94
+ for (const pending of inflight)
95
+ pending.cancel?.();
96
+ },
97
+ stats: () => statsOf(counters, max),
98
+ async close() {
99
+ closed = true;
100
+ await pool.end();
101
+ }
102
+ };
103
+ function stream(text, params) {
104
+ return async function* () {
105
+ const connection = await checkout();
106
+ try {
107
+ yield* cursorStream((statement, values) => connection.execute(statement, values), text, params);
108
+ } finally {
109
+ await connection.release();
110
+ }
111
+ }();
112
+ }
113
+ async function listen(channel, onNotify) {
114
+ if (closed)
115
+ return rejectClosed();
116
+ const client = await connectClient(pool, acquireMs, counters);
117
+ counters.reserved += 1;
118
+ const quoted = quoteIdent2(channel);
119
+ const onNotification = (message) => {
120
+ if (message.channel === channel)
121
+ onNotify(message.payload ?? "");
122
+ };
123
+ client.on("notification", onNotification);
124
+ try {
125
+ await client.query(`LISTEN ${quoted}`);
126
+ } catch (error) {
127
+ counters.reserved = Math.max(0, counters.reserved - 1);
128
+ client.release();
129
+ throw error;
130
+ }
131
+ let stopped = false;
132
+ return async () => {
133
+ if (stopped)
134
+ return;
135
+ stopped = true;
136
+ try {
137
+ await client.query(`UNLISTEN ${quoted}`);
138
+ } finally {
139
+ client.removeListener("notification", onNotification);
140
+ counters.reserved = Math.max(0, counters.reserved - 1);
141
+ client.release();
142
+ }
143
+ };
144
+ }
145
+ }
146
+ function send(runner, text, params, named, client) {
147
+ const query = {
148
+ text,
149
+ rowMode: "array",
150
+ types: WIRE,
151
+ ...params !== undefined && params.length > 0 ? { values: params } : {},
152
+ ...named ? { name: statementName(text) } : {}
153
+ };
154
+ const pending = runner.query(query);
155
+ if (client !== undefined)
156
+ pending.cancel = () => cancelBackend(client);
157
+ return pending;
158
+ }
159
+ var names = new Map;
160
+ function statementName(text) {
161
+ const existing = names.get(text);
162
+ if (existing !== undefined)
163
+ return existing;
164
+ const name = `okm_${String(names.size)}`;
165
+ names.set(text, name);
166
+ return name;
167
+ }
168
+ function cancelBackend(client) {
169
+ const pid = client.processID;
170
+ const secret = client.secretKey;
171
+ if (typeof pid !== "number" || typeof secret !== "number")
172
+ return;
173
+ const socket = client.host.startsWith("/") === true ? net.connect(client.host) : net.connect(client.port, client.host);
174
+ socket.unref();
175
+ socket.once("error", () => {
176
+ socket.destroy();
177
+ });
178
+ socket.once("connect", () => {
179
+ const bytes = Buffer.alloc(16);
180
+ bytes.writeInt32BE(16, 0);
181
+ bytes.writeInt32BE(CANCEL_CODE, 4);
182
+ bytes.writeInt32BE(pid, 8);
183
+ bytes.writeInt32BE(secret, 12);
184
+ socket.end(bytes);
185
+ });
186
+ }
187
+ function connectClient(pool, acquireMs, counters) {
188
+ counters.waiting += 1;
189
+ let settled = false;
190
+ return new Promise((resolve, reject) => {
191
+ let timer;
192
+ if (acquireMs !== undefined) {
193
+ timer = setTimeout(() => {
194
+ settled = true;
195
+ counters.waiting = Math.max(0, counters.waiting - 1);
196
+ driverErrors().then((errors) => {
197
+ reject(errors.acquireTimeout());
198
+ });
199
+ }, acquireMs);
200
+ }
201
+ pool.connect().then((client) => {
202
+ if (settled) {
203
+ client.release();
204
+ return;
205
+ }
206
+ if (timer !== undefined)
207
+ clearTimeout(timer);
208
+ counters.waiting = Math.max(0, counters.waiting - 1);
209
+ resolve(client);
210
+ }, (error) => {
211
+ if (settled)
212
+ return;
213
+ if (timer !== undefined)
214
+ clearTimeout(timer);
215
+ counters.waiting = Math.max(0, counters.waiting - 1);
216
+ mapFailure(error).then(reject);
217
+ });
218
+ });
219
+ }
220
+ function statsOf(counters, size) {
221
+ return {
222
+ size,
223
+ idle: Math.max(0, size - counters.reserved - counters.busy),
224
+ inflight: counters.busy + counters.reservedBusy,
225
+ waiting: counters.waiting
226
+ };
227
+ }
228
+
229
+ export { open4, send, connectClient };
@@ -0,0 +1,31 @@
1
+ import {
2
+ withHttp
3
+ } from "./k34e0xf2.js";
4
+ import {
5
+ decodeResult
6
+ } from "./xrfnq5k8.js";
7
+ import {
8
+ OkmError2
9
+ } from "./52szzakm.js";
10
+
11
+ // src/runtime/read-stream.ts
12
+ async function* streamRows(session, call, prepare, log) {
13
+ await session.connected;
14
+ if (session.pool.capabilities.stream !== true || session.pool.stream === undefined) {
15
+ throw log(new OkmError2("OKM1111", "stream needs a driver with stream: true. This driver does not have it.", withHttp(session.http, {
16
+ fix: { summary: "Use a driver that sets stream, or read with find." }
17
+ })));
18
+ }
19
+ const { plan, params } = await prepare();
20
+ const decode = plan.outputs.includes.length === 0 ? decodeResult : (await import("./edwqqryd.js")).decodeIncluded;
21
+ for await (const chunk of session.pool.stream(plan.text, params)) {
22
+ const decoded = decode(plan, chunk, call.table);
23
+ if (!Array.isArray(decoded))
24
+ continue;
25
+ for (const row of decoded)
26
+ yield row;
27
+ }
28
+ }
29
+ export {
30
+ streamRows
31
+ };
@@ -0,0 +1,442 @@
1
+ import {
2
+ validationGate
3
+ } from "./9m80mkac.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
+ };