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,10 @@
1
1
  import {
2
2
  isConnectionSqlstate,
3
3
  isConnectionErrno
4
- } from "./gwn6v667.js";
4
+ } from "./cnkky55j.js";
5
5
  import {
6
6
  createClient
7
- } from "./j60w0dvr.js";
7
+ } from "./bah9nbg3.js";
8
8
  import {
9
9
  OkmError2
10
10
  } from "./52szzakm.js";
@@ -96,8 +96,7 @@ async function connectTopology(target, options, open) {
96
96
  const client = createClient(options.schema, router, { ownsPool: true, ...shared });
97
97
  const routed = client;
98
98
  routed.using = (route) => derive(options.schema, handle, router, shared, route);
99
- handles.set(routed, handle);
100
- return routed;
99
+ return instrument(routed, handle);
101
100
  } catch (error) {
102
101
  for (const endpoint of opened) {
103
102
  if (endpoint.timer !== undefined)
@@ -108,6 +107,105 @@ async function connectTopology(target, options, open) {
108
107
  throw error;
109
108
  }
110
109
  }
110
+ function instrument(client, handle, forced) {
111
+ const proxy = new Proxy(client, {
112
+ get(target, key, receiver) {
113
+ if (key === "using")
114
+ return Reflect.get(target, key, receiver);
115
+ const value = Reflect.get(target, key, receiver);
116
+ if (typeof value !== "function")
117
+ return retarget(value, handle, forced);
118
+ if (key === "tx") {
119
+ return (...args) => {
120
+ const index = typeof args[0] === "function" ? 0 : 1;
121
+ const fn = args[index];
122
+ if (typeof fn === "function") {
123
+ args[index] = (inner, ...rest) => fn(typeof inner === "object" && inner !== null ? instrument(inner, handle, "primary") : inner, ...rest);
124
+ }
125
+ return value.apply(target, args);
126
+ };
127
+ }
128
+ return (...args) => retarget(value.apply(target, args), handle, forced);
129
+ }
130
+ });
131
+ handles.set(proxy, handle);
132
+ return proxy;
133
+ }
134
+ function wrapTable(table, handle, forced) {
135
+ return new Proxy(table, {
136
+ get(target, key, receiver) {
137
+ const value = Reflect.get(target, key, receiver);
138
+ if (typeof value !== "function")
139
+ return value;
140
+ return (...args) => {
141
+ const route = forced ?? readCallRoute(args[0]);
142
+ return retarget(value.apply(target, args), handle, route);
143
+ };
144
+ }
145
+ });
146
+ }
147
+ function retarget(value, handle, route) {
148
+ if (typeof value !== "object" || value === null)
149
+ return value;
150
+ if (typeof Reflect.get(value, "inspect") === "function" && typeof Reflect.get(value, "then") === "function") {
151
+ return showReason(value, handle, route);
152
+ }
153
+ if (typeof Reflect.get(value, "find") === "function")
154
+ return wrapTable(value, handle, route);
155
+ if (typeof Reflect.get(value, "table") === "function")
156
+ return instrument(value, handle, route);
157
+ return value;
158
+ }
159
+ function showReason(query, handle, route) {
160
+ return new Proxy(query, {
161
+ get(target, key, receiver) {
162
+ const value = Reflect.get(target, key, receiver);
163
+ if (key === "inspect" && typeof value === "function") {
164
+ const inspect = value;
165
+ return () => Promise.resolve(inspect.call(target)).then(async (view) => {
166
+ if (typeof view !== "object" || view === null)
167
+ return view;
168
+ const sql = Reflect.get(view, "sql");
169
+ const text = typeof sql === "object" && sql !== null && typeof Reflect.get(sql, "text") === "string" ? Reflect.get(sql, "text") : "";
170
+ const choice = await preview(handle, text, route);
171
+ return {
172
+ ...view,
173
+ routing: {
174
+ endpoint: choice.endpoint.name,
175
+ role: choice.endpoint.role,
176
+ reason: choice.reason
177
+ }
178
+ };
179
+ });
180
+ }
181
+ if ((key === "all" || key === "required") && typeof value === "function") {
182
+ const method = value;
183
+ return (...args) => retarget(method.apply(target, args), handle, route);
184
+ }
185
+ return typeof value === "function" ? value.bind(target) : value;
186
+ }
187
+ });
188
+ }
189
+ function readCallRoute(value) {
190
+ if (typeof value !== "object" || value === null)
191
+ return;
192
+ const route = Reflect.get(value, "route");
193
+ return route === "primary" || route === "replica" ? route : undefined;
194
+ }
195
+ async function preview(handle, text, route) {
196
+ const currents = handle.endpoints.map((endpoint) => endpoint.current);
197
+ const rrLast = handle.rrLast;
198
+ try {
199
+ return await choose(handle, text, route);
200
+ } finally {
201
+ for (let index = 0;index < handle.endpoints.length; index += 1) {
202
+ const endpoint = handle.endpoints[index];
203
+ if (endpoint !== undefined)
204
+ endpoint.current = currents[index] ?? 0;
205
+ }
206
+ handle.rrLast = rrLast;
207
+ }
208
+ }
111
209
  function refuseRouting(routing) {
112
210
  if (routing === undefined)
113
211
  return;
@@ -506,7 +604,7 @@ function routePool(handle) {
506
604
  execute: async (text, params, options) => {
507
605
  if (handle.closed)
508
606
  closed();
509
- const choice = await choose(handle, text, options?.route);
607
+ const choice = await resolve(handle, text, options);
510
608
  const result = await attempt(handle, choice, (endpoint) => endpoint.pool.execute(text, params, strip(options)));
511
609
  if (choice.op === "write")
512
610
  await noteWrite(handle, primary.pool);
@@ -574,7 +672,7 @@ function derive(schema, handle, router, shared, route) {
574
672
  const name = readRoute(route);
575
673
  const client = createClient(schema, force(handle, router, name), { ...shared, ownsPool: false });
576
674
  delete client.close;
577
- return client;
675
+ return instrument(client, handle, name);
578
676
  }
579
677
  function force(handle, router, route) {
580
678
  const pool = {
@@ -628,7 +726,7 @@ function force(handle, router, route) {
628
726
  async function readOnlyUnit(handle, text, params, prelude, options) {
629
727
  if (handle.closed)
630
728
  closed();
631
- const choice = await choose(handle, text, options?.route);
729
+ const choice = await resolve(handle, text, options);
632
730
  return attempt(handle, choice, async (endpoint) => {
633
731
  if (endpoint.pool.reserve === undefined)
634
732
  replicaReserve();
@@ -764,7 +862,7 @@ async function attempt(handle, choice, run) {
764
862
  await callGuard(choice.endpoint);
765
863
  return await tracked(choice.endpoint, choice.op === "read", () => run(choice.endpoint));
766
864
  } catch (error) {
767
- if (choice.op !== "read" || !isUnreachable(error))
865
+ if (choice.pinned === true || choice.op !== "read" || !isUnreachable(error))
768
866
  throw error;
769
867
  choice.endpoint.generation += 1;
770
868
  const next = await retryOf(handle, choice);
@@ -817,9 +915,7 @@ function unreachableNode(node) {
817
915
  return true;
818
916
  }
819
917
  }
820
- if (!(node instanceof Error))
821
- return false;
822
- return node.message.includes("ECONNREFUSED") || node.message.includes("ECONNRESET") || node.message.includes("CONNECTION_CLOSED") || node.message.includes("terminating connection") || node.message.includes("The pool is closed");
918
+ return false;
823
919
  }
824
920
  async function tracked(endpoint, time, run) {
825
921
  const replica = endpoint.role === "replica";
@@ -1109,13 +1205,45 @@ function noteFailure(endpoint) {
1109
1205
  endpoint.circuit = "open";
1110
1206
  }
1111
1207
  function strip(options) {
1112
- if (options === undefined || options.route === undefined)
1208
+ if (options === undefined)
1209
+ return;
1210
+ if (!("route" in options) && !("pin" in options))
1113
1211
  return options;
1114
- const { route: _route, ...rest } = options;
1212
+ const bag = options;
1213
+ const { route: _route, pin: _pin, ...rest } = bag;
1115
1214
  if (rest.signal === undefined && rest.timeout === undefined)
1116
1215
  return;
1117
1216
  return rest;
1118
1217
  }
1218
+ async function resolve(handle, text, options) {
1219
+ const pin = taken(options);
1220
+ if (pin?.name !== undefined)
1221
+ return held(handle, pin.name);
1222
+ const choice = await choose(handle, text, options?.route);
1223
+ if (pin !== undefined && choice.op === "read") {
1224
+ pin.name = choice.endpoint.name;
1225
+ return { ...choice, pinned: true };
1226
+ }
1227
+ return choice;
1228
+ }
1229
+ function taken(options) {
1230
+ if (options === undefined || !("pin" in options))
1231
+ return;
1232
+ const pin = options.pin;
1233
+ if (typeof pin !== "object" || pin === null)
1234
+ return;
1235
+ return pin;
1236
+ }
1237
+ function held(handle, name) {
1238
+ for (const endpoint of handle.endpoints) {
1239
+ if (endpoint.name !== name)
1240
+ continue;
1241
+ return { endpoint, op: "read", reason: `stream:${name}`, pinned: true };
1242
+ }
1243
+ throw new OkmError2("OKM1120", `stream() stays on ${name}. That target is not in this topology.`, {
1244
+ fix: { summary: "The first batch chose this target. Later batches do not move." }
1245
+ });
1246
+ }
1119
1247
  async function noteWrite(handle, runner) {
1120
1248
  if (!tracks(handle))
1121
1249
  return;
@@ -1,18 +1,6 @@
1
1
  import {
2
2
  policyObject
3
3
  } from "./bdk4sty6.js";
4
- import {
5
- functionObject,
6
- triggerObject
7
- } from "./y34a8714.js";
8
- import {
9
- viewObject,
10
- materializedViewObject,
11
- materializedViewIndex
12
- } from "./zy8r7qvz.js";
13
- import {
14
- extensionObject
15
- } from "./r3j8s5y0.js";
16
4
  import {
17
5
  dependencyOrder,
18
6
  catalog,
@@ -20,10 +8,14 @@ import {
20
8
  domainType,
21
9
  isDomain
22
10
  } from "./ywspezzx.js";
11
+ import {
12
+ extensionObject
13
+ } from "./r3j8s5y0.js";
23
14
  import {
24
15
  canonicalJson,
25
16
  staticNamespace2,
26
17
  templateNamespace2,
18
+ assertNamespace2,
27
19
  identityKey2,
28
20
  identityLabel2,
29
21
  sameRef2,
@@ -48,7 +40,8 @@ import {
48
40
  import {
49
41
  sha2562,
50
42
  assertIdentifier2,
51
- fitIdentifier2
43
+ fitIdentifier2,
44
+ deterministicName2
52
45
  } from "./nfzc5z0w.js";
53
46
  import {
54
47
  catalogError2
@@ -164,6 +157,242 @@ function defaultPrivilegeObject(input) {
164
157
  };
165
158
  }
166
159
 
160
+ // src/contracts/catalog/routine.ts
161
+ var VOLATILITY = new Set(["volatile", "stable", "immutable"]);
162
+ var SECURITY = new Set(["invoker", "definer"]);
163
+ var TIMING = new Set(["before", "after", "instead"]);
164
+ var LEVEL = new Set(["row", "statement"]);
165
+ var EVENT_ORDER = ["insert", "update", "delete", "truncate"];
166
+ var TYPE_TEXT = /^[A-Za-z][A-Za-z0-9_ ]*(\(\d+(,\s*\d+)?\))?(\[\])*$/;
167
+ function functionObject(input) {
168
+ assertIdentifier2(input.name, "function");
169
+ const namespace = input.namespace ?? staticNamespace2("public");
170
+ assertNamespace2(namespace);
171
+ assertProvenance(input.provenance);
172
+ if (input.language !== "sql" && input.language !== "plpgsql") {
173
+ catalogError2("OKM1020", `Function ${input.name} language must be sql or plpgsql.`);
174
+ }
175
+ const volatility = input.volatility ?? "volatile";
176
+ if (!VOLATILITY.has(volatility)) {
177
+ catalogError2("OKM1020", `Function ${input.name} volatility ${volatility} is not supported.`);
178
+ }
179
+ const security = input.security ?? "invoker";
180
+ if (!SECURITY.has(security)) {
181
+ catalogError2("OKM1020", `Function ${input.name} security ${security} is not supported.`);
182
+ }
183
+ assertTypeName(input.returns, `function ${input.name} return type`);
184
+ const args = (input.arguments ?? []).map((argument) => readArgument(input.name, argument));
185
+ if (input.body.length === 0) {
186
+ catalogError2("OKM1020", `Function ${input.name} needs a body.`);
187
+ }
188
+ const atomic = input.atomic === true;
189
+ if (atomic && input.language !== "sql") {
190
+ catalogError2("OKM1020", `Function ${input.name} BEGIN ATOMIC needs language sql.`);
191
+ }
192
+ if (input.searchPath !== undefined && input.searchPath.trim().length === 0) {
193
+ catalogError2("OKM1020", `Function ${input.name} search_path is empty.`);
194
+ }
195
+ const definition = {
196
+ arguments: args,
197
+ returns: input.returns,
198
+ language: input.language,
199
+ volatility,
200
+ security,
201
+ body: input.body,
202
+ ...input.searchPath !== undefined ? { searchPath: input.searchPath } : {},
203
+ ...atomic ? { atomic: true } : {}
204
+ };
205
+ return {
206
+ kind: "function",
207
+ identity: {
208
+ kind: "function",
209
+ namespace,
210
+ name: input.name,
211
+ argTypes: args.map((arg) => arg.type)
212
+ },
213
+ owner: input.owner ?? "managed",
214
+ definition,
215
+ dependencies: normaliseEdges(input.dependencies ?? []),
216
+ provenance: input.provenance
217
+ };
218
+ }
219
+ function triggerObject(input) {
220
+ assertIdentifier2(input.name, "trigger");
221
+ assertNamespace2(input.parent.namespace);
222
+ assertIdentifier2(input.parent.name, "trigger table");
223
+ assertProvenance(input.provenance);
224
+ if (!TIMING.has(input.timing)) {
225
+ catalogError2("OKM1020", `Trigger ${input.name} timing ${input.timing} is not supported.`);
226
+ }
227
+ if (!LEVEL.has(input.level)) {
228
+ catalogError2("OKM1020", `Trigger ${input.name} level ${input.level} is not supported.`);
229
+ }
230
+ const events = EVENT_ORDER.filter((event) => input.events.includes(event));
231
+ if (events.length === 0) {
232
+ catalogError2("OKM1020", `Trigger ${input.name} needs an event.`);
233
+ }
234
+ const updateOf = input.updateOf === undefined ? undefined : [...input.updateOf];
235
+ if (updateOf !== undefined) {
236
+ if (!events.includes("update")) {
237
+ catalogError2("OKM1020", `Trigger ${input.name} lists UPDATE OF columns without an update event.`);
238
+ }
239
+ if (input.level !== "row") {
240
+ catalogError2("OKM1020", `Trigger ${input.name} UPDATE OF columns need level row.`);
241
+ }
242
+ for (const column of updateOf)
243
+ assertIdentifier2(column, "trigger column");
244
+ }
245
+ assertIdentifier2(input.calls.name, "trigger function");
246
+ assertNamespace2(input.calls.namespace);
247
+ for (const typeName of input.calls.argTypes)
248
+ assertTypeName(typeName, `trigger ${input.name}`);
249
+ const edges = [
250
+ { kind: "table", namespace: input.parent.namespace, name: input.parent.name },
251
+ {
252
+ kind: "function",
253
+ namespace: input.calls.namespace,
254
+ name: input.calls.name,
255
+ argTypes: input.calls.argTypes
256
+ },
257
+ ...(updateOf ?? []).map((column) => ({
258
+ kind: "column",
259
+ parent: input.parent,
260
+ name: column
261
+ })),
262
+ ...input.dependencies ?? []
263
+ ];
264
+ const definition = {
265
+ timing: input.timing,
266
+ events,
267
+ level: input.level,
268
+ calls: {
269
+ namespace: input.calls.namespace,
270
+ name: input.calls.name,
271
+ argTypes: [...input.calls.argTypes]
272
+ },
273
+ ...updateOf !== undefined && updateOf.length > 0 ? { updateOf } : {},
274
+ ...input.when !== undefined && input.when.length > 0 ? { when: input.when } : {}
275
+ };
276
+ return {
277
+ kind: "trigger",
278
+ identity: { kind: "trigger", parent: input.parent, name: input.name },
279
+ owner: input.owner ?? "managed",
280
+ definition,
281
+ dependencies: normaliseEdges(edges),
282
+ provenance: input.provenance
283
+ };
284
+ }
285
+ function readArgument(fn, argument) {
286
+ assertIdentifier2(argument.name, `function ${fn} argument`);
287
+ assertTypeName(argument.type, `function ${fn} argument ${argument.name}`);
288
+ return { name: argument.name, type: argument.type };
289
+ }
290
+ function assertTypeName(typeName, role) {
291
+ if (!TYPE_TEXT.test(typeName)) {
292
+ catalogError2("OKM1020", `${role} ${typeName} is not a type name.`);
293
+ }
294
+ }
295
+
296
+ // src/contracts/catalog/view.ts
297
+ function normaliseViewQuery(query) {
298
+ return query.trim().replace(/;\s*$/, "");
299
+ }
300
+ function viewObject(input) {
301
+ const namespace = input.namespace ?? staticNamespace2("public");
302
+ const columns = readColumns(input.name, input.columns);
303
+ const query = readQuery(input.name, input.query);
304
+ assertNamespace2(namespace);
305
+ assertProvenance(input.provenance);
306
+ const definition = {
307
+ columns,
308
+ query,
309
+ ...input.securityInvoker === true ? { securityInvoker: true } : {}
310
+ };
311
+ return {
312
+ kind: "view",
313
+ identity: { kind: "view", namespace, name: input.name },
314
+ owner: input.owner ?? "managed",
315
+ definition,
316
+ dependencies: normaliseEdges(input.dependencies ?? []),
317
+ provenance: input.provenance
318
+ };
319
+ }
320
+ function materializedViewObject(input) {
321
+ const namespace = input.namespace ?? staticNamespace2("public");
322
+ const columns = readColumns(input.name, input.columns);
323
+ const query = readQuery(input.name, input.query);
324
+ assertNamespace2(namespace);
325
+ assertProvenance(input.provenance);
326
+ const definition = {
327
+ columns,
328
+ query,
329
+ ...input.refresh === "concurrently" ? { refresh: "concurrently" } : {}
330
+ };
331
+ return {
332
+ kind: "materializedView",
333
+ identity: { kind: "materializedView", namespace, name: input.name },
334
+ owner: input.owner ?? "managed",
335
+ definition,
336
+ dependencies: normaliseEdges(input.dependencies ?? []),
337
+ provenance: input.provenance
338
+ };
339
+ }
340
+ function materializedViewIndex(input) {
341
+ assertNamespace2(input.parent.namespace);
342
+ assertIdentifier2(input.parent.name, "index parent");
343
+ assertProvenance(input.provenance);
344
+ const columns = [...input.columns];
345
+ for (const name of columns)
346
+ assertIdentifier2(name, "index column");
347
+ if (columns.length === 0 && input.expression === undefined) {
348
+ catalogError2("OKM1020", `Index on ${input.parent.name} needs a column or an expression.`);
349
+ }
350
+ const nameKey = input.nameKey ?? (columns.length > 0 ? columns.join("_") : "expr");
351
+ const name = input.name === undefined ? deterministicName2({ parent: input.parent.name, purpose: "index", nameKey }) : fitIdentifier2(input.name);
352
+ const parentView = {
353
+ kind: "materializedView",
354
+ namespace: input.parent.namespace,
355
+ name: input.parent.name
356
+ };
357
+ return {
358
+ kind: "index",
359
+ identity: { kind: "index", parent: input.parent, name },
360
+ owner: input.owner ?? "managed",
361
+ definition: {
362
+ columns,
363
+ unique: input.unique === true,
364
+ nameKey,
365
+ ...input.expression !== undefined ? { expression: input.expression } : {},
366
+ ...input.predicate !== undefined ? { predicate: input.predicate } : {}
367
+ },
368
+ dependencies: normaliseEdges(input.dependencies ?? [parentView]),
369
+ provenance: input.provenance
370
+ };
371
+ }
372
+ function readColumns(view, columns) {
373
+ assertIdentifier2(view, "view");
374
+ if (columns.length === 0)
375
+ catalogError2("OKM1020", `View ${view} needs a column.`);
376
+ const seen = new Set;
377
+ return columns.map((column) => {
378
+ assertIdentifier2(column.name, `view ${view} column`);
379
+ if (seen.has(column.name)) {
380
+ catalogError2("OKM1020", `View ${view} lists column ${column.name} twice.`);
381
+ }
382
+ seen.add(column.name);
383
+ if (column.dataType.length === 0 || /[;'"\\]/.test(column.dataType)) {
384
+ catalogError2("OKM1020", `View ${view} column ${column.name} type is not a type name.`);
385
+ }
386
+ return { name: column.name, dataType: column.dataType };
387
+ });
388
+ }
389
+ function readQuery(view, query) {
390
+ const text = normaliseViewQuery(query);
391
+ if (text.length === 0)
392
+ catalogError2("OKM1020", `View ${view} needs a query.`);
393
+ return text;
394
+ }
395
+
167
396
  // src/contracts/catalog/document.ts
168
397
  function creationOrder2(source) {
169
398
  return dependencyOrder(source.objects);
@@ -1565,4 +1794,4 @@ function assertNeverKind(value) {
1565
1794
  return catalogError2("OKM1020", `Unexpected kind ${JSON.stringify(value)}.`);
1566
1795
  }
1567
1796
 
1568
- export { roleObject, grantObject, defaultPrivilegeObject, creationOrder2, renameColumn2, renameTable2, serializeCatalog2, catalogHash2, startupCatalog2, startupHash2, loadTrustedCatalog2, catalogFileText2, parseCatalog2 };
1797
+ export { roleObject, grantObject, defaultPrivilegeObject, functionObject, triggerObject, normaliseViewQuery, viewObject, materializedViewObject, materializedViewIndex, creationOrder2, renameColumn2, renameTable2, serializeCatalog2, catalogHash2, startupCatalog2, startupHash2, loadTrustedCatalog2, catalogFileText2, parseCatalog2 };
@@ -1,16 +1,14 @@
1
1
  import {
2
- quoteIdent2,
3
2
  noticeBuffer,
4
- WireSession,
5
- cursorStream
6
- } from "./4az561fn.js";
3
+ WireSession
4
+ } from "./mtw6jz76.js";
7
5
  import {
8
6
  NODE_POSTGRES_CAPABILITIES,
9
7
  driverErrors,
10
8
  rejectClosed,
11
9
  mapFailure,
12
10
  runCall
13
- } from "./cgbamx3e.js";
11
+ } from "./ae4j5f42.js";
14
12
 
15
13
  // src/adapters/pg/nodepostgres.ts
16
14
  import net from "node:net";
@@ -57,7 +55,7 @@ function open4(config) {
57
55
  counters.busy = Math.max(0, counters.busy - 1);
58
56
  }, inflight);
59
57
  const held = { pool, acquireMs, counters, notices, named, isClosed };
60
- const checkout = () => closed ? rejectClosed() : import("./zm0mafk4.js").then((mod) => mod.checkout(held));
58
+ const checkout = () => closed ? rejectClosed() : import("./mfedzr74.js").then((mod) => mod.checkout(held));
61
59
  return {
62
60
  capabilities: NODE_POSTGRES_CAPABILITIES,
63
61
  execute(text, params, options) {
@@ -89,10 +87,15 @@ function open4(config) {
89
87
  },
90
88
  reserve: () => checkout(),
91
89
  stream(text, params) {
92
- return stream(text, params);
90
+ return async function* () {
91
+ const { streamRows } = await import("./mfedzr74.js");
92
+ yield* streamRows(checkout, text, params);
93
+ }();
93
94
  },
94
95
  listen(channel, onNotify) {
95
- return listen(channel, onNotify);
96
+ if (closed)
97
+ return rejectClosed();
98
+ return import("./mfedzr74.js").then((mod) => mod.listen(held, channel, onNotify));
96
99
  },
97
100
  cancel() {
98
101
  for (const pending of inflight)
@@ -104,54 +107,6 @@ function open4(config) {
104
107
  await pool.end();
105
108
  }
106
109
  };
107
- function stream(text, params) {
108
- return async function* () {
109
- const connection = await checkout();
110
- try {
111
- yield* cursorStream((statement, values) => connection.execute(statement, values), text, params);
112
- } finally {
113
- await connection.release();
114
- }
115
- }();
116
- }
117
- async function listen(channel, onNotify) {
118
- if (closed)
119
- return rejectClosed();
120
- const client = await connectClient(pool, acquireMs, counters);
121
- counters.reserved += 1;
122
- const quoted = quoteIdent2(channel);
123
- const onNotification = (message) => {
124
- if (message.channel === channel)
125
- onNotify(message.payload ?? "");
126
- };
127
- client.on("notification", onNotification);
128
- let released = false;
129
- const release = (dead) => {
130
- if (released)
131
- return;
132
- released = true;
133
- client.removeListener("notification", onNotification);
134
- client.removeListener("error", release);
135
- counters.reserved = Math.max(0, counters.reserved - 1);
136
- client.release(Boolean(dead));
137
- };
138
- armDriverError(client, release);
139
- try {
140
- await client.query(`LISTEN ${quoted}`);
141
- } catch (error) {
142
- release(false);
143
- throw error;
144
- }
145
- return async () => {
146
- if (released)
147
- return;
148
- try {
149
- await client.query(`UNLISTEN ${quoted}`);
150
- } finally {
151
- release(false);
152
- }
153
- };
154
- }
155
110
  }
156
111
  function send(runner, text, params, named, client) {
157
112
  const query = {
@@ -236,4 +191,4 @@ function statsOf(counters, size) {
236
191
  };
237
192
  }
238
193
 
239
- export { open4, send, connectClient };
194
+ export { armDriverError, open4, send, connectClient };
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  catalogFileText2
3
- } from "./qq3899ht.js";
3
+ } from "./12z7aznj.js";
4
4
  import {
5
5
  OkmError2
6
6
  } from "./52szzakm.js";
@@ -14,7 +14,7 @@ async function assertCompatible(pool, schema, recordedHash, source, options) {
14
14
  if (code.artifact !== undefined) {
15
15
  const artifact = code.artifact;
16
16
  await loadOnce(`text\x00${artifact.hash}`, async () => {
17
- const { loadTrustedCatalog } = await import("./dk8v79p3.js");
17
+ const { loadTrustedCatalog } = await import("./d5nmj7vv.js");
18
18
  loadTrustedCatalog(artifact.text, artifact.hash);
19
19
  });
20
20
  } else if (code.fromFile) {
@@ -28,7 +28,7 @@ async function assertCompatible(pool, schema, recordedHash, source, options) {
28
28
  }
29
29
  });
30
30
  }
31
- const { loadTrustedCatalog } = await import("./dk8v79p3.js");
31
+ const { loadTrustedCatalog } = await import("./d5nmj7vv.js");
32
32
  loadTrustedCatalog(text, code.hash);
33
33
  });
34
34
  }
@@ -42,7 +42,7 @@ async function codeHash(schema, source) {
42
42
  const listed = await readHashFile(source.catalogDir);
43
43
  if (listed !== undefined)
44
44
  return { hash: listed, fromFile: true };
45
- const { catalogHash } = await import("./dk8v79p3.js");
45
+ const { catalogHash } = await import("./d5nmj7vv.js");
46
46
  return { hash: catalogHash(schemaCatalog(schema)), fromFile: false };
47
47
  }
48
48
  function schemaCatalog(schema) {
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  open2
3
- } from "./wkme6tep.js";
4
- import"./qq3899ht.js";
3
+ } from "./35q0n9tz.js";
4
+ import"./12z7aznj.js";
5
5
  import"./n442h54c.js";
6
6
  import {
7
7
  CURRENT,
@@ -14,21 +14,21 @@ import {
14
14
  import {
15
15
  expandProvisioned,
16
16
  provisionHistoryIsKnown
17
- } from "./p248q79y.js";
18
- import"./knmes2kh.js";
19
- import"./vjpnzwjz.js";
17
+ } from "./mnvjvqyp.js";
18
+ import"./kj9d8maq.js";
19
+ import"./p3xbdcvt.js";
20
20
  import {
21
21
  projectPath,
22
22
  loadMigrations,
23
23
  loadConfig
24
- } from "./t3g0k88x.js";
24
+ } from "./f4jy1j7x.js";
25
25
  import {
26
26
  sessionFailure,
27
27
  finishQuiet,
28
28
  assertTargetPolicy,
29
29
  listTargets
30
- } from "./ttmg09vd.js";
31
- import"./9s0ypp87.js";
30
+ } from "./dpc2r7rb.js";
31
+ import"./sjk86yrx.js";
32
32
  import {
33
33
  OkmError2
34
34
  } from "./52szzakm.js";