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,10 +1,13 @@
1
1
  import {
2
+ orderHasRelevance,
2
3
  emitRelationFrom,
4
+ needsOperatorSql,
3
5
  decodeResult,
4
6
  emitJoin,
5
7
  emitWhere,
6
8
  emitOrder,
7
9
  emitPrimaryOrder,
10
+ refuseHidden,
8
11
  selectedColumns,
9
12
  rejectKeys,
10
13
  readLimit,
@@ -16,8 +19,8 @@ import {
16
19
  list,
17
20
  isRecord,
18
21
  registerFailFix,
19
- fail
20
- } from "./2z7wv5tj.js";
22
+ fail2
23
+ } from "./kgwp4jdd.js";
21
24
  import {
22
25
  throwNamed2
23
26
  } from "./52szzakm.js";
@@ -26,6 +29,32 @@ import {
26
29
  registerFailFix("OKM1105", "Pass limit on the include, or call .all(reason).");
27
30
  var INCLUDE_OPTIONS = ["all", "include", "limit", "orderBy", "select", "where"];
28
31
  function emitIncludeSelect(include, alias, sink, outputs, column) {
32
+ const morph = include.morphTo;
33
+ if (morph !== undefined) {
34
+ if (column > 0 || outputs?.fields.length)
35
+ sink.text(", ");
36
+ sink.text(alias);
37
+ sink.text(".payload");
38
+ outputs?.includes.push({
39
+ name: include.name,
40
+ kind: "one",
41
+ at: column,
42
+ fields: [],
43
+ nested: [],
44
+ morph: {
45
+ typeKey: morph.typeKey,
46
+ targets: morph.targets.map((target) => ({
47
+ type: target.type,
48
+ fields: target.columns.map((item, at) => ({
49
+ key: item.field,
50
+ at,
51
+ decode: item.decode
52
+ }))
53
+ }))
54
+ }
55
+ });
56
+ return column + 1;
57
+ }
29
58
  const selected = include.columns;
30
59
  sink.mark(`i:${include.name}:${include.relation.kind}`);
31
60
  if (include.relation.kind === "many") {
@@ -109,9 +138,13 @@ function emitIncludeSelect(include, alias, sink, outputs, column) {
109
138
  return cursor;
110
139
  }
111
140
  function emitIncludeJoin(schema, include, sink, parentAlias, alias, depth) {
141
+ if (include.morphTo !== undefined) {
142
+ emitMorphJoin(schema, include, sink, parentAlias, alias, depth);
143
+ return;
144
+ }
112
145
  const child = indexes(schema).get(include.relation.table);
113
146
  if (child === undefined) {
114
- fail("OKM1120", `Relation ${include.name} names ${include.relation.table}, which is not in the schema.`);
147
+ fail2("OKM1120", `Relation ${include.name} names ${include.relation.table}, which is not in the schema.`);
115
148
  }
116
149
  const childAlias = `r${String(depth)}_${include.name}`;
117
150
  sink.text(" left join lateral (");
@@ -135,10 +168,7 @@ function emitIncludeJoin(schema, include, sink, parentAlias, alias, depth) {
135
168
  sink.text(") as item from ");
136
169
  emitRelationFrom(schema, include.relation, child, sink, parentAlias, childAlias, depth + 1);
137
170
  emitWhere(schema, child, include.where, sink, childAlias, depth + 1, true);
138
- emitOrder(child, include.orderBy, sink, childAlias);
139
- if (include.orderBy === undefined && child.model.primary.length > 0) {
140
- emitPrimaryOrder(child, sink, childAlias);
141
- }
171
+ emitIncludeOrder(child, include.orderBy, sink, childAlias);
142
172
  if (include.limit !== undefined) {
143
173
  sink.text(" limit ");
144
174
  sink.param(String(include.limit));
@@ -207,9 +237,12 @@ function emitIncludeJoin(schema, include, sink, parentAlias, alias, depth) {
207
237
  sink.text(" on true");
208
238
  }
209
239
  function emitNestedAggregate(schema, include, sink, alias, depth) {
240
+ if (include.relation.morph?.direction === "to") {
241
+ fail2("OKM1120", `Include ${include.name} is a morph and cannot be nested in this version.`);
242
+ }
210
243
  const child = indexes(schema).get(include.relation.table);
211
244
  if (child === undefined) {
212
- fail("OKM1120", `Relation ${include.name} names ${include.relation.table}, which is not in the schema.`);
245
+ fail2("OKM1120", `Relation ${include.name} names ${include.relation.table}, which is not in the schema.`);
213
246
  }
214
247
  const childAlias = `n${String(depth)}_${include.name}`;
215
248
  sink.text("(select coalesce(json_agg(item order by ord), '[]'::json) from (select item, row_number() over () as ord from (select json_build_array(");
@@ -226,10 +259,7 @@ function emitNestedAggregate(schema, include, sink, alias, depth) {
226
259
  sink.text(") as item from ");
227
260
  emitRelationFrom(schema, include.relation, child, sink, alias, childAlias, depth);
228
261
  emitWhere(schema, child, include.where, sink, childAlias, depth, true);
229
- emitOrder(child, include.orderBy, sink, childAlias);
230
- if (include.orderBy === undefined && child.model.primary.length > 0) {
231
- emitPrimaryOrder(child, sink, childAlias);
232
- }
262
+ emitIncludeOrder(child, include.orderBy, sink, childAlias);
233
263
  if (include.limit !== undefined) {
234
264
  sink.text(" limit ");
235
265
  sink.param(String(include.limit));
@@ -237,6 +267,14 @@ function emitNestedAggregate(schema, include, sink, alias, depth) {
237
267
  }
238
268
  sink.text(") limited) ordered)");
239
269
  }
270
+ function includeColumns(table, select) {
271
+ const columns = selectedColumns(table, select);
272
+ if (select === undefined)
273
+ return omitHidden(columns);
274
+ for (const column of columns)
275
+ refuseHidden(column);
276
+ return columns;
277
+ }
240
278
  function omitHidden(columns) {
241
279
  for (const column of columns) {
242
280
  if (!column.hidden)
@@ -249,7 +287,7 @@ function readIncludes(schema, table, include, all) {
249
287
  if (include === undefined)
250
288
  return [];
251
289
  if (!isRecord(include)) {
252
- fail("OKM1120", "include must be an object of relations.");
290
+ fail2("OKM1120", "include must be an object of relations.");
253
291
  }
254
292
  const parsed = [];
255
293
  for (const name of Object.keys(include)) {
@@ -262,9 +300,11 @@ function readIncludes(schema, table, include, all) {
262
300
  return parsed;
263
301
  }
264
302
  function parseInclude(schema, name, relation, value, all) {
303
+ if (relation.morph?.direction === "to")
304
+ return parseMorph(schema, name, relation, value);
265
305
  if (value === true) {
266
306
  if (relation.kind === "many" && all === undefined) {
267
- fail("OKM1105", `Include ${name} is to-many and has no limit. Pass limit, or call .all(reason).`);
307
+ fail2("OKM1105", `Include ${name} is to-many and has no limit. Pass limit, or call .all(reason).`);
268
308
  }
269
309
  const child = childIndex(schema, relation);
270
310
  const columns = omitHidden(selectedColumns(child, undefined));
@@ -280,7 +320,7 @@ function parseInclude(schema, name, relation, value, all) {
280
320
  };
281
321
  }
282
322
  if (!isRecord(value)) {
283
- fail("OKM1120", `Include ${name} must be true or an object.`);
323
+ fail2("OKM1120", `Include ${name} must be true or an object.`);
284
324
  }
285
325
  rejectKeys(value, INCLUDE_OPTIONS, `include ${name}`);
286
326
  const reason = typeof value.all === "string" ? value.all : all;
@@ -288,10 +328,10 @@ function parseInclude(schema, name, relation, value, all) {
288
328
  if (value.limit !== undefined)
289
329
  limit = readLimit(value.limit);
290
330
  if (relation.kind === "many" && limit === undefined && reason === undefined) {
291
- fail("OKM1105", `Include ${name} is to-many and has no limit. Pass limit, or call .all(reason).`);
331
+ fail2("OKM1105", `Include ${name} is to-many and has no limit. Pass limit, or call .all(reason).`);
292
332
  }
293
333
  const child = childIndex(schema, relation);
294
- const columns = omitHidden(selectedColumns(child, value.select));
334
+ const columns = includeColumns(child, value.select);
295
335
  const nestedValue = value.include;
296
336
  const nested = nestedValue === undefined ? [] : readIncludes(schema, child, nestedValue, reason);
297
337
  return {
@@ -317,10 +357,125 @@ function anchored(table, columns) {
317
357
  function childIndex(schema, relation) {
318
358
  const child = indexes(schema).get(relation.table);
319
359
  if (child === undefined) {
320
- fail("OKM1120", `Relation ${relation.name} names ${relation.table}, which is not in the schema.`);
360
+ fail2("OKM1120", `Relation ${relation.name} names ${relation.table}, which is not in the schema.`);
321
361
  }
322
362
  return child;
323
363
  }
364
+ function parseMorph(schema, name, relation, value) {
365
+ if (value !== true) {
366
+ fail2("OKM1120", `Include ${name} is a morph. Pass true. select, where, orderBy, and limit are not available on it.`);
367
+ }
368
+ const morph = relation.morph;
369
+ if (morph === undefined)
370
+ fail2("OKM1120", `Include ${name} is not a morph.`);
371
+ const planned = [];
372
+ for (const target of morph.targets) {
373
+ const child = indexes(schema).get(target.table);
374
+ if (child === undefined) {
375
+ fail2("OKM1120", `Relation ${name} names ${target.table}, which is not in the schema.`);
376
+ }
377
+ planned.push({
378
+ type: target.type,
379
+ table: target.table,
380
+ sql: target.sql,
381
+ keySql: target.keySql,
382
+ columns: omitHidden(selectedColumns(child, undefined))
383
+ });
384
+ }
385
+ return {
386
+ name,
387
+ relation,
388
+ where: undefined,
389
+ orderBy: undefined,
390
+ limit: undefined,
391
+ columns: [],
392
+ presence: undefined,
393
+ anchor: 0,
394
+ nested: [],
395
+ morphTo: {
396
+ typeKey: morph.typeField,
397
+ typeSql: morph.typeSql,
398
+ idSql: morph.idSql,
399
+ targets: planned
400
+ }
401
+ };
402
+ }
403
+ function emitMorphJoin(schema, include, sink, parentAlias, alias, depth) {
404
+ const morph = include.morphTo;
405
+ if (morph === undefined)
406
+ fail2("OKM1120", `Include ${include.name} is not a morph.`);
407
+ sink.text(" left join lateral (select payload from (");
408
+ for (let index = 0;index < morph.targets.length; index += 1) {
409
+ const target = morph.targets[index];
410
+ if (target === undefined)
411
+ continue;
412
+ if (index > 0)
413
+ sink.text(" union all ");
414
+ const childAlias = `m${String(depth)}_${include.name}_${String(index)}`;
415
+ const child = indexes(schema).get(target.table);
416
+ if (child === undefined) {
417
+ fail2("OKM1120", `Relation ${include.name} names ${target.table}, which is not in the schema.`);
418
+ }
419
+ sink.text("select json_build_array(");
420
+ sink.param(target.type);
421
+ sink.text("::text");
422
+ sink.mark(`mt:${target.type}`);
423
+ for (const column of target.columns) {
424
+ sink.text(", ");
425
+ sink.text(jsonExpr(`${childAlias}.${quote(column.sql)}`, column.dataType));
426
+ }
427
+ sink.text(") as payload from ");
428
+ sink.text(quote(target.sql));
429
+ sink.text(" ");
430
+ sink.text(childAlias);
431
+ sink.text(" where ");
432
+ sink.text(parentAlias);
433
+ sink.text(".");
434
+ sink.text(quote(morph.typeSql));
435
+ sink.text(" = ");
436
+ sink.param(target.type);
437
+ sink.text("::text");
438
+ sink.text(" and ");
439
+ sink.text(childAlias);
440
+ sink.text(".");
441
+ sink.text(quote(target.keySql));
442
+ sink.text(" = ");
443
+ sink.text(parentAlias);
444
+ sink.text(".");
445
+ sink.text(quote(morph.idSql));
446
+ emitWhere(schema, child, undefined, sink, childAlias, depth + 1, true);
447
+ }
448
+ sink.text(") matched limit 1) ");
449
+ sink.text(alias);
450
+ sink.text(" on true");
451
+ }
452
+ function decodeMorph(include, row) {
453
+ const morph = include.morph;
454
+ if (morph === undefined)
455
+ return null;
456
+ const raw = row[include.at];
457
+ if (raw === null || raw === undefined)
458
+ return null;
459
+ const parsed = JSON.parse(raw);
460
+ if (!Array.isArray(parsed))
461
+ return null;
462
+ const type = parsed[0];
463
+ if (typeof type !== "string")
464
+ return null;
465
+ let target;
466
+ for (const item of morph.targets) {
467
+ if (item.type === type)
468
+ target = item;
469
+ }
470
+ if (target === undefined)
471
+ return null;
472
+ const child = {};
473
+ for (const field of target.fields) {
474
+ child[field.key] = jsonCell(parsed[field.at + 1], field.decode);
475
+ }
476
+ child[morph.typeKey] = type;
477
+ return child;
478
+ }
324
479
  function decodeIncluded(plan, rows, table) {
325
480
  return decodeResult(plan, rows, table, decodeRowIncluded);
326
481
  }
@@ -335,6 +490,8 @@ function decodeRowIncluded(outputs, row) {
335
490
  return result;
336
491
  }
337
492
  function decodeInclude(include, row) {
493
+ if (include.morph !== undefined)
494
+ return decodeMorph(include, row);
338
495
  if (include.kind === "many") {
339
496
  const raw = row[include.at];
340
497
  if (raw === null || raw === undefined)
@@ -390,10 +547,58 @@ function jsonCell(value, decode) {
390
547
  const wire = typeof value === "string" ? value : JSON.stringify(value);
391
548
  return decode === undefined ? wire : decode(wire);
392
549
  }
550
+ function emitIncludeOrder(child, orderBy, sink, alias) {
551
+ emitOrder(child, orderBy, sink, alias);
552
+ const named = isRecord(orderBy) ? Object.keys(orderBy) : [];
553
+ if (named.length === 0) {
554
+ if (child.model.primary.length > 0)
555
+ emitPrimaryOrder(child, sink, alias);
556
+ return;
557
+ }
558
+ for (const field of child.model.primary) {
559
+ if (named.includes(field))
560
+ continue;
561
+ const column = child.columns.get(field);
562
+ if (column === undefined)
563
+ continue;
564
+ sink.text(", ");
565
+ sink.text(alias);
566
+ sink.text(".");
567
+ sink.text(quote(column.sql));
568
+ sink.text(" asc");
569
+ }
570
+ }
571
+ function needsOperator(schema, call) {
572
+ const table = indexes(schema).get(call.table);
573
+ if (table === undefined)
574
+ return false;
575
+ return walkOperators(schema, table, call.include);
576
+ }
577
+ function walkOperators(schema, table, include) {
578
+ if (!isRecord(include))
579
+ return false;
580
+ for (const name of Object.keys(include)) {
581
+ const value = include[name];
582
+ if (!isRecord(value))
583
+ continue;
584
+ const relation = table.relations.get(name);
585
+ if (relation === undefined)
586
+ continue;
587
+ const child = indexes(schema).get(relation.table);
588
+ if (child === undefined)
589
+ continue;
590
+ if (needsOperatorSql(child, value.where) || orderHasRelevance(value.orderBy))
591
+ return true;
592
+ if (walkOperators(schema, child, value.include))
593
+ return true;
594
+ }
595
+ return false;
596
+ }
393
597
  var includeHooks = {
394
598
  read: readIncludes,
395
599
  select: emitIncludeSelect,
396
- join: emitIncludeJoin
600
+ join: emitIncludeJoin,
601
+ needsOperator
397
602
  };
398
603
  export {
399
604
  decodeIncluded,
@@ -0,0 +1,179 @@
1
+ import {
2
+ build3
3
+ } from "./n0jmpgj1.js";
4
+ import {
5
+ attachHttp
6
+ } from "./bah9nbg3.js";
7
+ import {
8
+ rejectKeys,
9
+ readLimit,
10
+ indexes,
11
+ isRecord,
12
+ fail2
13
+ } from "./kgwp4jdd.js";
14
+ import {
15
+ OkmError2
16
+ } from "./52szzakm.js";
17
+
18
+ // src/runtime/read-stream.ts
19
+ var DEFAULT_BATCH = 1000;
20
+ var MAX_BATCH = 2147483647;
21
+ async function* streamRows(session, call, mods, view, batchOptions, log) {
22
+ const stop = (error) => {
23
+ throw log(attachHttp(session.http, error));
24
+ };
25
+ let batch;
26
+ let total;
27
+ let signal;
28
+ let deadline;
29
+ try {
30
+ if (call.op !== "find") {
31
+ fail2("OKM1120", `stream() reads rows from find(). ${call.op}() returns one value.`);
32
+ }
33
+ if (call.search !== undefined) {
34
+ fail2("OKM1120", "stream() 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.");
35
+ }
36
+ if (Array.isArray(call.select) && call.select.some((item) => typeof item !== "string")) {
37
+ fail2("OKM1120", "stream() does not take a window. The keyset cursor has no window value to encode.");
38
+ }
39
+ const indexed = indexes(session.schema).get(call.table);
40
+ if (indexed === undefined)
41
+ fail2("OKM1120", `Table ${call.table} is not in the schema.`);
42
+ if (indexed.model.primary.length === 0) {
43
+ fail2("OKM1120", `stream() needs a primary key on ${indexed.model.name}. A keyset batch orders by that key.`, "Give the table a primary key. stream() orders by it.");
44
+ }
45
+ batch = readBatch(batchOptions);
46
+ if (mods.all !== undefined && mods.all.length === 0) {
47
+ fail2("OKM1101", "find() .all needs a reason.");
48
+ }
49
+ if (call.limit !== undefined)
50
+ total = readLimit(call.limit);
51
+ else if (mods.all === undefined) {
52
+ fail2("OKM1101", "find() needs a limit, or .all(reason) when the full set is intentional.");
53
+ }
54
+ const timeout = call.timeout;
55
+ if (timeout !== undefined && (typeof timeout !== "number" || !(timeout > 0))) {
56
+ fail2("OKM1121", "timeout must be a number of milliseconds above 0.");
57
+ }
58
+ signal = signalOf(call.signal);
59
+ deadline = timeout === undefined ? undefined : Date.now() + timeout;
60
+ } catch (error) {
61
+ if (error instanceof OkmError2)
62
+ stop(error);
63
+ throw error;
64
+ }
65
+ if (total === 0)
66
+ return;
67
+ const cancel = session.pool.capabilities.cancel === true;
68
+ const statement = session.timeouts?.statement;
69
+ const pin = {};
70
+ let after;
71
+ let left = total;
72
+ let pending;
73
+ try {
74
+ while (left === undefined || left > 0) {
75
+ if (signal?.aborted === true) {
76
+ stop(new OkmError2("cancelled", "The stream was cancelled.", { kind: "cancelled" }));
77
+ }
78
+ const remaining = timeLeft(deadline);
79
+ const size = left === undefined ? batch : Math.min(batch, left);
80
+ const handle = build3(session, call.table, pageFields(call, size, after, signal, batchTimeout(remaining, cancel, statement)), mods, view, pin);
81
+ pending = handle;
82
+ const value = asPage(await handle);
83
+ pending = undefined;
84
+ for (const row of value.items) {
85
+ yield row;
86
+ if (left !== undefined) {
87
+ left -= 1;
88
+ if (left === 0)
89
+ return;
90
+ }
91
+ }
92
+ if (value.next === null || value.items.length < size)
93
+ return;
94
+ after = value.next;
95
+ }
96
+ } catch (error) {
97
+ if (error instanceof OkmError2 && error.kind === "timeout" && error.message === "The stream timed out.") {
98
+ stop(error);
99
+ }
100
+ throw error;
101
+ } finally {
102
+ if (pending !== undefined) {
103
+ pending.then(() => {
104
+ return;
105
+ }, () => {
106
+ return;
107
+ });
108
+ }
109
+ }
110
+ }
111
+ function readBatch(options) {
112
+ if (options === undefined)
113
+ return DEFAULT_BATCH;
114
+ if (!isRecord(options))
115
+ fail2("OKM1120", "stream() takes an object. Pass { batch }.");
116
+ rejectKeys(options, ["batch"], "stream");
117
+ const batch = options.batch;
118
+ if (batch === undefined)
119
+ return DEFAULT_BATCH;
120
+ if (typeof batch !== "number" || !Number.isInteger(batch) || batch < 1 || batch > MAX_BATCH) {
121
+ fail2("OKM1121", "stream() batch must be an integer from 1 to 2147483647.");
122
+ }
123
+ return batch;
124
+ }
125
+ function pageFields(call, size, after, signal, timeout) {
126
+ const out = { limit: size };
127
+ if (call.where !== undefined)
128
+ out.where = call.where;
129
+ if (call.select !== undefined)
130
+ out.select = call.select;
131
+ if (call.orderBy !== undefined)
132
+ out.orderBy = call.orderBy;
133
+ if (call.include !== undefined)
134
+ out.include = call.include;
135
+ if (call.route !== undefined)
136
+ out.route = call.route;
137
+ if (after !== undefined)
138
+ out.after = after;
139
+ if (signal !== undefined)
140
+ out.signal = signal;
141
+ if (timeout !== undefined)
142
+ out.timeout = timeout;
143
+ return out;
144
+ }
145
+ function timeLeft(deadline) {
146
+ if (deadline === undefined)
147
+ return;
148
+ const left = deadline - Date.now();
149
+ if (left <= 0)
150
+ throw new OkmError2("timeout", "The stream timed out.", { kind: "timeout" });
151
+ return left;
152
+ }
153
+ function batchTimeout(remaining, cancel, statement) {
154
+ if (!cancel || remaining === undefined)
155
+ return;
156
+ if (statement !== undefined && statement > 0 && statement < remaining)
157
+ return statement;
158
+ return remaining;
159
+ }
160
+ function signalOf(value) {
161
+ if (typeof value === "object" && value !== null && "aborted" in value) {
162
+ return value;
163
+ }
164
+ return;
165
+ }
166
+ function asPage(value) {
167
+ if (typeof value !== "object" || value === null || !("items" in value) || !("next" in value)) {
168
+ throw new OkmError2("OKM1120", "stream() did not get a page of rows.");
169
+ }
170
+ const items = value.items;
171
+ const next = value.next;
172
+ if (!Array.isArray(items) || next !== null && typeof next !== "string") {
173
+ throw new OkmError2("OKM1120", "stream() did not get a page of rows.");
174
+ }
175
+ return { items, next };
176
+ }
177
+ export {
178
+ streamRows
179
+ };
@@ -1,32 +1,33 @@
1
1
  import {
2
2
  parseCatalog2
3
- } from "./qq3899ht.js";
3
+ } from "./12z7aznj.js";
4
4
  import"./n442h54c.js";
5
- import"./wkme6tep.js";
5
+ import"./cm3w0ywp.js";
6
+ import"./35q0n9tz.js";
6
7
  import {
7
8
  watchReserved2
8
- } from "./q3j1tq7a.js";
9
+ } from "./wcrheftc.js";
9
10
  import {
10
11
  ensureMigrationTables2
11
- } from "./nx9hh281.js";
12
+ } from "./pan0370z.js";
12
13
  import {
13
14
  sealViews,
14
15
  catalogQuery2,
15
16
  managedRoleOptions2
16
- } from "./f0f1n281.js";
17
+ } from "./mkrn359n.js";
17
18
  import {
18
19
  assumeRole,
19
20
  grantSchema,
20
21
  assertSchemaUsageLanded,
21
22
  catalogsEqual,
22
23
  snapshotDifference
23
- } from "./p248q79y.js";
24
+ } from "./mnvjvqyp.js";
24
25
  import {
25
26
  roleExists
26
- } from "./knmes2kh.js";
27
+ } from "./kj9d8maq.js";
27
28
  import {
28
29
  introspectSchema
29
- } from "./vjpnzwjz.js";
30
+ } from "./p3xbdcvt.js";
30
31
  import {
31
32
  projectPath,
32
33
  loadMigrations,
@@ -35,7 +36,7 @@ import {
35
36
  hasError,
36
37
  lintRefusal,
37
38
  openProject
38
- } from "./t3g0k88x.js";
39
+ } from "./f4jy1j7x.js";
39
40
  import {
40
41
  sqlStatements,
41
42
  transactionControl,
@@ -50,13 +51,13 @@ import {
50
51
  assertDirectConnection,
51
52
  assertStableBackend,
52
53
  selectTarget
53
- } from "./ttmg09vd.js";
54
+ } from "./dpc2r7rb.js";
54
55
  import {
55
56
  isConnectionLost
56
- } from "./gwn6v667.js";
57
+ } from "./cnkky55j.js";
57
58
  import {
58
59
  quoteIdent
59
- } from "./9s0ypp87.js";
60
+ } from "./sjk86yrx.js";
60
61
  import {
61
62
  catalog,
62
63
  isDomain
@@ -151,12 +152,18 @@ async function inCheckTransaction(url, scratch, config, allowPooler, run) {
151
152
  onnotice: () => {}
152
153
  });
153
154
  const reserved = await sql.reserve();
154
- const lost = { on: false };
155
+ const lost = { on: false, stop: () => {
156
+ return;
157
+ } };
155
158
  watchReserved2(reserved, lost);
156
159
  refuseWhenLost(reserved, lost);
157
160
  let open = false;
158
161
  let failure;
159
162
  let value;
163
+ let created = false;
164
+ let sawCommit = false;
165
+ let rolledBack = false;
166
+ let xid = "";
160
167
  try {
161
168
  await assertStableBackend(async () => {
162
169
  const rows = await reserved.unsafe("select pg_backend_pid()::text as pid");
@@ -165,8 +172,9 @@ async function inCheckTransaction(url, scratch, config, allowPooler, run) {
165
172
  }, allowPooler);
166
173
  await reserved.unsafe("begin");
167
174
  open = true;
168
- const xid = await readXid(reserved);
175
+ xid = await readXid(reserved);
169
176
  await reserved.unsafe(`create schema ${quoteIdent(scratch)}`);
177
+ created = true;
170
178
  await reserved.unsafe(`set local search_path to ${quoteIdent(scratch)}`);
171
179
  await setLocalTimeout(reserved, "lock_timeout", config.timeouts?.lock);
172
180
  await setLocalTimeout(reserved, "statement_timeout", config.timeouts?.statement);
@@ -178,21 +186,36 @@ async function inCheckTransaction(url, scratch, config, allowPooler, run) {
178
186
  lost.on = true;
179
187
  return;
180
188
  });
181
- if (!lost.on && after !== xid)
189
+ if (!lost.on && after !== xid) {
190
+ sawCommit = true;
182
191
  throw xidChanged();
183
- if (!lost.on)
192
+ }
193
+ if (!lost.on) {
184
194
  await reserved.unsafe("rollback");
195
+ rolledBack = true;
196
+ }
185
197
  open = false;
186
198
  } catch (error) {
187
199
  if (isConnectionLost(error))
188
200
  lost.on = true;
189
201
  failure = error;
190
202
  } finally {
203
+ if (open && !lost.on && !sawCommit) {
204
+ const after = await readXid(reserved).catch((error) => {
205
+ if (isConnectionLost(error))
206
+ lost.on = true;
207
+ return;
208
+ });
209
+ if (!lost.on && after !== xid)
210
+ sawCommit = true;
211
+ }
191
212
  if (open && !lost.on) {
192
213
  await reserved.unsafe("rollback").catch((error) => {
193
214
  if (isConnectionLost(error))
194
215
  lost.on = true;
195
216
  });
217
+ if (!lost.on && !sawCommit)
218
+ rolledBack = true;
196
219
  }
197
220
  if (!lost.on)
198
221
  await reserved.unsafe("reset role").catch(() => {
@@ -207,6 +230,11 @@ async function inCheckTransaction(url, scratch, config, allowPooler, run) {
207
230
  }
208
231
  await finishQuiet(() => sql.end({ timeout: 5 }));
209
232
  }
233
+ if (!created || rolledBack) {
234
+ if (failure !== undefined)
235
+ throw sessionFailure(failure) ?? failure;
236
+ return value;
237
+ }
210
238
  const left = await dropScratch(url, scratch);
211
239
  if (left !== undefined) {
212
240
  const note = `Scratch schema ${left} is still present.`;