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,294 @@
1
+ import {
2
+ assertNoControl
3
+ } from "./w5d89rb4.js";
4
+ import {
5
+ OkmError,
6
+ throwNamed,
7
+ isOperator,
8
+ operatorName,
9
+ operatorValue,
10
+ tag,
11
+ refuseHidden,
12
+ quote,
13
+ list,
14
+ fail2
15
+ } from "./hfq3n3bd.js";
16
+
17
+ // src/dialects/pg/ops/fragment.ts
18
+ var IDENT = Symbol("okmodel.sql.ident");
19
+ var COLUMN = Symbol("okmodel.sql.column");
20
+ function fragmentError(message, fix) {
21
+ throw new OkmError("OKM1125", message, { fix: { summary: fix } });
22
+ }
23
+ function isFragmentParts(value) {
24
+ return Array.isArray(value) && value.every(isPart);
25
+ }
26
+ function isPart(value) {
27
+ if (typeof value !== "object" || value === null || !("k" in value))
28
+ return false;
29
+ const kind = value.k;
30
+ if (kind === "t")
31
+ return "s" in value && typeof value.s === "string";
32
+ if (kind === "p")
33
+ return "v" in value && typeof value.v === "string";
34
+ if (kind === "n" || kind === "c")
35
+ return true;
36
+ if (kind === "i")
37
+ return "n" in value && typeof value.n === "string";
38
+ if (kind === "f")
39
+ return "parts" in value && isFragmentParts(value.parts);
40
+ return false;
41
+ }
42
+ function readSelectFragment(value) {
43
+ if (!isOperator(value) || operatorName(value) !== "sql")
44
+ return;
45
+ const named = value;
46
+ if (typeof named.as !== "string") {
47
+ fragmentError("A select fragment needs as(name).", 'Call .as("len") and pass that value in select.');
48
+ }
49
+ const stored = operatorValue(value);
50
+ if (!isFragmentParts(stored)) {
51
+ fragmentError("A fragment was not created by fragment``.", "Call fragment`...`.");
52
+ }
53
+ const textual = named.text === true;
54
+ return textual ? { as: named.as, parts: stored, textual: true } : { as: named.as, parts: stored };
55
+ }
56
+ function scanLiteral(literal, state) {
57
+ for (let index = 0;index < literal.length; index += 1) {
58
+ const char = literal[index] ?? "";
59
+ const code = literal.charCodeAt(index);
60
+ if (state.quote !== undefined) {
61
+ if (char !== state.quote)
62
+ continue;
63
+ if (literal[index + 1] === state.quote) {
64
+ index += 1;
65
+ continue;
66
+ }
67
+ state.quote = undefined;
68
+ continue;
69
+ }
70
+ if (code <= 31 && code !== 9 && code !== 10 && code !== 13 || code === 127) {
71
+ fragmentError("A fragment literal contains a control character.", "Remove NUL and other control characters. A newline, tab, or return is allowed.");
72
+ }
73
+ if (char === "'" || char === '"') {
74
+ state.quote = char;
75
+ continue;
76
+ }
77
+ if (char === "(") {
78
+ state.paren += 1;
79
+ continue;
80
+ }
81
+ if (char === ")") {
82
+ state.paren -= 1;
83
+ if (state.paren < 0) {
84
+ fragmentError("A fragment has unbalanced SQL.", "Keep parentheses and quotes balanced. A fragment cannot close the surrounding predicate.");
85
+ }
86
+ continue;
87
+ }
88
+ if (char === ";") {
89
+ fragmentError("A fragment cannot include a semicolon.", "Pass the value as an interpolation. The fragment is one expression.");
90
+ }
91
+ if (char === "$") {
92
+ fragmentError("A fragment cannot include a dollar quote or a parameter marker.", "Interpolate the value. The planner numbers parameters.");
93
+ }
94
+ if (char === "-" && literal[index + 1] === "-" || char === "/" && literal[index + 1] === "*") {
95
+ fragmentError("A fragment cannot include a comment.", "Remove the comment. A comment can hide the rest of the predicate.");
96
+ }
97
+ }
98
+ }
99
+ function appendText(parts, literal) {
100
+ if (literal.length === 0)
101
+ return;
102
+ parts.push({ k: "t", s: literal });
103
+ }
104
+ function appendValue(parts, value) {
105
+ if (value === null) {
106
+ parts.push({ k: "n" });
107
+ return;
108
+ }
109
+ if (typeof value === "string") {
110
+ parts.push({ k: "p", v: value });
111
+ return;
112
+ }
113
+ if (typeof value === "number") {
114
+ if (!Number.isFinite(value)) {
115
+ fragmentError("A fragment needs a finite number.", "Pass a finite number, or leave it out.");
116
+ }
117
+ parts.push({ k: "p", v: String(value) });
118
+ return;
119
+ }
120
+ if (typeof value === "boolean") {
121
+ parts.push({ k: "p", v: value ? "true" : "false" });
122
+ return;
123
+ }
124
+ if (typeof value === "bigint") {
125
+ parts.push({ k: "p", v: String(value) });
126
+ return;
127
+ }
128
+ if (typeof value === "object") {
129
+ if (COLUMN in value) {
130
+ parts.push({ k: "c" });
131
+ return;
132
+ }
133
+ if (IDENT in value) {
134
+ const name = value[IDENT];
135
+ if (typeof name === "string") {
136
+ parts.push({ k: "i", n: name });
137
+ return;
138
+ }
139
+ }
140
+ if (isOperator(value) && operatorName(value) === "sql") {
141
+ const nested = operatorValue(value);
142
+ if (!isFragmentParts(nested)) {
143
+ fragmentError("A fragment was not created by fragment``.", "Call fragment`...`.");
144
+ }
145
+ parts.push({ k: "f", parts: nested });
146
+ return;
147
+ }
148
+ }
149
+ fragmentError("A fragment cannot interpolate that value.", "Interpolate a string, number, boolean, null, ident(name), fragment.column, or another fragment.");
150
+ }
151
+ function hasSql(parts) {
152
+ for (const part of parts) {
153
+ if (part.k === "t") {
154
+ if (part.s.trim().length > 0)
155
+ return true;
156
+ continue;
157
+ }
158
+ return true;
159
+ }
160
+ return false;
161
+ }
162
+ function compile(strings, values) {
163
+ const state = { quote: undefined, paren: 0 };
164
+ const parts = [];
165
+ for (let index = 0;index < values.length + 1; index += 1) {
166
+ const literal = strings[index] ?? "";
167
+ scanLiteral(literal, state);
168
+ appendText(parts, literal);
169
+ if (index === values.length)
170
+ continue;
171
+ if (state.quote !== undefined) {
172
+ fragmentError("A fragment cannot interpolate inside quotes.", "Do not put an interpolation inside quotes. Write title = ${name}.");
173
+ }
174
+ appendValue(parts, values[index]);
175
+ }
176
+ if (state.quote !== undefined || state.paren !== 0) {
177
+ fragmentError("A fragment has unbalanced SQL.", "Keep parentheses and quotes balanced. A fragment cannot close the surrounding predicate.");
178
+ }
179
+ if (!hasSql(parts)) {
180
+ fragmentError("A fragment needs SQL text.", "Write an expression, or interpolate a value.");
181
+ }
182
+ return parts;
183
+ }
184
+ function isTemplate(value) {
185
+ return Array.isArray(value) && "raw" in value;
186
+ }
187
+ function bind(parts, textual = false) {
188
+ const tagged = tag("sql", parts);
189
+ return Object.assign(tagged, {
190
+ as(name) {
191
+ if (typeof name !== "string" || name.length === 0) {
192
+ fragmentError("as(name) needs a result name.", 'Call .as("len") with a name that is not a column.');
193
+ }
194
+ assertNoControl(name, "fragment as");
195
+ const selected = Object.assign(tag("sql", parts), { as: name });
196
+ return textual ? Object.assign(selected, { text: true }) : selected;
197
+ }
198
+ });
199
+ }
200
+ function fragmentTag(strings, ...values) {
201
+ if (!isTemplate(strings)) {
202
+ fragmentError("Call fragment as a template: fragment`...`.", "Write fragment`length(${fragment.column}) > ${1}`.");
203
+ }
204
+ return bind(compile(strings, values));
205
+ }
206
+ var fragment = Object.assign(fragmentTag, {
207
+ column: { [COLUMN]: true }
208
+ });
209
+
210
+ // src/runtime/sql-fragment.ts
211
+ function emitSqlValue(table, ref, value, sink) {
212
+ const dot = ref.indexOf(".");
213
+ const alias = dot < 0 ? ref : ref.slice(0, dot);
214
+ writeFragment(requireParts(value), sink, alias, ref, table, "where");
215
+ }
216
+ function emitSqlSelect(table, parts, sink, alias) {
217
+ writeFragment(parts, sink, alias, undefined, table, "select");
218
+ }
219
+ function decodeSqlNumber(wire) {
220
+ const parsed = Number(wire);
221
+ if (!Number.isFinite(parsed)) {
222
+ throw new OkmError("OKM1125", "A select fragment must be a number.", {
223
+ fix: { summary: "Use a numeric expression, such as length(title). Text stays in where." }
224
+ });
225
+ }
226
+ return parsed;
227
+ }
228
+ function requireParts(value) {
229
+ if (!isFragmentParts(value)) {
230
+ throw new OkmError("OKM1125", "A fragment was not created by fragment``.", {
231
+ fix: { summary: "Call fragment`...`." }
232
+ });
233
+ }
234
+ return value;
235
+ }
236
+ function writeFragment(parts, sink, alias, ref, table, place) {
237
+ sink.mark(":g");
238
+ sink.text("(");
239
+ for (const part of parts)
240
+ writePart(part, sink, alias, ref, table, place);
241
+ sink.mark(":g");
242
+ sink.text(")");
243
+ }
244
+ function writePart(part, sink, alias, ref, table, place) {
245
+ if (part.k === "t") {
246
+ sink.mark(`:t:${String(part.s.length)}:${part.s}`);
247
+ sink.text(part.s);
248
+ return;
249
+ }
250
+ if (part.k === "p") {
251
+ sink.mark(":p");
252
+ sink.param(part.v);
253
+ return;
254
+ }
255
+ if (part.k === "n") {
256
+ sink.mark(":n");
257
+ sink.text("null");
258
+ return;
259
+ }
260
+ if (part.k === "i") {
261
+ writeIdent(part.n, table, sink, alias, place);
262
+ return;
263
+ }
264
+ if (part.k === "c") {
265
+ if (ref === undefined) {
266
+ throw new OkmError("OKM1125", "fragment.column belongs on a where field.", {
267
+ fix: { summary: "Use ident(name) in select, or put fragment.column on a where field." }
268
+ });
269
+ }
270
+ sink.mark(":c");
271
+ sink.text(ref);
272
+ return;
273
+ }
274
+ writeFragment(part.parts, sink, alias, ref, table, place);
275
+ }
276
+ function writeIdent(name, table, sink, alias, place) {
277
+ const column = table.columns.get(name);
278
+ if (column === undefined) {
279
+ throwNamed("OKM1120", name, table.names, `Field ${name} is not on ${table.model.name}. Accepted names: ${list(table.names)}.`);
280
+ }
281
+ if (place === "select") {
282
+ if (column.hidden) {
283
+ fail2("OKM1120", `Field ${column.field} is hidden.`, "A select fragment cannot name a hidden field.");
284
+ }
285
+ } else {
286
+ refuseHidden(column);
287
+ }
288
+ sink.mark(`:i:${String(column.field.length)}:${column.field}`);
289
+ sink.text(alias);
290
+ sink.text(".");
291
+ sink.text(quote(column.sql));
292
+ }
293
+
294
+ export { readSelectFragment, emitSqlValue, emitSqlSelect, decodeSqlNumber };
@@ -0,0 +1,6 @@
1
+ import {
2
+ mapPostgresError2
3
+ } from "./hfq3n3bd.js";
4
+ export {
5
+ mapPostgresError2 as mapPostgresError
6
+ };
@@ -0,0 +1,177 @@
1
+ import {
2
+ build3
3
+ } from "./829fjxyw.js";
4
+ import {
5
+ attachHttp
6
+ } from "./jr2nmr81.js";
7
+ import {
8
+ OkmError,
9
+ rejectKeys,
10
+ readLimit,
11
+ indexes,
12
+ isRecord,
13
+ fail2
14
+ } from "./hfq3n3bd.js";
15
+
16
+ // src/runtime/read-stream.ts
17
+ var DEFAULT_BATCH = 1000;
18
+ var MAX_BATCH = 2147483647;
19
+ async function* streamRows(session, call, mods, view, batchOptions, log) {
20
+ const stop = (error) => {
21
+ throw log(attachHttp(session.http, error));
22
+ };
23
+ let batch;
24
+ let total;
25
+ let signal;
26
+ let deadline;
27
+ try {
28
+ if (call.op !== "find") {
29
+ fail2("OKM1120", `stream() reads rows from find(). ${call.op}() returns one value.`);
30
+ }
31
+ if (call.search !== undefined) {
32
+ 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.");
33
+ }
34
+ if (Array.isArray(call.select) && call.select.some((item) => typeof item !== "string")) {
35
+ fail2("OKM1120", "stream() does not take a window. The keyset cursor has no window value to encode.");
36
+ }
37
+ const indexed = indexes(session.schema).get(call.table);
38
+ if (indexed === undefined)
39
+ fail2("OKM1120", `Table ${call.table} is not in the schema.`);
40
+ if (indexed.model.primary.length === 0) {
41
+ 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.");
42
+ }
43
+ batch = readBatch(batchOptions);
44
+ if (mods.all !== undefined && mods.all.length === 0) {
45
+ fail2("OKM1101", "find() .all needs a reason.");
46
+ }
47
+ if (call.limit !== undefined)
48
+ total = readLimit(call.limit);
49
+ else if (mods.all === undefined) {
50
+ fail2("OKM1101", "find() needs a limit, or .all(reason) when the full set is intentional.");
51
+ }
52
+ const timeout = call.timeout;
53
+ if (timeout !== undefined && (typeof timeout !== "number" || !(timeout > 0))) {
54
+ fail2("OKM1121", "timeout must be a number of milliseconds above 0.");
55
+ }
56
+ signal = signalOf(call.signal);
57
+ deadline = timeout === undefined ? undefined : Date.now() + timeout;
58
+ } catch (error) {
59
+ if (error instanceof OkmError)
60
+ stop(error);
61
+ throw error;
62
+ }
63
+ if (total === 0)
64
+ return;
65
+ const cancel = session.pool.capabilities.cancel === true;
66
+ const statement = session.timeouts?.statement;
67
+ const pin = {};
68
+ let after;
69
+ let left = total;
70
+ let pending;
71
+ try {
72
+ while (left === undefined || left > 0) {
73
+ if (signal?.aborted === true) {
74
+ stop(new OkmError("cancelled", "The stream was cancelled.", { kind: "cancelled" }));
75
+ }
76
+ const remaining = timeLeft(deadline);
77
+ const size = left === undefined ? batch : Math.min(batch, left);
78
+ const handle = build3(session, call.table, pageFields(call, size, after, signal, batchTimeout(remaining, cancel, statement)), mods, view, pin);
79
+ pending = handle;
80
+ const value = asPage(await handle);
81
+ pending = undefined;
82
+ for (const row of value.items) {
83
+ yield row;
84
+ if (left !== undefined) {
85
+ left -= 1;
86
+ if (left === 0)
87
+ return;
88
+ }
89
+ }
90
+ if (value.next === null || value.items.length < size)
91
+ return;
92
+ after = value.next;
93
+ }
94
+ } catch (error) {
95
+ if (error instanceof OkmError && error.kind === "timeout" && error.message === "The stream timed out.") {
96
+ stop(error);
97
+ }
98
+ throw error;
99
+ } finally {
100
+ if (pending !== undefined) {
101
+ pending.then(() => {
102
+ return;
103
+ }, () => {
104
+ return;
105
+ });
106
+ }
107
+ }
108
+ }
109
+ function readBatch(options) {
110
+ if (options === undefined)
111
+ return DEFAULT_BATCH;
112
+ if (!isRecord(options))
113
+ fail2("OKM1120", "stream() takes an object. Pass { batch }.");
114
+ rejectKeys(options, ["batch"], "stream");
115
+ const batch = options.batch;
116
+ if (batch === undefined)
117
+ return DEFAULT_BATCH;
118
+ if (typeof batch !== "number" || !Number.isInteger(batch) || batch < 1 || batch > MAX_BATCH) {
119
+ fail2("OKM1121", "stream() batch must be an integer from 1 to 2147483647.");
120
+ }
121
+ return batch;
122
+ }
123
+ function pageFields(call, size, after, signal, timeout) {
124
+ const out = { limit: size };
125
+ if (call.where !== undefined)
126
+ out.where = call.where;
127
+ if (call.select !== undefined)
128
+ out.select = call.select;
129
+ if (call.orderBy !== undefined)
130
+ out.orderBy = call.orderBy;
131
+ if (call.include !== undefined)
132
+ out.include = call.include;
133
+ if (call.route !== undefined)
134
+ out.route = call.route;
135
+ if (after !== undefined)
136
+ out.after = after;
137
+ if (signal !== undefined)
138
+ out.signal = signal;
139
+ if (timeout !== undefined)
140
+ out.timeout = timeout;
141
+ return out;
142
+ }
143
+ function timeLeft(deadline) {
144
+ if (deadline === undefined)
145
+ return;
146
+ const left = deadline - Date.now();
147
+ if (left <= 0)
148
+ throw new OkmError("timeout", "The stream timed out.", { kind: "timeout" });
149
+ return left;
150
+ }
151
+ function batchTimeout(remaining, cancel, statement) {
152
+ if (!cancel || remaining === undefined)
153
+ return;
154
+ if (statement !== undefined && statement > 0 && statement < remaining)
155
+ return statement;
156
+ return remaining;
157
+ }
158
+ function signalOf(value) {
159
+ if (typeof value === "object" && value !== null && "aborted" in value) {
160
+ return value;
161
+ }
162
+ return;
163
+ }
164
+ function asPage(value) {
165
+ if (typeof value !== "object" || value === null || !("items" in value) || !("next" in value)) {
166
+ throw new OkmError("OKM1120", "stream() did not get a page of rows.");
167
+ }
168
+ const items = value.items;
169
+ const next = value.next;
170
+ if (!Array.isArray(items) || next !== null && typeof next !== "string") {
171
+ throw new OkmError("OKM1120", "stream() did not get a page of rows.");
172
+ }
173
+ return { items, next };
174
+ }
175
+ export {
176
+ streamRows
177
+ };