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.
- package/README.md +100 -1131
- package/dist/adapters/capabilities.d.ts +35 -0
- package/dist/adapters/failure.d.ts +13 -2
- package/dist/adapters/lazy-errors.d.ts +22 -0
- package/dist/adapters/pg/bun-sql.d.ts +53 -0
- package/dist/adapters/pg/bunsql-extra.d.ts +54 -3
- package/dist/adapters/pg/bunsql.d.ts +15 -2
- package/dist/adapters/pg/cursor.d.ts +17 -0
- package/dist/adapters/pg/neon-extra.d.ts +18 -0
- package/dist/adapters/pg/neon.d.ts +104 -0
- package/dist/adapters/pg/no-inflight-timeout.d.ts +11 -8
- package/dist/adapters/pg/nodepostgres-extra.d.ts +22 -1
- package/dist/adapters/pg/nodepostgres.d.ts +12 -0
- package/dist/adapters/pg/pglite-wire.d.ts +24 -0
- package/dist/adapters/pg/pglite.d.ts +10 -0
- package/dist/adapters/pg/postgres-extra.d.ts +27 -11
- package/dist/adapters/pg/reserved.d.ts +42 -0
- package/dist/adapters/pg/session.d.ts +0 -18
- package/dist/contracts/connection.d.ts +7 -6
- package/dist/contracts/error.d.ts +1 -1
- package/dist/dialects/pg/floor.d.ts +68 -0
- package/dist/dialects/pg/fn/index.d.ts +39 -1
- package/dist/dialects/pg/fn/index.js +118 -26
- package/dist/dialects/pg/index.d.ts +8 -1
- package/dist/dialects/pg/index.js +435 -170
- package/dist/dialects/pg/introspect.d.ts +11 -0
- package/dist/dialects/pg/keys.d.ts +11 -8
- package/dist/dialects/pg/model.d.ts +40 -0
- package/dist/dialects/pg/morph.d.ts +43 -0
- package/dist/dialects/pg/operator-fit.d.ts +2 -2
- package/dist/dialects/pg/operators.d.ts +14 -7
- package/dist/dialects/pg/ops/bucket.d.ts +65 -0
- package/dist/dialects/pg/ops/fragment.d.ts +119 -0
- package/dist/dialects/pg/ops/inc.d.ts +2 -2
- package/dist/dialects/pg/ops/lane.d.ts +49 -0
- package/dist/dialects/pg/ops/matches.d.ts +9 -12
- package/dist/dialects/pg/ops/relevance.d.ts +13 -0
- package/dist/dialects/pg/ops/search-query.d.ts +22 -0
- package/dist/dialects/pg/ops/window.d.ts +103 -0
- package/dist/dialects/pg/preset.d.ts +1 -1
- package/dist/dialects/pg/relations.d.ts +100 -3
- package/dist/dialects/pg/schema.d.ts +31 -3
- package/dist/dialects/pg/view/client.d.ts +14 -6
- package/dist/dialects/pg/view/index.d.ts +73 -1
- package/dist/dialects/pg/view/index.js +156 -5
- package/dist/dialects/pg/view/scratch.d.ts +8 -5
- package/dist/runtime/aggregate.d.ts +8 -2
- package/dist/runtime/bucket-group.d.ts +70 -0
- package/dist/runtime/conflict.d.ts +22 -4
- package/dist/runtime/include.d.ts +0 -8
- package/dist/runtime/inflight.d.ts +1 -1
- package/dist/runtime/otel.d.ts +105 -0
- package/dist/runtime/otel.js +97 -0
- package/dist/runtime/page.d.ts +4 -1
- package/dist/runtime/pg/bun.js +12 -6
- package/dist/runtime/pg/neon.d.ts +38 -0
- package/dist/runtime/pg/neon.js +64 -0
- package/dist/runtime/pg/pg.js +5 -5
- package/dist/runtime/pg/pglite.d.ts +1 -0
- package/dist/runtime/pg/pglite.js +9 -9
- package/dist/runtime/pg/postgresjs.js +5 -5
- package/dist/runtime/plan.d.ts +95 -5
- package/dist/runtime/read-stream.d.ts +13 -16
- package/dist/runtime/result-name.d.ts +17 -0
- package/dist/runtime/server-check.d.ts +3 -3
- package/dist/runtime/sort-bind.d.ts +21 -0
- package/dist/runtime/sort.d.ts +56 -0
- package/dist/runtime/sql-fragment.d.ts +43 -0
- package/dist/runtime/sync.d.ts +70 -0
- package/dist/runtime/tenancy/index.js +15 -13
- package/dist/runtime/text-search.d.ts +20 -0
- package/dist/runtime/topology.d.ts +1 -1
- package/dist/runtime/traits/index.d.ts +80 -0
- package/dist/runtime/traits/index.js +184 -21
- package/dist/runtime/tree.d.ts +38 -0
- package/dist/runtime/types.d.ts +232 -22
- package/dist/runtime/validate/index.js +4 -4
- package/dist/runtime/version.d.ts +39 -0
- package/dist/runtime/window.d.ts +21 -0
- package/dist/runtime/write.d.ts +54 -2
- package/dist/shared/{929h19xp.js → 0wrea3jq.js} +141 -13
- package/dist/shared/{qq3899ht.js → 12z7aznj.js} +243 -14
- package/dist/shared/{1z04fa85.js → 14x60fbf.js} +12 -57
- package/dist/shared/{ek2wt2dz.js → 16kvjebh.js} +4 -4
- package/dist/shared/{wynzeggs.js → 195pzzzz.js} +8 -8
- package/dist/shared/1d9q9cv2.js +312 -0
- package/dist/shared/2c13q96h.js +18 -0
- package/dist/shared/{b4v57v3x.js → 2d3xq4ps.js} +2 -2
- package/dist/shared/{1f0xa3cb.js → 3449rcae.js} +5 -5
- package/dist/shared/{wkme6tep.js → 35q0n9tz.js} +8 -16
- package/dist/shared/3bb7wsqt.js +99 -0
- package/dist/shared/3t6kw83m.js +249 -0
- package/dist/shared/{5j36z8dn.js → 4m6hskg8.js} +39 -70
- package/dist/shared/{n076ydsc.js → 4vbf83xx.js} +69 -28
- package/dist/shared/{vmfkhszw.js → 63f4ksbz.js} +33 -26
- package/dist/shared/{c58secgj.js → 63qtkrca.js} +38 -4
- package/dist/shared/6m78c8ss.js +6 -0
- package/dist/shared/{x3jc53kg.js → 760ppr76.js} +15 -7
- package/dist/shared/7ghwamrf.js +40 -0
- package/dist/shared/7kms6fvj.js +20 -0
- package/dist/shared/7n7ddwt4.js +6 -0
- package/dist/shared/9a4ae4p8.js +31 -0
- package/dist/shared/{hazqqecq.js → 9dr2qnx7.js} +63 -34
- package/dist/shared/{h15jx9bc.js → 9dvh1dr6.js} +1 -1
- package/dist/shared/a62rb2da.js +13 -0
- package/dist/shared/{cgbamx3e.js → ae4j5f42.js} +23 -4
- package/dist/shared/agsfb86s.js +233 -0
- package/dist/shared/{8x72s86k.js → b6d6p0zc.js} +13 -7
- package/dist/shared/{ag4c7516.js → b76b8d95.js} +2 -2
- package/dist/shared/{j60w0dvr.js → bah9nbg3.js} +58 -38
- package/dist/shared/bnby0esn.js +10 -0
- package/dist/shared/bsek72fg.js +225 -0
- package/dist/shared/{2cg3wnxc.js → bxas03mq.js} +608 -141
- package/dist/shared/{31vm914h.js → cm3w0ywp.js} +15 -16
- package/dist/shared/{gwn6v667.js → cnkky55j.js} +25 -14
- package/dist/shared/{dk8v79p3.js → d5nmj7vv.js} +1 -1
- package/dist/shared/{ttmg09vd.js → dpc2r7rb.js} +8 -8
- package/dist/shared/dzj09cye.js +10 -0
- package/dist/shared/{sbdme3vy.js → e8zyaqvj.js} +60 -10
- package/dist/shared/{2jd1sn0e.js → ecn8mfdq.js} +6 -6
- package/dist/shared/exn3sd90.js +359 -0
- package/dist/shared/{t3g0k88x.js → f4jy1j7x.js} +39 -22
- package/dist/shared/{y2t3jktd.js → fjpnhqan.js} +20 -13
- package/dist/shared/g5yc06ka.js +272 -0
- package/dist/shared/ga9j92qj.js +25 -0
- package/dist/shared/{3fqwmr09.js → hfqmpasr.js} +6 -6
- package/dist/shared/{wt5rzv1h.js → hm5f8jby.js} +7 -7
- package/dist/shared/{ys0xzyaj.js → hqpxf4a4.js} +224 -19
- package/dist/shared/jebgrkeq.js +179 -0
- package/dist/shared/{09p049xa.js → jp36k7dp.js} +44 -16
- package/dist/shared/jx2m62cs.js +96 -0
- package/dist/shared/{r11n8grd.js → k86n8vq3.js} +7 -4
- package/dist/shared/{2z7wv5tj.js → kgwp4jdd.js} +56 -35
- package/dist/shared/{knmes2kh.js → kj9d8maq.js} +1 -1
- package/dist/shared/{g90m9b0g.js → m4xff2a6.js} +6 -5
- package/dist/shared/{r6nwkxnt.js → m7n9h2a5.js} +4 -4
- package/dist/shared/mfedzr74.js +145 -0
- package/dist/shared/{f0f1n281.js → mkrn359n.js} +60 -11
- package/dist/shared/{p248q79y.js → mnvjvqyp.js} +10 -10
- package/dist/shared/{672t5vym.js → mss6ctgv.js} +2 -2
- package/dist/shared/{4az561fn.js → mtw6jz76.js} +8 -23
- package/dist/shared/n05z9nwz.js +313 -0
- package/dist/shared/{pjr6xrm5.js → n0jmpgj1.js} +20 -17
- package/dist/shared/{p6bjzq9q.js → n41w4fbz.js} +14 -6
- package/dist/shared/{b3awxn7h.js → n454kcnw.js} +1 -1
- package/dist/shared/n7kg8mmx.js +61 -0
- package/dist/shared/nbw1z4yd.js +18 -0
- package/dist/shared/needfe47.js +66 -0
- package/dist/shared/{1nw1f3vs.js → njga14v4.js} +9 -9
- package/dist/shared/{5yv4k6a6.js → nmnsgj7x.js} +145 -3
- package/dist/shared/{vjpnzwjz.js → p3xbdcvt.js} +14 -18
- package/dist/shared/p5nh3r8q.js +622 -0
- package/dist/shared/{nx9hh281.js → pan0370z.js} +13 -12
- package/dist/shared/q0cqt2sp.js +12 -0
- package/dist/shared/qd9hhd8f.js +37 -0
- package/dist/shared/{mb2ycs1p.js → qfw14tna.js} +15 -15
- package/dist/shared/{9s0ypp87.js → sjk86yrx.js} +1 -1
- package/dist/shared/th3be82n.js +208 -0
- package/dist/shared/{ed0b8k4s.js → typ3vy7t.js} +1 -1
- package/dist/shared/{5d8t84hg.js → v1wad97q.js} +14 -14
- package/dist/shared/{x72jb7ad.js → v8g1vgv8.js} +1 -1
- package/dist/shared/{9628a9xt.js → vk7rag46.js} +1 -1
- package/dist/shared/{ze4hxsmy.js → w9ejzaqf.js} +1 -1
- package/dist/shared/{q3j1tq7a.js → wcrheftc.js} +148 -23
- package/dist/shared/wvdjznah.js +213 -0
- package/dist/shared/wz2a78mp.js +8 -0
- package/dist/shared/xfhe73sb.js +436 -0
- package/dist/shared/{r1fdd0f6.js → z581d7ny.js} +5 -2
- package/dist/shared/{8etn7d8w.js → z7s6fpxv.js} +1 -1
- package/dist/shared/{32cnm7kn.js → zbqbmmrp.js} +76 -40
- package/dist/shared/{n7mcc1gk.js → zchaghkf.js} +4 -3
- package/dist/shared/{xq0mvhts.js → zgex00fd.js} +6 -6
- package/dist/shared/{nsva2a6b.js → znm0bpp5.js} +3 -0
- package/dist/shared/zq1he88x.js +155 -0
- package/dist/tooling/cli.js +26 -13
- package/dist/tooling/driver-run.js +4971 -0
- package/dist/tooling/driver-test.d.ts +36 -0
- package/dist/tooling/migrate/commands.d.ts +2 -2
- package/dist/tooling/migrate/engine.d.ts +3 -2
- package/dist/tooling/migrate/index.js +9 -9
- package/dist/tooling/migrate/project.d.ts +3 -0
- package/dist/tooling/shared/03k6res6.js +210 -0
- package/dist/tooling/shared/08kemxjd.js +10 -0
- package/dist/tooling/shared/2revefqt.js +209 -0
- package/dist/tooling/shared/2wtvc7rw.js +1425 -0
- package/dist/tooling/shared/345tbc2k.js +16 -0
- package/dist/tooling/shared/3m1hkg7z.js +344 -0
- package/dist/tooling/shared/3rrw5w8f.js +81 -0
- package/dist/tooling/shared/3tq8pnb8.js +131 -0
- package/dist/tooling/shared/417zdsk7.js +194 -0
- package/dist/tooling/shared/4d2w25vw.js +40 -0
- package/dist/tooling/shared/54tqkxja.js +51 -0
- package/dist/tooling/shared/5bvw2nev.js +12 -0
- package/dist/tooling/shared/6nyfyw70.js +231 -0
- package/dist/tooling/shared/72fyg4zv.js +160 -0
- package/dist/tooling/shared/733a1tq3.js +38 -0
- package/dist/tooling/shared/7ae7rjmn.js +20 -0
- package/dist/tooling/shared/7kms6fvj.js +20 -0
- package/dist/tooling/shared/8sqrsw3s.js +49 -0
- package/dist/tooling/shared/96ps4g1z.js +32 -0
- package/dist/tooling/shared/9kazs5p6.js +195 -0
- package/dist/tooling/shared/9xzm7hjg.js +38 -0
- package/dist/tooling/shared/a6pp9d12.js +49 -0
- package/dist/tooling/shared/a7q7t4n3.js +22 -0
- package/dist/tooling/shared/aftb2mwr.js +18 -0
- package/dist/tooling/shared/bz2dqhab.js +311 -0
- package/dist/tooling/shared/cmptmb4f.js +12 -0
- package/dist/tooling/shared/ddjtfgnx.js +177 -0
- package/dist/tooling/shared/djydjpqt.js +261 -0
- package/dist/tooling/shared/dw08f3m8.js +9 -0
- package/dist/tooling/shared/dy672fz6.js +16 -0
- package/dist/tooling/shared/dzx85q70.js +66 -0
- package/dist/tooling/shared/ec71zqgt.js +270 -0
- package/dist/{shared/d5azex6s.js → tooling/shared/ep8zh3bk.js} +1 -1
- package/dist/tooling/shared/f9mphxej.js +3789 -0
- package/dist/tooling/shared/fb9k2qwd.js +89 -0
- package/dist/tooling/shared/g9ad4n80.js +211 -0
- package/dist/tooling/shared/gfvtj6d0.js +94 -0
- package/dist/tooling/shared/gmcf9cxf.js +294 -0
- package/dist/tooling/shared/htnaj77v.js +17 -0
- package/dist/tooling/shared/j9t5s12a.js +7 -0
- package/dist/tooling/shared/jne7b93p.js +42 -0
- package/dist/tooling/shared/ma2340rx.js +118 -0
- package/dist/tooling/shared/mtrqn2hn.js +505 -0
- package/dist/tooling/shared/n4jqhpqz.js +27 -0
- package/dist/tooling/shared/nf7jmhfa.js +20 -0
- package/dist/tooling/shared/p7f08ktn.js +332 -0
- package/dist/tooling/shared/ppnnnvj8.js +13 -0
- package/dist/tooling/shared/pqy21k24.js +312 -0
- package/dist/tooling/shared/pw8fgahx.js +69 -0
- package/dist/tooling/shared/pwsytxm5.js +17 -0
- package/dist/tooling/shared/rf83gr2d.js +94 -0
- package/dist/tooling/shared/rq5kk8j6.js +401 -0
- package/dist/tooling/shared/rs1jb051.js +12 -0
- package/dist/tooling/shared/smn6kz1p.js +157 -0
- package/dist/tooling/shared/spk1jg3r.js +1696 -0
- package/dist/tooling/shared/tn8gay14.js +143 -0
- package/dist/tooling/shared/vraj6d74.js +42 -0
- package/dist/tooling/shared/vs0t6qt8.js +64 -0
- package/dist/tooling/shared/vx86qjv8.js +150 -0
- package/dist/tooling/shared/w7qe8sj1.js +140 -0
- package/dist/tooling/shared/wt7j7nf6.js +15 -0
- package/dist/tooling/shared/wwnekq5n.js +6 -0
- package/dist/tooling/shared/xhrnjch9.js +185 -0
- package/dist/tooling/shared/xjs6zqtc.js +190 -0
- package/dist/tooling/shared/y031jct9.js +16 -0
- package/dist/tooling/shared/y5656f0e.js +233 -0
- package/dist/tooling/shared/yfr8hzhm.js +604 -0
- package/dist/tooling/shared/yk9mp7vb.js +250 -0
- package/dist/tooling/shared/yw30vnq2.js +139 -0
- package/dist/tooling/shared/z8d7ptht.js +618 -0
- package/dist/tooling/shared/zhwnfyb2.js +9 -0
- package/dist/tooling/shared/zvnhh1n0.js +198 -0
- package/dist/tooling/testing/index.js +3 -3
- package/package.json +17 -3
- package/dist/shared/3qnhc7w5.js +0 -38
- package/dist/shared/7m88x3e9.js +0 -45
- package/dist/shared/8dqhth8c.js +0 -12
- package/dist/shared/j3rkk835.js +0 -99
- package/dist/shared/k68eedmy.js +0 -85
- package/dist/shared/venqrjna.js +0 -216
- package/dist/shared/wxr8cwns.js +0 -6
- package/dist/shared/y34a8714.js +0 -150
- package/dist/shared/zm0mafk4.js +0 -92
- package/dist/shared/zy8r7qvz.js +0 -116
- package/dist/shared/{ywr8hmey.js → 606g0nbf.js} +3 -3
- 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
|
+
};
|