okmodel 0.6.0 → 0.8.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 (267) hide show
  1. package/README.md +100 -1131
  2. package/dist/adapters/capabilities.d.ts +35 -0
  3. package/dist/adapters/failure.d.ts +13 -2
  4. package/dist/adapters/lazy-errors.d.ts +22 -0
  5. package/dist/adapters/pg/bun-sql.d.ts +53 -0
  6. package/dist/adapters/pg/bunsql-extra.d.ts +54 -3
  7. package/dist/adapters/pg/bunsql.d.ts +15 -2
  8. package/dist/adapters/pg/cursor.d.ts +17 -0
  9. package/dist/adapters/pg/neon-extra.d.ts +18 -0
  10. package/dist/adapters/pg/neon.d.ts +104 -0
  11. package/dist/adapters/pg/no-inflight-timeout.d.ts +11 -8
  12. package/dist/adapters/pg/nodepostgres-extra.d.ts +22 -1
  13. package/dist/adapters/pg/nodepostgres.d.ts +12 -0
  14. package/dist/adapters/pg/pglite-wire.d.ts +24 -0
  15. package/dist/adapters/pg/pglite.d.ts +10 -0
  16. package/dist/adapters/pg/postgres-extra.d.ts +27 -11
  17. package/dist/adapters/pg/reserved.d.ts +42 -0
  18. package/dist/adapters/pg/session.d.ts +0 -18
  19. package/dist/contracts/connection.d.ts +7 -6
  20. package/dist/contracts/error.d.ts +1 -1
  21. package/dist/dialects/pg/floor.d.ts +68 -0
  22. package/dist/dialects/pg/fn/index.d.ts +39 -1
  23. package/dist/dialects/pg/fn/index.js +118 -26
  24. package/dist/dialects/pg/index.d.ts +8 -1
  25. package/dist/dialects/pg/index.js +435 -170
  26. package/dist/dialects/pg/introspect.d.ts +11 -0
  27. package/dist/dialects/pg/keys.d.ts +11 -8
  28. package/dist/dialects/pg/model.d.ts +40 -0
  29. package/dist/dialects/pg/morph.d.ts +43 -0
  30. package/dist/dialects/pg/operator-fit.d.ts +2 -2
  31. package/dist/dialects/pg/operators.d.ts +14 -7
  32. package/dist/dialects/pg/ops/bucket.d.ts +65 -0
  33. package/dist/dialects/pg/ops/fragment.d.ts +119 -0
  34. package/dist/dialects/pg/ops/inc.d.ts +2 -2
  35. package/dist/dialects/pg/ops/lane.d.ts +49 -0
  36. package/dist/dialects/pg/ops/matches.d.ts +9 -12
  37. package/dist/dialects/pg/ops/relevance.d.ts +13 -0
  38. package/dist/dialects/pg/ops/search-query.d.ts +22 -0
  39. package/dist/dialects/pg/ops/window.d.ts +103 -0
  40. package/dist/dialects/pg/preset.d.ts +1 -1
  41. package/dist/dialects/pg/relations.d.ts +100 -3
  42. package/dist/dialects/pg/schema.d.ts +31 -3
  43. package/dist/dialects/pg/view/client.d.ts +14 -6
  44. package/dist/dialects/pg/view/index.d.ts +73 -1
  45. package/dist/dialects/pg/view/index.js +156 -5
  46. package/dist/dialects/pg/view/scratch.d.ts +8 -5
  47. package/dist/runtime/aggregate.d.ts +8 -2
  48. package/dist/runtime/bucket-group.d.ts +70 -0
  49. package/dist/runtime/conflict.d.ts +22 -4
  50. package/dist/runtime/include.d.ts +0 -8
  51. package/dist/runtime/inflight.d.ts +1 -1
  52. package/dist/runtime/otel.d.ts +105 -0
  53. package/dist/runtime/otel.js +97 -0
  54. package/dist/runtime/page.d.ts +4 -1
  55. package/dist/runtime/pg/bun.js +12 -6
  56. package/dist/runtime/pg/neon.d.ts +38 -0
  57. package/dist/runtime/pg/neon.js +64 -0
  58. package/dist/runtime/pg/pg.js +5 -5
  59. package/dist/runtime/pg/pglite.d.ts +1 -0
  60. package/dist/runtime/pg/pglite.js +9 -9
  61. package/dist/runtime/pg/postgresjs.js +5 -5
  62. package/dist/runtime/plan.d.ts +95 -5
  63. package/dist/runtime/read-stream.d.ts +13 -16
  64. package/dist/runtime/result-name.d.ts +17 -0
  65. package/dist/runtime/server-check.d.ts +3 -3
  66. package/dist/runtime/sort-bind.d.ts +21 -0
  67. package/dist/runtime/sort.d.ts +56 -0
  68. package/dist/runtime/sql-fragment.d.ts +43 -0
  69. package/dist/runtime/sync.d.ts +70 -0
  70. package/dist/runtime/tenancy/index.js +15 -13
  71. package/dist/runtime/text-search.d.ts +20 -0
  72. package/dist/runtime/topology.d.ts +1 -1
  73. package/dist/runtime/traits/index.d.ts +80 -0
  74. package/dist/runtime/traits/index.js +184 -21
  75. package/dist/runtime/tree.d.ts +38 -0
  76. package/dist/runtime/types.d.ts +232 -22
  77. package/dist/runtime/validate/index.js +4 -4
  78. package/dist/runtime/version.d.ts +39 -0
  79. package/dist/runtime/window.d.ts +21 -0
  80. package/dist/runtime/write.d.ts +54 -2
  81. package/dist/shared/{929h19xp.js → 0wrea3jq.js} +141 -13
  82. package/dist/shared/{qq3899ht.js → 12z7aznj.js} +243 -14
  83. package/dist/shared/{1z04fa85.js → 14x60fbf.js} +12 -57
  84. package/dist/shared/{ek2wt2dz.js → 16kvjebh.js} +4 -4
  85. package/dist/shared/{wynzeggs.js → 195pzzzz.js} +8 -8
  86. package/dist/shared/1d9q9cv2.js +312 -0
  87. package/dist/shared/2c13q96h.js +18 -0
  88. package/dist/shared/{b4v57v3x.js → 2d3xq4ps.js} +2 -2
  89. package/dist/shared/{1f0xa3cb.js → 3449rcae.js} +5 -5
  90. package/dist/shared/{wkme6tep.js → 35q0n9tz.js} +8 -16
  91. package/dist/shared/3bb7wsqt.js +99 -0
  92. package/dist/shared/3t6kw83m.js +249 -0
  93. package/dist/shared/{5j36z8dn.js → 4m6hskg8.js} +39 -70
  94. package/dist/shared/{n076ydsc.js → 4vbf83xx.js} +69 -28
  95. package/dist/shared/{vmfkhszw.js → 63f4ksbz.js} +33 -26
  96. package/dist/shared/{c58secgj.js → 63qtkrca.js} +38 -4
  97. package/dist/shared/6m78c8ss.js +6 -0
  98. package/dist/shared/{x3jc53kg.js → 760ppr76.js} +15 -7
  99. package/dist/shared/7ghwamrf.js +40 -0
  100. package/dist/shared/7kms6fvj.js +20 -0
  101. package/dist/shared/7n7ddwt4.js +6 -0
  102. package/dist/shared/9a4ae4p8.js +31 -0
  103. package/dist/shared/{hazqqecq.js → 9dr2qnx7.js} +63 -34
  104. package/dist/shared/{h15jx9bc.js → 9dvh1dr6.js} +1 -1
  105. package/dist/shared/a62rb2da.js +13 -0
  106. package/dist/shared/{cgbamx3e.js → ae4j5f42.js} +23 -4
  107. package/dist/shared/agsfb86s.js +233 -0
  108. package/dist/shared/{8x72s86k.js → b6d6p0zc.js} +13 -7
  109. package/dist/shared/{ag4c7516.js → b76b8d95.js} +2 -2
  110. package/dist/shared/{j60w0dvr.js → bah9nbg3.js} +58 -38
  111. package/dist/shared/bnby0esn.js +10 -0
  112. package/dist/shared/bsek72fg.js +225 -0
  113. package/dist/shared/{2cg3wnxc.js → bxas03mq.js} +608 -141
  114. package/dist/shared/{31vm914h.js → cm3w0ywp.js} +15 -16
  115. package/dist/shared/{gwn6v667.js → cnkky55j.js} +25 -14
  116. package/dist/shared/{dk8v79p3.js → d5nmj7vv.js} +1 -1
  117. package/dist/shared/{ttmg09vd.js → dpc2r7rb.js} +8 -8
  118. package/dist/shared/dzj09cye.js +10 -0
  119. package/dist/shared/{sbdme3vy.js → e8zyaqvj.js} +60 -10
  120. package/dist/shared/{2jd1sn0e.js → ecn8mfdq.js} +6 -6
  121. package/dist/shared/exn3sd90.js +359 -0
  122. package/dist/shared/{t3g0k88x.js → f4jy1j7x.js} +39 -22
  123. package/dist/shared/{y2t3jktd.js → fjpnhqan.js} +20 -13
  124. package/dist/shared/g5yc06ka.js +272 -0
  125. package/dist/shared/ga9j92qj.js +25 -0
  126. package/dist/shared/{3fqwmr09.js → hfqmpasr.js} +6 -6
  127. package/dist/shared/{wt5rzv1h.js → hm5f8jby.js} +7 -7
  128. package/dist/shared/{ys0xzyaj.js → hqpxf4a4.js} +224 -19
  129. package/dist/shared/jebgrkeq.js +179 -0
  130. package/dist/shared/{09p049xa.js → jp36k7dp.js} +44 -16
  131. package/dist/shared/jx2m62cs.js +96 -0
  132. package/dist/shared/{r11n8grd.js → k86n8vq3.js} +7 -4
  133. package/dist/shared/{2z7wv5tj.js → kgwp4jdd.js} +56 -35
  134. package/dist/shared/{knmes2kh.js → kj9d8maq.js} +1 -1
  135. package/dist/shared/{g90m9b0g.js → m4xff2a6.js} +6 -5
  136. package/dist/shared/{r6nwkxnt.js → m7n9h2a5.js} +4 -4
  137. package/dist/shared/mfedzr74.js +145 -0
  138. package/dist/shared/{f0f1n281.js → mkrn359n.js} +60 -11
  139. package/dist/shared/{p248q79y.js → mnvjvqyp.js} +10 -10
  140. package/dist/shared/{672t5vym.js → mss6ctgv.js} +2 -2
  141. package/dist/shared/{4az561fn.js → mtw6jz76.js} +8 -23
  142. package/dist/shared/n05z9nwz.js +313 -0
  143. package/dist/shared/{pjr6xrm5.js → n0jmpgj1.js} +20 -17
  144. package/dist/shared/{p6bjzq9q.js → n41w4fbz.js} +14 -6
  145. package/dist/shared/{b3awxn7h.js → n454kcnw.js} +1 -1
  146. package/dist/shared/n7kg8mmx.js +61 -0
  147. package/dist/shared/nbw1z4yd.js +18 -0
  148. package/dist/shared/needfe47.js +66 -0
  149. package/dist/shared/{1nw1f3vs.js → njga14v4.js} +9 -9
  150. package/dist/shared/{5yv4k6a6.js → nmnsgj7x.js} +145 -3
  151. package/dist/shared/{vjpnzwjz.js → p3xbdcvt.js} +14 -18
  152. package/dist/shared/p5nh3r8q.js +622 -0
  153. package/dist/shared/{nx9hh281.js → pan0370z.js} +13 -12
  154. package/dist/shared/q0cqt2sp.js +12 -0
  155. package/dist/shared/qd9hhd8f.js +37 -0
  156. package/dist/shared/{mb2ycs1p.js → qfw14tna.js} +15 -15
  157. package/dist/shared/{9s0ypp87.js → sjk86yrx.js} +1 -1
  158. package/dist/shared/th3be82n.js +208 -0
  159. package/dist/shared/{ed0b8k4s.js → typ3vy7t.js} +1 -1
  160. package/dist/shared/{5d8t84hg.js → v1wad97q.js} +14 -14
  161. package/dist/shared/{x72jb7ad.js → v8g1vgv8.js} +1 -1
  162. package/dist/shared/{9628a9xt.js → vk7rag46.js} +1 -1
  163. package/dist/shared/{ze4hxsmy.js → w9ejzaqf.js} +1 -1
  164. package/dist/shared/{q3j1tq7a.js → wcrheftc.js} +148 -23
  165. package/dist/shared/wvdjznah.js +213 -0
  166. package/dist/shared/wz2a78mp.js +8 -0
  167. package/dist/shared/xfhe73sb.js +436 -0
  168. package/dist/shared/{r1fdd0f6.js → z581d7ny.js} +5 -2
  169. package/dist/shared/{8etn7d8w.js → z7s6fpxv.js} +1 -1
  170. package/dist/shared/{32cnm7kn.js → zbqbmmrp.js} +76 -40
  171. package/dist/shared/{n7mcc1gk.js → zchaghkf.js} +4 -3
  172. package/dist/shared/{xq0mvhts.js → zgex00fd.js} +6 -6
  173. package/dist/shared/{nsva2a6b.js → znm0bpp5.js} +3 -0
  174. package/dist/shared/zq1he88x.js +155 -0
  175. package/dist/tooling/cli.js +26 -13
  176. package/dist/tooling/driver-run.js +4971 -0
  177. package/dist/tooling/driver-test.d.ts +36 -0
  178. package/dist/tooling/migrate/commands.d.ts +2 -2
  179. package/dist/tooling/migrate/engine.d.ts +3 -2
  180. package/dist/tooling/migrate/index.js +9 -9
  181. package/dist/tooling/migrate/project.d.ts +3 -0
  182. package/dist/tooling/shared/03k6res6.js +210 -0
  183. package/dist/tooling/shared/08kemxjd.js +10 -0
  184. package/dist/tooling/shared/2revefqt.js +209 -0
  185. package/dist/tooling/shared/2wtvc7rw.js +1425 -0
  186. package/dist/tooling/shared/345tbc2k.js +16 -0
  187. package/dist/tooling/shared/3m1hkg7z.js +344 -0
  188. package/dist/tooling/shared/3rrw5w8f.js +81 -0
  189. package/dist/tooling/shared/3tq8pnb8.js +131 -0
  190. package/dist/tooling/shared/417zdsk7.js +194 -0
  191. package/dist/tooling/shared/4d2w25vw.js +40 -0
  192. package/dist/tooling/shared/54tqkxja.js +51 -0
  193. package/dist/tooling/shared/5bvw2nev.js +12 -0
  194. package/dist/tooling/shared/6nyfyw70.js +231 -0
  195. package/dist/tooling/shared/72fyg4zv.js +160 -0
  196. package/dist/tooling/shared/733a1tq3.js +38 -0
  197. package/dist/tooling/shared/7ae7rjmn.js +20 -0
  198. package/dist/tooling/shared/7kms6fvj.js +20 -0
  199. package/dist/tooling/shared/8sqrsw3s.js +49 -0
  200. package/dist/tooling/shared/96ps4g1z.js +32 -0
  201. package/dist/tooling/shared/9kazs5p6.js +195 -0
  202. package/dist/tooling/shared/9xzm7hjg.js +38 -0
  203. package/dist/tooling/shared/a6pp9d12.js +49 -0
  204. package/dist/tooling/shared/a7q7t4n3.js +22 -0
  205. package/dist/tooling/shared/aftb2mwr.js +18 -0
  206. package/dist/tooling/shared/bz2dqhab.js +311 -0
  207. package/dist/tooling/shared/cmptmb4f.js +12 -0
  208. package/dist/tooling/shared/ddjtfgnx.js +177 -0
  209. package/dist/tooling/shared/djydjpqt.js +261 -0
  210. package/dist/tooling/shared/dw08f3m8.js +9 -0
  211. package/dist/tooling/shared/dy672fz6.js +16 -0
  212. package/dist/tooling/shared/dzx85q70.js +66 -0
  213. package/dist/tooling/shared/ec71zqgt.js +270 -0
  214. package/dist/{shared/d5azex6s.js → tooling/shared/ep8zh3bk.js} +1 -1
  215. package/dist/tooling/shared/f9mphxej.js +3789 -0
  216. package/dist/tooling/shared/fb9k2qwd.js +89 -0
  217. package/dist/tooling/shared/g9ad4n80.js +211 -0
  218. package/dist/tooling/shared/gfvtj6d0.js +94 -0
  219. package/dist/tooling/shared/gmcf9cxf.js +294 -0
  220. package/dist/tooling/shared/htnaj77v.js +17 -0
  221. package/dist/tooling/shared/j9t5s12a.js +7 -0
  222. package/dist/tooling/shared/jne7b93p.js +42 -0
  223. package/dist/tooling/shared/ma2340rx.js +118 -0
  224. package/dist/tooling/shared/mtrqn2hn.js +505 -0
  225. package/dist/tooling/shared/n4jqhpqz.js +27 -0
  226. package/dist/tooling/shared/nf7jmhfa.js +20 -0
  227. package/dist/tooling/shared/p7f08ktn.js +332 -0
  228. package/dist/tooling/shared/ppnnnvj8.js +13 -0
  229. package/dist/tooling/shared/pqy21k24.js +312 -0
  230. package/dist/tooling/shared/pw8fgahx.js +69 -0
  231. package/dist/tooling/shared/pwsytxm5.js +17 -0
  232. package/dist/tooling/shared/rf83gr2d.js +94 -0
  233. package/dist/tooling/shared/rq5kk8j6.js +401 -0
  234. package/dist/tooling/shared/rs1jb051.js +12 -0
  235. package/dist/tooling/shared/smn6kz1p.js +157 -0
  236. package/dist/tooling/shared/spk1jg3r.js +1696 -0
  237. package/dist/tooling/shared/tn8gay14.js +143 -0
  238. package/dist/tooling/shared/vraj6d74.js +42 -0
  239. package/dist/tooling/shared/vs0t6qt8.js +64 -0
  240. package/dist/tooling/shared/vx86qjv8.js +150 -0
  241. package/dist/tooling/shared/w7qe8sj1.js +140 -0
  242. package/dist/tooling/shared/wt7j7nf6.js +15 -0
  243. package/dist/tooling/shared/wwnekq5n.js +6 -0
  244. package/dist/tooling/shared/xhrnjch9.js +185 -0
  245. package/dist/tooling/shared/xjs6zqtc.js +190 -0
  246. package/dist/tooling/shared/y031jct9.js +16 -0
  247. package/dist/tooling/shared/y5656f0e.js +233 -0
  248. package/dist/tooling/shared/yfr8hzhm.js +604 -0
  249. package/dist/tooling/shared/yk9mp7vb.js +250 -0
  250. package/dist/tooling/shared/yw30vnq2.js +139 -0
  251. package/dist/tooling/shared/z8d7ptht.js +618 -0
  252. package/dist/tooling/shared/zhwnfyb2.js +9 -0
  253. package/dist/tooling/shared/zvnhh1n0.js +198 -0
  254. package/dist/tooling/testing/index.js +3 -3
  255. package/package.json +17 -3
  256. package/dist/shared/3qnhc7w5.js +0 -38
  257. package/dist/shared/7m88x3e9.js +0 -45
  258. package/dist/shared/8dqhth8c.js +0 -12
  259. package/dist/shared/j3rkk835.js +0 -99
  260. package/dist/shared/k68eedmy.js +0 -85
  261. package/dist/shared/venqrjna.js +0 -216
  262. package/dist/shared/wxr8cwns.js +0 -6
  263. package/dist/shared/y34a8714.js +0 -150
  264. package/dist/shared/zm0mafk4.js +0 -92
  265. package/dist/shared/zy8r7qvz.js +0 -116
  266. package/dist/shared/{ywr8hmey.js → 606g0nbf.js} +3 -3
  267. package/dist/shared/{rpycarcr.js → gcefca6p.js} +4 -4
