okmodel 0.7.0 → 0.9.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 (258) hide show
  1. package/README.md +14 -0
  2. package/dist/adapters/capabilities.d.ts +32 -0
  3. package/dist/adapters/failure.d.ts +1 -1
  4. package/dist/adapters/pg/bun-sql.d.ts +53 -0
  5. package/dist/adapters/pg/bunsql-extra.d.ts +1 -1
  6. package/dist/adapters/pg/bunsql.d.ts +1 -1
  7. package/dist/adapters/pg/neon-extra.d.ts +18 -0
  8. package/dist/adapters/pg/neon.d.ts +104 -0
  9. package/dist/adapters/pg/no-inflight-timeout.d.ts +1 -1
  10. package/dist/contracts/error.d.ts +1 -1
  11. package/dist/dialects/pg/ext/btree-gin.d.ts +26 -0
  12. package/dist/dialects/pg/ext/btree-gin.js +20 -0
  13. package/dist/dialects/pg/ext/btree-gist.d.ts +26 -0
  14. package/dist/dialects/pg/ext/btree-gist.js +20 -0
  15. package/dist/dialects/pg/ext/call.d.ts +53 -0
  16. package/dist/dialects/pg/ext/citext.js +1 -1
  17. package/dist/dialects/pg/ext/feature.d.ts +26 -0
  18. package/dist/dialects/pg/ext/fuzzystrmatch.d.ts +34 -0
  19. package/dist/dialects/pg/ext/fuzzystrmatch.js +33 -0
  20. package/dist/dialects/pg/ext/hstore.d.ts +41 -0
  21. package/dist/dialects/pg/ext/hstore.js +29 -0
  22. package/dist/dialects/pg/ext/index.d.ts +56 -0
  23. package/dist/dialects/pg/ext/index.js +1 -1
  24. package/dist/dialects/pg/ext/ltree.d.ts +41 -0
  25. package/dist/dialects/pg/ext/ltree.js +29 -0
  26. package/dist/dialects/pg/ext/pg-trgm.js +4 -13
  27. package/dist/dialects/pg/ext/pgcrypto.d.ts +33 -0
  28. package/dist/dialects/pg/ext/pgcrypto.js +33 -0
  29. package/dist/dialects/pg/ext/unaccent.d.ts +26 -0
  30. package/dist/dialects/pg/ext/unaccent.js +21 -0
  31. package/dist/dialects/pg/ext/using.d.ts +23 -0
  32. package/dist/dialects/pg/ext/versions.d.ts +36 -0
  33. package/dist/dialects/pg/external.d.ts +19 -0
  34. package/dist/dialects/pg/fn/index.d.ts +39 -1
  35. package/dist/dialects/pg/fn/index.js +118 -26
  36. package/dist/dialects/pg/index.d.ts +3 -0
  37. package/dist/dialects/pg/index.js +394 -14
  38. package/dist/dialects/pg/introspect.d.ts +11 -0
  39. package/dist/dialects/pg/operators.d.ts +1 -1
  40. package/dist/dialects/pg/ops/fragment.d.ts +119 -0
  41. package/dist/dialects/pg/ops/lane.d.ts +49 -0
  42. package/dist/dialects/pg/view/client.d.ts +14 -6
  43. package/dist/dialects/pg/view/index.d.ts +73 -1
  44. package/dist/dialects/pg/view/index.js +156 -5
  45. package/dist/dialects/pg/view/scratch.d.ts +11 -5
  46. package/dist/runtime/artifact-file.d.ts +22 -0
  47. package/dist/runtime/client.d.ts +10 -0
  48. package/dist/runtime/drift-detail.d.ts +2 -1
  49. package/dist/runtime/drift.d.ts +3 -10
  50. package/dist/runtime/otel.d.ts +105 -0
  51. package/dist/runtime/otel.js +97 -0
  52. package/dist/runtime/pg/bun.js +4 -4
  53. package/dist/runtime/pg/neon.d.ts +38 -0
  54. package/dist/runtime/pg/neon.js +64 -0
  55. package/dist/runtime/pg/pg.js +4 -4
  56. package/dist/runtime/pg/pglite.js +4 -4
  57. package/dist/runtime/pg/postgresjs.js +4 -4
  58. package/dist/runtime/plan.d.ts +1 -1
  59. package/dist/runtime/server-check.d.ts +36 -6
  60. package/dist/runtime/sql-fragment.d.ts +43 -0
  61. package/dist/runtime/tenancy/index.js +10 -10
  62. package/dist/runtime/topology.d.ts +1 -1
  63. package/dist/runtime/traits/index.js +16 -17
  64. package/dist/runtime/types.d.ts +15 -1
  65. package/dist/runtime/validate/index.js +4 -4
  66. package/dist/shared/{81ebdx9p.js → 0j6qe91y.js} +4 -4
  67. package/dist/shared/{qq3899ht.js → 12z7aznj.js} +243 -14
  68. package/dist/shared/{gzaveazc.js → 1d9q9cv2.js} +29 -2
  69. package/dist/shared/{w79hsckn.js → 1mxm04fy.js} +5 -5
  70. package/dist/shared/{7k9grvf6.js → 2szgv6yj.js} +5 -5
  71. package/dist/shared/{gs74dbj5.js → 2zdxkt7b.js} +933 -111
  72. package/dist/shared/{m6p12kbr.js → 3456242y.js} +23 -23
  73. package/dist/shared/3bb7wsqt.js +99 -0
  74. package/dist/shared/{y6njgyda.js → 3t6kw83m.js} +2 -2
  75. package/dist/shared/{xhfsfkgk.js → 4vbf83xx.js} +9 -2
  76. package/dist/shared/{pt94kz7f.js → 5kyb3tqx.js} +1 -1
  77. package/dist/shared/61bmrmhr.js +35 -0
  78. package/dist/shared/{x933k9a3.js → 62b603gr.js} +10 -10
  79. package/dist/shared/{jgqj11w4.js → 6f0z3hzh.js} +4 -0
  80. package/dist/shared/{tsvww22q.js → 6nae0spm.js} +156 -95
  81. package/dist/shared/{mtaszv5a.js → 71n0jmhc.js} +102 -4
  82. package/dist/shared/{5e3tf63j.js → 760ppr76.js} +14 -6
  83. package/dist/shared/7ba5h6pb.js +17 -0
  84. package/dist/shared/{sfz1ksn6.js → 8jesqsrk.js} +2 -2
  85. package/dist/shared/{5v3xf80k.js → 8n688yjg.js} +1 -1
  86. package/dist/shared/{rs73fca9.js → 8qzkd9f3.js} +6 -6
  87. package/dist/shared/{ax7qv55k.js → 90sg0w5a.js} +202 -25
  88. package/dist/shared/{zm0ptry0.js → 99qp9vtg.js} +1 -1
  89. package/dist/shared/{xnxsed62.js → 9dr2qnx7.js} +14 -14
  90. package/dist/shared/{1w508jbr.js → 9dtgn1z2.js} +21 -17
  91. package/dist/shared/{h15jx9bc.js → 9dvh1dr6.js} +1 -1
  92. package/dist/shared/{q5c6ngn3.js → a62rb2da.js} +1 -1
  93. package/dist/shared/{te5as7z7.js → aksb89mn.js} +3 -3
  94. package/dist/shared/{bz84tkzm.js → bphrgwz9.js} +2 -2
  95. package/dist/shared/bsek72fg.js +225 -0
  96. package/dist/shared/{7mnme5tn.js → c3mvnq7a.js} +1 -1
  97. package/dist/shared/{p6qc5gcp.js → ccsmhv9r.js} +25 -7
  98. package/dist/shared/{pbp1ss85.js → cexb7z6b.js} +2 -2
  99. package/dist/shared/{ab9gmbwy.js → cf8zdz5p.js} +5 -5
  100. package/dist/shared/{jvd0mqag.js → ckz05z15.js} +5 -5
  101. package/dist/shared/{dk8v79p3.js → d5nmj7vv.js} +1 -1
  102. package/dist/shared/{14vx95q4.js → dd5pmnky.js} +7 -7
  103. package/dist/shared/{vb6qfyve.js → ds229x4r.js} +2 -2
  104. package/dist/shared/{1qa0qajz.js → e275q7sd.js} +1 -1
  105. package/dist/shared/{zp9fmend.js → ebhz8vh6.js} +4 -4
  106. package/dist/shared/{hyh73qk6.js → ecn8mfdq.js} +5 -5
  107. package/dist/shared/{je4axnm8.js → fdsz3w80.js} +2 -2
  108. package/dist/shared/{9s0ypp87.js → fk9vef30.js} +85 -74
  109. package/dist/shared/{g6evj1j3.js → g89fmz5s.js} +1 -1
  110. package/dist/shared/{aytqh5y2.js → ghqzdp94.js} +2 -2
  111. package/dist/shared/h9811jjm.js +17 -0
  112. package/dist/shared/{dkxgzaqd.js → hqpxf4a4.js} +1 -1
  113. package/dist/shared/{8skf0cam.js → jpybhf98.js} +1 -1
  114. package/dist/shared/{67txcrcw.js → kgwp4jdd.js} +4 -4
  115. package/dist/shared/kzgrnckd.js +29 -0
  116. package/dist/shared/mazpbcdj.js +233 -0
  117. package/dist/shared/{kb2sdhfb.js → mfvx6kjx.js} +17 -1
  118. package/dist/shared/{svncdeh5.js → mgaq2sd3.js} +11 -8
  119. package/dist/shared/{5mtf6cj3.js → mvmm3xpb.js} +5 -2
  120. package/dist/shared/{b5pftnn2.js → myk8fswx.js} +66 -21
  121. package/dist/shared/{2shdbn8q.js → n306gqkd.js} +9 -9
  122. package/dist/shared/{n816peh1.js → n454kcnw.js} +1 -1
  123. package/dist/shared/n5whk3mt.js +665 -0
  124. package/dist/shared/n7kg8mmx.js +61 -0
  125. package/dist/shared/{vqrjpcqn.js → nmnsgj7x.js} +1 -1
  126. package/dist/shared/{tbrvgw6z.js → npdftanb.js} +19 -26
  127. package/dist/shared/{ek2wt2dz.js → pg4ezgyf.js} +11 -30
  128. package/dist/shared/{5fe3kbsh.js → pgsak1yw.js} +1 -1
  129. package/dist/shared/{bm22fvzr.js → qfw14tna.js} +2 -2
  130. package/dist/shared/{wa07eas9.js → qvwwp1mp.js} +62 -41
  131. package/dist/shared/rbxt795q.js +1766 -0
  132. package/dist/shared/{xjk7hnn5.js → s7he5e5d.js} +2 -2
  133. package/dist/shared/{w72kgg4q.js → ss2kv2rs.js} +6 -6
  134. package/dist/shared/stfa9xcw.js +450 -0
  135. package/dist/shared/{3v7t1yrv.js → th3be82n.js} +2 -2
  136. package/dist/shared/{79dt02vc.js → tr64w5p0.js} +1 -1
  137. package/dist/shared/{cn957f8d.js → tw1nvvfj.js} +3 -3
  138. package/dist/shared/{ed0b8k4s.js → typ3vy7t.js} +1 -1
  139. package/dist/shared/{9628a9xt.js → vk7rag46.js} +1 -1
  140. package/dist/shared/{ze4hxsmy.js → w9ejzaqf.js} +1 -1
  141. package/dist/shared/{vczs8gd4.js → wvtjg5m3.js} +2 -2
  142. package/dist/shared/{9y3mmmh1.js → xfhe73sb.js} +1 -1
  143. package/dist/shared/xqacq37x.js +61 -0
  144. package/dist/shared/{1est4ezg.js → xtsydxjg.js} +3 -3
  145. package/dist/shared/xv8qj97g.js +96 -0
  146. package/dist/shared/{bg3rym3g.js → xwhdcrm4.js} +2 -2
  147. package/dist/shared/{kr0jzbw2.js → yvabyg9k.js} +7 -7
  148. package/dist/shared/yybhre6n.js +136 -0
  149. package/dist/shared/zdqf8k5d.js +8 -0
  150. package/dist/shared/{jznfax0y.js → zq1he88x.js} +4 -4
  151. package/dist/tooling/cli.js +108 -23
  152. package/dist/tooling/driver-run.js +4654 -0
  153. package/dist/tooling/driver-test.d.ts +36 -0
  154. package/dist/tooling/github/comment.d.ts +50 -0
  155. package/dist/tooling/github/post-comment.d.ts +17 -0
  156. package/dist/tooling/migrate/commands.d.ts +3 -2
  157. package/dist/tooling/migrate/config.d.ts +13 -0
  158. package/dist/tooling/migrate/export.d.ts +165 -0
  159. package/dist/tooling/migrate/ext-surface.d.ts +80 -0
  160. package/dist/tooling/migrate/ext.d.ts +31 -4
  161. package/dist/tooling/migrate/import-drizzle.d.ts +40 -0
  162. package/dist/tooling/migrate/index.d.ts +3 -0
  163. package/dist/tooling/migrate/index.js +55 -10
  164. package/dist/tooling/migrate/lint-rules.d.ts +18 -1
  165. package/dist/tooling/migrate/lint.d.ts +14 -1
  166. package/dist/tooling/migrate/place.d.ts +18 -0
  167. package/dist/tooling/migrate/plan-json.d.ts +65 -0
  168. package/dist/tooling/migrate/project.d.ts +13 -1
  169. package/dist/tooling/migrate/pull-emit.d.ts +98 -0
  170. package/dist/tooling/migrate/pull-routines.d.ts +126 -0
  171. package/dist/tooling/migrate/pull-verify.d.ts +26 -0
  172. package/dist/tooling/migrate/pull.d.ts +62 -0
  173. package/dist/tooling/migrate/reprint.d.ts +32 -0
  174. package/dist/tooling/migrate/sandbox.d.ts +8 -2
  175. package/dist/tooling/migrate/schema-api.d.ts +43 -0
  176. package/dist/tooling/migrate/sql-lex.d.ts +18 -0
  177. package/dist/tooling/shared/04svnxxj.js +344 -0
  178. package/dist/tooling/shared/0j0jtse1.js +13 -0
  179. package/dist/tooling/shared/0r05zhyq.js +618 -0
  180. package/dist/tooling/shared/1800eymw.js +49 -0
  181. package/dist/tooling/shared/35zemrmf.js +12 -0
  182. package/dist/tooling/shared/3rmaz3pq.js +140 -0
  183. package/dist/tooling/shared/3yn79q5m.js +12 -0
  184. package/dist/tooling/shared/54tqkxja.js +51 -0
  185. package/dist/tooling/shared/57btdkgm.js +209 -0
  186. package/dist/tooling/shared/5bmksndh.js +118 -0
  187. package/dist/tooling/shared/5p8km5jb.js +38 -0
  188. package/dist/tooling/shared/5x3pf400.js +185 -0
  189. package/dist/tooling/shared/5zrzffnk.js +6 -0
  190. package/dist/tooling/shared/62fxpc1p.js +332 -0
  191. package/dist/tooling/shared/68tf18v4.js +20 -0
  192. package/dist/tooling/shared/6vk28rjp.js +98 -0
  193. package/dist/tooling/shared/76pv6qff.js +9 -0
  194. package/dist/tooling/shared/7aews1zg.js +311 -0
  195. package/dist/tooling/shared/7b3xp9gc.js +195 -0
  196. package/dist/tooling/shared/7kms6fvj.js +20 -0
  197. package/dist/tooling/shared/7pxncnzs.js +40 -0
  198. package/dist/tooling/shared/829fjxyw.js +198 -0
  199. package/dist/tooling/shared/8m5znpdt.js +66 -0
  200. package/dist/tooling/shared/8yr7ges1.js +211 -0
  201. package/dist/tooling/shared/96ps4g1z.js +32 -0
  202. package/dist/tooling/shared/9qr9rkbh.js +143 -0
  203. package/dist/tooling/shared/9sfbtwsk.js +49 -0
  204. package/dist/tooling/shared/9wsnsvbf.js +10 -0
  205. package/dist/tooling/shared/a2xe04bm.js +18 -0
  206. package/dist/tooling/shared/a4pzghha.js +20 -0
  207. package/dist/tooling/shared/am04v8na.js +233 -0
  208. package/dist/tooling/shared/asgr07mp.js +194 -0
  209. package/dist/tooling/shared/c0xevn1h.js +312 -0
  210. package/dist/tooling/shared/dhatassp.js +134 -0
  211. package/dist/tooling/shared/ezqn88nj.js +1696 -0
  212. package/dist/tooling/shared/f77nnfqy.js +231 -0
  213. package/dist/tooling/shared/fh07z1dt.js +15 -0
  214. package/dist/tooling/shared/gqwevek4.js +81 -0
  215. package/dist/tooling/shared/hfq3n3bd.js +4116 -0
  216. package/dist/tooling/shared/htnaj77v.js +17 -0
  217. package/dist/tooling/shared/j664qjrt.js +64 -0
  218. package/dist/tooling/shared/jpgmjsx3.js +38 -0
  219. package/dist/tooling/shared/jr2nmr81.js +497 -0
  220. package/dist/tooling/shared/jsjjz875.js +139 -0
  221. package/dist/tooling/shared/kbqnrs1x.js +1425 -0
  222. package/dist/tooling/shared/kz715295.js +94 -0
  223. package/dist/tooling/shared/m3xt8b1h.js +22 -0
  224. package/dist/tooling/shared/m4hhb8aa.js +9 -0
  225. package/dist/tooling/shared/mw5npye0.js +16 -0
  226. package/dist/tooling/shared/n4jqhpqz.js +27 -0
  227. package/dist/tooling/shared/nxq95eb6.js +42 -0
  228. package/dist/tooling/shared/qartgazh.js +70 -0
  229. package/dist/tooling/shared/rcywj2w8.js +190 -0
  230. package/dist/tooling/shared/s6qha57m.js +16 -0
  231. package/dist/tooling/shared/shjs0379.js +150 -0
  232. package/dist/tooling/shared/sjwkt1pg.js +16 -0
  233. package/dist/tooling/shared/stebqyyd.js +401 -0
  234. package/dist/tooling/shared/t4xveemq.js +250 -0
  235. package/dist/tooling/shared/t6zjeake.js +261 -0
  236. package/dist/tooling/shared/tcwpjvxe.js +9 -0
  237. package/dist/tooling/shared/tpssp7ac.js +160 -0
  238. package/dist/tooling/shared/v6j1ep6w.js +42 -0
  239. package/dist/tooling/shared/vwr2y833.js +270 -0
  240. package/dist/tooling/shared/w17sj05d.js +7 -0
  241. package/dist/tooling/shared/w5d89rb4.js +131 -0
  242. package/dist/tooling/shared/wbwq1jsa.js +157 -0
  243. package/dist/tooling/shared/wezxf3h8.js +17 -0
  244. package/dist/tooling/shared/yb4kd19s.js +12 -0
  245. package/dist/tooling/shared/ybdew6j7.js +294 -0
  246. package/dist/tooling/shared/yfedr1zb.js +6 -0
  247. package/dist/tooling/shared/yhp829bx.js +177 -0
  248. package/dist/tooling/shared/ytk1tjs0.js +604 -0
  249. package/dist/tooling/shared/zs9aymcp.js +210 -0
  250. package/dist/tooling/shared/zx3v9phv.js +8 -0
  251. package/dist/tooling/testing/index.js +3 -3
  252. package/package.json +50 -9
  253. package/dist/shared/ktz42yez.js +0 -113
  254. package/dist/shared/ttsqgp4p.js +0 -71
  255. package/dist/shared/y34a8714.js +0 -150
  256. package/dist/shared/zy8r7qvz.js +0 -116
  257. package/dist/shared/{ywr8hmey.js → 606g0nbf.js} +3 -3
  258. package/dist/shared/{rpycarcr.js → gcefca6p.js} +4 -4
