okmodel 0.7.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 (195) hide show
  1. package/dist/adapters/capabilities.d.ts +32 -0
  2. package/dist/adapters/failure.d.ts +1 -1
  3. package/dist/adapters/pg/bun-sql.d.ts +53 -0
  4. package/dist/adapters/pg/bunsql-extra.d.ts +1 -1
  5. package/dist/adapters/pg/bunsql.d.ts +1 -1
  6. package/dist/adapters/pg/neon-extra.d.ts +18 -0
  7. package/dist/adapters/pg/neon.d.ts +104 -0
  8. package/dist/adapters/pg/no-inflight-timeout.d.ts +1 -1
  9. package/dist/contracts/error.d.ts +1 -1
  10. package/dist/dialects/pg/fn/index.d.ts +39 -1
  11. package/dist/dialects/pg/fn/index.js +118 -26
  12. package/dist/dialects/pg/index.d.ts +2 -0
  13. package/dist/dialects/pg/index.js +393 -18
  14. package/dist/dialects/pg/introspect.d.ts +11 -0
  15. package/dist/dialects/pg/operators.d.ts +1 -1
  16. package/dist/dialects/pg/ops/fragment.d.ts +119 -0
  17. package/dist/dialects/pg/ops/lane.d.ts +49 -0
  18. package/dist/dialects/pg/view/client.d.ts +14 -6
  19. package/dist/dialects/pg/view/index.d.ts +73 -1
  20. package/dist/dialects/pg/view/index.js +156 -5
  21. package/dist/dialects/pg/view/scratch.d.ts +8 -5
  22. package/dist/runtime/otel.d.ts +105 -0
  23. package/dist/runtime/otel.js +97 -0
  24. package/dist/runtime/pg/bun.js +4 -4
  25. package/dist/runtime/pg/neon.d.ts +38 -0
  26. package/dist/runtime/pg/neon.js +64 -0
  27. package/dist/runtime/pg/pg.js +4 -4
  28. package/dist/runtime/pg/pglite.js +4 -4
  29. package/dist/runtime/pg/postgresjs.js +4 -4
  30. package/dist/runtime/plan.d.ts +1 -1
  31. package/dist/runtime/sql-fragment.d.ts +43 -0
  32. package/dist/runtime/tenancy/index.js +10 -10
  33. package/dist/runtime/topology.d.ts +1 -1
  34. package/dist/runtime/traits/index.js +15 -16
  35. package/dist/runtime/types.d.ts +15 -1
  36. package/dist/runtime/validate/index.js +4 -4
  37. package/dist/shared/{mtaszv5a.js → 0wrea3jq.js} +102 -4
  38. package/dist/shared/{qq3899ht.js → 12z7aznj.js} +243 -14
  39. package/dist/shared/{w79hsckn.js → 14x60fbf.js} +5 -5
  40. package/dist/shared/{ek2wt2dz.js → 16kvjebh.js} +4 -4
  41. package/dist/shared/{svncdeh5.js → 195pzzzz.js} +8 -8
  42. package/dist/shared/{gzaveazc.js → 1d9q9cv2.js} +29 -2
  43. package/dist/shared/{jvd0mqag.js → 3449rcae.js} +5 -5
  44. package/dist/shared/{14vx95q4.js → 35q0n9tz.js} +7 -7
  45. package/dist/shared/3bb7wsqt.js +99 -0
  46. package/dist/shared/{y6njgyda.js → 3t6kw83m.js} +2 -2
  47. package/dist/shared/{81ebdx9p.js → 4m6hskg8.js} +4 -4
  48. package/dist/shared/{xhfsfkgk.js → 4vbf83xx.js} +9 -2
  49. package/dist/shared/{bg3rym3g.js → 63f4ksbz.js} +2 -2
  50. package/dist/shared/{pt94kz7f.js → 6m78c8ss.js} +1 -1
  51. package/dist/shared/{5e3tf63j.js → 760ppr76.js} +14 -6
  52. package/dist/shared/{zm0ptry0.js → 7ghwamrf.js} +1 -1
  53. package/dist/shared/{xnxsed62.js → 9dr2qnx7.js} +14 -14
  54. package/dist/shared/{h15jx9bc.js → 9dvh1dr6.js} +1 -1
  55. package/dist/shared/{q5c6ngn3.js → a62rb2da.js} +1 -1
  56. package/dist/shared/{kb2sdhfb.js → ae4j5f42.js} +17 -1
  57. package/dist/shared/agsfb86s.js +233 -0
  58. package/dist/shared/{7k9grvf6.js → b6d6p0zc.js} +5 -5
  59. package/dist/shared/{tbrvgw6z.js → bah9nbg3.js} +17 -16
  60. package/dist/shared/{5fe3kbsh.js → bnby0esn.js} +1 -1
  61. package/dist/shared/bsek72fg.js +225 -0
  62. package/dist/shared/{1w508jbr.js → bxas03mq.js} +21 -17
  63. package/dist/shared/{dk8v79p3.js → d5nmj7vv.js} +1 -1
  64. package/dist/shared/{tsvww22q.js → dpc2r7rb.js} +7 -7
  65. package/dist/shared/{vb6qfyve.js → e8zyaqvj.js} +2 -2
  66. package/dist/shared/{hyh73qk6.js → ecn8mfdq.js} +5 -5
  67. package/dist/shared/{gs74dbj5.js → f4jy1j7x.js} +27 -14
  68. package/dist/shared/{ttsqgp4p.js → fjpnhqan.js} +2 -2
  69. package/dist/shared/{xjk7hnn5.js → g5yc06ka.js} +2 -2
  70. package/dist/shared/{w72kgg4q.js → hfqmpasr.js} +6 -6
  71. package/dist/shared/{kr0jzbw2.js → hm5f8jby.js} +7 -7
  72. package/dist/shared/{dkxgzaqd.js → hqpxf4a4.js} +1 -1
  73. package/dist/shared/{cn957f8d.js → jebgrkeq.js} +3 -3
  74. package/dist/shared/{wa07eas9.js → jp36k7dp.js} +11 -11
  75. package/dist/shared/jx2m62cs.js +96 -0
  76. package/dist/shared/{sfz1ksn6.js → k86n8vq3.js} +2 -2
  77. package/dist/shared/{67txcrcw.js → kgwp4jdd.js} +4 -4
  78. package/dist/shared/{8skf0cam.js → kj9d8maq.js} +1 -1
  79. package/dist/shared/{aytqh5y2.js → m4xff2a6.js} +2 -2
  80. package/dist/shared/{zp9fmend.js → mfedzr74.js} +4 -4
  81. package/dist/shared/{ax7qv55k.js → mkrn359n.js} +60 -11
  82. package/dist/shared/{m6p12kbr.js → mnvjvqyp.js} +10 -10
  83. package/dist/shared/{1qa0qajz.js → mtw6jz76.js} +1 -1
  84. package/dist/shared/{1est4ezg.js → n05z9nwz.js} +3 -3
  85. package/dist/shared/{pbp1ss85.js → n0jmpgj1.js} +2 -2
  86. package/dist/shared/{ktz42yez.js → n41w4fbz.js} +5 -5
  87. package/dist/shared/{n816peh1.js → n454kcnw.js} +1 -1
  88. package/dist/shared/n7kg8mmx.js +61 -0
  89. package/dist/shared/{g6evj1j3.js → needfe47.js} +1 -1
  90. package/dist/shared/{2shdbn8q.js → njga14v4.js} +9 -9
  91. package/dist/shared/{vqrjpcqn.js → nmnsgj7x.js} +1 -1
  92. package/dist/shared/{b5pftnn2.js → p3xbdcvt.js} +14 -18
  93. package/dist/shared/{te5as7z7.js → p5nh3r8q.js} +3 -3
  94. package/dist/shared/{p6qc5gcp.js → pan0370z.js} +6 -6
  95. package/dist/shared/{79dt02vc.js → q0cqt2sp.js} +1 -1
  96. package/dist/shared/{5v3xf80k.js → qd9hhd8f.js} +1 -1
  97. package/dist/shared/{bm22fvzr.js → qfw14tna.js} +2 -2
  98. package/dist/shared/{9s0ypp87.js → sjk86yrx.js} +1 -1
  99. package/dist/shared/{3v7t1yrv.js → th3be82n.js} +2 -2
  100. package/dist/shared/{ed0b8k4s.js → typ3vy7t.js} +1 -1
  101. package/dist/shared/{vczs8gd4.js → v1wad97q.js} +2 -2
  102. package/dist/shared/{9628a9xt.js → vk7rag46.js} +1 -1
  103. package/dist/shared/{ze4hxsmy.js → w9ejzaqf.js} +1 -1
  104. package/dist/shared/{bz84tkzm.js → wcrheftc.js} +2 -2
  105. package/dist/shared/{ab9gmbwy.js → wvdjznah.js} +5 -5
  106. package/dist/shared/{7mnme5tn.js → wz2a78mp.js} +1 -1
  107. package/dist/shared/{9y3mmmh1.js → xfhe73sb.js} +1 -1
  108. package/dist/shared/{je4axnm8.js → z581d7ny.js} +2 -2
  109. package/dist/shared/{x933k9a3.js → zbqbmmrp.js} +10 -10
  110. package/dist/shared/{rs73fca9.js → zgex00fd.js} +6 -6
  111. package/dist/shared/{jznfax0y.js → zq1he88x.js} +4 -4
  112. package/dist/tooling/cli.js +26 -13
  113. package/dist/tooling/driver-run.js +4971 -0
  114. package/dist/tooling/driver-test.d.ts +36 -0
  115. package/dist/tooling/migrate/commands.d.ts +2 -2
  116. package/dist/tooling/migrate/index.js +9 -9
  117. package/dist/tooling/migrate/project.d.ts +3 -0
  118. package/dist/tooling/shared/03k6res6.js +210 -0
  119. package/dist/tooling/shared/08kemxjd.js +10 -0
  120. package/dist/tooling/shared/2revefqt.js +209 -0
  121. package/dist/tooling/shared/2wtvc7rw.js +1425 -0
  122. package/dist/tooling/shared/345tbc2k.js +16 -0
  123. package/dist/tooling/shared/3m1hkg7z.js +344 -0
  124. package/dist/tooling/shared/3rrw5w8f.js +81 -0
  125. package/dist/tooling/shared/3tq8pnb8.js +131 -0
  126. package/dist/tooling/shared/417zdsk7.js +194 -0
  127. package/dist/tooling/shared/4d2w25vw.js +40 -0
  128. package/dist/tooling/shared/54tqkxja.js +51 -0
  129. package/dist/tooling/shared/5bvw2nev.js +12 -0
  130. package/dist/tooling/shared/6nyfyw70.js +231 -0
  131. package/dist/tooling/shared/72fyg4zv.js +160 -0
  132. package/dist/tooling/shared/733a1tq3.js +38 -0
  133. package/dist/tooling/shared/7ae7rjmn.js +20 -0
  134. package/dist/tooling/shared/7kms6fvj.js +20 -0
  135. package/dist/tooling/shared/8sqrsw3s.js +49 -0
  136. package/dist/tooling/shared/96ps4g1z.js +32 -0
  137. package/dist/tooling/shared/9kazs5p6.js +195 -0
  138. package/dist/tooling/shared/9xzm7hjg.js +38 -0
  139. package/dist/tooling/shared/a6pp9d12.js +49 -0
  140. package/dist/tooling/shared/a7q7t4n3.js +22 -0
  141. package/dist/tooling/shared/aftb2mwr.js +18 -0
  142. package/dist/tooling/shared/bz2dqhab.js +311 -0
  143. package/dist/tooling/shared/cmptmb4f.js +12 -0
  144. package/dist/tooling/shared/ddjtfgnx.js +177 -0
  145. package/dist/tooling/shared/djydjpqt.js +261 -0
  146. package/dist/tooling/shared/dw08f3m8.js +9 -0
  147. package/dist/tooling/shared/dy672fz6.js +16 -0
  148. package/dist/tooling/shared/dzx85q70.js +66 -0
  149. package/dist/tooling/shared/ec71zqgt.js +270 -0
  150. package/dist/tooling/shared/ep8zh3bk.js +6 -0
  151. package/dist/tooling/shared/f9mphxej.js +3789 -0
  152. package/dist/tooling/shared/fb9k2qwd.js +89 -0
  153. package/dist/tooling/shared/g9ad4n80.js +211 -0
  154. package/dist/tooling/shared/gfvtj6d0.js +94 -0
  155. package/dist/tooling/shared/gmcf9cxf.js +294 -0
  156. package/dist/tooling/shared/htnaj77v.js +17 -0
  157. package/dist/tooling/shared/j9t5s12a.js +7 -0
  158. package/dist/tooling/shared/jne7b93p.js +42 -0
  159. package/dist/tooling/shared/ma2340rx.js +118 -0
  160. package/dist/tooling/shared/mtrqn2hn.js +505 -0
  161. package/dist/tooling/shared/n4jqhpqz.js +27 -0
  162. package/dist/tooling/shared/nf7jmhfa.js +20 -0
  163. package/dist/tooling/shared/p7f08ktn.js +332 -0
  164. package/dist/tooling/shared/ppnnnvj8.js +13 -0
  165. package/dist/tooling/shared/pqy21k24.js +312 -0
  166. package/dist/tooling/shared/pw8fgahx.js +69 -0
  167. package/dist/tooling/shared/pwsytxm5.js +17 -0
  168. package/dist/tooling/shared/rf83gr2d.js +94 -0
  169. package/dist/tooling/shared/rq5kk8j6.js +401 -0
  170. package/dist/tooling/shared/rs1jb051.js +12 -0
  171. package/dist/tooling/shared/smn6kz1p.js +157 -0
  172. package/dist/tooling/shared/spk1jg3r.js +1696 -0
  173. package/dist/tooling/shared/tn8gay14.js +143 -0
  174. package/dist/tooling/shared/vraj6d74.js +42 -0
  175. package/dist/tooling/shared/vs0t6qt8.js +64 -0
  176. package/dist/tooling/shared/vx86qjv8.js +150 -0
  177. package/dist/tooling/shared/w7qe8sj1.js +140 -0
  178. package/dist/tooling/shared/wt7j7nf6.js +15 -0
  179. package/dist/tooling/shared/wwnekq5n.js +6 -0
  180. package/dist/tooling/shared/xhrnjch9.js +185 -0
  181. package/dist/tooling/shared/xjs6zqtc.js +190 -0
  182. package/dist/tooling/shared/y031jct9.js +16 -0
  183. package/dist/tooling/shared/y5656f0e.js +233 -0
  184. package/dist/tooling/shared/yfr8hzhm.js +604 -0
  185. package/dist/tooling/shared/yk9mp7vb.js +250 -0
  186. package/dist/tooling/shared/yw30vnq2.js +139 -0
  187. package/dist/tooling/shared/z8d7ptht.js +618 -0
  188. package/dist/tooling/shared/zhwnfyb2.js +9 -0
  189. package/dist/tooling/shared/zvnhh1n0.js +198 -0
  190. package/dist/tooling/testing/index.js +3 -3
  191. package/package.json +14 -2
  192. package/dist/shared/y34a8714.js +0 -150
  193. package/dist/shared/zy8r7qvz.js +0 -116
  194. package/dist/shared/{ywr8hmey.js → 606g0nbf.js} +3 -3
  195. package/dist/shared/{rpycarcr.js → gcefca6p.js} +4 -4
