okmodel 0.5.1 → 0.7.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 (233) hide show
  1. package/README.md +100 -1116
  2. package/dist/adapters/capabilities.d.ts +3 -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/bunsql-extra.d.ts +54 -3
  6. package/dist/adapters/pg/bunsql.d.ts +14 -1
  7. package/dist/adapters/pg/cursor.d.ts +17 -0
  8. package/dist/adapters/pg/no-inflight-timeout.d.ts +20 -0
  9. package/dist/adapters/pg/nodepostgres-extra.d.ts +22 -1
  10. package/dist/adapters/pg/nodepostgres.d.ts +12 -0
  11. package/dist/adapters/pg/pglite-wire.d.ts +24 -0
  12. package/dist/adapters/pg/pglite.d.ts +10 -0
  13. package/dist/adapters/pg/postgres-extra.d.ts +41 -3
  14. package/dist/adapters/pg/reserved.d.ts +42 -0
  15. package/dist/adapters/pg/session.d.ts +0 -18
  16. package/dist/contracts/catalog/document.d.ts +19 -5
  17. package/dist/contracts/catalog/object.d.ts +2 -1
  18. package/dist/contracts/catalog/policy.d.ts +48 -0
  19. package/dist/contracts/catalog/types.d.ts +25 -1
  20. package/dist/contracts/catalog/view.d.ts +2 -0
  21. package/dist/contracts/connection.d.ts +7 -6
  22. package/dist/contracts/internal.js +1 -1
  23. package/dist/contracts/location.d.ts +10 -0
  24. package/dist/dialects/pg/column.d.ts +13 -2
  25. package/dist/dialects/pg/ddl.d.ts +37 -1
  26. package/dist/dialects/pg/emit.d.ts +19 -1
  27. package/dist/dialects/pg/errors.d.ts +32 -1
  28. package/dist/dialects/pg/ext/citext.js +1 -1
  29. package/dist/dialects/pg/ext/index.js +1 -1
  30. package/dist/dialects/pg/ext/pg-trgm.js +1 -1
  31. package/dist/dialects/pg/filter-parse.d.ts +5 -4
  32. package/dist/dialects/pg/floor.d.ts +68 -0
  33. package/dist/dialects/pg/fn/index.js +2 -2
  34. package/dist/dialects/pg/index.d.ts +8 -2
  35. package/dist/dialects/pg/index.js +67 -171
  36. package/dist/dialects/pg/introspect.d.ts +7 -0
  37. package/dist/dialects/pg/keys.d.ts +11 -8
  38. package/dist/dialects/pg/model.d.ts +47 -2
  39. package/dist/dialects/pg/morph.d.ts +43 -0
  40. package/dist/dialects/pg/operator-fit.d.ts +2 -2
  41. package/dist/dialects/pg/operators.d.ts +14 -7
  42. package/dist/dialects/pg/ops/bucket.d.ts +65 -0
  43. package/dist/dialects/pg/ops/inc.d.ts +2 -2
  44. package/dist/dialects/pg/ops/matches.d.ts +9 -12
  45. package/dist/dialects/pg/ops/relevance.d.ts +13 -0
  46. package/dist/dialects/pg/ops/search-query.d.ts +22 -0
  47. package/dist/dialects/pg/ops/window.d.ts +103 -0
  48. package/dist/dialects/pg/owner.d.ts +42 -0
  49. package/dist/dialects/pg/path-endpoint.d.ts +20 -0
  50. package/dist/dialects/pg/preset.d.ts +1 -1
  51. package/dist/dialects/pg/relations.d.ts +100 -3
  52. package/dist/dialects/pg/role/check.d.ts +3 -0
  53. package/dist/dialects/pg/role/index.d.ts +71 -6
  54. package/dist/dialects/pg/role/sql.d.ts +11 -0
  55. package/dist/dialects/pg/schema.d.ts +33 -3
  56. package/dist/dialects/pg/table.d.ts +25 -3
  57. package/dist/dialects/pg/tenancy.d.ts +45 -3
  58. package/dist/dialects/pg/view/client.d.ts +19 -1
  59. package/dist/dialects/pg/view/index.d.ts +4 -0
  60. package/dist/dialects/pg/view/index.js +21 -178
  61. package/dist/dialects/pg/view/refresh.d.ts +24 -0
  62. package/dist/runtime/aggregate.d.ts +8 -2
  63. package/dist/runtime/batch.d.ts +1 -1
  64. package/dist/runtime/bucket-group.d.ts +70 -0
  65. package/dist/runtime/conflict.d.ts +24 -6
  66. package/dist/runtime/drift.d.ts +9 -0
  67. package/dist/runtime/include.d.ts +0 -8
  68. package/dist/runtime/inflight.d.ts +14 -0
  69. package/dist/runtime/page.d.ts +4 -1
  70. package/dist/runtime/pg/bun.js +12 -5
  71. package/dist/runtime/pg/pg.js +5 -5
  72. package/dist/runtime/pg/pglite.d.ts +1 -0
  73. package/dist/runtime/pg/pglite.js +9 -8
  74. package/dist/runtime/pg/postgresjs.js +5 -5
  75. package/dist/runtime/plan.d.ts +113 -15
  76. package/dist/runtime/predicate.d.ts +19 -0
  77. package/dist/runtime/read-stream.d.ts +13 -16
  78. package/dist/runtime/result-name.d.ts +17 -0
  79. package/dist/runtime/safety/index.js +1 -1
  80. package/dist/runtime/server-check.d.ts +3 -3
  81. package/dist/runtime/sort-bind.d.ts +21 -0
  82. package/dist/runtime/sort.d.ts +56 -0
  83. package/dist/runtime/sync.d.ts +70 -0
  84. package/dist/runtime/tenancy/composite.d.ts +26 -0
  85. package/dist/runtime/tenancy/context.d.ts +16 -0
  86. package/dist/runtime/tenancy/index.d.ts +9 -5
  87. package/dist/runtime/tenancy/index.js +1511 -38
  88. package/dist/runtime/tenancy/path.d.ts +87 -0
  89. package/dist/runtime/tenancy/rls.d.ts +67 -0
  90. package/dist/runtime/tenancy/via.d.ts +22 -0
  91. package/dist/runtime/text-search.d.ts +20 -0
  92. package/dist/runtime/traits/index.d.ts +80 -0
  93. package/dist/runtime/traits/index.js +187 -17
  94. package/dist/runtime/tree.d.ts +38 -0
  95. package/dist/runtime/types.d.ts +231 -23
  96. package/dist/runtime/validate/index.js +4 -4
  97. package/dist/runtime/version.d.ts +39 -0
  98. package/dist/runtime/window.d.ts +21 -0
  99. package/dist/runtime/write.d.ts +55 -3
  100. package/dist/shared/{krxx77g0.js → 14vx95q4.js} +12 -18
  101. package/dist/shared/1est4ezg.js +313 -0
  102. package/dist/shared/{4az561fn.js → 1qa0qajz.js} +8 -23
  103. package/dist/shared/1w508jbr.js +1611 -0
  104. package/dist/shared/2c13q96h.js +18 -0
  105. package/dist/shared/2d3xq4ps.js +9 -0
  106. package/dist/shared/2shdbn8q.js +23 -0
  107. package/dist/shared/{t4tbsy9h.js → 358zsv4r.js} +1 -1
  108. package/dist/shared/3v7t1yrv.js +208 -0
  109. package/dist/shared/5e3tf63j.js +220 -0
  110. package/dist/shared/5fe3kbsh.js +10 -0
  111. package/dist/shared/5v3xf80k.js +37 -0
  112. package/dist/shared/{51mvgj8w.js → 63qtkrca.js} +130 -3
  113. package/dist/shared/{gbtcc2d7.js → 67txcrcw.js} +62 -66
  114. package/dist/shared/79dt02vc.js +12 -0
  115. package/dist/shared/{v36c5g6p.js → 7k9grvf6.js} +407 -14
  116. package/dist/shared/7kms6fvj.js +20 -0
  117. package/dist/shared/7mnme5tn.js +8 -0
  118. package/dist/shared/7n7ddwt4.js +6 -0
  119. package/dist/shared/{87jvvhnt.js → 81ebdx9p.js} +48 -68
  120. package/dist/shared/{szm0cksa.js → 8skf0cam.js} +34 -6
  121. package/dist/shared/{gjthds9j.js → 9628a9xt.js} +1 -1
  122. package/dist/shared/9a4ae4p8.js +31 -0
  123. package/dist/shared/{8xsbagad.js → 9s0ypp87.js} +88 -7
  124. package/dist/shared/9y3mmmh1.js +436 -0
  125. package/dist/shared/ab9gmbwy.js +213 -0
  126. package/dist/shared/{sycnx8gt.js → ax7qv55k.js} +18 -8
  127. package/dist/shared/{f5cw5q5r.js → aytqh5y2.js} +6 -4
  128. package/dist/shared/{fancm5pt.js → b5pftnn2.js} +138 -15
  129. package/dist/shared/{efmy2gcg.js → b76b8d95.js} +2 -2
  130. package/dist/shared/bdk4sty6.js +58 -0
  131. package/dist/shared/{32zqc1dw.js → bg3rym3g.js} +63 -32
  132. package/dist/shared/{0t22wwh1.js → bm22fvzr.js} +16 -14
  133. package/dist/shared/bz84tkzm.js +342 -0
  134. package/dist/shared/{31vm914h.js → cm3w0ywp.js} +15 -16
  135. package/dist/shared/cn957f8d.js +179 -0
  136. package/dist/shared/{pf0d8f77.js → cnkky55j.js} +25 -14
  137. package/dist/shared/{5fqx9yh4.js → dk8v79p3.js} +3 -1
  138. package/dist/shared/{nkaxvt9q.js → dkxgzaqd.js} +224 -19
  139. package/dist/shared/dzj09cye.js +10 -0
  140. package/dist/shared/{4m9fn52z.js → ed0b8k4s.js} +1 -1
  141. package/dist/shared/{z2rnngj4.js → ek2wt2dz.js} +8 -5
  142. package/dist/shared/exn3sd90.js +359 -0
  143. package/dist/shared/g6evj1j3.js +66 -0
  144. package/dist/shared/ga9j92qj.js +25 -0
  145. package/dist/shared/{qv2ydck9.js → gs74dbj5.js} +651 -76
  146. package/dist/shared/gzaveazc.js +285 -0
  147. package/dist/shared/{8rsy3hxz.js → h15jx9bc.js} +1 -1
  148. package/dist/shared/{dyj86cmj.js → hyh73qk6.js} +3 -3
  149. package/dist/shared/je4axnm8.js +22 -0
  150. package/dist/shared/{ycatkxmq.js → jgqj11w4.js} +1 -1
  151. package/dist/shared/{6qd27mx0.js → jvd0mqag.js} +5 -5
  152. package/dist/shared/jznfax0y.js +155 -0
  153. package/dist/shared/{cgbamx3e.js → kb2sdhfb.js} +7 -4
  154. package/dist/shared/{ps7a6h14.js → kr0jzbw2.js} +12 -8
  155. package/dist/shared/{janx09m5.js → ktz42yez.js} +20 -8
  156. package/dist/shared/{gde73es6.js → m6p12kbr.js} +56 -55
  157. package/dist/shared/{r6nwkxnt.js → m7n9h2a5.js} +4 -4
  158. package/dist/shared/{672t5vym.js → mss6ctgv.js} +2 -2
  159. package/dist/shared/{nxdp2ze3.js → mtaszv5a.js} +144 -12
  160. package/dist/shared/n816peh1.js +45 -0
  161. package/dist/shared/nbw1z4yd.js +18 -0
  162. package/dist/shared/{cctw17hj.js → p6qc5gcp.js} +38 -34
  163. package/dist/shared/{6rk2gen5.js → pbp1ss85.js} +20 -17
  164. package/dist/shared/pt94kz7f.js +6 -0
  165. package/dist/shared/{wq9n6pwr.js → pz6rj8ay.js} +82 -9
  166. package/dist/shared/q5c6ngn3.js +13 -0
  167. package/dist/shared/{8bwbwy8h.js → qq3899ht.js} +128 -18
  168. package/dist/shared/{8ktwd3rh.js → r3j8s5y0.js} +1 -1
  169. package/dist/shared/rhs3xe4k.js +7 -0
  170. package/dist/shared/{ntneex2a.js → rpycarcr.js} +6 -6
  171. package/dist/shared/{sdc7r2ww.js → rs73fca9.js} +8 -8
  172. package/dist/shared/{tm5ab8we.js → sfz1ksn6.js} +7 -4
  173. package/dist/shared/{6mnp99hx.js → svncdeh5.js} +16 -12
  174. package/dist/shared/{hf22e0wt.js → tbrvgw6z.js} +62 -42
  175. package/dist/shared/te5as7z7.js +622 -0
  176. package/dist/shared/{ga3sfy93.js → tsvww22q.js} +535 -200
  177. package/dist/shared/{47x8x9ze.js → ttsqgp4p.js} +20 -13
  178. package/dist/shared/{x72jb7ad.js → v8g1vgv8.js} +1 -1
  179. package/dist/shared/vb6qfyve.js +152 -0
  180. package/dist/shared/{r2sp8hrc.js → vczs8gd4.js} +22 -13
  181. package/dist/shared/{1g0drakv.js → vqrjpcqn.js} +145 -3
  182. package/dist/shared/{2pqx26h8.js → w72kgg4q.js} +31 -22
  183. package/dist/shared/{96qcj21c.js → w79hsckn.js} +16 -51
  184. package/dist/shared/wa07eas9.js +730 -0
  185. package/dist/shared/{k8v5v5wj.js → x933k9a3.js} +75 -34
  186. package/dist/shared/{cr74e96e.js → xhfsfkgk.js} +62 -28
  187. package/dist/shared/xjk7hnn5.js +272 -0
  188. package/dist/shared/{5enne4ma.js → xnxsed62.js} +66 -113
  189. package/dist/shared/xqve9598.js +53 -0
  190. package/dist/shared/{9x25fxhc.js → y34a8714.js} +1 -1
  191. package/dist/shared/{k7dwkhvq.js → y6jdds7g.js} +4 -1
  192. package/dist/shared/y6njgyda.js +249 -0
  193. package/dist/shared/{c9z5g0fm.js → ywr8hmey.js} +5 -2
  194. package/dist/shared/{g7ez901n.js → ywspezzx.js} +1 -1
  195. package/dist/shared/z7s6fpxv.js +62 -0
  196. package/dist/shared/{n7mcc1gk.js → zchaghkf.js} +4 -3
  197. package/dist/shared/{9m80mkac.js → ze4hxsmy.js} +1 -1
  198. package/dist/shared/zm0ptry0.js +40 -0
  199. package/dist/shared/{nsva2a6b.js → znm0bpp5.js} +3 -0
  200. package/dist/shared/zp9fmend.js +145 -0
  201. package/dist/shared/{y005b761.js → zy8r7qvz.js} +6 -2
  202. package/dist/tooling/cli.js +41 -15
  203. package/dist/tooling/migrate/apply.d.ts +14 -0
  204. package/dist/tooling/migrate/check.d.ts +3 -4
  205. package/dist/tooling/migrate/classify.d.ts +4 -0
  206. package/dist/tooling/migrate/config.d.ts +3 -2
  207. package/dist/tooling/migrate/engine.d.ts +3 -2
  208. package/dist/tooling/migrate/files.d.ts +11 -1
  209. package/dist/tooling/migrate/index.js +14 -14
  210. package/dist/tooling/migrate/lint.d.ts +1 -1
  211. package/dist/tooling/migrate/lost.d.ts +28 -0
  212. package/dist/tooling/migrate/paths.d.ts +14 -0
  213. package/dist/tooling/migrate/plan.d.ts +6 -3
  214. package/dist/tooling/migrate/runner.d.ts +25 -0
  215. package/dist/tooling/migrate/sandbox.d.ts +57 -0
  216. package/dist/tooling/migrate/sql-lex.d.ts +20 -3
  217. package/dist/tooling/migrate/text.d.ts +42 -0
  218. package/dist/tooling/node-version.d.ts +21 -0
  219. package/dist/tooling/testing/facts.d.ts +33 -5
  220. package/dist/tooling/testing/index.d.ts +1 -1
  221. package/dist/tooling/testing/index.js +3 -3
  222. package/dist/tooling/testing/isolation.d.ts +11 -5
  223. package/package.json +8 -3
  224. package/dist/shared/7m88x3e9.js +0 -45
  225. package/dist/shared/7zwwyh0v.js +0 -448
  226. package/dist/shared/aj2qxyhj.js +0 -90
  227. package/dist/shared/caw8b9d8.js +0 -204
  228. package/dist/shared/jwtxdj06.js +0 -76
  229. package/dist/shared/kfjp0gck.js +0 -212
  230. package/dist/shared/q58y17hy.js +0 -21
  231. package/dist/shared/qm7cdtvr.js +0 -912
  232. package/dist/shared/r8x5qhd7.js +0 -86
  233. package/dist/shared/zqrnhwsq.js +0 -31