@@ -0,0 +1,17 @@
1
+ // src/runtime/catalog-cache.ts
2
+ var inflight = new Map;
3
+ function loadOnce(key, load) {
4
+ const found = inflight.get(key);
5
+ if (found !== undefined)
6
+ return found;
7
+ const pending = Promise.resolve().then(load);
8
+ inflight.set(key, pending);
9
+ pending.catch(() => {
10
+ if (inflight.get(key) === pending)
11
+ inflight.delete(key);
12
+ });
13
+ return pending;
14
+ }
15
+ export {
16
+ loadOnce
17
+ };
@@ -0,0 +1,64 @@
1
+ import {
2
+ decodeRows,
3
+ readHandle,
4
+ readCall,
5
+ attachHttp
6
+ } from "./jr2nmr81.js";
7
+ import {
8
+ OkmError,
9
+ throwNamed,
10
+ fail2
11
+ } from "./hfq3n3bd.js";
12
+
13
+ // src/runtime/lock.ts
14
+ var LOCKS = ["update", "share"];
15
+ var WAITS = ["nowait", "skip"];
16
+ function shown(value) {
17
+ return typeof value === "string" ? value : JSON.stringify(value) ?? "undefined";
18
+ }
19
+ function build(session, table, options, mods, view) {
20
+ try {
21
+ return plan(session, table, options, mods, view);
22
+ } catch (error) {
23
+ throw error instanceof OkmError ? attachHttp(session.http, error) : error;
24
+ }
25
+ }
26
+ function plan(session, table, options, mods, view) {
27
+ const { lock, wait, ...rest } = options;
28
+ const forced = session.pool.forcedRoute;
29
+ if (rest.route === "replica" || forced === "replica") {
30
+ throw new OkmError("OKM1840", "A locking read needs the primary.", {
31
+ fix: {
32
+ summary: "Run the operation on the primary. Writes, batch, locks, and tx() never go to a replica."
33
+ }
34
+ });
35
+ }
36
+ if (lock === "skip") {
37
+ fail2("OKM1120", 'lock: "skip" claims rows on update. A read uses lock: "update" and wait: "skip".', 'Pass lock: "update" and wait: "skip" inside tx(). An update can claim rows on its own.');
38
+ }
39
+ if (session.tx === undefined) {
40
+ throw new OkmError("OKM1830", "A row lock needs a transaction. find was called outside tx().", {
41
+ fix: { summary: "Move the find into tx(): the lock lasts until the transaction ends." }
42
+ });
43
+ }
44
+ if (lock === undefined)
45
+ fail2("OKM1121", "wait needs lock: pass lock: update or lock: share.");
46
+ if (typeof lock !== "string" || !LOCKS.includes(lock)) {
47
+ throwNamed("OKM1120", shown(lock), LOCKS, `lock must be one of: ${LOCKS.join(", ")}.`);
48
+ }
49
+ if (wait !== undefined && (typeof wait !== "string" || !WAITS.includes(wait))) {
50
+ throwNamed("OKM1120", shown(wait), WAITS, `wait must be one of: ${WAITS.join(", ")}.`);
51
+ }
52
+ const clause = ` for ${lock} of t${wait === undefined ? "" : wait === "skip" ? " skip locked" : " nowait"}`;
53
+ const call = {
54
+ ...readCall("find", table, rest, mods.all, session.scope, session.schema.model[table], session.schema, view),
55
+ tail(sink) {
56
+ sink.text(clause);
57
+ sink.mark(`lock:${clause}`);
58
+ }
59
+ };
60
+ return readHandle(session, table, call, mods, (compiled, rows) => decodeRows(compiled, rows, table));
61
+ }
62
+ export {
63
+ build
64
+ };
@@ -0,0 +1,38 @@
1
+ import {
2
+ WRITE_PARAM_BUDGET2,
3
+ executeWrite2,
4
+ explainWrite2,
5
+ updateSliceBody2,
6
+ rejectDuplicateTargets2,
7
+ emitSet2,
8
+ Sql2,
9
+ writeSet2,
10
+ rejectMixedSets2,
11
+ readClaim2,
12
+ rejectUpdateKeys2,
13
+ requireFilter2,
14
+ returningOption2,
15
+ expectOf2,
16
+ allowSet2,
17
+ noteGuarded2
18
+ } from "./ezqn88nj.js";
19
+ import"./w5d89rb4.js";
20
+ import"./35zemrmf.js";
21
+ export {
22
+ Sql2 as Sql,
23
+ WRITE_PARAM_BUDGET2 as WRITE_PARAM_BUDGET,
24
+ allowSet2 as allowSet,
25
+ emitSet2 as emitSet,
26
+ executeWrite2 as executeWrite,
27
+ expectOf2 as expectOf,
28
+ explainWrite2 as explainWrite,
29
+ noteGuarded2 as noteGuarded,
30
+ readClaim2 as readClaim,
31
+ rejectDuplicateTargets2 as rejectDuplicateTargets,
32
+ rejectMixedSets2 as rejectMixedSets,
33
+ rejectUpdateKeys2 as rejectUpdateKeys,
34
+ requireFilter2 as requireFilter,
35
+ returningOption2 as returningOption,
36
+ updateSliceBody2 as updateSliceBody,
37
+ writeSet2 as writeSet
38
+ };
@@ -0,0 +1,497 @@
1
+ import {
2
+ OkmError,
3
+ safe,
4
+ throwNamed,
5
+ archiveRules,
6
+ readOptionNames,
7
+ readNeedsOperatorSql,
8
+ bindCall,
9
+ compileCall,
10
+ decodeResult,
11
+ logicalIntent,
12
+ appliedRules,
13
+ rejectKeys
14
+ } from "./hfq3n3bd.js";
15
+
16
+ // src/runtime/client.ts
17
+ var PLAN_LIMIT = 64;
18
+ var SINGLE_ENDPOINT = {
19
+ endpoint: "primary",
20
+ role: "primary",
21
+ reason: "single-endpoint"
22
+ };
23
+ function createClient(schema, pool, options) {
24
+ const closing = {
25
+ current: undefined,
26
+ early: false
27
+ };
28
+ const checked = checkServer(pool, schema, options.http, options.requireMeta === true, callOptions(options), {
29
+ ...options.catalog !== undefined ? { catalog: options.catalog } : {},
30
+ ...options.catalogDir !== undefined ? { catalogDir: options.catalogDir } : {}
31
+ });
32
+ const connected = checked.catch((error) => {
33
+ if (!closing.early)
34
+ throw error;
35
+ throw new OkmError("unavailable", "The client was closed before it connected. Await connected before close(), or do not close it before use.", { kind: "unavailable", fix: { summary: "Await client.connected before calling close()." } });
36
+ });
37
+ connected.catch(() => {
38
+ return;
39
+ });
40
+ const session = {
41
+ schema,
42
+ pool,
43
+ ownsPool: options.ownsPool,
44
+ http: options.http,
45
+ includeValues: options.includeValues === true,
46
+ logger: options.logger,
47
+ connected,
48
+ cache: new Map,
49
+ generators: options.generators,
50
+ timeouts: options.timeouts,
51
+ hookm: options.hookm,
52
+ closing
53
+ };
54
+ return openClient(session);
55
+ }
56
+ function openClient(session) {
57
+ const names = Object.keys(session.schema.model);
58
+ const tables = {};
59
+ for (const name of names)
60
+ tables[name] = tableApi(session, name);
61
+ const close = () => {
62
+ if (!session.ownsPool)
63
+ return Promise.resolve();
64
+ session.closing.early = true;
65
+ session.closing.current ??= session.pool.close();
66
+ return session.closing.current;
67
+ };
68
+ const client = {
69
+ ...tables,
70
+ table(name) {
71
+ const found = tables[name];
72
+ if (found !== undefined)
73
+ return found;
74
+ try {
75
+ if (session.schema.model[name] !== undefined && session.schema.tenancy !== undefined && session.scope === undefined) {
76
+ session.schema.tenancy.missing(name);
77
+ }
78
+ throwNamed("OKM1120", name, names, `Table ${name} is not in the schema. Accepted names: ${names.join(", ")}.`);
79
+ } catch (error) {
80
+ if (error instanceof OkmError)
81
+ throw attachHttp(session.http, error);
82
+ throw error;
83
+ }
84
+ },
85
+ close,
86
+ connected: session.connected,
87
+ tx: (...args) => import("./stebqyyd.js").then((mod) => mod.tx(session, args)),
88
+ batch: (ops, options) => batchHandle(session, ops, options),
89
+ ...session.tx?.extras
90
+ };
91
+ const hooks = session.schema.hooks;
92
+ if (hooks !== undefined) {
93
+ for (const hook of hooks) {
94
+ hook(client, {
95
+ names,
96
+ tables,
97
+ scoped: session.scope !== undefined,
98
+ open: (scope) => openClient({ ...session, scope })
99
+ });
100
+ }
101
+ }
102
+ attachAsyncDispose(client, close);
103
+ return client;
104
+ }
105
+ function attachAsyncDispose(client, close) {
106
+ const symbol = Symbol.asyncDispose;
107
+ if (typeof symbol !== "symbol")
108
+ return;
109
+ Object.defineProperty(client, symbol, {
110
+ configurable: true,
111
+ enumerable: false,
112
+ writable: true,
113
+ value: () => close()
114
+ });
115
+ }
116
+ function tableApi(session, table, view, uses) {
117
+ const base = { uses };
118
+ const mods = { archive: view, uses };
119
+ const api = {
120
+ find(options = {}) {
121
+ if ("lock" in options) {
122
+ return lazyRead(session, () => import("./j664qjrt.js"), table, options, base, view);
123
+ }
124
+ return start(session, "find", table, options, base, view);
125
+ },
126
+ one(options = {}) {
127
+ return start(session, "one", table, options, base, view);
128
+ },
129
+ count(options = {}) {
130
+ return start(session, "count", table, options, base, view);
131
+ },
132
+ exists(options = {}) {
133
+ return start(session, "exists", table, options, base, view);
134
+ },
135
+ page(options) {
136
+ return lazyRead(session, () => import("./w17sj05d.js"), table, options, base, view);
137
+ },
138
+ aggregate(options = {}) {
139
+ return lazyRead(session, () => import("./7aews1zg.js"), table, options, base, view);
140
+ },
141
+ insert(data, options = {}) {
142
+ return writeHandle(session, "insert", table, data, options, mods);
143
+ },
144
+ update(target, options = {}) {
145
+ return writeHandle(session, "update", table, target, options, mods);
146
+ },
147
+ delete(target, options = {}) {
148
+ return writeHandle(session, "delete", table, target, options, mods);
149
+ }
150
+ };
151
+ for (const walk of ["descendants", "ancestors"]) {
152
+ api[walk] = (id, options) => lazyRead(session, () => import("./m4hhb8aa.js"), table, { id, walk, options }, base, view);
153
+ }
154
+ api.sync = (id, relation, ids, options) => lazyRead(session, () => import("./0r05zhyq.js"), table, { id, relation, ids, options }, base, view, true);
155
+ const hooks = session.schema.hooks;
156
+ if (hooks !== undefined) {
157
+ for (const hook of hooks) {
158
+ hook(api, {
159
+ table,
160
+ ...view !== undefined ? { view } : {},
161
+ uses,
162
+ reopen: (next) => tableApi(session, table, next, uses),
163
+ session
164
+ });
165
+ }
166
+ }
167
+ for (const name in session.schema.model[table]?.presets) {
168
+ api[name] = (...args) => tableApi(session, table, view, [uses, name, args]);
169
+ }
170
+ api[Symbol.for("okm.lane")] = [session, table, mapError];
171
+ return api;
172
+ }
173
+ function batchHandle(session, ops, options, replica) {
174
+ return queryHandle(() => import("./shjs0379.js").then((mod) => mod.batch(session, ops, options, replica)), {
175
+ replica: () => batchHandle(session, ops, options, true)
176
+ });
177
+ }
178
+ var writers;
179
+ function loadWrite() {
180
+ writers ??= import("./jpgmjsx3.js");
181
+ return writers;
182
+ }
183
+ function queryHandle(run, extra) {
184
+ let pending;
185
+ const once = () => {
186
+ pending ??= run();
187
+ return pending;
188
+ };
189
+ const self = {
190
+ then(onFulfilled, onRejected) {
191
+ return once().then(onFulfilled, onRejected);
192
+ },
193
+ catch(onRejected) {
194
+ return self.then(undefined, onRejected);
195
+ },
196
+ finally(fn) {
197
+ return self.then((value) => {
198
+ fn();
199
+ return value;
200
+ }, (error) => {
201
+ fn();
202
+ throw error;
203
+ });
204
+ },
205
+ safe() {
206
+ return safe(once());
207
+ },
208
+ ...extra
209
+ };
210
+ return self;
211
+ }
212
+ function writeHandle(session, op, table, input, options, mods) {
213
+ return queryHandle(() => runWrite(session, op, table, input, options, mods), {
214
+ "~plan": () => runWrite(session, op, table, input, options, mods, true),
215
+ sql() {
216
+ return loadWrite().then((mod) => mod.explainWrite(session.schema, op, table, input, options, mods, session.generators, session.scope));
217
+ },
218
+ expect(count) {
219
+ return writeHandle(session, op, table, input, options, { ...mods, expect: count });
220
+ },
221
+ all(reason) {
222
+ return writeHandle(session, op, table, input, options, { ...mods, all: reason });
223
+ }
224
+ });
225
+ }
226
+ async function runWrite(session, op, table, input, options, mods, plan) {
227
+ await session.connected;
228
+ const mod = await loadWrite();
229
+ try {
230
+ return await mod.executeWrite(session, op, table, input, options, mods, session.scope, plan);
231
+ } catch (error) {
232
+ throw logged(session, await mapError(session, error));
233
+ }
234
+ }
235
+ function rejectedRead(error) {
236
+ const fail = () => Promise.reject(error);
237
+ return queryHandle(fail, {
238
+ all: () => rejectedRead(error),
239
+ required: () => rejectedRead(error),
240
+ inspect: fail,
241
+ sql: fail,
242
+ stream() {
243
+ return {
244
+ [Symbol.asyncIterator]() {
245
+ return { next: fail };
246
+ }
247
+ };
248
+ }
249
+ });
250
+ }
251
+ function start(session, op, table, options, mods, view) {
252
+ const model = session.schema.model[table];
253
+ let call;
254
+ try {
255
+ call = readCall(op, table, options, mods.all, session.scope, model, session.schema, view);
256
+ } catch (error) {
257
+ return rejectedRead(error instanceof OkmError ? attachHttp(session.http, error) : error);
258
+ }
259
+ return readHandle(session, table, call, mods, async (plan, rows) => {
260
+ const value = await decodeRows(plan, rows, table);
261
+ if (mods.required === true && value === null) {
262
+ throw new OkmError("not_found", `one() on ${table} matched no row.`, withHttp(session.http, { kind: "not_found", table }));
263
+ }
264
+ return value;
265
+ }, () => ({
266
+ all(reason) {
267
+ return start(session, op, table, options, { ...mods, all: reason }, view);
268
+ },
269
+ required() {
270
+ return start(session, op, table, options, { ...mods, required: true }, view);
271
+ },
272
+ stream(batch) {
273
+ return streamLazy(session, call, mods, view, batch);
274
+ }
275
+ }));
276
+ }
277
+ function lazyRead(session, load, table, options, mods, view, sync) {
278
+ const read = () => load().then((mod) => ({ it: mod.build(session, table, options, mods, view) }));
279
+ const sql = () => read().then((box) => box.it.sql());
280
+ const extra = { sql };
281
+ if (sync) {
282
+ extra.expect = (count) => lazyRead(session, load, table, options, { ...mods, expect: count }, view, true);
283
+ } else {
284
+ extra.inspect = () => read().then((box) => box.it.inspect());
285
+ extra.all = (reason) => lazyRead(session, load, table, options, { ...mods, all: reason }, view);
286
+ }
287
+ return queryHandle(() => read().then((box) => box.it), extra);
288
+ }
289
+ async function decodeRows(plan, rows, table) {
290
+ return plan.outputs.includes.length === 0 ? decodeResult(plan, rows, table) : (await import("./ytk1tjs0.js")).decodeIncluded(plan, rows, table);
291
+ }
292
+ function readHandle(session, table, call, mods, decode, extra) {
293
+ let prepared;
294
+ const origin = call;
295
+ const searched = "search" in call;
296
+ const prepare = async () => {
297
+ if (prepared !== undefined)
298
+ return prepared;
299
+ if (mods.uses !== undefined) {
300
+ call = (await import("./5bmksndh.js")).refine(session.schema, origin, mods.uses);
301
+ }
302
+ if (searched) {
303
+ call = (await import("./wbwq1jsa.js")).applyTextSearch(session.schema, call);
304
+ }
305
+ const hooks = await hooksFor(call);
306
+ if (readNeedsOperatorSql(session.schema, call) || hooks?.needsOperator(session.schema, call)) {
307
+ await loadOperatorSql();
308
+ }
309
+ if (call.build === undefined && selectHasWindow(call.select)) {
310
+ call = (await import("./c0xevn1h.js")).attach(session.schema, call);
311
+ }
312
+ const bound = bindCall(session.schema, call, hooks);
313
+ let plan = session.cache.get(bound.key);
314
+ if (plan === undefined) {
315
+ plan = compileCall(session.schema, call, bound.key, hooks);
316
+ if (session.cache.size >= PLAN_LIMIT) {
317
+ const oldest = session.cache.keys().next().value;
318
+ if (oldest !== undefined)
319
+ session.cache.delete(oldest);
320
+ }
321
+ session.cache.set(bound.key, plan);
322
+ }
323
+ prepared = { plan, params: bound.params };
324
+ return prepared;
325
+ };
326
+ const sync = () => !searched && call.include === undefined && mods.uses === undefined && !selectHasWindow(call.select) && !readNeedsOperatorSql(session.schema, call);
327
+ const run = async () => {
328
+ await session.connected;
329
+ const { plan, params } = await prepare();
330
+ try {
331
+ const result = await session.pool.execute(plan.text, params, callOptions(call, session.timeouts?.statement));
332
+ if (session.hookm && result.notices.length) {
333
+ import("./96ps4g1z.js").then((mod) => mod.notices(session.hookm, result.notices));
334
+ }
335
+ return await decode(plan, result.rows);
336
+ } catch (error) {
337
+ const mapped = await mapError(session, error);
338
+ if (session.schema.model[table]?.conceal !== true)
339
+ throw logged(session, mapped);
340
+ const { scrubCall } = await import("./tpssp7ac.js");
341
+ throw logged(session, scrubCall(mapped, session.schema, call));
342
+ }
343
+ };
344
+ return queryHandle(run, {
345
+ inspect() {
346
+ const source = session.schema.model[table]?.source;
347
+ if (call.include === undefined && prepared !== undefined) {
348
+ return finish(session, inspection(prepared.plan, prepared.params, call, source), call);
349
+ }
350
+ if (sync()) {
351
+ const bound = bindNow(session.schema, call);
352
+ prepared = bound;
353
+ return finish(session, inspection(bound.plan, bound.params, call, source), call);
354
+ }
355
+ return prepare().then(({ plan, params }) => finish(session, inspection(plan, params, call, source), call));
356
+ },
357
+ sql() {
358
+ if (call.include === undefined && prepared !== undefined) {
359
+ return { text: prepared.plan.text, params: prepared.params };
360
+ }
361
+ if (sync()) {
362
+ const { plan, params } = bindNow(session.schema, call);
363
+ prepared = { plan, params };
364
+ return { text: plan.text, params };
365
+ }
366
+ return prepare().then(({ plan, params }) => ({ text: plan.text, params }));
367
+ },
368
+ ...extra?.(prepare)
369
+ });
370
+ }
371
+ function streamLazy(session, call, mods, view, batch) {
372
+ return async function* () {
373
+ const { streamRows } = await import("./yhp829bx.js");
374
+ yield* streamRows(session, call, mods, view, batch, (error) => logged(session, error));
375
+ }();
376
+ }
377
+ function archiveRecord(model, view) {
378
+ if (model?.archive === undefined)
379
+ return;
380
+ const rules = archiveRules(model, view);
381
+ if (rules === undefined)
382
+ return;
383
+ return { ruleLines: rules };
384
+ }
385
+ function readCall(op, table, options, all, scope, model, schema, view) {
386
+ const record = options;
387
+ rejectKeys(record, readOptionNames(op), op, true);
388
+ return {
389
+ op,
390
+ table,
391
+ ...record.where !== undefined ? { where: record.where } : {},
392
+ ...record.select !== undefined ? { select: record.select } : {},
393
+ ...record.orderBy !== undefined ? { orderBy: record.orderBy } : {},
394
+ ...record.limit !== undefined ? { limit: record.limit } : {},
395
+ ...record.include !== undefined ? { include: record.include } : {},
396
+ ..."search" in record ? { search: record.search } : {},
397
+ ...callOptions(record),
398
+ ...all !== undefined ? { all } : {},
399
+ ...scope !== undefined ? { scope } : {},
400
+ ...schema.tenancy !== undefined && model !== undefined ? { tenancyRules: schema.tenancy.rules(table, model.source, scope) } : {},
401
+ ...view !== undefined ? { archive: view } : {},
402
+ ...archiveRecord(model, view)
403
+ };
404
+ }
405
+ async function checkServer(pool, schema, http, requireMeta, options, source) {
406
+ const { acceptServer } = await import("./76pv6qff.js");
407
+ await acceptServer(pool, schema, http, requireMeta, options, source);
408
+ }
409
+ async function mapError(session, error) {
410
+ if (error instanceof OkmError) {
411
+ return session.http === undefined ? error : new OkmError(error.code, error.message, { ...errorFields(error), http: session.http });
412
+ }
413
+ const { mapPostgresError } = await import("./yfedr1zb.js");
414
+ return mapPostgresError(error, mapOptions(session.http, session.includeValues, session.schema));
415
+ }
416
+ function errorFields(error) {
417
+ return {
418
+ kind: error.kind,
419
+ ...error.table !== undefined ? { table: error.table } : {},
420
+ ...error.columns.length > 0 ? { columns: error.columns } : {},
421
+ fix: error.fix
422
+ };
423
+ }
424
+ var includeHooks;
425
+ var operatorSql;
426
+ function loadOperatorSql() {
427
+ operatorSql ??= import("./7b3xp9gc.js").then(() => {
428
+ return;
429
+ });
430
+ return operatorSql;
431
+ }
432
+ function selectHasWindow(select) {
433
+ return Array.isArray(select) && select.some((item) => typeof item !== "string");
434
+ }
435
+ function hooksFor(call) {
436
+ if (call.include === undefined)
437
+ return Promise.resolve(undefined);
438
+ includeHooks ??= import("./ytk1tjs0.js").then((mod) => mod.includeHooks);
439
+ return includeHooks;
440
+ }
441
+ function bindNow(schema, call) {
442
+ const bound = bindCall(schema, call);
443
+ const plan = compileCall(schema, call, bound.key);
444
+ return { plan, params: bound.params };
445
+ }
446
+ function finish(session, view, call) {
447
+ if (session.schema.model[call.table]?.conceal !== true)
448
+ return view;
449
+ return import("./tpssp7ac.js").then((mod) => mod.redactView(session.schema, view, call));
450
+ }
451
+ function inspection(plan, params, call, source) {
452
+ return {
453
+ intent: logicalIntent(call),
454
+ rules: appliedRules(call, source),
455
+ plan: { strategy: "postgres:single-statement", statements: 1, fingerprint: plan.fingerprint },
456
+ sql: { text: plan.text, params },
457
+ routing: SINGLE_ENDPOINT
458
+ };
459
+ }
460
+ function withHttp(http, options) {
461
+ return http === undefined ? options : { ...options, http };
462
+ }
463
+ function attachHttp(http, error) {
464
+ if (http === undefined)
465
+ return error;
466
+ return new OkmError(error.code, error.message, { ...errorFields(error), http });
467
+ }
468
+ function callOptions(input, statement) {
469
+ const timeout = input.timeout ?? statement;
470
+ const route = input.route;
471
+ const pin = input.pin;
472
+ if (input.signal === undefined && timeout === undefined && route === undefined && pin === undefined) {
473
+ return;
474
+ }
475
+ return {
476
+ ...input.signal !== undefined ? { signal: input.signal } : {},
477
+ ...timeout !== undefined ? { timeout } : {},
478
+ ...route !== undefined ? { route } : {},
479
+ ...pin !== undefined ? { pin } : {}
480
+ };
481
+ }
482
+ function mapOptions(http, includeValues, schema) {
483
+ return {
484
+ ...http !== undefined ? { http } : {},
485
+ ...includeValues ? { includeValues: true } : {},
486
+ schema
487
+ };
488
+ }
489
+ async function settleCall(session, error) {
490
+ throw logged(session, await mapError(session, error));
491
+ }
492
+ function logged(session, error) {
493
+ session.logger?.error?.({ code: error.code, summary: error.summary });
494
+ return error;
495
+ }
496
+
497
+ export { createClient, openClient, queryHandle, decodeRows, readHandle, readCall, withHttp, attachHttp, mapOptions, settleCall };