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,604 @@
1
+ import {
2
+ throwNamed,
3
+ orderHasRelevance,
4
+ emitRelationFrom,
5
+ needsOperatorSql,
6
+ decodeResult,
7
+ emitJoin,
8
+ emitWhere,
9
+ emitOrder,
10
+ emitPrimaryOrder,
11
+ refuseHidden,
12
+ selectedColumns,
13
+ rejectKeys,
14
+ readLimit,
15
+ projectExpr,
16
+ jsonExpr,
17
+ cell,
18
+ indexes,
19
+ quote,
20
+ list,
21
+ isRecord,
22
+ registerFailFix,
23
+ fail2
24
+ } from "./hfq3n3bd.js";
25
+
26
+ // src/runtime/include.ts
27
+ registerFailFix("OKM1105", "Pass limit on the include, or call .all(reason).");
28
+ var INCLUDE_OPTIONS = ["all", "include", "limit", "orderBy", "select", "where"];
29
+ function emitIncludeSelect(include, alias, sink, outputs, column) {
30
+ const morph = include.morphTo;
31
+ if (morph !== undefined) {
32
+ if (column > 0 || outputs?.fields.length)
33
+ sink.text(", ");
34
+ sink.text(alias);
35
+ sink.text(".payload");
36
+ outputs?.includes.push({
37
+ name: include.name,
38
+ kind: "one",
39
+ at: column,
40
+ fields: [],
41
+ nested: [],
42
+ morph: {
43
+ typeKey: morph.typeKey,
44
+ targets: morph.targets.map((target) => ({
45
+ type: target.type,
46
+ fields: target.columns.map((item, at) => ({
47
+ key: item.field,
48
+ at,
49
+ decode: item.decode
50
+ }))
51
+ }))
52
+ }
53
+ });
54
+ return column + 1;
55
+ }
56
+ const selected = include.columns;
57
+ sink.mark(`i:${include.name}:${include.relation.kind}`);
58
+ if (include.relation.kind === "many") {
59
+ if (column > 0 || outputs?.fields.length)
60
+ sink.text(", ");
61
+ sink.text(alias);
62
+ sink.text(".rows");
63
+ outputs?.includes.push({
64
+ name: include.name,
65
+ kind: "many",
66
+ at: column,
67
+ fields: selected.map((item, index) => ({
68
+ key: item.field,
69
+ at: index,
70
+ decode: item.decode
71
+ })),
72
+ nested: include.nested.map((nested, index) => ({
73
+ name: nested.name,
74
+ kind: nested.relation.kind,
75
+ at: selected.length + index,
76
+ fields: nested.columns.map((item, at) => ({
77
+ key: item.field,
78
+ at,
79
+ decode: item.decode
80
+ })),
81
+ nested: []
82
+ }))
83
+ });
84
+ return column + 1;
85
+ }
86
+ const projected = include.presence === undefined ? selected : [...selected, include.presence];
87
+ const start = column;
88
+ for (let index = 0;index < projected.length; index += 1) {
89
+ if (start + index > 0)
90
+ sink.text(", ");
91
+ sink.text(alias);
92
+ sink.text(".c");
93
+ sink.text(String(index));
94
+ sink.mark(`s:${projected[index]?.field ?? ""}`);
95
+ }
96
+ const nestedStart = start + projected.length;
97
+ let cursor = nestedStart;
98
+ const nestedOut = [];
99
+ for (let index = 0;index < include.nested.length; index += 1) {
100
+ const nested = include.nested[index];
101
+ if (nested === undefined)
102
+ continue;
103
+ const width = nested.relation.kind === "many" ? 1 : nested.presence === undefined ? nested.columns.length : nested.columns.length + 1;
104
+ for (let slot = 0;slot < width; slot += 1) {
105
+ sink.text(", ");
106
+ sink.text(alias);
107
+ sink.text(".n");
108
+ sink.text(String(index));
109
+ sink.text("_");
110
+ sink.text(String(slot));
111
+ }
112
+ nestedOut.push({
113
+ name: nested.name,
114
+ kind: nested.relation.kind,
115
+ at: nested.relation.kind === "many" ? cursor : nested.presence === undefined ? cursor : cursor + nested.columns.length,
116
+ fields: nested.columns.map((item, at) => ({
117
+ key: item.field,
118
+ at: nested.relation.kind === "many" ? at : cursor + at,
119
+ decode: item.decode
120
+ })),
121
+ nested: []
122
+ });
123
+ cursor += width;
124
+ }
125
+ outputs?.includes.push({
126
+ name: include.name,
127
+ kind: "one",
128
+ at: start + include.anchor,
129
+ fields: selected.map((item, index) => ({
130
+ key: item.field,
131
+ at: start + index,
132
+ decode: item.decode
133
+ })),
134
+ nested: nestedOut
135
+ });
136
+ return cursor;
137
+ }
138
+ function emitIncludeJoin(schema, include, sink, parentAlias, alias, depth) {
139
+ if (include.morphTo !== undefined) {
140
+ emitMorphJoin(schema, include, sink, parentAlias, alias, depth);
141
+ return;
142
+ }
143
+ const child = indexes(schema).get(include.relation.table);
144
+ if (child === undefined) {
145
+ fail2("OKM1120", `Relation ${include.name} names ${include.relation.table}, which is not in the schema.`);
146
+ }
147
+ const childAlias = `r${String(depth)}_${include.name}`;
148
+ sink.text(" left join lateral (");
149
+ if (include.relation.kind === "many") {
150
+ sink.text("select coalesce(json_agg(item order by ord), '[]'::json) as rows from (select item, row_number() over () as ord from (select json_build_array(");
151
+ const selected = include.columns;
152
+ for (let index = 0;index < selected.length; index += 1) {
153
+ const item = selected[index];
154
+ if (item === undefined)
155
+ continue;
156
+ if (index > 0)
157
+ sink.text(", ");
158
+ sink.text(jsonExpr(`${childAlias}.${quote(item.sql)}`, item.dataType));
159
+ }
160
+ if (selected.length === 0)
161
+ sink.text("1");
162
+ for (const nested of include.nested) {
163
+ sink.text(", ");
164
+ emitNestedAggregate(schema, nested, sink, childAlias, depth + 1);
165
+ }
166
+ sink.text(") as item from ");
167
+ emitRelationFrom(schema, include.relation, child, sink, parentAlias, childAlias, depth + 1);
168
+ emitWhere(schema, child, include.where, sink, childAlias, depth + 1, true);
169
+ emitIncludeOrder(child, include.orderBy, sink, childAlias);
170
+ if (include.limit !== undefined) {
171
+ sink.text(" limit ");
172
+ sink.param(String(include.limit));
173
+ sink.mark(`l:${String(include.limit)}`);
174
+ }
175
+ sink.text(") limited) ordered) ");
176
+ sink.text(alias);
177
+ sink.text(" on true");
178
+ return;
179
+ }
180
+ sink.text("select ");
181
+ const projected = include.presence === undefined ? include.columns : [...include.columns, include.presence];
182
+ for (let index = 0;index < projected.length; index += 1) {
183
+ const item = projected[index];
184
+ if (item === undefined)
185
+ continue;
186
+ if (index > 0)
187
+ sink.text(", ");
188
+ sink.text(projectExpr(`${childAlias}.${quote(item.sql)}`, item.dataType));
189
+ sink.text(" as c");
190
+ sink.text(String(index));
191
+ }
192
+ if (projected.length === 0)
193
+ sink.text("1 as c0");
194
+ for (let index = 0;index < include.nested.length; index += 1) {
195
+ const nested = include.nested[index];
196
+ if (nested === undefined)
197
+ continue;
198
+ if (nested.relation.kind === "many") {
199
+ sink.text(", ");
200
+ sink.text("n");
201
+ sink.text(String(index));
202
+ sink.text(".rows as n");
203
+ sink.text(String(index));
204
+ sink.text("_0");
205
+ continue;
206
+ }
207
+ const width = nested.presence === undefined ? nested.columns.length : nested.columns.length + 1;
208
+ for (let slot = 0;slot < width; slot += 1) {
209
+ sink.text(", ");
210
+ sink.text("n");
211
+ sink.text(String(index));
212
+ sink.text(".c");
213
+ sink.text(String(slot));
214
+ sink.text(" as n");
215
+ sink.text(String(index));
216
+ sink.text("_");
217
+ sink.text(String(slot));
218
+ }
219
+ }
220
+ sink.text(" from ");
221
+ sink.text(quote(child.model.sql));
222
+ sink.text(" ");
223
+ sink.text(childAlias);
224
+ for (let index = 0;index < include.nested.length; index += 1) {
225
+ const nested = include.nested[index];
226
+ if (nested === undefined)
227
+ continue;
228
+ emitIncludeJoin(schema, nested, sink, childAlias, `n${String(index)}`, depth + 1);
229
+ }
230
+ sink.text(" where ");
231
+ emitJoin(sink, parentAlias, childAlias, include.relation);
232
+ emitWhere(schema, child, include.where, sink, childAlias, depth + 1, true);
233
+ sink.text(" limit 1) ");
234
+ sink.text(alias);
235
+ sink.text(" on true");
236
+ }
237
+ function emitNestedAggregate(schema, include, sink, alias, depth) {
238
+ if (include.relation.morph?.direction === "to") {
239
+ fail2("OKM1120", `Include ${include.name} is a morph and cannot be nested in this version.`);
240
+ }
241
+ const child = indexes(schema).get(include.relation.table);
242
+ if (child === undefined) {
243
+ fail2("OKM1120", `Relation ${include.name} names ${include.relation.table}, which is not in the schema.`);
244
+ }
245
+ const childAlias = `n${String(depth)}_${include.name}`;
246
+ sink.text("(select coalesce(json_agg(item order by ord), '[]'::json) from (select item, row_number() over () as ord from (select json_build_array(");
247
+ for (let index = 0;index < include.columns.length; index += 1) {
248
+ const item = include.columns[index];
249
+ if (item === undefined)
250
+ continue;
251
+ if (index > 0)
252
+ sink.text(", ");
253
+ sink.text(jsonExpr(`${childAlias}.${quote(item.sql)}`, item.dataType));
254
+ }
255
+ if (include.columns.length === 0)
256
+ sink.text("1");
257
+ sink.text(") as item from ");
258
+ emitRelationFrom(schema, include.relation, child, sink, alias, childAlias, depth);
259
+ emitWhere(schema, child, include.where, sink, childAlias, depth, true);
260
+ emitIncludeOrder(child, include.orderBy, sink, childAlias);
261
+ if (include.limit !== undefined) {
262
+ sink.text(" limit ");
263
+ sink.param(String(include.limit));
264
+ sink.mark(`l:${String(include.limit)}`);
265
+ }
266
+ sink.text(") limited) ordered)");
267
+ }
268
+ function includeColumns(table, select) {
269
+ const columns = selectedColumns(table, select);
270
+ if (select === undefined)
271
+ return omitHidden(columns);
272
+ for (const column of columns)
273
+ refuseHidden(column);
274
+ return columns;
275
+ }
276
+ function omitHidden(columns) {
277
+ for (const column of columns) {
278
+ if (!column.hidden)
279
+ continue;
280
+ return columns.filter((item) => !item.hidden);
281
+ }
282
+ return columns;
283
+ }
284
+ function readIncludes(schema, table, include, all) {
285
+ if (include === undefined)
286
+ return [];
287
+ if (!isRecord(include)) {
288
+ fail2("OKM1120", "include must be an object of relations.");
289
+ }
290
+ const parsed = [];
291
+ for (const name of Object.keys(include)) {
292
+ const relation = table.relations.get(name);
293
+ if (relation === undefined) {
294
+ throwNamed("OKM1120", name, [...table.relations.keys()], `Include ${name} is not a relation of ${table.model.name}. Accepted names: ${list([...table.relations.keys()])}.`);
295
+ }
296
+ parsed.push(parseInclude(schema, name, relation, include[name], all));
297
+ }
298
+ return parsed;
299
+ }
300
+ function parseInclude(schema, name, relation, value, all) {
301
+ if (relation.morph?.direction === "to")
302
+ return parseMorph(schema, name, relation, value);
303
+ if (value === true) {
304
+ if (relation.kind === "many" && all === undefined) {
305
+ fail2("OKM1105", `Include ${name} is to-many and has no limit. Pass limit, or call .all(reason).`);
306
+ }
307
+ const child = childIndex(schema, relation);
308
+ const columns = omitHidden(selectedColumns(child, undefined));
309
+ return {
310
+ name,
311
+ relation,
312
+ where: undefined,
313
+ orderBy: undefined,
314
+ limit: undefined,
315
+ columns,
316
+ ...anchored(child, columns),
317
+ nested: []
318
+ };
319
+ }
320
+ if (!isRecord(value)) {
321
+ fail2("OKM1120", `Include ${name} must be true or an object.`);
322
+ }
323
+ rejectKeys(value, INCLUDE_OPTIONS, `include ${name}`);
324
+ const reason = typeof value.all === "string" ? value.all : all;
325
+ let limit;
326
+ if (value.limit !== undefined)
327
+ limit = readLimit(value.limit);
328
+ if (relation.kind === "many" && limit === undefined && reason === undefined) {
329
+ fail2("OKM1105", `Include ${name} is to-many and has no limit. Pass limit, or call .all(reason).`);
330
+ }
331
+ const child = childIndex(schema, relation);
332
+ const columns = includeColumns(child, value.select);
333
+ const nestedValue = value.include;
334
+ const nested = nestedValue === undefined ? [] : readIncludes(schema, child, nestedValue, reason);
335
+ return {
336
+ name,
337
+ relation,
338
+ where: value.where,
339
+ orderBy: value.orderBy,
340
+ limit,
341
+ columns,
342
+ ...anchored(child, columns),
343
+ nested
344
+ };
345
+ }
346
+ function anchored(table, columns) {
347
+ const pk = table.model.primary[0];
348
+ if (pk === undefined)
349
+ return { presence: undefined, anchor: 0 };
350
+ const selected = columns.findIndex((column) => column.field === pk);
351
+ if (selected >= 0)
352
+ return { presence: undefined, anchor: selected };
353
+ return { presence: table.columns.get(pk), anchor: columns.length };
354
+ }
355
+ function childIndex(schema, relation) {
356
+ const child = indexes(schema).get(relation.table);
357
+ if (child === undefined) {
358
+ fail2("OKM1120", `Relation ${relation.name} names ${relation.table}, which is not in the schema.`);
359
+ }
360
+ return child;
361
+ }
362
+ function parseMorph(schema, name, relation, value) {
363
+ if (value !== true) {
364
+ fail2("OKM1120", `Include ${name} is a morph. Pass true. select, where, orderBy, and limit are not available on it.`);
365
+ }
366
+ const morph = relation.morph;
367
+ if (morph === undefined)
368
+ fail2("OKM1120", `Include ${name} is not a morph.`);
369
+ const planned = [];
370
+ for (const target of morph.targets) {
371
+ const child = indexes(schema).get(target.table);
372
+ if (child === undefined) {
373
+ fail2("OKM1120", `Relation ${name} names ${target.table}, which is not in the schema.`);
374
+ }
375
+ planned.push({
376
+ type: target.type,
377
+ table: target.table,
378
+ sql: target.sql,
379
+ keySql: target.keySql,
380
+ columns: omitHidden(selectedColumns(child, undefined))
381
+ });
382
+ }
383
+ return {
384
+ name,
385
+ relation,
386
+ where: undefined,
387
+ orderBy: undefined,
388
+ limit: undefined,
389
+ columns: [],
390
+ presence: undefined,
391
+ anchor: 0,
392
+ nested: [],
393
+ morphTo: {
394
+ typeKey: morph.typeField,
395
+ typeSql: morph.typeSql,
396
+ idSql: morph.idSql,
397
+ targets: planned
398
+ }
399
+ };
400
+ }
401
+ function emitMorphJoin(schema, include, sink, parentAlias, alias, depth) {
402
+ const morph = include.morphTo;
403
+ if (morph === undefined)
404
+ fail2("OKM1120", `Include ${include.name} is not a morph.`);
405
+ sink.text(" left join lateral (select payload from (");
406
+ for (let index = 0;index < morph.targets.length; index += 1) {
407
+ const target = morph.targets[index];
408
+ if (target === undefined)
409
+ continue;
410
+ if (index > 0)
411
+ sink.text(" union all ");
412
+ const childAlias = `m${String(depth)}_${include.name}_${String(index)}`;
413
+ const child = indexes(schema).get(target.table);
414
+ if (child === undefined) {
415
+ fail2("OKM1120", `Relation ${include.name} names ${target.table}, which is not in the schema.`);
416
+ }
417
+ sink.text("select json_build_array(");
418
+ sink.param(target.type);
419
+ sink.text("::text");
420
+ sink.mark(`mt:${target.type}`);
421
+ for (const column of target.columns) {
422
+ sink.text(", ");
423
+ sink.text(jsonExpr(`${childAlias}.${quote(column.sql)}`, column.dataType));
424
+ }
425
+ sink.text(") as payload from ");
426
+ sink.text(quote(target.sql));
427
+ sink.text(" ");
428
+ sink.text(childAlias);
429
+ sink.text(" where ");
430
+ sink.text(parentAlias);
431
+ sink.text(".");
432
+ sink.text(quote(morph.typeSql));
433
+ sink.text(" = ");
434
+ sink.param(target.type);
435
+ sink.text("::text");
436
+ sink.text(" and ");
437
+ sink.text(childAlias);
438
+ sink.text(".");
439
+ sink.text(quote(target.keySql));
440
+ sink.text(" = ");
441
+ sink.text(parentAlias);
442
+ sink.text(".");
443
+ sink.text(quote(morph.idSql));
444
+ emitWhere(schema, child, undefined, sink, childAlias, depth + 1, true);
445
+ }
446
+ sink.text(") matched limit 1) ");
447
+ sink.text(alias);
448
+ sink.text(" on true");
449
+ }
450
+ function decodeMorph(include, row) {
451
+ const morph = include.morph;
452
+ if (morph === undefined)
453
+ return null;
454
+ const raw = row[include.at];
455
+ if (raw === null || raw === undefined)
456
+ return null;
457
+ const parsed = JSON.parse(raw);
458
+ if (!Array.isArray(parsed))
459
+ return null;
460
+ const type = parsed[0];
461
+ if (typeof type !== "string")
462
+ return null;
463
+ let target;
464
+ for (const item of morph.targets) {
465
+ if (item.type === type)
466
+ target = item;
467
+ }
468
+ if (target === undefined)
469
+ return null;
470
+ const child = {};
471
+ for (const field of target.fields) {
472
+ child[field.key] = jsonCell(parsed[field.at + 1], field.decode);
473
+ }
474
+ child[morph.typeKey] = type;
475
+ return child;
476
+ }
477
+ function decodeIncluded(plan, rows, table) {
478
+ return decodeResult(plan, rows, table, decodeRowIncluded);
479
+ }
480
+ function decodeRowIncluded(outputs, row) {
481
+ const result = {};
482
+ for (const field of outputs.fields) {
483
+ result[field.key] = cell(row[field.at], field.decode);
484
+ }
485
+ for (const include of outputs.includes) {
486
+ result[include.name] = decodeInclude(include, row);
487
+ }
488
+ return result;
489
+ }
490
+ function decodeInclude(include, row) {
491
+ if (include.morph !== undefined)
492
+ return decodeMorph(include, row);
493
+ if (include.kind === "many") {
494
+ const raw = row[include.at];
495
+ if (raw === null || raw === undefined)
496
+ return [];
497
+ const parsed = JSON.parse(raw);
498
+ if (!Array.isArray(parsed))
499
+ return [];
500
+ const rows = [];
501
+ for (const item of parsed) {
502
+ if (!Array.isArray(item))
503
+ continue;
504
+ const child = {};
505
+ for (const field of include.fields) {
506
+ child[field.key] = jsonCell(item[field.at], field.decode);
507
+ }
508
+ for (const nested of include.nested) {
509
+ child[nested.name] = decodeJsonInclude(nested, item[nested.at]);
510
+ }
511
+ rows.push(child);
512
+ }
513
+ return rows;
514
+ }
515
+ const presence = row[include.at];
516
+ if (presence === null || presence === undefined)
517
+ return null;
518
+ const child = {};
519
+ for (const field of include.fields) {
520
+ child[field.key] = cell(row[field.at], field.decode);
521
+ }
522
+ for (const nested of include.nested) {
523
+ child[nested.name] = decodeInclude(nested, row);
524
+ }
525
+ return child;
526
+ }
527
+ function decodeJsonInclude(include, value) {
528
+ if (!Array.isArray(value))
529
+ return include.kind === "many" ? [] : null;
530
+ const rows = [];
531
+ for (const item of value) {
532
+ if (!Array.isArray(item))
533
+ continue;
534
+ const child = {};
535
+ for (const field of include.fields) {
536
+ child[field.key] = jsonCell(item[field.at], field.decode);
537
+ }
538
+ rows.push(child);
539
+ }
540
+ return include.kind === "one" ? rows[0] ?? null : rows;
541
+ }
542
+ function jsonCell(value, decode) {
543
+ if (value === null || value === undefined)
544
+ return null;
545
+ const wire = typeof value === "string" ? value : JSON.stringify(value);
546
+ return decode === undefined ? wire : decode(wire);
547
+ }
548
+ function emitIncludeOrder(child, orderBy, sink, alias) {
549
+ emitOrder(child, orderBy, sink, alias);
550
+ const named = isRecord(orderBy) ? Object.keys(orderBy) : [];
551
+ if (named.length === 0) {
552
+ if (child.model.primary.length > 0)
553
+ emitPrimaryOrder(child, sink, alias);
554
+ return;
555
+ }
556
+ for (const field of child.model.primary) {
557
+ if (named.includes(field))
558
+ continue;
559
+ const column = child.columns.get(field);
560
+ if (column === undefined)
561
+ continue;
562
+ sink.text(", ");
563
+ sink.text(alias);
564
+ sink.text(".");
565
+ sink.text(quote(column.sql));
566
+ sink.text(" asc");
567
+ }
568
+ }
569
+ function needsOperator(schema, call) {
570
+ const table = indexes(schema).get(call.table);
571
+ if (table === undefined)
572
+ return false;
573
+ return walkOperators(schema, table, call.include);
574
+ }
575
+ function walkOperators(schema, table, include) {
576
+ if (!isRecord(include))
577
+ return false;
578
+ for (const name of Object.keys(include)) {
579
+ const value = include[name];
580
+ if (!isRecord(value))
581
+ continue;
582
+ const relation = table.relations.get(name);
583
+ if (relation === undefined)
584
+ continue;
585
+ const child = indexes(schema).get(relation.table);
586
+ if (child === undefined)
587
+ continue;
588
+ if (needsOperatorSql(child, value.where) || orderHasRelevance(value.orderBy))
589
+ return true;
590
+ if (walkOperators(schema, child, value.include))
591
+ return true;
592
+ }
593
+ return false;
594
+ }
595
+ var includeHooks = {
596
+ read: readIncludes,
597
+ select: emitIncludeSelect,
598
+ join: emitIncludeJoin,
599
+ needsOperator
600
+ };
601
+ export {
602
+ decodeIncluded,
603
+ includeHooks
604
+ };