@@ -1,26 +1,27 @@
1
+ import {
2
+ normaliseViewQuery,
3
+ viewObject,
4
+ materializedViewObject
5
+ } from "./12z7aznj.js";
1
6
  import {
2
7
  cellText,
3
8
  VIEW_DEPENDENCIES,
4
9
  viewDependencyEdges,
5
10
  PUBLIC,
6
- introspectSchema
7
- } from "./vjpnzwjz.js";
11
+ introspectSchema,
12
+ atomicBody
13
+ } from "./p3xbdcvt.js";
8
14
  import {
9
15
  planMigration,
10
16
  sessionFailure,
11
17
  finishQuiet,
12
18
  assertDirectConnection,
13
19
  assertStableBackend
14
- } from "./ttmg09vd.js";
20
+ } from "./dpc2r7rb.js";
15
21
  import {
16
22
  quoteIdent,
17
23
  createObjectSql
18
- } from "./9s0ypp87.js";
19
- import {
20
- normaliseViewQuery,
21
- viewObject,
22
- materializedViewObject
23
- } from "./zy8r7qvz.js";
24
+ } from "./sjk86yrx.js";
24
25
  import {
25
26
  catalog
26
27
  } from "./ywspezzx.js";
@@ -49,7 +50,8 @@ var VIEW_SHAPE = `
49
50
  `;