@@ -0,0 +1,20 @@
1
+ import {
2
+ armDriverError2,
3
+ open4,
4
+ send3,
5
+ connectClient2
6
+ } from "./417zdsk7.js";
7
+ import {
8
+ NODE_POSTGRES_CAPABILITIES,
9
+ readCapability2,
10
+ hasCapability2
11
+ } from "./xhrnjch9.js";
12
+ export {
13
+ armDriverError2 as armDriverError,
14
+ NODE_POSTGRES_CAPABILITIES as capabilities,
15
+ connectClient2 as connectClient,
16
+ hasCapability2 as hasCapability,
17
+ open4 as open,
18
+ readCapability2 as readCapability,
19
+ send3 as send
20
+ };
@@ -0,0 +1,332 @@
1
+ import {
2
+ POSTGRESJS_CAPABILITIES,
3
+ rejectClosed,
4
+ mapFailure,
5
+ nextTransactionDepth,
6
+ runCall,
7
+ EMPTY_NOTICES,
8
+ resultFrom
9
+ } from "./xhrnjch9.js";
10
+
11
+ // src/adapters/pg/postgresjs.ts
12
+ import net from "node:net";
13
+ import postgres from "postgres";
14
+ class PoolSockets {
15
+ held = [];
16
+ busy = 0;
17
+ listeners = 0;
18
+ hostCursor = 0;
19
+ connect(target) {
20
+ const path = typeof target.path === "string" ? target.path : undefined;
21
+ const hosts = target.host;
22
+ const ports = target.port;
23
+ const index = hosts.length === 0 ? 0 : this.hostCursor % hosts.length;
24
+ this.hostCursor += 1;
25
+ const host = hosts[index] ?? "localhost";
26
+ const port = ports[index] ?? ports[0] ?? 5432;
27
+ const socket = path !== undefined ? net.connect(path) : net.connect(port, host);
28
+ this.held.push(socket);
29
+ return socket;
30
+ }
31
+ occupy(run) {
32
+ this.busy += 1;
33
+ this.wake();
34
+ return run().finally(() => {
35
+ this.busy -= 1;
36
+ this.park();
37
+ });
38
+ }
39
+ busyGate() {
40
+ return {
41
+ enter: () => {
42
+ this.busy += 1;
43
+ this.wake();
44
+ },
45
+ leave: () => {
46
+ this.busy -= 1;
47
+ this.park();
48
+ }
49
+ };
50
+ }
51
+ listenerGate() {
52
+ return {
53
+ enter: () => {
54
+ this.listeners += 1;
55
+ this.wake();
56
+ },
57
+ leave: () => {
58
+ this.listeners -= 1;
59
+ this.park();
60
+ }
61
+ };
62
+ }
63
+ wake() {
64
+ for (const socket of this.held)
65
+ if (socket.writable)
66
+ socket.ref();
67
+ }
68
+ park() {
69
+ if (this.busy > 0 || this.listeners > 0)
70
+ return;
71
+ for (const socket of this.held)
72
+ if (socket.writable)
73
+ socket.unref();
74
+ }
75
+ }
76
+ function driverOptions(config, state) {
77
+ return {
78
+ max: state.max,
79
+ prepare: state.named,
80
+ idle_timeout: state.plain ? 0 : 30,
81
+ ...state.plain ? { max_lifetime: null } : {},
82
+ connect_timeout: 5,
83
+ types: WIRE_TYPES,
84
+ ...state.plain ? { socket: (options) => state.sockets.connect(options) } : {},
85
+ ...config.ssl !== undefined ? { ssl: config.ssl } : {},
86
+ connection: {
87
+ application_name: "okmodel",
88
+ ...config.searchPath !== undefined ? { search_path: config.searchPath } : {}
89
+ },
90
+ onnotice(notice) {
91
+ state.notices.push(notice);
92
+ }
93
+ };
94
+ }
95
+ function open3(config) {
96
+ const max = config.max ?? 10;
97
+ const acquireMs = config.timeouts?.acquire;
98
+ const counters = { reserved: 0, busy: 0, reservedBusy: 0, waiting: 0 };
99
+ const poolInflight = new Set;
100
+ const notices = noticeBuffer();
101
+ let closed = false;
102
+ const isClosed = () => closed;
103
+ const named = config.prepared === "named";
104
+ const plain = config.ssl === undefined;
105
+ const sockets = new PoolSockets;
106
+ const sql = postgres(config.url, driverOptions(config, {
107
+ max,
108
+ named,
109
+ plain,
110
+ notices,
111
+ sockets
112
+ }));
113
+ const root = new PgSession2(sql, counters, "pool", poolInflight, isClosed, undefined, notices, named);
114
+ const held = { sql, acquireMs, counters, isClosed, notices, named, poolInflight };
115
+ return {
116
+ capabilities: POSTGRESJS_CAPABILITIES,
117
+ execute(text, params, options) {
118
+ return sockets.occupy(() => runCall(closed, options, (watch) => root.query(text, params, watch)));
119
+ },
120
+ batch(statements, options) {
121
+ return sockets.occupy(() => import("./3m1hkg7z.js").then((mod) => mod.runBatch(held, statements, options)));
122
+ },
123
+ reserve() {
124
+ const gate = sockets.busyGate();
125
+ gate.enter();
126
+ return import("./3m1hkg7z.js").then((mod) => mod.holdCheckout(gate, () => mod.checkout(held)), (error) => {
127
+ gate.leave();
128
+ throw error;
129
+ });
130
+ },
131
+ describe: (text, params) => sockets.occupy(() => import("./3m1hkg7z.js").then((mod) => mod.describe(sql, text, params))),
132
+ stream(text, params) {
133
+ const gate = sockets.busyGate();
134
+ return async function* () {
135
+ gate.enter();
136
+ try {
137
+ yield* loadStream(sql, text, params);
138
+ } finally {
139
+ gate.leave();
140
+ }
141
+ }();
142
+ },
143
+ listen(channel, onNotify) {
144
+ const gate = sockets.listenerGate();
145
+ gate.enter();
146
+ return import("./3m1hkg7z.js").then((mod) => mod.holdListener(gate, async () => {
147
+ const listening = await sql.listen(channel, onNotify);
148
+ return async () => {
149
+ await listening.unlisten();
150
+ };
151
+ }), (error) => {
152
+ gate.leave();
153
+ throw error;
154
+ });
155
+ },
156
+ cancel() {
157
+ for (const pending of poolInflight)
158
+ pending.cancel();
159
+ },
160
+ stats: () => statsOf(counters, max),
161
+ async close() {
162
+ closed = true;
163
+ await sql.end({ timeout: 5 });
164
+ }
165
+ };
166
+ }
167
+
168
+ class PgSession2 {
169
+ canCancel = true;
170
+ transactionDepth = 0;
171
+ sql;
172
+ counters;
173
+ scope;
174
+ local;
175
+ isClosed;
176
+ shared;
177
+ notices;
178
+ named;
179
+ constructor(sql, counters, scope, local, isClosed, shared, notices, named) {
180
+ this.sql = sql;
181
+ this.counters = counters;
182
+ this.scope = scope;
183
+ this.local = local;
184
+ this.isClosed = isClosed;
185
+ this.shared = shared;
186
+ this.notices = notices;
187
+ this.named = named;
188
+ }
189
+ query(text, params, watch) {
190
+ if (this.isClosed())
191
+ return rejectClosed();
192
+ const start = this.notices.start();
193
+ this.enter();
194
+ const pending = send(this.sql, text, params, this.named);
195
+ if (watch === undefined) {
196
+ return pending.then((result) => {
197
+ this.leave();
198
+ this.note(text);
199
+ return resultFrom(result, this.notices.since(start));
200
+ }, (error) => {
201
+ this.leave();
202
+ return mapFailure(error).then((mapped) => {
203
+ throw mapped;
204
+ });
205
+ });
206
+ }
207
+ this.local.add(pending);
208
+ this.shared?.add(pending);
209
+ const onAbort = () => {
210
+ pending.cancel();
211
+ };
212
+ if (watch.signal.aborted)
213
+ onAbort();
214
+ else
215
+ watch.signal.addEventListener("abort", onAbort, { once: true });
216
+ return import("./3m1hkg7z.js").then((mod) => mod.watchQuery(pending, watch, onAbort, {
217
+ start,
218
+ text,
219
+ notices: this.notices,
220
+ leave: () => {
221
+ this.leave();
222
+ },
223
+ note: (sqlText) => {
224
+ this.note(sqlText);
225
+ },
226
+ untrack: (item) => {
227
+ this.local.delete(item);
228
+ this.shared?.delete(item);
229
+ }
230
+ }), (error) => {
231
+ this.leave();
232
+ this.local.delete(pending);
233
+ this.shared?.delete(pending);
234
+ watch.signal.removeEventListener("abort", onAbort);
235
+ throw error;
236
+ });
237
+ }
238
+ inTransaction() {
239
+ return this.transactionDepth > 0;
240
+ }
241
+ abandon() {
242
+ this.transactionDepth = 0;
243
+ }
244
+ depth() {
245
+ return this.transactionDepth;
246
+ }
247
+ cancel() {
248
+ for (const pending of this.local)
249
+ pending.cancel();
250
+ }
251
+ note(text) {
252
+ this.transactionDepth = nextTransactionDepth(this.transactionDepth, text);
253
+ }
254
+ enter() {
255
+ if (this.scope === "pool")
256
+ this.counters.busy += 1;
257
+ else
258
+ this.counters.reservedBusy += 1;
259
+ }
260
+ leave() {
261
+ if (this.scope === "pool")
262
+ this.counters.busy = Math.max(0, this.counters.busy - 1);
263
+ else
264
+ this.counters.reservedBusy = Math.max(0, this.counters.reservedBusy - 1);
265
+ }
266
+ }
267
+ async function* loadStream(sql, text, params) {
268
+ const { stream } = await import("./3m1hkg7z.js");
269
+ yield* stream(sql, text, params);
270
+ }
271
+ function send(sql, text, params, named) {
272
+ if (!named && (params === undefined || params.length === 0))
273
+ return sql.unsafe(text).raw();
274
+ return sql.unsafe(text, asParams(params), { prepare: named }).raw();
275
+ }
276
+ function asParams(params) {
277
+ if (params === undefined)
278
+ return [];
279
+ return params;
280
+ }
281
+ function statsOf(counters, size) {
282
+ return {
283
+ size,
284
+ idle: Math.max(0, size - counters.reserved - counters.busy),
285
+ inflight: counters.busy + counters.reservedBusy,
286
+ waiting: counters.waiting
287
+ };
288
+ }
289
+ function noticeBuffer() {
290
+ let notices;
291
+ return {
292
+ push(notice) {
293
+ const severity = typeof notice.severity === "string" ? notice.severity : "";
294
+ const message = typeof notice.message === "string" ? notice.message : "";
295
+ const code = typeof notice.code === "string" && notice.code.length > 0 ? notice.code : undefined;
296
+ const entry = code === undefined ? { severity, message } : { severity, message, code };
297
+ (notices ??= []).push(entry);
298
+ },
299
+ start() {
300
+ return notices?.length ?? 0;
301
+ },
302
+ since(start) {
303
+ if (notices === undefined || notices.length === start)
304
+ return EMPTY_NOTICES;
305
+ return notices.slice(start);
306
+ }
307
+ };
308
+ }
309
+ function wire(to, from) {
310
+ return {
311
+ to,
312
+ from: [...from],
313
+ serialize: (value) => value,
314
+ parse: (value) => value
315
+ };
316
+ }
317
+ var WIRE_TYPES = {
318
+ number: wire(0, [21, 23, 26, 700, 701]),
319
+ boolean: wire(16, [16]),
320
+ bytea: wire(17, [17]),
321
+ json: wire(114, [114, 3802]),
322
+ date: wire(1184, [1082, 1114, 1184]),
323
+ boolArray: wire(1000, [1000]),
324
+ int2Array: wire(1005, [1005]),
325
+ int4Array: wire(1007, [1007]),
326
+ textArray: wire(1009, [1009]),
327
+ int8Array: wire(1016, [1016]),
328
+ numericArray: wire(1231, [1231]),
329
+ jsonbArray: wire(3807, [3807])
330
+ };
331
+
332
+ export { open3, PgSession2 };
@@ -0,0 +1,13 @@
1
+ import {
2
+ fail2
3
+ } from "./f9mphxej.js";
4
+
5
+ // src/runtime/result-name.ts
6
+ var PROTOTYPE_KEYS = ",__proto__,constructor,toString,hasOwnProperty,valueOf,isPrototypeOf,propertyIsEnumerable,toLocaleString,__defineGetter__,__defineSetter__,__lookupGetter__,__lookupSetter__,then,";
7
+ function refusePrototypeKey(name, where) {
8
+ if (!PROTOTYPE_KEYS.includes(`,${name},`))
9
+ return;
10
+ fail2("OKM1120", `${where} ${name} is a prototype key. Use another name for the result.`);
11
+ }
12
+
13
+ export { refusePrototypeKey };
@@ -0,0 +1,312 @@
1
+ import {
2
+ readSelectFragment,
3
+ emitSqlSelect,
4
+ decodeSqlNumber
5
+ } from "./gmcf9cxf.js";
6
+ import"./3tq8pnb8.js";
7
+ import {
8
+ refusePrototypeKey
9
+ } from "./ppnnnvj8.js";
10
+ import {
11
+ throwNamed,
12
+ emitWhere,
13
+ emitOrder,
14
+ refuseHidden,
15
+ emitLimit,
16
+ parseOrder,
17
+ projectExpr,
18
+ indexes,
19
+ quote,
20
+ list,
21
+ isRecord,
22
+ fail2
23
+ } from "./f9mphxej.js";
24
+
25
+ // src/dialects/pg/ops/window.ts
26
+ var RUNTIME = Symbol.for("okmodel.window");
27
+ function isWindow(value) {
28
+ return typeof value === "object" && value !== null && RUNTIME in value;
29
+ }
30
+
31
+ // src/runtime/window.ts
32
+ var AS_NUMBER = (wire) => Number(wire);
33
+ var AS_TEXT = (wire) => wire;
34
+ var NUMERIC = /^(smallint|integer|bigint|real|double precision|numeric|decimal)\b/;
35
+ var OPTION_KEYS = ["as", "orderBy", "partitionBy"];
36
+ function attach(schema, call) {
37
+ const table = indexesOf(schema, call.table);
38
+ return { ...call, build: statement(readSpec(table, call.select)) };
39
+ }
40
+ function statement(spec) {
41
+ return (schema, table, sink, outputs, call, hooks) => {
42
+ sink.mark(`${call.op}|${table.model.name}|`);
43
+ if (call.include !== undefined && hooks === undefined) {
44
+ fail2("OKM1120", "An include needs the include planner.");
45
+ }
46
+ const includes = hooks === undefined || call.include === undefined ? [] : hooks.read(schema, table, call.include, call.all);
47
+ sink.text("select ");
48
+ let column = 0;
49
+ for (const item of spec.items) {
50
+ if (column > 0)
51
+ sink.text(", ");
52
+ if (item.kind === "column") {
53
+ sink.text(projectExpr(`t.${quote(item.column.sql)}`, item.column.dataType));
54
+ sink.mark(`s:${item.column.field}`);
55
+ outputs?.fields.push({ key: item.column.field, at: column, decode: item.column.decode });
56
+ } else if (item.kind === "sql") {
57
+ emitSqlSelect(table, item.parts, sink, "t");
58
+ sink.text("::text");
59
+ sink.mark(`s:${item.as}`);
60
+ outputs?.fields.push({
61
+ key: item.as,
62
+ at: column,
63
+ decode: item.textual === true ? AS_TEXT : decodeSqlNumber
64
+ });
65
+ } else {
66
+ emitWindow(table, item.window, sink);
67
+ sink.text("::text");
68
+ outputs?.fields.push({ key: item.window.as, at: column, decode: item.window.decode });
69
+ }
70
+ column += 1;
71
+ }
72
+ if (spec.items.length === 0 && includes.length === 0)
73
+ sink.text("1");
74
+ for (let index = 0;index < includes.length; index += 1) {
75
+ const include = includes[index];
76
+ if (include === undefined || hooks === undefined)
77
+ continue;
78
+ column = hooks.select(include, `i${String(index)}`, sink, outputs, column);
79
+ }
80
+ sink.text(" from ");
81
+ sink.text(quote(table.model.sql));
82
+ sink.text(" t");
83
+ for (let index = 0;index < includes.length; index += 1) {
84
+ const include = includes[index];
85
+ if (include === undefined || hooks === undefined)
86
+ continue;
87
+ hooks.join(schema, include, sink, "t", `i${String(index)}`, 0);
88
+ }
89
+ emitWhere(schema, table, call.where, sink, "t", 0, false, call.after);
90
+ emitResultOrder(table, call.orderBy, sink, spec.ordinals);
91
+ emitLimit(call, sink);
92
+ call.tail?.(sink);
93
+ };
94
+ }
95
+ function emitResultOrder(table, orderBy, sink, ordinals) {
96
+ if (!isRecord(orderBy) || !mentionsOrdinal(orderBy, ordinals)) {
97
+ emitOrder(table, orderBy, sink, "t");
98
+ return;
99
+ }
100
+ const keys = Object.keys(orderBy);
101
+ sink.text(" order by ");
102
+ for (let index = 0;index < keys.length; index += 1) {
103
+ const key = keys[index];
104
+ if (key === undefined)
105
+ continue;
106
+ const parsed = parseOrder(key, orderBy[key]);
107
+ if (index > 0)
108
+ sink.text(", ");
109
+ const ordinal = ordinals.get(key);
110
+ if (ordinal !== undefined) {
111
+ sink.text(ordinal);
112
+ } else {
113
+ const column = table.columns.get(key);
114
+ if (column === undefined) {
115
+ throwNamed("OKM1120", key, table.names, `Field ${key} is not on ${table.model.name}. Accepted names: ${list(table.names)}.`);
116
+ }
117
+ refuseHidden(column);
118
+ sink.text("t.");
119
+ sink.text(quote(column.sql));
120
+ }
121
+ sink.text(parsed.dir === "asc" ? " asc" : " desc");
122
+ sink.text(parsed.nulls === "first" ? " nulls first" : " nulls last");
123
+ sink.mark(`o:${key}:${parsed.dir}:${parsed.nulls}`);
124
+ }
125
+ }
126
+ function mentionsOrdinal(orderBy, ordinals) {
127
+ if (ordinals.size === 0)
128
+ return false;
129
+ for (const key of Object.keys(orderBy)) {
130
+ if (ordinals.has(key))
131
+ return true;
132
+ }
133
+ return false;
134
+ }
135
+ function emitWindow(table, win, sink) {
136
+ sink.mark(`w:${win.fn}:${win.as}:p:${win.partition.map((column) => column.field).join("+")}`);
137
+ if (win.fn === "sum") {
138
+ const column = win.column;
139
+ if (column === undefined)
140
+ fail2("OKM1120", "runningTotal needs a column.");
141
+ sink.text("sum(t.");
142
+ sink.text(quote(column.sql));
143
+ sink.text(")");
144
+ } else {
145
+ sink.text(win.fn);
146
+ sink.text("()");
147
+ }
148
+ sink.text(" over (");
149
+ if (win.partition.length > 0) {
150
+ sink.text("partition by ");
151
+ for (let index = 0;index < win.partition.length; index += 1) {
152
+ const column = win.partition[index];
153
+ if (column === undefined)
154
+ continue;
155
+ if (index > 0)
156
+ sink.text(", ");
157
+ sink.text("t.");
158
+ sink.text(quote(column.sql));
159
+ }
160
+ }
161
+ emitOrder(table, win.orderBy, sink, "t");
162
+ if (win.tieBreak) {
163
+ const named = isRecord(win.orderBy) ? Object.keys(win.orderBy) : [];
164
+ for (const field of table.model.primary) {
165
+ if (named.includes(field))
166
+ continue;
167
+ const column = table.columns.get(field);
168
+ if (column === undefined)
169
+ continue;
170
+ sink.text(", t.");
171
+ sink.text(quote(column.sql));
172
+ sink.text(" asc");
173
+ }
174
+ }
175
+ sink.text(")");
176
+ }
177
+ function readSpec(table, select) {
178
+ if (!Array.isArray(select))
179
+ fail2("OKM1120", "select must be a list of field names.");
180
+ const items = [];
181
+ const ordinals = new Map;
182
+ const names = new Set;
183
+ for (const entry of select) {
184
+ if (typeof entry === "string") {
185
+ const column = columnOn(table, entry, "select");
186
+ names.add(column.field);
187
+ items.push({ kind: "column", column });
188
+ continue;
189
+ }
190
+ const sql = readSelectFragment(entry);
191
+ if (sql !== undefined) {
192
+ const as = readAs(sql.as, "fragment");
193
+ if (names.has(as) || table.columns.has(as)) {
194
+ fail2("OKM1120", `fragment as ${as} clashes with a column of ${table.model.name}.`, "Pick an as name that is not a column.");
195
+ }
196
+ names.add(as);
197
+ ordinals.set(as, String(items.length + 1));
198
+ items.push(sql.textual === true ? { kind: "sql", as, parts: sql.parts, textual: true } : { kind: "sql", as, parts: sql.parts });
199
+ continue;
200
+ }
201
+ if (!isWindow(entry)) {
202
+ fail2("OKM1120", "select takes field names or a window helper. A plain object is not a window.", "Pass rowNumber, rank, denseRank, or runningTotal.");
203
+ }
204
+ const win = readWindow(table, entry, names);
205
+ names.add(win.as);
206
+ ordinals.set(win.as, String(items.length + 1));
207
+ items.push({ kind: "window", window: win });
208
+ }
209
+ return { items, ordinals };
210
+ }
211
+ function readWindow(table, call, taken) {
212
+ if (!isRecord(call))
213
+ fail2("OKM1120", "A window helper was not created by its function.");
214
+ for (const key of Object.keys(call)) {
215
+ if (key === "label" || key === "fn" || key === "column")
216
+ continue;
217
+ if (OPTION_KEYS.includes(key))
218
+ continue;
219
+ fail2("OKM1120", `${call.label} does not take ${key}. Accepted: partitionBy, orderBy, as.`, "Pass partitionBy, orderBy, and as.");
220
+ }
221
+ const as = readAs(call.as, call.label);
222
+ if (taken.has(as) || table.columns.has(as)) {
223
+ fail2("OKM1120", `${call.label} as ${as} clashes with a column of ${table.model.name}.`, "Pick an as name that is not a column.");
224
+ }
225
+ const partition = readPartition(table, call.partitionBy, call.label);
226
+ if (!isRecord(call.orderBy) || Object.keys(call.orderBy).length === 0) {
227
+ fail2("OKM1120", `${call.label} needs orderBy, the order inside the group.`, 'Pass orderBy: { field: "desc" }.');
228
+ }
229
+ for (const field of Object.keys(call.orderBy))
230
+ columnOn(table, field, `${call.label} orderBy`);
231
+ let column;
232
+ let decode = AS_NUMBER;
233
+ if (call.fn === "sum") {
234
+ if (typeof call.column !== "string") {
235
+ fail2("OKM1120", "runningTotal needs the column to total.");
236
+ }
237
+ column = columnOn(table, call.column, "runningTotal");
238
+ if (!NUMERIC.test(column.dataType)) {
239
+ fail2("OKM1124", `runningTotal does not apply to ${column.dataType} column ${column.field}. It takes integer, real, and numeric columns.`);
240
+ }
241
+ decode = totalDecode(column);
242
+ }
243
+ return {
244
+ fn: call.fn,
245
+ label: call.label,
246
+ column,
247
+ partition,
248
+ orderBy: call.orderBy,
249
+ as,
250
+ decode,
251
+ tieBreak: call.fn === "row_number" || call.fn === "sum"
252
+ };
253
+ }
254
+ function readPartition(table, value, label) {
255
+ if (value === undefined)
256
+ return [];
257
+ if (!Array.isArray(value) || value.length === 0) {
258
+ fail2("OKM1120", `${label} partitionBy needs the fields that form the group.`, 'Pass partitionBy: ["status"], or leave it out to use the whole result.');
259
+ }
260
+ const columns = [];
261
+ const seen = new Set;
262
+ for (const name of value) {
263
+ if (typeof name !== "string") {
264
+ fail2("OKM1120", `${label} partitionBy takes field names.`);
265
+ }
266
+ const column = columnOn(table, name, `${label} partitionBy`);
267
+ if (seen.has(column.field)) {
268
+ fail2("OKM1120", `${label} partitionBy lists ${column.field} twice.`);
269
+ }
270
+ seen.add(column.field);
271
+ columns.push(column);
272
+ }
273
+ return columns;
274
+ }
275
+ function readAs(value, label) {
276
+ if (typeof value !== "string" || value.length === 0) {
277
+ fail2("OKM1120", `${label} needs as, the name of the result key.`, "Set as to a name, such as n.");
278
+ }
279
+ for (let index = 0;index < value.length; index += 1) {
280
+ if ((value.charCodeAt(index) ?? 0) < 32) {
281
+ fail2("OKM1120", `${label} as has a control character.`);
282
+ }
283
+ }
284
+ refusePrototypeKey(value, `${label} as`);
285
+ return value;
286
+ }
287
+ function totalDecode(column) {
288
+ const kind = typeof (column.decode?.("0") ?? "");
289
+ if (kind === "string")
290
+ return AS_TEXT;
291
+ if (kind === "number")
292
+ return AS_NUMBER;
293
+ return fail2("OKM1124", `runningTotal needs a column whose value is a number or a string. ${column.field} reads as ${kind}.`);
294
+ }
295
+ function columnOn(table, name, where) {
296
+ const column = table.columns.get(name);
297
+ if (column === undefined) {
298
+ throwNamed("OKM1120", name, table.names, `Field ${name} is not on ${table.model.name}. ${where} accepts: ${list(table.names)}.`);
299
+ }
300
+ if (where !== "select")
301
+ refuseHidden(column);
302
+ return column;
303
+ }
304
+ function indexesOf(schema, name) {
305
+ const table = indexes(schema).get(name);
306
+ if (table === undefined)
307
+ fail2("OKM1120", `Table ${name} is not in the schema.`);
308
+ return table;
309
+ }
310
+ export {
311
+ attach
312
+ };