@@ -1,204 +0,0 @@
1
- import {
2
- PgSession
3
- } from "./krxx77g0.js";
4
- import {
5
- isConnectionLost
6
- } from "./pf0d8f77.js";
7
- import {
8
- driverErrors,
9
- rejectClosed,
10
- mapFailure,
11
- runCall,
12
- rowsFrom,
13
- resultFrom
14
- } from "./cgbamx3e.js";
15
-
16
- // src/adapters/pg/postgres-extra.ts
17
- var RESET_ALL = "RESET ALL";
18
- var UNLOCK = "SELECT pg_advisory_unlock_all()";
19
- async function checkout(pool) {
20
- if (pool.isClosed())
21
- return rejectClosed();
22
- const reserved = await reserveConnection(pool.sql, pool.acquireMs, pool.counters);
23
- pool.counters.reserved += 1;
24
- const local = new Set;
25
- const session = new PgSession(reserved, pool.counters, "reserved", local, pool.isClosed, pool.poolInflight, pool.notices, pool.named);
26
- let released = false;
27
- let lost = false;
28
- const guard = (run) => lost ? rejectClosed("The connection was lost.") : run().catch((error) => {
29
- if (isConnectionLost(error))
30
- lost = true;
31
- throw error;
32
- });
33
- return {
34
- execute: (text, params, options) => guard(() => runCall(pool.isClosed(), options, (watch) => session.query(text, params, watch))),
35
- batch: (statements, options) => guard(() => runCall(pool.isClosed(), options, async (watch) => {
36
- const { runAtomicBatch } = await import("./efmy2gcg.js");
37
- return runAtomicBatch(session, statements, watch);
38
- })),
39
- cancel() {
40
- session.cancel();
41
- },
42
- async release() {
43
- if (released)
44
- return;
45
- released = true;
46
- try {
47
- if (!lost)
48
- await resetConnection(reserved, session.depth());
49
- } finally {
50
- pool.counters.reserved = Math.max(0, pool.counters.reserved - 1);
51
- if (!lost)
52
- reserved.release();
53
- }
54
- }
55
- };
56
- }
57
- async function describe(sql, text, params) {
58
- const pending = sql.unsafe(text, asParams(params), { prepare: true });
59
- const described = await pending.describe();
60
- return {
61
- columns: described.columns.map((column) => column.name),
62
- parameterCount: described.types.length
63
- };
64
- }
65
- async function* stream(sql, text, params) {
66
- const pending = sql.unsafe(text, asParams(params));
67
- for await (const chunk of pending.cursor(64)) {
68
- yield rowsFrom(chunk);
69
- }
70
- }
71
- function reserveConnection(sql, acquireMs, counters) {
72
- counters.waiting += 1;
73
- let settled = false;
74
- return new Promise((resolve, reject) => {
75
- let timer;
76
- if (acquireMs !== undefined) {
77
- timer = setTimeout(() => {
78
- settled = true;
79
- counters.waiting = Math.max(0, counters.waiting - 1);
80
- driverErrors().then((errors) => {
81
- reject(errors.acquireTimeout());
82
- });
83
- }, acquireMs);
84
- }
85
- sql.reserve().then((connection) => {
86
- if (settled) {
87
- resetConnection(connection, 0).finally(() => {
88
- connection.release();
89
- });
90
- return;
91
- }
92
- if (timer !== undefined)
93
- clearTimeout(timer);
94
- counters.waiting = Math.max(0, counters.waiting - 1);
95
- resolve(connection);
96
- }, (error) => {
97
- if (settled)
98
- return;
99
- if (timer !== undefined)
100
- clearTimeout(timer);
101
- counters.waiting = Math.max(0, counters.waiting - 1);
102
- mapFailure(error).then(reject);
103
- });
104
- });
105
- }
106
- async function resetConnection(sql, depth) {
107
- if (depth > 0)
108
- await sql.unsafe("ROLLBACK").catch(() => {
109
- return;
110
- });
111
- try {
112
- await sql.unsafe(RESET_ALL);
113
- await sql.unsafe(UNLOCK);
114
- } catch {
115
- await sql.unsafe("ROLLBACK").catch(() => {
116
- return;
117
- });
118
- await sql.unsafe(RESET_ALL);
119
- await sql.unsafe(UNLOCK);
120
- }
121
- }
122
- function asParams(params) {
123
- if (params === undefined)
124
- return [];
125
- return params;
126
- }
127
- async function holdCheckout(gate, checkout) {
128
- try {
129
- const connection = await checkout();
130
- let released = false;
131
- const release = connection.release.bind(connection);
132
- connection.release = () => {
133
- if (released)
134
- return Promise.resolve();
135
- released = true;
136
- return release().finally(() => {
137
- gate.leave();
138
- });
139
- };
140
- return connection;
141
- } catch (error) {
142
- gate.leave();
143
- throw error;
144
- }
145
- }
146
- async function holdListener(gate, start) {
147
- try {
148
- const stop = await start();
149
- let stopped = false;
150
- return async () => {
151
- if (stopped)
152
- return;
153
- stopped = true;
154
- try {
155
- await stop();
156
- } finally {
157
- gate.leave();
158
- }
159
- };
160
- } catch (error) {
161
- gate.leave();
162
- throw error;
163
- }
164
- }
165
- function watchQuery(pending, watch, onAbort, ctx) {
166
- return pending.then((result) => {
167
- finishWatch(pending, watch, onAbort, ctx);
168
- const why = watch.reason();
169
- if (why === "timeout" || why === "cancelled")
170
- return classify(why, watch);
171
- ctx.note(ctx.text);
172
- return resultFrom(result, ctx.notices.since(ctx.start));
173
- }, (error) => {
174
- finishWatch(pending, watch, onAbort, ctx);
175
- return classify(error, watch);
176
- });
177
- }
178
- function finishWatch(pending, watch, onAbort, ctx) {
179
- ctx.leave();
180
- ctx.untrack(pending);
181
- watch.signal.removeEventListener("abort", onAbort);
182
- }
183
- async function classify(error, watch) {
184
- const errors = await driverErrors();
185
- if (error instanceof errors.DriverError)
186
- return Promise.reject(error);
187
- const why = typeof error === "string" ? error : watch.reason();
188
- if (why === "timeout") {
189
- return Promise.reject(errors.timedOut(typeof error === "string" ? undefined : error));
190
- }
191
- const code = errors.errorField(error, "code");
192
- if (why === "cancelled" || code === "57014") {
193
- return Promise.reject(errors.cancelled(typeof error === "string" ? undefined : error));
194
- }
195
- return Promise.reject(errors.mapDriverError(error));
196
- }
197
- export {
198
- checkout,
199
- describe,
200
- holdCheckout,
201
- holdListener,
202
- stream,
203
- watchQuery
204
- };
@@ -1,76 +0,0 @@
1
- import {
2
- send2,
3
- reserve,
4
- park
5
- } from "./k8v5v5wj.js";
6
- import {
7
- isConnectionLost
8
- } from "./pf0d8f77.js";
9
- import {
10
- WireSession
11
- } from "./4az561fn.js";
12
- import {
13
- rejectClosed,
14
- runCall
15
- } from "./cgbamx3e.js";
16
-
17
- // src/adapters/pg/bunsql-extra.ts
18
- async function checkout(held) {
19
- const { counters, isClosed } = held;
20
- if (isClosed())
21
- return rejectClosed();
22
- const reserved = held.parked.pop() ?? await reserve(held.sql, held.acquireMs, counters);
23
- counters.reserved += 1;
24
- const session = new WireSession((text, params) => send2(reserved, text, params), held.notices, isClosed, false, () => {
25
- counters.reservedBusy += 1;
26
- }, () => {
27
- counters.reservedBusy = Math.max(0, counters.reservedBusy - 1);
28
- });
29
- let lost = false;
30
- const guard = (run) => lost ? rejectClosed("The connection was lost.") : run().catch((error) => {
31
- if (isConnectionLost(error))
32
- lost = true;
33
- throw error;
34
- });
35
- let released = false;
36
- return {
37
- execute: (text, params, options) => guard(() => runCall(isClosed(), options, (watch) => session.query(text, params, watch))),
38
- batch: (statements, options) => guard(() => runCall(isClosed(), options, async (watch) => {
39
- const { runAtomicBatch } = await import("./efmy2gcg.js");
40
- return runAtomicBatch(session, statements, watch);
41
- })),
42
- async release() {
43
- if (released)
44
- return;
45
- released = true;
46
- try {
47
- if (!lost)
48
- await reset(session, reserved);
49
- } finally {
50
- counters.reserved = Math.max(0, counters.reserved - 1);
51
- if (!lost)
52
- await park(held.parked, reserved);
53
- }
54
- }
55
- };
56
- }
57
- async function reset(session, sql) {
58
- const run = (text) => sql.unsafe(text);
59
- if (session.depth() > 0)
60
- await run("ROLLBACK").catch(() => {
61
- return;
62
- });
63
- try {
64
- await run("RESET ALL");
65
- await run("SELECT pg_advisory_unlock_all()");
66
- } catch {
67
- await run("ROLLBACK").catch(() => {
68
- return;
69
- });
70
- await run("RESET ALL");
71
- await run("SELECT pg_advisory_unlock_all()");
72
- }
73
- }
74
- export {
75
- checkout
76
- };
@@ -1,212 +0,0 @@
1
- import {
2
- readHandle,
3
- readCall,
4
- attachHttp
5
- } from "./hf22e0wt.js";
6
- import {
7
- emitWhere,
8
- emitOrder,
9
- rejectKeys,
10
- readLimit,
11
- projectExpr,
12
- cell,
13
- indexes,
14
- quote,
15
- list,
16
- isRecord,
17
- fail
18
- } from "./gbtcc2d7.js";
19
- import {
20
- OkmError2,
21
- throwNamed2
22
- } from "./52szzakm.js";
23
-
24
- // src/runtime/aggregate.ts
25
- var OPTIONS = [
26
- "where",
27
- "groupBy",
28
- "count",
29
- "sum",
30
- "avg",
31
- "min",
32
- "max",
33
- "orderBy",
34
- "limit",
35
- "route",
36
- "signal",
37
- "timeout"
38
- ];
39
- var NUMERIC = /^(smallint|integer|bigint|real|double precision|numeric|decimal)\b/;
40
- var AS_NUMBER = (wire) => Number(wire);
41
- var AS_TEXT = (wire) => wire;
42
- var UNORDERED = /^(json|jsonb|bytea|point|line|tsvector)\b|\[\]$/;
43
- var FUNCTIONS = [
44
- { option: "sum", tag: "s", numeric: true },
45
- { option: "avg", tag: "a", numeric: true },
46
- { option: "min", tag: "n", numeric: false },
47
- { option: "max", tag: "x", numeric: false }
48
- ];
49
- function build(session, table, options, mods, view) {
50
- try {
51
- return plan(session, table, options, mods, view);
52
- } catch (error) {
53
- throw error instanceof OkmError2 ? attachHttp(session.http, error) : error;
54
- }
55
- }
56
- function plan(session, table, options, mods, view) {
57
- const record = isRecord(options) ? options : {};
58
- rejectKeys(record, OPTIONS, "aggregate");
59
- const indexed = indexes(session.schema).get(table);
60
- if (indexed === undefined)
61
- fail("OKM1120", `Table ${table} is not in the schema.`);
62
- const spec = parse(indexed, record);
63
- if (spec.groups.length > 0 && record.limit === undefined && mods.all === undefined) {
64
- fail("OKM1101", "aggregate() with groupBy needs a limit, or .all(reason) for every group.");
65
- }
66
- const orderBy = record.orderBy;
67
- if (orderBy !== undefined) {
68
- if (!isRecord(orderBy))
69
- fail("OKM1120", "orderBy must be an object of fields.");
70
- for (const field of Object.keys(orderBy)) {
71
- if (!spec.groups.some((column) => column.field === field)) {
72
- throwNamed2("OKM1120", field, spec.groups.map((column) => column.field), `aggregate() orders by a groupBy field. Accepted names: ${list(spec.groups.map((column) => column.field))}.`);
73
- }
74
- }
75
- }
76
- if (record.limit !== undefined)
77
- readLimit(record.limit);
78
- const base = readCall("find", table, {
79
- where: record.where,
80
- orderBy,
81
- limit: record.limit,
82
- signal: record.signal,
83
- timeout: record.timeout,
84
- route: record.route
85
- }, mods.all, session.scope, session.schema.model[table], session.schema, view);
86
- const call = { ...base, op: "aggregate", build: statement(spec) };
87
- return readHandle(session, table, call, mods, (compiled, rows) => decode(compiled, rows));
88
- }
89
- function parse(table, record) {
90
- const groups = columns(table, record.groupBy, "groupBy");
91
- const count = record.count === true;
92
- if (record.count !== undefined && typeof record.count !== "boolean") {
93
- fail("OKM1120", "count is true when you want a row count.");
94
- }
95
- const calls = [];
96
- for (const { option, tag, numeric } of FUNCTIONS) {
97
- for (const column of columns(table, record[option], option)) {
98
- const fits = numeric ? NUMERIC.test(column.dataType) : !UNORDERED.test(column.dataType);
99
- if (!fits) {
100
- fail("OKM1124", `${option} does not apply to ${column.dataType} column ${column.field}. ${numeric ? "sum and avg take integer, real, and numeric columns." : "min and max need an ordered type."}`);
101
- }
102
- calls.push({ option, tag, column, decode: numeric ? sumDecode(column) : column.decode });
103
- }
104
- }
105
- if (!count && calls.length === 0 && groups.length === 0) {
106
- fail("OKM1120", "aggregate() needs count, sum, avg, min, max, or groupBy.");
107
- }
108
- return { groups, count, calls };
109
- }
110
- function sumDecode(column) {
111
- const kind = typeof (column.decode?.("0") ?? "");
112
- if (kind === "string")
113
- return AS_TEXT;
114
- if (kind === "number")
115
- return AS_NUMBER;
116
- return fail("OKM1124", `sum and avg need a column whose value is a number or a string. ${column.field} reads as ${kind}.`);
117
- }
118
- function columns(table, value, option) {
119
- if (value === undefined)
120
- return [];
121
- if (!Array.isArray(value))
122
- fail("OKM1120", `${option} must be a list of field names.`);
123
- const out = [];
124
- for (const name of value) {
125
- const column = typeof name === "string" ? table.columns.get(name) : undefined;
126
- if (typeof name !== "string" || column === undefined) {
127
- throwNamed2("OKM1120", String(name), table.names, `Field ${String(name)} is not on ${table.model.name}. Accepted names: ${list(table.names)}.`);
128
- }
129
- if (column.hidden) {
130
- fail("OKM1120", `Field ${column.field} is hidden, so ${option} cannot read it.`);
131
- }
132
- out.push(column);
133
- }
134
- return out;
135
- }
136
- function statement(spec) {
137
- return (schema, table, sink, outputs, call) => {
138
- sink.mark(`aggregate|${table.model.name}|g:${spec.groups.map((column) => column.field).join(",")}|c:${String(spec.count)}|${spec.calls.map((item) => `${item.tag}:${item.column.field}`).join(",")}|`);
139
- sink.text("select ");
140
- const wrote = { at: 0 };
141
- for (const column of spec.groups) {
142
- put(sink, outputs, wrote, projectExpr(`t.${quote(column.sql)}`, column.dataType), {
143
- key: `g:${column.field}`,
144
- decode: column.decode
145
- });
146
- }
147
- if (spec.count)
148
- put(sink, outputs, wrote, "count(*)::text", { key: "c:", decode: undefined });
149
- for (const item of spec.calls) {
150
- const ref = `t.${quote(item.column.sql)}`;
151
- const expr = item.option === "sum" || item.option === "avg" ? `${item.option}(${ref})::text` : projectExpr(`${item.option}(${ref})`, item.column.dataType);
152
- put(sink, outputs, wrote, expr, {
153
- key: `${item.tag}:${item.column.field}`,
154
- decode: item.decode
155
- });
156
- }
157
- sink.text(" from ");
158
- sink.text(quote(table.model.sql));
159
- sink.text(" t");
160
- emitWhere(schema, table, call.where, sink, "t", 0);
161
- if (spec.groups.length > 0) {
162
- sink.text(" group by ");
163
- sink.text(spec.groups.map((column) => `t.${quote(column.sql)}`).join(", "));
164
- if (isRecord(call.orderBy) && Object.keys(call.orderBy).length > 0) {
165
- emitOrder(table, call.orderBy, sink, "t");
166
- } else {
167
- sink.text(" order by ");
168
- sink.text(spec.groups.map((column) => `t.${quote(column.sql)} asc nulls last`).join(", "));
169
- }
170
- }
171
- if (call.limit !== undefined) {
172
- const limit = String(call.limit);
173
- sink.text(" limit ");
174
- sink.param(limit);
175
- sink.mark(`l:${limit}`);
176
- }
177
- if (typeof call.all === "string")
178
- sink.mark(`all:${call.all}`);
179
- };
180
- }
181
- function put(sink, outputs, wrote, expr, out) {
182
- if (wrote.at > 0)
183
- sink.text(", ");
184
- sink.text(expr);
185
- outputs?.fields.push({ key: out.key, at: wrote.at, decode: out.decode });
186
- wrote.at += 1;
187
- }
188
- function decode(compiled, rows) {
189
- const items = [];
190
- for (const row of rows) {
191
- const item = {};
192
- for (const field of compiled.outputs.fields) {
193
- const value = row[field.at];
194
- const tag = field.key.slice(0, 2);
195
- const name = field.key.slice(2);
196
- if (tag === "g:") {
197
- item[name] = cell(value, field.decode);
198
- } else if (tag === "c:") {
199
- item.count = Number(value ?? 0);
200
- } else {
201
- const group = tag === "s:" ? "sum" : tag === "a:" ? "avg" : tag === "n:" ? "min" : "max";
202
- const into = item[group] ??= {};
203
- into[name] = cell(value, field.decode);
204
- }
205
- }
206
- items.push(item);
207
- }
208
- return items;
209
- }
210
- export {
211
- build
212
- };
@@ -1,21 +0,0 @@
1
- import {
2
- applyTarget2,
3
- applyProject2,
4
- pushProject2,
5
- backfillTiming2
6
- } from "./cctw17hj.js";
7
- import"./gde73es6.js";
8
- import"./szm0cksa.js";
9
- import"./fancm5pt.js";
10
- import"./qv2ydck9.js";
11
- import"./ga3sfy93.js";
12
- import"./n442h54c.js";
13
- import"./8xsbagad.js";
14
- import"./8bwbwy8h.js";
15
- import"./krxx77g0.js";
16
- export {
17
- applyProject2 as applyProject,
18
- applyTarget2 as applyTarget,
19
- backfillTiming2 as backfillTiming,
20
- pushProject2 as pushProject
21
- };