50
51
  async function sealViews(runner, source) {
51
52
  const views = source.objects.filter((object) => object.kind === "view" || object.kind === "materializedView");
52
- if (views.length === 0)
53
+ const hasAtomic = source.objects.some((object) => object.kind === "function" && object.definition.atomic === true);
54
+ if (views.length === 0 && !hasAtomic)
53
55
  return source;
54
56
  await dropScratch(runner);
55
57
  await runner.query(`create schema ${quoteIdent(SCRATCH)}`);
@@ -65,12 +67,15 @@ async function sealViews(runner, source) {
65
67
  await runner.query(stubTable(object, source));
66
68
  }
67
69
  await createObjects(runner, source, "function");
70
+ const reprinted = await reprintAtomicBodies(runner, source);
71
+ if (views.length === 0)
72
+ return reprinted;
68
73
  await createViews(runner, views);
69
74
  const shapes = await runner.query(VIEW_SHAPE, [SCRATCH]);
70
75
  const deps = await runner.query(VIEW_DEPENDENCIES, [SCRATCH]);
71
76
  const edges = viewDependencyEdges(deps, PUBLIC, source.objects);
72
77
  const byName = new Map(shapes.map((row) => [cellText(row.name), row]));
73
- const next = source.objects.map((object) => {
78
+ const next = reprinted.objects.map((object) => {
74
79
  if (object.kind !== "view" && object.kind !== "materializedView")
75
80
  return object;
76
81
  const row = byName.get(object.identity.name);
@@ -147,6 +152,40 @@ async function createObjects(runner, source, kind) {
147
152
  await runner.query(statement);
148
153
  }
149
154
  }
155
+ var FUNCTION_DEFS = `
156
+ select pg_get_functiondef(p.oid) as definition
157
+ from pg_proc p
158
+ join pg_namespace n on n.oid = p.pronamespace
159
+ where n.nspname = $1 and p.prokind = 'f'
160
+ order by p.oid
161
+ `;
162
+ async function reprintAtomicBodies(runner, source) {
163
+ const created = source.objects.filter((object) => object.kind === "function");
164
+ if (!created.some((object) => object.definition.atomic === true))
165
+ return source;
166
+ const rows = await runner.query(FUNCTION_DEFS, [SCRATCH]);
167
+ if (rows.length !== created.length) {
168
+ throw new OkmError2("OKM1020", `Expected ${String(created.length)} functions in the scratch schema and found ${String(rows.length)}.`, { fix: { summary: "The function body has to run against the tables in this schema." } });
169
+ }
170
+ const bodies = new Map;
171
+ for (let index = 0;index < created.length; index += 1) {
172
+ const object = created[index];
173
+ if (object === undefined || object.definition.atomic !== true)
174
+ continue;
175
+ bodies.set(object, atomicBody(cellText(rows[index]?.definition)));
176
+ }
177
+ let changed = false;
178
+ const objects = source.objects.map((object) => {
179
+ if (object.kind !== "function")
180
+ return object;
181
+ const body = bodies.get(object);
182
+ if (body === undefined || body === object.definition.body)
183
+ return object;
184
+ changed = true;
185
+ return { ...object, definition: { ...object.definition, body } };
186
+ });
187
+ return changed ? catalog(objects) : source;
188
+ }
150
189
  async function createViews(runner, views) {
151
190
  const pending = [...views];
152
191
  let last = "";
@@ -210,6 +249,12 @@ async function dropScratch(runner) {
210
249
  join pg_namespace n on n.oid = c.relnamespace
211
250
  where n.nspname = $1 and c.relkind in ('v', 'm', 'r')
212
251
  `, [SCRATCH]);
252
+ const routines = await runner.query(`
253
+ select p.proname as name, pg_get_function_identity_arguments(p.oid) as args
254
+ from pg_proc p
255
+ join pg_namespace n on n.oid = p.pronamespace
256
+ where n.nspname = $1
257
+ `, [SCRATCH]);
213
258
  const views = rows.filter((row) => cellText(row.kind) === "v" || cellText(row.kind) === "m");
214
259
  for (let pass = 0;pass < Math.max(views.length, 1); pass += 1) {
215
260
  for (const row of views) {
@@ -219,6 +264,10 @@ async function dropScratch(runner) {
219
264
  } catch {}
220
265
  }
221
266
  }
267
+ for (const row of routines) {
268
+ const args = cellText(row.args);
269
+ await runner.query(`drop function if exists ${quoteIdent(SCRATCH)}.${quoteIdent(cellText(row.name))}(${args})`);
270
+ }
222
271
  for (const row of rows) {
223
272
  if (cellText(row.kind) !== "r")
224
273
  continue;
@@ -1,9 +1,9 @@
1
1
  import {
2
2
  serializeCatalog2
3
- } from "./qq3899ht.js";
3
+ } from "./12z7aznj.js";
4
4
  import {
5
5
  roleExists
6
- } from "./knmes2kh.js";
6
+ } from "./kj9d8maq.js";
7
7
  import {
8
8
  canonicalTypeName,
9
9
  createdRoleName,
@@ -16,24 +16,24 @@ import {
16
16
  planMigration,
17
17
  sessionFailure,
18
18
  assertTargetPolicy
19
- } from "./ttmg09vd.js";
19
+ } from "./dpc2r7rb.js";
20
+ import {
21
+ snakeCase
22
+ } from "./gcefca6p.js";
20
23
  import {
21
24
  quoteIdent,
22
25
  qualify
23
- } from "./9s0ypp87.js";
24
- import {
25
- alignExtensionVersions
26
- } from "./r3j8s5y0.js";
26
+ } from "./sjk86yrx.js";
27
27
  import {
28
28
  catalog,
29
29
  isDomain
30
30
  } from "./ywspezzx.js";
31
- import {
32
- snakeCase
33
- } from "./rpycarcr.js";
34
31
  import {
35
32
  quoteLiteral
36
33
  } from "./94ez8kzh.js";
34
+ import {
35
+ alignExtensionVersions
36
+ } from "./r3j8s5y0.js";
37
37
  import {
38
38
  identityKey2,
39
39
  identityLabel2
@@ -3,10 +3,10 @@ import {
3
3
  } from "./cn0n68ft.js";
4
4
 
5
5
  // src/runtime/preset-stack.ts
6
- function stack(where, presets) {
6
+ function stack2(where, presets) {
7
7
  if (presets === undefined || presets.length === 0)
8
8
  return where;
9
9
  return tag("and", where === undefined ? presets : [where, ...presets]);
10
10
  }
11
11
 
12
- export { stack };
12
+ export { stack2 };
@@ -4,12 +4,9 @@ import {
4
4
  nextTransactionDepth,
5
5
  EMPTY_NOTICES,
6
6
  resultFrom
7
- } from "./cgbamx3e.js";
7
+ } from "./ae4j5f42.js";
8
8
 
9
9
  // src/adapters/pg/session.ts
10
- function quoteIdent2(name) {
11
- return `"${name.replaceAll('"', '""')}"`;
12
- }
13
10
  function noticeBuffer() {
14
11
  let notices;
15
12
  return {
@@ -63,7 +60,7 @@ class WireSession {
63
60
  return resultFrom(result, this.notices.since(start));
64
61
  }, (error) => {
65
62
  this.onLeave();
66
- return driverErrors().then((errors) => Promise.reject(errors.mapDriverError(error)));
63
+ return driverErrors().then((errors) => Promise.reject(errors.mapDriverError(stampQuery(error, text))));
67
64
  });
68
65
  }
69
66
  return this.watched(pending, watch, start, text);
@@ -110,7 +107,7 @@ class WireSession {
110
107
  return resultFrom(result, this.notices.since(start));
111
108
  }, (error) => {
112
109
  this.finishWatched(pending, watch, onAbort);
113
- return classify(error, watch);
110
+ return classify(stampQuery(error, text), watch);
114
111
  });
115
112
  }
116
113
  finishWatched(pending, watch, onAbort) {
@@ -119,22 +116,10 @@ class WireSession {
119
116
  watch.signal.removeEventListener("abort", onAbort);
120
117
  }
121
118
  }
122
- async function* cursorStream(run, text, params) {
123
- const name = "okm_stream";
124
- await run("BEGIN");
125
- try {
126
- await run(`DECLARE ${name} NO SCROLL CURSOR FOR ${text}`, params);
127
- for (;; ) {
128
- const chunk = await run(`FETCH 64 FROM ${name}`);
129
- if (chunk.rows.length === 0)
130
- break;
131
- yield chunk.rows;
132
- }
133
- } finally {
134
- await run("ROLLBACK").catch(() => {
135
- return;
136
- });
137
- }
119
+ function stampQuery(error, text) {
120
+ if (typeof error === "object" && error !== null && !("query" in error))
121
+ Reflect.set(error, "query", text);
122
+ return error;
138
123
  }
139
124
  async function classify(error, watch) {
140
125
  const errors = await driverErrors();
@@ -151,4 +136,4 @@ async function classify(error, watch) {
151
136
  return Promise.reject(errors.mapDriverError(error));
152
137
  }
153
138
 
154
- export { quoteIdent2, noticeBuffer, WireSession, cursorStream };
139
+ export { noticeBuffer, WireSession };
@@ -0,0 +1,313 @@
1
+ import {
2
+ queryHandle,
3
+ readHandle,
4
+ readCall,
5
+ attachHttp
6
+ } from "./bah9nbg3.js";
7
+ import {
8
+ emitWhere,
9
+ emitOrder,
10
+ refuseHidden,
11
+ rejectKeys,
12
+ readLimit,
13
+ projectExpr,
14
+ cell,
15
+ indexes,
16
+ quote,
17
+ list,
18
+ isRecord,
19
+ fail2
20
+ } from "./kgwp4jdd.js";
21
+ import {
22
+ OkmError2,
23
+ throwNamed2
24
+ } from "./52szzakm.js";
25
+
26
+ // src/runtime/aggregate.ts
27
+ var BUCKET = Symbol.for("okmodel.bucket");
28
+ var bucketChunk;
29
+ function loadBucketGroup() {
30
+ return bucketChunk ??= import("./th3be82n.js");
31
+ }
32
+ var OPTIONS = [
33
+ "where",
34
+ "groupBy",
35
+ "count",
36
+ "sum",
37
+ "avg",
38
+ "min",
39
+ "max",
40
+ "orderBy",
41
+ "limit",
42
+ "route",
43
+ "signal",
44
+ "timeout"
45
+ ];
46
+ var NUMERIC = /^(smallint|integer|bigint|real|double precision|numeric|decimal)\b/;
47
+ var AS_NUMBER = (wire) => Number(wire);
48
+ var AS_TEXT = (wire) => wire;
49
+ var UNORDERED = /^(json|jsonb|bytea|point|line|tsvector)\b|\[\]$/;
50
+ var FUNCTIONS = [
51
+ { option: "sum", tag: "s", numeric: true },
52
+ { option: "avg", tag: "a", numeric: true },
53
+ { option: "min", tag: "n", numeric: false },
54
+ { option: "max", tag: "x", numeric: false }
55
+ ];
56
+ function build(session, table, options, mods, view) {
57
+ try {
58
+ if (seesBucket(options))
59
+ return bucketHandle(session, table, options, mods, view);
60
+ return plan(session, table, options, mods, view);
61
+ } catch (error) {
62
+ throw error instanceof OkmError2 ? attachHttp(session.http, error) : error;
63
+ }
64
+ }
65
+ function bucketHandle(session, table, options, mods, view) {
66
+ const open = () => loadBucketGroup().then((mod) => {
67
+ try {
68
+ return { it: plan(session, table, options, mods, view, mod) };
69
+ } catch (error) {
70
+ throw error instanceof OkmError2 ? attachHttp(session.http, error) : error;
71
+ }
72
+ });
73
+ return queryHandle(() => open().then((box) => box.it), {
74
+ inspect: () => open().then((box) => box.it.inspect()),
75
+ sql: () => open().then((box) => box.it.sql()),
76
+ all: (reason) => build(session, table, options, { ...mods, all: reason }, view)
77
+ });
78
+ }
79
+ function seesBucket(options) {
80
+ if (!isRecord(options))
81
+ return false;
82
+ const groupBy = options.groupBy;
83
+ if (!Array.isArray(groupBy))
84
+ return false;
85
+ for (const item of groupBy) {
86
+ if (typeof item === "object" && item !== null && BUCKET in item)
87
+ return true;
88
+ }
89
+ return false;
90
+ }
91
+ function plan(session, table, options, mods, view, buckets) {
92
+ const record = isRecord(options) ? options : {};
93
+ rejectKeys(record, OPTIONS, "aggregate");
94
+ const indexed = indexes(session.schema).get(table);
95
+ if (indexed === undefined)
96
+ fail2("OKM1120", `Table ${table} is not in the schema.`);
97
+ const spec = parse(indexed, record, buckets);
98
+ if (spec.groups.length > 0 && record.limit === undefined && mods.all === undefined) {
99
+ fail2("OKM1101", "aggregate() with groupBy needs a limit, or .all(reason) for every group.");
100
+ }
101
+ const names = groupNames(spec.groups);
102
+ const orderBy = record.orderBy;
103
+ if (orderBy !== undefined) {
104
+ if (!isRecord(orderBy))
105
+ fail2("OKM1120", "orderBy must be an object of fields.");
106
+ for (const field of Object.keys(orderBy)) {
107
+ if (!names.includes(field)) {
108
+ const column = indexed.columns.get(field);
109
+ if (column !== undefined)
110
+ refuseHidden(column);
111
+ throwNamed2("OKM1120", field, names, `aggregate() orders by a groupBy field. Accepted names: ${list(names)}.`);
112
+ }
113
+ }
114
+ }
115
+ if (record.limit !== undefined)
116
+ readLimit(record.limit);
117
+ const base = readCall("find", table, {
118
+ where: record.where,
119
+ orderBy,
120
+ limit: record.limit,
121
+ signal: record.signal,
122
+ timeout: record.timeout,
123
+ route: record.route
124
+ }, mods.all, session.scope, session.schema.model[table], session.schema, view);
125
+ const call = { ...base, op: "aggregate", build: statement(spec, buckets) };
126
+ return readHandle(session, table, call, mods, (compiled, rows) => decode(compiled, rows));
127
+ }
128
+ function parse(table, record, buckets) {
129
+ const grouped = readGroups(table, record.groupBy, buckets);
130
+ const count = record.count === true;
131
+ if (record.count !== undefined && typeof record.count !== "boolean") {
132
+ fail2("OKM1120", "count is true when you want a row count.");
133
+ }
134
+ const calls = [];
135
+ for (const { option, tag, numeric } of FUNCTIONS) {
136
+ for (const column of columns(table, record[option], option)) {
137
+ const fits = numeric ? NUMERIC.test(column.dataType) : !UNORDERED.test(column.dataType);
138
+ if (!fits) {
139
+ fail2("OKM1124", `${option} does not apply to ${column.dataType} column ${column.field}. ${numeric ? "sum and avg take integer, real, and numeric columns." : "min and max need an ordered type."}`);
140
+ }
141
+ calls.push({ option, tag, column, decode: numeric ? sumDecode(column) : column.decode });
142
+ }
143
+ }
144
+ if (!count && calls.length === 0 && grouped.groups.length === 0) {
145
+ fail2("OKM1120", "aggregate() needs count, sum, avg, min, max, or groupBy.");
146
+ }
147
+ return { groups: grouped.groups, bucketed: grouped.bucketed, count, calls };
148
+ }
149
+ function sumDecode(column) {
150
+ const kind = typeof (column.decode?.("0") ?? "");
151
+ if (kind === "string")
152
+ return AS_TEXT;
153
+ if (kind === "number")
154
+ return AS_NUMBER;
155
+ return fail2("OKM1124", `sum and avg need a column whose value is a number or a string. ${column.field} reads as ${kind}.`);
156
+ }
157
+ function columns(table, value, option) {
158
+ if (value === undefined)
159
+ return [];
160
+ if (!Array.isArray(value))
161
+ fail2("OKM1120", `${option} must be a list of field names.`);
162
+ const out = [];
163
+ for (const name of value) {
164
+ const column = typeof name === "string" ? table.columns.get(name) : undefined;
165
+ if (typeof name !== "string" || column === undefined) {
166
+ throwNamed2("OKM1120", String(name), table.names, `Field ${String(name)} is not on ${table.model.name}. Accepted names: ${list(table.names)}.`);
167
+ }
168
+ if (column.hidden) {
169
+ fail2("OKM1120", `Field ${column.field} is hidden, so ${option} cannot read it.`);
170
+ }
171
+ out.push(column);
172
+ }
173
+ return out;
174
+ }
175
+ function groupNames(groups) {
176
+ const names = [];
177
+ for (const group of groups)
178
+ names.push(group.kind === "field" ? group.column.field : group.as);
179
+ return names;
180
+ }
181
+ function readGroups(table, value, buckets) {
182
+ if (value === undefined)
183
+ return { groups: [], bucketed: false };
184
+ if (!Array.isArray(value))
185
+ fail2("OKM1120", "groupBy must be a list of field names.");
186
+ const fields = new Set;
187
+ const aliases = new Set;
188
+ const groups = [];
189
+ let bucketed = false;
190
+ for (const item of value) {
191
+ if (typeof item === "object" && item !== null && BUCKET in item) {
192
+ if (buckets === undefined)
193
+ fail2("OKM1120", "bucket group");
194
+ groups.push(buckets.readBucket(table, item, fields, aliases));
195
+ bucketed = true;
196
+ continue;
197
+ }
198
+ const column = typeof item === "string" ? table.columns.get(item) : undefined;
199
+ if (typeof item !== "string" || column === undefined) {
200
+ throwNamed2("OKM1120", String(item), table.names, `Field ${String(item)} is not on ${table.model.name}. Accepted names: ${list(table.names)}.`);
201
+ }
202
+ if (column.hidden) {
203
+ fail2("OKM1120", `Field ${column.field} is hidden, so groupBy cannot read it.`);
204
+ }
205
+ if (aliases.has(column.field)) {
206
+ fail2("OKM1120", `group field ${column.field} clashes with a bucket as.`);
207
+ }
208
+ fields.add(column.field);
209
+ groups.push({ kind: "field", column });
210
+ }
211
+ return { groups, bucketed };
212
+ }
213
+ function statement(spec, buckets) {
214
+ return (schema, table, sink, outputs, call) => {
215
+ sink.mark(`aggregate|${table.model.name}|g:${groupMark(spec.groups)}|c:${String(spec.count)}|${spec.calls.map((item) => `${item.tag}:${item.column.field}`).join(",")}|`);
216
+ sink.text("select ");
217
+ const wrote = { at: 0 };
218
+ for (const group of spec.groups) {
219
+ if (group.kind === "field") {
220
+ put(sink, outputs, wrote, projectExpr(`t.${quote(group.column.sql)}`, group.column.dataType), {
221
+ key: `g:${group.column.field}`,
222
+ decode: group.column.decode
223
+ });
224
+ } else {
225
+ if (buckets === undefined)
226
+ fail2("OKM1120", "bucket group");
227
+ buckets.writeBucket(sink, outputs, wrote, group);
228
+ }
229
+ }
230
+ if (spec.count)
231
+ put(sink, outputs, wrote, "count(*)::text", { key: "c:", decode: undefined });
232
+ for (const item of spec.calls) {
233
+ const ref = `t.${quote(item.column.sql)}`;
234
+ const expr = item.option === "sum" || item.option === "avg" ? `${item.option}(${ref})::text` : projectExpr(`${item.option}(${ref})`, item.column.dataType);
235
+ put(sink, outputs, wrote, expr, {
236
+ key: `${item.tag}:${item.column.field}`,
237
+ decode: item.decode
238
+ });
239
+ }
240
+ sink.text(" from ");
241
+ sink.text(quote(table.model.sql));
242
+ sink.text(" t");
243
+ emitWhere(schema, table, call.where, sink, "t", 0);
244
+ if (spec.groups.length > 0) {
245
+ sink.text(" group by ");
246
+ if (spec.bucketed) {
247
+ if (buckets === undefined)
248
+ fail2("OKM1120", "bucket group");
249
+ buckets.writeGrouped(sink, groupNames(spec.groups), call.orderBy);
250
+ } else {
251
+ sink.text(spec.groups.map((group) => fieldRef(group)).join(", "));
252
+ if (isRecord(call.orderBy) && Object.keys(call.orderBy).length > 0) {
253
+ emitOrder(table, call.orderBy, sink, "t");
254
+ } else {
255
+ sink.text(" order by ");
256
+ sink.text(spec.groups.map((group) => `${fieldRef(group)} asc nulls last`).join(", "));
257
+ }
258
+ }
259
+ }
260
+ if (call.limit !== undefined) {
261
+ const limit = String(call.limit);
262
+ sink.text(" limit ");
263
+ sink.param(limit);
264
+ sink.mark(`l:${limit}`);
265
+ }
266
+ if (typeof call.all === "string")
267
+ sink.mark(`all:${call.all}`);
268
+ };
269
+ }
270
+ function groupMark(groups) {
271
+ const parts = [];
272
+ for (const group of groups) {
273
+ parts.push(group.kind === "field" ? group.column.field : `b:${group.column.field}:${group.as}:${group.unit}:${String(group.shift)}`);
274
+ }
275
+ return parts.join(",");
276
+ }
277
+ function fieldRef(group) {
278
+ if (group.kind !== "field")
279
+ return fail2("OKM1120", "bucket group");
280
+ return `t.${quote(group.column.sql)}`;
281
+ }
282
+ function put(sink, outputs, wrote, expr, out) {
283
+ if (wrote.at > 0)
284
+ sink.text(", ");
285
+ sink.text(expr);
286
+ outputs?.fields.push({ key: out.key, at: wrote.at, decode: out.decode });
287
+ wrote.at += 1;
288
+ }
289
+ function decode(compiled, rows) {
290
+ const items = [];
291
+ for (const row of rows) {
292
+ const item = {};
293
+ for (const field of compiled.outputs.fields) {
294
+ const value = row[field.at];
295
+ const tag = field.key.slice(0, 2);
296
+ const name = field.key.slice(2);
297
+ if (tag === "g:") {
298
+ item[name] = cell(value, field.decode);
299
+ } else if (tag === "c:") {
300
+ item.count = Number(value ?? 0);
301
+ } else {
302
+ const group = tag === "s:" ? "sum" : tag === "a:" ? "avg" : tag === "n:" ? "min" : "max";
303
+ const into = item[group] ??= {};
304
+ into[name] = cell(value, field.decode);
305
+ }
306
+ }
307
+ items.push(item);
308
+ }
309
+ return items;
310
+ }
311
+ export {
312
+ build
313
+ };
@@ -3,7 +3,7 @@ import {
3
3
  readHandle,
4
4
  readCall,
5
5
  attachHttp
6
- } from "./j60w0dvr.js";
6
+ } from "./bah9nbg3.js";
7
7
  import {
8
8
  selectedColumns,
9
9
  parseOrder,
@@ -14,8 +14,8 @@ import {
14
14
  list,
15
15
  isRecord,
16
16
  registerFailFix,
17
- fail
18
- } from "./2z7wv5tj.js";
17
+ fail2
18
+ } from "./kgwp4jdd.js";
19
19
  import {
20
20
  OkmError2,
21
21
  throwNamed2
@@ -34,21 +34,24 @@ var OPTIONS = [
34
34
  "signal",
35
35
  "timeout"
36
36
  ];
37
- function build(session, table, options, mods, view) {
37
+ function build3(session, table, options, mods, view, pin) {
38
38
  try {
39
- return plan(session, table, options, mods, view);
39
+ return plan(session, table, options, mods, view, pin);
40
40
  } catch (error) {
41
41
  throw error instanceof OkmError2 ? attachHttp(session.http, error) : error;
42
42
  }
43
43
  }
44
- function plan(session, table, options, mods, view) {
44
+ function plan(session, table, options, mods, view, pin) {
45
45
  const record = isRecord(options) ? options : {};
46
+ if (Object.hasOwn(record, "search")) {
47
+ fail2("OKM1120", "page() does not take search in this version. Use find({ search, limit }) for a ranked list.", "Ranked pages are later. find({ search, limit }) returns one ranked list.");
48
+ }
46
49
  rejectKeys(record, OPTIONS, "page");
47
50
  const indexed = indexes(session.schema).get(table);
48
51
  if (indexed === undefined)
49
- fail("OKM1120", `Table ${table} is not in the schema.`);
52
+ fail2("OKM1120", `Table ${table} is not in the schema.`);
50
53
  if (record.limit === undefined)
51
- fail("OKM1101", "page() needs a limit.");
54
+ fail2("OKM1101", "page() needs a limit.");
52
55
  const size = readLimit(record.limit);
53
56
  if (size < 1) {
54
57
  throw new OkmError2("invalid", "page() limit must be at least 1.", { kind: "invalid" });
@@ -73,7 +76,8 @@ function plan(session, table, options, mods, view) {
73
76
  timeout: record.timeout,
74
77
  route: record.route
75
78
  }, undefined, session.scope, session.schema.model[table], session.schema, view),
76
- ...after !== undefined ? { after } : {}
79
+ ...after !== undefined ? { after } : {},
80
+ ...pin !== undefined ? { pin } : {}
77
81
  };
78
82
  return readHandle(session, table, call, mods, async (compiled, rows) => {
79
83
  const items = await decodeRows(compiled, rows, table);
@@ -92,7 +96,7 @@ function plan(session, table, options, mods, view) {
92
96
  }
93
97
  function orderKeys(table, orderBy) {
94
98
  if (orderBy !== undefined && !isRecord(orderBy)) {
95
- fail("OKM1120", "orderBy must be an object of fields.");
99
+ fail2("OKM1120", "orderBy must be an object of fields.");
96
100
  }
97
101
  const keys = [];
98
102
  for (const field of Object.keys(orderBy ?? {})) {
@@ -103,7 +107,7 @@ function orderKeys(table, orderBy) {
103
107
  keys.push({ column, ...parseOrder(field, orderBy?.[field]) });
104
108
  }
105
109
  if (table.model.primary.length === 0) {
106
- fail("OKM1120", `page() needs a primary key on ${table.model.name} to break ties.`);
110
+ fail2("OKM1120", `page() needs a primary key on ${table.model.name} to break ties.`);
107
111
  }
108
112
  for (const field of table.model.primary) {
109
113
  const column = table.columns.get(field);
@@ -181,17 +185,16 @@ function readCursor(token, signature, count) {
181
185
  }
182
186
  }
183
187
  if (!Array.isArray(parsed) || typeof parsed[0] !== "string") {
184
- fail("OKM1130", "after is not a cursor that page() returned.");
188
+ fail2("OKM1130", "after is not a cursor that page() returned.");
185
189
  }
186
190
  if (parsed[0] !== signature) {
187
- fail("OKM1130", `The cursor was made for another order: ${parsed[0]}. This page orders by ${signature}.`);
191
+ fail2("OKM1130", `The cursor was made for another order: ${parsed[0]}. This page orders by ${signature}.`);
188
192
  }
189
193
  const values = parsed.slice(1);
190
194
  if (values.length !== count || !values.every((value) => value === null || typeof value === "string")) {
191
- fail("OKM1130", "after is not a cursor that page() returned.");
195
+ fail2("OKM1130", "after is not a cursor that page() returned.");
192
196
  }
193
197
  return values;
194
198
  }
195
- export {
196
- build
197
- };
199
+
200
+ export { build3 };