okmodel 0.7.0 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +14 -0
- package/dist/adapters/capabilities.d.ts +32 -0
- package/dist/adapters/failure.d.ts +1 -1
- package/dist/adapters/pg/bun-sql.d.ts +53 -0
- package/dist/adapters/pg/bunsql-extra.d.ts +1 -1
- package/dist/adapters/pg/bunsql.d.ts +1 -1
- 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 +1 -1
- package/dist/contracts/error.d.ts +1 -1
- package/dist/dialects/pg/ext/btree-gin.d.ts +26 -0
- package/dist/dialects/pg/ext/btree-gin.js +20 -0
- package/dist/dialects/pg/ext/btree-gist.d.ts +26 -0
- package/dist/dialects/pg/ext/btree-gist.js +20 -0
- package/dist/dialects/pg/ext/call.d.ts +53 -0
- package/dist/dialects/pg/ext/citext.js +1 -1
- package/dist/dialects/pg/ext/feature.d.ts +26 -0
- package/dist/dialects/pg/ext/fuzzystrmatch.d.ts +34 -0
- package/dist/dialects/pg/ext/fuzzystrmatch.js +33 -0
- package/dist/dialects/pg/ext/hstore.d.ts +41 -0
- package/dist/dialects/pg/ext/hstore.js +29 -0
- package/dist/dialects/pg/ext/index.d.ts +56 -0
- package/dist/dialects/pg/ext/index.js +1 -1
- package/dist/dialects/pg/ext/ltree.d.ts +41 -0
- package/dist/dialects/pg/ext/ltree.js +29 -0
- package/dist/dialects/pg/ext/pg-trgm.js +4 -13
- package/dist/dialects/pg/ext/pgcrypto.d.ts +33 -0
- package/dist/dialects/pg/ext/pgcrypto.js +33 -0
- package/dist/dialects/pg/ext/unaccent.d.ts +26 -0
- package/dist/dialects/pg/ext/unaccent.js +21 -0
- package/dist/dialects/pg/ext/using.d.ts +23 -0
- package/dist/dialects/pg/ext/versions.d.ts +36 -0
- package/dist/dialects/pg/external.d.ts +19 -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 +3 -0
- package/dist/dialects/pg/index.js +394 -14
- package/dist/dialects/pg/introspect.d.ts +11 -0
- package/dist/dialects/pg/operators.d.ts +1 -1
- package/dist/dialects/pg/ops/fragment.d.ts +119 -0
- package/dist/dialects/pg/ops/lane.d.ts +49 -0
- 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 +11 -5
- package/dist/runtime/artifact-file.d.ts +22 -0
- package/dist/runtime/client.d.ts +10 -0
- package/dist/runtime/drift-detail.d.ts +2 -1
- package/dist/runtime/drift.d.ts +3 -10
- package/dist/runtime/otel.d.ts +105 -0
- package/dist/runtime/otel.js +97 -0
- package/dist/runtime/pg/bun.js +4 -4
- package/dist/runtime/pg/neon.d.ts +38 -0
- package/dist/runtime/pg/neon.js +64 -0
- package/dist/runtime/pg/pg.js +4 -4
- package/dist/runtime/pg/pglite.js +4 -4
- package/dist/runtime/pg/postgresjs.js +4 -4
- package/dist/runtime/plan.d.ts +1 -1
- package/dist/runtime/server-check.d.ts +36 -6
- package/dist/runtime/sql-fragment.d.ts +43 -0
- package/dist/runtime/tenancy/index.js +10 -10
- package/dist/runtime/topology.d.ts +1 -1
- package/dist/runtime/traits/index.js +16 -17
- package/dist/runtime/types.d.ts +15 -1
- package/dist/runtime/validate/index.js +4 -4
- package/dist/shared/{81ebdx9p.js → 0j6qe91y.js} +4 -4
- package/dist/shared/{qq3899ht.js → 12z7aznj.js} +243 -14
- package/dist/shared/{gzaveazc.js → 1d9q9cv2.js} +29 -2
- package/dist/shared/{w79hsckn.js → 1mxm04fy.js} +5 -5
- package/dist/shared/{7k9grvf6.js → 2szgv6yj.js} +5 -5
- package/dist/shared/{gs74dbj5.js → 2zdxkt7b.js} +933 -111
- package/dist/shared/{m6p12kbr.js → 3456242y.js} +23 -23
- package/dist/shared/3bb7wsqt.js +99 -0
- package/dist/shared/{y6njgyda.js → 3t6kw83m.js} +2 -2
- package/dist/shared/{xhfsfkgk.js → 4vbf83xx.js} +9 -2
- package/dist/shared/{pt94kz7f.js → 5kyb3tqx.js} +1 -1
- package/dist/shared/61bmrmhr.js +35 -0
- package/dist/shared/{x933k9a3.js → 62b603gr.js} +10 -10
- package/dist/shared/{jgqj11w4.js → 6f0z3hzh.js} +4 -0
- package/dist/shared/{tsvww22q.js → 6nae0spm.js} +156 -95
- package/dist/shared/{mtaszv5a.js → 71n0jmhc.js} +102 -4
- package/dist/shared/{5e3tf63j.js → 760ppr76.js} +14 -6
- package/dist/shared/7ba5h6pb.js +17 -0
- package/dist/shared/{sfz1ksn6.js → 8jesqsrk.js} +2 -2
- package/dist/shared/{5v3xf80k.js → 8n688yjg.js} +1 -1
- package/dist/shared/{rs73fca9.js → 8qzkd9f3.js} +6 -6
- package/dist/shared/{ax7qv55k.js → 90sg0w5a.js} +202 -25
- package/dist/shared/{zm0ptry0.js → 99qp9vtg.js} +1 -1
- package/dist/shared/{xnxsed62.js → 9dr2qnx7.js} +14 -14
- package/dist/shared/{1w508jbr.js → 9dtgn1z2.js} +21 -17
- package/dist/shared/{h15jx9bc.js → 9dvh1dr6.js} +1 -1
- package/dist/shared/{q5c6ngn3.js → a62rb2da.js} +1 -1
- package/dist/shared/{te5as7z7.js → aksb89mn.js} +3 -3
- package/dist/shared/{bz84tkzm.js → bphrgwz9.js} +2 -2
- package/dist/shared/bsek72fg.js +225 -0
- package/dist/shared/{7mnme5tn.js → c3mvnq7a.js} +1 -1
- package/dist/shared/{p6qc5gcp.js → ccsmhv9r.js} +25 -7
- package/dist/shared/{pbp1ss85.js → cexb7z6b.js} +2 -2
- package/dist/shared/{ab9gmbwy.js → cf8zdz5p.js} +5 -5
- package/dist/shared/{jvd0mqag.js → ckz05z15.js} +5 -5
- package/dist/shared/{dk8v79p3.js → d5nmj7vv.js} +1 -1
- package/dist/shared/{14vx95q4.js → dd5pmnky.js} +7 -7
- package/dist/shared/{vb6qfyve.js → ds229x4r.js} +2 -2
- package/dist/shared/{1qa0qajz.js → e275q7sd.js} +1 -1
- package/dist/shared/{zp9fmend.js → ebhz8vh6.js} +4 -4
- package/dist/shared/{hyh73qk6.js → ecn8mfdq.js} +5 -5
- package/dist/shared/{je4axnm8.js → fdsz3w80.js} +2 -2
- package/dist/shared/{9s0ypp87.js → fk9vef30.js} +85 -74
- package/dist/shared/{g6evj1j3.js → g89fmz5s.js} +1 -1
- package/dist/shared/{aytqh5y2.js → ghqzdp94.js} +2 -2
- package/dist/shared/h9811jjm.js +17 -0
- package/dist/shared/{dkxgzaqd.js → hqpxf4a4.js} +1 -1
- package/dist/shared/{8skf0cam.js → jpybhf98.js} +1 -1
- package/dist/shared/{67txcrcw.js → kgwp4jdd.js} +4 -4
- package/dist/shared/kzgrnckd.js +29 -0
- package/dist/shared/mazpbcdj.js +233 -0
- package/dist/shared/{kb2sdhfb.js → mfvx6kjx.js} +17 -1
- package/dist/shared/{svncdeh5.js → mgaq2sd3.js} +11 -8
- package/dist/shared/{5mtf6cj3.js → mvmm3xpb.js} +5 -2
- package/dist/shared/{b5pftnn2.js → myk8fswx.js} +66 -21
- package/dist/shared/{2shdbn8q.js → n306gqkd.js} +9 -9
- package/dist/shared/{n816peh1.js → n454kcnw.js} +1 -1
- package/dist/shared/n5whk3mt.js +665 -0
- package/dist/shared/n7kg8mmx.js +61 -0
- package/dist/shared/{vqrjpcqn.js → nmnsgj7x.js} +1 -1
- package/dist/shared/{tbrvgw6z.js → npdftanb.js} +19 -26
- package/dist/shared/{ek2wt2dz.js → pg4ezgyf.js} +11 -30
- package/dist/shared/{5fe3kbsh.js → pgsak1yw.js} +1 -1
- package/dist/shared/{bm22fvzr.js → qfw14tna.js} +2 -2
- package/dist/shared/{wa07eas9.js → qvwwp1mp.js} +62 -41
- package/dist/shared/rbxt795q.js +1766 -0
- package/dist/shared/{xjk7hnn5.js → s7he5e5d.js} +2 -2
- package/dist/shared/{w72kgg4q.js → ss2kv2rs.js} +6 -6
- package/dist/shared/stfa9xcw.js +450 -0
- package/dist/shared/{3v7t1yrv.js → th3be82n.js} +2 -2
- package/dist/shared/{79dt02vc.js → tr64w5p0.js} +1 -1
- package/dist/shared/{cn957f8d.js → tw1nvvfj.js} +3 -3
- package/dist/shared/{ed0b8k4s.js → typ3vy7t.js} +1 -1
- package/dist/shared/{9628a9xt.js → vk7rag46.js} +1 -1
- package/dist/shared/{ze4hxsmy.js → w9ejzaqf.js} +1 -1
- package/dist/shared/{vczs8gd4.js → wvtjg5m3.js} +2 -2
- package/dist/shared/{9y3mmmh1.js → xfhe73sb.js} +1 -1
- package/dist/shared/xqacq37x.js +61 -0
- package/dist/shared/{1est4ezg.js → xtsydxjg.js} +3 -3
- package/dist/shared/xv8qj97g.js +96 -0
- package/dist/shared/{bg3rym3g.js → xwhdcrm4.js} +2 -2
- package/dist/shared/{kr0jzbw2.js → yvabyg9k.js} +7 -7
- package/dist/shared/yybhre6n.js +136 -0
- package/dist/shared/zdqf8k5d.js +8 -0
- package/dist/shared/{jznfax0y.js → zq1he88x.js} +4 -4
- package/dist/tooling/cli.js +108 -23
- package/dist/tooling/driver-run.js +4654 -0
- package/dist/tooling/driver-test.d.ts +36 -0
- package/dist/tooling/github/comment.d.ts +50 -0
- package/dist/tooling/github/post-comment.d.ts +17 -0
- package/dist/tooling/migrate/commands.d.ts +3 -2
- package/dist/tooling/migrate/config.d.ts +13 -0
- package/dist/tooling/migrate/export.d.ts +165 -0
- package/dist/tooling/migrate/ext-surface.d.ts +80 -0
- package/dist/tooling/migrate/ext.d.ts +31 -4
- package/dist/tooling/migrate/import-drizzle.d.ts +40 -0
- package/dist/tooling/migrate/index.d.ts +3 -0
- package/dist/tooling/migrate/index.js +55 -10
- package/dist/tooling/migrate/lint-rules.d.ts +18 -1
- package/dist/tooling/migrate/lint.d.ts +14 -1
- package/dist/tooling/migrate/place.d.ts +18 -0
- package/dist/tooling/migrate/plan-json.d.ts +65 -0
- package/dist/tooling/migrate/project.d.ts +13 -1
- package/dist/tooling/migrate/pull-emit.d.ts +98 -0
- package/dist/tooling/migrate/pull-routines.d.ts +126 -0
- package/dist/tooling/migrate/pull-verify.d.ts +26 -0
- package/dist/tooling/migrate/pull.d.ts +62 -0
- package/dist/tooling/migrate/reprint.d.ts +32 -0
- package/dist/tooling/migrate/sandbox.d.ts +8 -2
- package/dist/tooling/migrate/schema-api.d.ts +43 -0
- package/dist/tooling/migrate/sql-lex.d.ts +18 -0
- package/dist/tooling/shared/04svnxxj.js +344 -0
- package/dist/tooling/shared/0j0jtse1.js +13 -0
- package/dist/tooling/shared/0r05zhyq.js +618 -0
- package/dist/tooling/shared/1800eymw.js +49 -0
- package/dist/tooling/shared/35zemrmf.js +12 -0
- package/dist/tooling/shared/3rmaz3pq.js +140 -0
- package/dist/tooling/shared/3yn79q5m.js +12 -0
- package/dist/tooling/shared/54tqkxja.js +51 -0
- package/dist/tooling/shared/57btdkgm.js +209 -0
- package/dist/tooling/shared/5bmksndh.js +118 -0
- package/dist/tooling/shared/5p8km5jb.js +38 -0
- package/dist/tooling/shared/5x3pf400.js +185 -0
- package/dist/tooling/shared/5zrzffnk.js +6 -0
- package/dist/tooling/shared/62fxpc1p.js +332 -0
- package/dist/tooling/shared/68tf18v4.js +20 -0
- package/dist/tooling/shared/6vk28rjp.js +98 -0
- package/dist/tooling/shared/76pv6qff.js +9 -0
- package/dist/tooling/shared/7aews1zg.js +311 -0
- package/dist/tooling/shared/7b3xp9gc.js +195 -0
- package/dist/tooling/shared/7kms6fvj.js +20 -0
- package/dist/tooling/shared/7pxncnzs.js +40 -0
- package/dist/tooling/shared/829fjxyw.js +198 -0
- package/dist/tooling/shared/8m5znpdt.js +66 -0
- package/dist/tooling/shared/8yr7ges1.js +211 -0
- package/dist/tooling/shared/96ps4g1z.js +32 -0
- package/dist/tooling/shared/9qr9rkbh.js +143 -0
- package/dist/tooling/shared/9sfbtwsk.js +49 -0
- package/dist/tooling/shared/9wsnsvbf.js +10 -0
- package/dist/tooling/shared/a2xe04bm.js +18 -0
- package/dist/tooling/shared/a4pzghha.js +20 -0
- package/dist/tooling/shared/am04v8na.js +233 -0
- package/dist/tooling/shared/asgr07mp.js +194 -0
- package/dist/tooling/shared/c0xevn1h.js +312 -0
- package/dist/tooling/shared/dhatassp.js +134 -0
- package/dist/tooling/shared/ezqn88nj.js +1696 -0
- package/dist/tooling/shared/f77nnfqy.js +231 -0
- package/dist/tooling/shared/fh07z1dt.js +15 -0
- package/dist/tooling/shared/gqwevek4.js +81 -0
- package/dist/tooling/shared/hfq3n3bd.js +4116 -0
- package/dist/tooling/shared/htnaj77v.js +17 -0
- package/dist/tooling/shared/j664qjrt.js +64 -0
- package/dist/tooling/shared/jpgmjsx3.js +38 -0
- package/dist/tooling/shared/jr2nmr81.js +497 -0
- package/dist/tooling/shared/jsjjz875.js +139 -0
- package/dist/tooling/shared/kbqnrs1x.js +1425 -0
- package/dist/tooling/shared/kz715295.js +94 -0
- package/dist/tooling/shared/m3xt8b1h.js +22 -0
- package/dist/tooling/shared/m4hhb8aa.js +9 -0
- package/dist/tooling/shared/mw5npye0.js +16 -0
- package/dist/tooling/shared/n4jqhpqz.js +27 -0
- package/dist/tooling/shared/nxq95eb6.js +42 -0
- package/dist/tooling/shared/qartgazh.js +70 -0
- package/dist/tooling/shared/rcywj2w8.js +190 -0
- package/dist/tooling/shared/s6qha57m.js +16 -0
- package/dist/tooling/shared/shjs0379.js +150 -0
- package/dist/tooling/shared/sjwkt1pg.js +16 -0
- package/dist/tooling/shared/stebqyyd.js +401 -0
- package/dist/tooling/shared/t4xveemq.js +250 -0
- package/dist/tooling/shared/t6zjeake.js +261 -0
- package/dist/tooling/shared/tcwpjvxe.js +9 -0
- package/dist/tooling/shared/tpssp7ac.js +160 -0
- package/dist/tooling/shared/v6j1ep6w.js +42 -0
- package/dist/tooling/shared/vwr2y833.js +270 -0
- package/dist/tooling/shared/w17sj05d.js +7 -0
- package/dist/tooling/shared/w5d89rb4.js +131 -0
- package/dist/tooling/shared/wbwq1jsa.js +157 -0
- package/dist/tooling/shared/wezxf3h8.js +17 -0
- package/dist/tooling/shared/yb4kd19s.js +12 -0
- package/dist/tooling/shared/ybdew6j7.js +294 -0
- package/dist/tooling/shared/yfedr1zb.js +6 -0
- package/dist/tooling/shared/yhp829bx.js +177 -0
- package/dist/tooling/shared/ytk1tjs0.js +604 -0
- package/dist/tooling/shared/zs9aymcp.js +210 -0
- package/dist/tooling/shared/zx3v9phv.js +8 -0
- package/dist/tooling/testing/index.js +3 -3
- package/package.json +50 -9
- package/dist/shared/ktz42yez.js +0 -113
- package/dist/shared/ttsqgp4p.js +0 -71
- package/dist/shared/y34a8714.js +0 -150
- 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,140 @@
|
|
|
1
|
+
import {
|
|
2
|
+
quote,
|
|
3
|
+
fail2
|
|
4
|
+
} from "./hfq3n3bd.js";
|
|
5
|
+
|
|
6
|
+
// src/runtime/sort.ts
|
|
7
|
+
function insertAtEnd(table, columns, rows, group, tenantFields) {
|
|
8
|
+
const position = requiredColumn(table, "position");
|
|
9
|
+
const groupColumn = group === undefined ? undefined : requiredColumn(table, group);
|
|
10
|
+
const tenants = tenantColumns(table, tenantFields);
|
|
11
|
+
const planned = [];
|
|
12
|
+
const countOf = new Map;
|
|
13
|
+
const firstOf = new Map;
|
|
14
|
+
for (const row of rows) {
|
|
15
|
+
if (groupColumn !== undefined && !Object.hasOwn(row, groupColumn.field)) {
|
|
16
|
+
fail2("OKM1121", `insert on ${table.model.name} needs ${groupColumn.field} so position has a group.`, "Pass the group on the row. Position is assigned for you.");
|
|
17
|
+
}
|
|
18
|
+
const key = lockText(table.model.sql, wires(tenants, row), groupColumn === undefined ? null : cellWire(groupColumn, row[groupColumn.field]));
|
|
19
|
+
if (!firstOf.has(key))
|
|
20
|
+
firstOf.set(key, row);
|
|
21
|
+
const n = (countOf.get(key) ?? 0) + 1;
|
|
22
|
+
countOf.set(key, n);
|
|
23
|
+
planned.push({ key, n, row });
|
|
24
|
+
}
|
|
25
|
+
const params = [];
|
|
26
|
+
const bind = (wire, dataType) => {
|
|
27
|
+
params.push(wire);
|
|
28
|
+
return `$${String(params.length)}::${dataType}`;
|
|
29
|
+
};
|
|
30
|
+
const slotOf = new Map;
|
|
31
|
+
for (const key of firstOf.keys())
|
|
32
|
+
slotOf.set(key, bind(key, "text"));
|
|
33
|
+
const baseRows = [];
|
|
34
|
+
for (const [key, row] of firstOf) {
|
|
35
|
+
const parts = [slotOf.get(key) ?? bind(key, "text")];
|
|
36
|
+
if (groupColumn !== undefined)
|
|
37
|
+
parts.push(bind(cellWire(groupColumn, row[groupColumn.field]), groupColumn.dataType));
|
|
38
|
+
for (const column of tenants)
|
|
39
|
+
parts.push(bind(cellWire(column, row[column.field]), column.dataType));
|
|
40
|
+
baseRows.push(`(${parts.join(", ")})`);
|
|
41
|
+
}
|
|
42
|
+
const baseNames = ["okm_lock"];
|
|
43
|
+
if (groupColumn !== undefined)
|
|
44
|
+
baseNames.push("okm_g");
|
|
45
|
+
for (let index = 0;index < tenants.length; index += 1)
|
|
46
|
+
baseNames.push(`okm_t${String(index)}`);
|
|
47
|
+
const lists = planned.map((item) => {
|
|
48
|
+
const slot = slotOf.get(item.key) ?? bind(item.key, "text");
|
|
49
|
+
const slots = columns.map((column) => cell(column, item.row[column.field], bind));
|
|
50
|
+
slots.push(`((select b.pos from base b where b.okm_lock = ${slot}) + ${String(item.n)})::integer`);
|
|
51
|
+
return `(${slots.join(", ")})`;
|
|
52
|
+
});
|
|
53
|
+
const insertColumns = [...columns.map((column) => quote(column.sql)), quote(position.sql)];
|
|
54
|
+
const text = [
|
|
55
|
+
"with base as (",
|
|
56
|
+
"select i.okm_lock, coalesce(max(t.",
|
|
57
|
+
quote(position.sql),
|
|
58
|
+
"), 0) as pos from (values ",
|
|
59
|
+
baseRows.join(", "),
|
|
60
|
+
") as i(",
|
|
61
|
+
baseNames.join(", "),
|
|
62
|
+
") left join ",
|
|
63
|
+
quote(table.model.sql),
|
|
64
|
+
" as t on ",
|
|
65
|
+
joinOn(groupColumn, tenants),
|
|
66
|
+
" group by i.okm_lock",
|
|
67
|
+
") insert into ",
|
|
68
|
+
quote(table.model.sql),
|
|
69
|
+
" (",
|
|
70
|
+
insertColumns.join(", "),
|
|
71
|
+
") values ",
|
|
72
|
+
lists.join(", ")
|
|
73
|
+
].join("");
|
|
74
|
+
return { lock: groupLock([...firstOf.keys()]), text, params };
|
|
75
|
+
}
|
|
76
|
+
function joinOn(group, tenants) {
|
|
77
|
+
const parts = [];
|
|
78
|
+
if (group !== undefined)
|
|
79
|
+
parts.push(`t.${quote(group.sql)} is not distinct from i.okm_g`);
|
|
80
|
+
for (let index = 0;index < tenants.length; index += 1) {
|
|
81
|
+
const column = tenants[index];
|
|
82
|
+
if (column === undefined)
|
|
83
|
+
continue;
|
|
84
|
+
parts.push(`t.${quote(column.sql)} is not distinct from i.okm_t${String(index)}`);
|
|
85
|
+
}
|
|
86
|
+
return parts.length === 0 ? "true" : parts.join(" and ");
|
|
87
|
+
}
|
|
88
|
+
function lockText(sqlName, tenants, group) {
|
|
89
|
+
return JSON.stringify([sqlName, tenants, group]);
|
|
90
|
+
}
|
|
91
|
+
function groupLock(keys) {
|
|
92
|
+
const ordered = [...keys].sort(compareText);
|
|
93
|
+
const values = ordered.map((_, index) => `($${String(index + 1)}::text)`).join(", ");
|
|
94
|
+
return {
|
|
95
|
+
text: `select pg_advisory_xact_lock(hashtextextended(k.okm_lock, 0::bigint)) from (select v.okm_lock from (values ${values}) as v(okm_lock) order by v.okm_lock) as k`,
|
|
96
|
+
params: ordered
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
function compareText(left, right) {
|
|
100
|
+
if (left < right)
|
|
101
|
+
return -1;
|
|
102
|
+
if (left > right)
|
|
103
|
+
return 1;
|
|
104
|
+
return 0;
|
|
105
|
+
}
|
|
106
|
+
function wires(columns, row) {
|
|
107
|
+
return columns.map((column) => cellWire(column, row[column.field]));
|
|
108
|
+
}
|
|
109
|
+
function cellWire(column, value) {
|
|
110
|
+
if (value === undefined || value === null)
|
|
111
|
+
return null;
|
|
112
|
+
return column.encode(value);
|
|
113
|
+
}
|
|
114
|
+
function cell(column, value, bind) {
|
|
115
|
+
if (value === undefined)
|
|
116
|
+
return "default";
|
|
117
|
+
if (value === null)
|
|
118
|
+
return bind(null, column.dataType);
|
|
119
|
+
return bind(column.encode(value), column.dataType);
|
|
120
|
+
}
|
|
121
|
+
function tenantColumns(table, fields) {
|
|
122
|
+
if (fields === undefined)
|
|
123
|
+
return [];
|
|
124
|
+
const columns = [];
|
|
125
|
+
for (const field of fields) {
|
|
126
|
+
const column = table.columns.get(field);
|
|
127
|
+
if (column !== undefined)
|
|
128
|
+
columns.push(column);
|
|
129
|
+
}
|
|
130
|
+
return columns;
|
|
131
|
+
}
|
|
132
|
+
function requiredColumn(table, field) {
|
|
133
|
+
const column = table.columns.get(field);
|
|
134
|
+
if (column === undefined)
|
|
135
|
+
fail2("OKM1120", `Table ${table.model.name} has no column ${field}.`);
|
|
136
|
+
return column;
|
|
137
|
+
}
|
|
138
|
+
export {
|
|
139
|
+
insertAtEnd
|
|
140
|
+
};
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
// src/adapters/pg/watch.ts
|
|
2
|
+
var callers = new WeakMap;
|
|
3
|
+
function listenOn(signal, onAbort) {
|
|
4
|
+
let set = callers.get(signal);
|
|
5
|
+
if (set === undefined) {
|
|
6
|
+
const created = new Set;
|
|
7
|
+
callers.set(signal, created);
|
|
8
|
+
signal.addEventListener("abort", () => {
|
|
9
|
+
for (const each of created)
|
|
10
|
+
each();
|
|
11
|
+
}, { once: true });
|
|
12
|
+
set = created;
|
|
13
|
+
}
|
|
14
|
+
const registered = set;
|
|
15
|
+
registered.add(onAbort);
|
|
16
|
+
return () => {
|
|
17
|
+
registered.delete(onAbort);
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
function openWatch(options) {
|
|
21
|
+
let kind;
|
|
22
|
+
const controller = new AbortController;
|
|
23
|
+
const onAbort = () => {
|
|
24
|
+
if (kind === undefined)
|
|
25
|
+
kind = "cancelled";
|
|
26
|
+
controller.abort();
|
|
27
|
+
};
|
|
28
|
+
const release = options.signal === undefined ? undefined : listenOn(options.signal, onAbort);
|
|
29
|
+
let timer;
|
|
30
|
+
if (options.timeout !== undefined) {
|
|
31
|
+
timer = setTimeout(() => {
|
|
32
|
+
if (kind === undefined)
|
|
33
|
+
kind = "timeout";
|
|
34
|
+
controller.abort();
|
|
35
|
+
}, options.timeout);
|
|
36
|
+
}
|
|
37
|
+
return {
|
|
38
|
+
signal: controller.signal,
|
|
39
|
+
finish() {
|
|
40
|
+
if (timer !== undefined)
|
|
41
|
+
clearTimeout(timer);
|
|
42
|
+
release?.();
|
|
43
|
+
},
|
|
44
|
+
reason() {
|
|
45
|
+
return kind;
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
export {
|
|
50
|
+
openWatch
|
|
51
|
+
};
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import {
|
|
2
|
+
refusePrototypeKey
|
|
3
|
+
} from "./0j0jtse1.js";
|
|
4
|
+
import {
|
|
5
|
+
throwNamed,
|
|
6
|
+
parseOrder,
|
|
7
|
+
projectExpr,
|
|
8
|
+
quote,
|
|
9
|
+
list,
|
|
10
|
+
isRecord,
|
|
11
|
+
fail2
|
|
12
|
+
} from "./hfq3n3bd.js";
|
|
13
|
+
|
|
14
|
+
// src/dialects/pg/ops/bucket.ts
|
|
15
|
+
var RUNTIME = Symbol.for("okmodel.bucket");
|
|
16
|
+
function isBucket(value) {
|
|
17
|
+
return typeof value === "object" && value !== null && RUNTIME in value;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// src/runtime/bucket-group.ts
|
|
21
|
+
function readBucket(table, value, fields, aliases) {
|
|
22
|
+
if (!isBucket(value))
|
|
23
|
+
fail2("OKM1120", "bucket group");
|
|
24
|
+
const column = typeof value.column === "string" ? table.columns.get(value.column) : undefined;
|
|
25
|
+
if (typeof value.column !== "string" || column === undefined) {
|
|
26
|
+
throwNamed("OKM1120", String(value.column), table.names, `Field ${String(value.column)} is not on ${table.model.name}. Accepted names: ${list(table.names)}.`);
|
|
27
|
+
}
|
|
28
|
+
if (column.hidden) {
|
|
29
|
+
fail2("OKM1120", `Field ${column.field} is hidden, so groupBy cannot read it.`);
|
|
30
|
+
}
|
|
31
|
+
const unit = unitOf(value.unit);
|
|
32
|
+
if (unit === undefined) {
|
|
33
|
+
fail2("OKM1120", `bucket() unit ${String(value.unit)} is not a time unit. Accepted units: second, minute, hour, day, week, month, quarter, year.`);
|
|
34
|
+
}
|
|
35
|
+
const shift = weekShift(value.weekStart, unit);
|
|
36
|
+
const kind = temporalKind(column.dataType);
|
|
37
|
+
if (kind !== "timestamptz" && kind !== "date") {
|
|
38
|
+
fail2("OKM1120", `bucket() does not apply to ${kind === "timestamp" ? "timestamp" : column.dataType} column ${column.field}. Use timestamptz or date.`);
|
|
39
|
+
}
|
|
40
|
+
if (kind === "date" && clockUnit(unit)) {
|
|
41
|
+
fail2("OKM1120", `bucket() unit ${unit} does not apply to date column ${column.field}. Accepted units: day, week, month, quarter, year.`);
|
|
42
|
+
}
|
|
43
|
+
let tz;
|
|
44
|
+
if (kind === "timestamptz") {
|
|
45
|
+
if (typeof value.tz !== "string") {
|
|
46
|
+
fail2("OKM1120", `bucket() on timestamptz column ${column.field} needs tz, an IANA time zone.`);
|
|
47
|
+
}
|
|
48
|
+
if (!iana(value.tz))
|
|
49
|
+
fail2("OKM1120", `bucket() tz ${value.tz} is not an IANA time zone.`);
|
|
50
|
+
tz = value.tz;
|
|
51
|
+
} else if (value.tz !== undefined) {
|
|
52
|
+
fail2("OKM1120", `bucket() on date column ${column.field} does not take tz.`);
|
|
53
|
+
}
|
|
54
|
+
if (typeof value.as !== "string" || value.as.length === 0) {
|
|
55
|
+
fail2("OKM1120", "bucket() needs as, the name of the result key.");
|
|
56
|
+
}
|
|
57
|
+
refusePrototypeKey(value.as, "bucket as");
|
|
58
|
+
if (value.as === "count" || value.as === "sum" || value.as === "avg" || value.as === "min" || value.as === "max") {
|
|
59
|
+
fail2("OKM1120", `bucket as ${value.as} clashes with an aggregate key.`);
|
|
60
|
+
}
|
|
61
|
+
if (fields.has(value.as) || aliases.has(value.as)) {
|
|
62
|
+
fail2("OKM1120", `bucket as ${value.as} clashes with another group.`);
|
|
63
|
+
}
|
|
64
|
+
aliases.add(value.as);
|
|
65
|
+
return { kind: "bucket", column, unit, tz, as: value.as, shift, clock: clockUnit(unit) };
|
|
66
|
+
}
|
|
67
|
+
function writeBucket(sink, outputs, wrote, group) {
|
|
68
|
+
if (wrote.at > 0)
|
|
69
|
+
sink.text(", ");
|
|
70
|
+
if (group.clock)
|
|
71
|
+
emitClock(sink, group);
|
|
72
|
+
else
|
|
73
|
+
emitCalendar(sink, group);
|
|
74
|
+
outputs?.fields.push({
|
|
75
|
+
key: `g:${group.as}`,
|
|
76
|
+
at: wrote.at,
|
|
77
|
+
decode: bucketDecode(group)
|
|
78
|
+
});
|
|
79
|
+
wrote.at += 1;
|
|
80
|
+
}
|
|
81
|
+
function writeGrouped(sink, names, orderBy) {
|
|
82
|
+
sink.text(ordinals(names.length));
|
|
83
|
+
if (isRecord(orderBy) && Object.keys(orderBy).length > 0) {
|
|
84
|
+
sink.text(" order by ");
|
|
85
|
+
const keys = Object.keys(orderBy);
|
|
86
|
+
for (let index = 0;index < keys.length; index += 1) {
|
|
87
|
+
const key = keys[index];
|
|
88
|
+
if (key === undefined)
|
|
89
|
+
continue;
|
|
90
|
+
const parsed = parseOrder(key, orderBy[key]);
|
|
91
|
+
if (index > 0)
|
|
92
|
+
sink.text(", ");
|
|
93
|
+
sink.text(String(names.indexOf(key) + 1));
|
|
94
|
+
sink.text(parsed.dir === "asc" ? " asc" : " desc");
|
|
95
|
+
sink.text(parsed.nulls === "first" ? " nulls first" : " nulls last");
|
|
96
|
+
sink.mark(`o:${key}:${parsed.dir}:${parsed.nulls}`);
|
|
97
|
+
}
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
sink.text(" order by ");
|
|
101
|
+
const parts = [];
|
|
102
|
+
for (let index = 1;index <= names.length; index += 1) {
|
|
103
|
+
parts.push(`${String(index)} asc nulls last`);
|
|
104
|
+
}
|
|
105
|
+
sink.text(parts.join(", "));
|
|
106
|
+
}
|
|
107
|
+
function unitOf(value) {
|
|
108
|
+
if (value === "second" || value === "minute" || value === "hour" || value === "day" || value === "week" || value === "month" || value === "quarter" || value === "year") {
|
|
109
|
+
return value;
|
|
110
|
+
}
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
function clockUnit(unit) {
|
|
114
|
+
return unit === "second" || unit === "minute" || unit === "hour";
|
|
115
|
+
}
|
|
116
|
+
function weekShift(value, unit) {
|
|
117
|
+
if (value === undefined)
|
|
118
|
+
return 0;
|
|
119
|
+
if (unit !== "week")
|
|
120
|
+
fail2("OKM1120", "bucket() weekStart applies to unit week.");
|
|
121
|
+
if (value === "monday")
|
|
122
|
+
return 0;
|
|
123
|
+
if (value === "sunday")
|
|
124
|
+
return 1;
|
|
125
|
+
if (value === "saturday")
|
|
126
|
+
return 2;
|
|
127
|
+
return fail2("OKM1120", "bucket() weekStart must be monday, sunday, or saturday.");
|
|
128
|
+
}
|
|
129
|
+
function temporalKind(dataType) {
|
|
130
|
+
if (dataType === "date")
|
|
131
|
+
return "date";
|
|
132
|
+
if (dataType === "timestamptz" || dataType.startsWith("timestamptz("))
|
|
133
|
+
return "timestamptz";
|
|
134
|
+
if (dataType === "timestamp" || dataType.startsWith("timestamp("))
|
|
135
|
+
return "timestamp";
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
function iana(tz) {
|
|
139
|
+
try {
|
|
140
|
+
Intl.DateTimeFormat("en-US", { timeZone: tz });
|
|
141
|
+
return true;
|
|
142
|
+
} catch {
|
|
143
|
+
return false;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
function bucketDecode(group) {
|
|
147
|
+
if (group.clock || !group.column.dataType.startsWith("timestamptz"))
|
|
148
|
+
return group.column.decode;
|
|
149
|
+
return decodeDay;
|
|
150
|
+
}
|
|
151
|
+
function decodeDay(wire) {
|
|
152
|
+
const temporal = globalThis.Temporal;
|
|
153
|
+
if (typeof temporal !== "object" || temporal === null || !("PlainDate" in temporal)) {
|
|
154
|
+
return fail2("OKM1120", "Date and time codecs need the Temporal global. okmodel does not ship a polyfill. Use a runtime that provides Temporal, or assign a polyfill to globalThis.Temporal before using these columns.");
|
|
155
|
+
}
|
|
156
|
+
try {
|
|
157
|
+
return temporal.PlainDate.from(wire);
|
|
158
|
+
} catch {
|
|
159
|
+
return fail2("OKM1120", `date ${wire} must be YYYY-MM-DD.`);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
var ZONE = "\x00";
|
|
163
|
+
function emitClock(sink, group) {
|
|
164
|
+
const column = `t.${quote(group.column.sql)}`;
|
|
165
|
+
const wrapped = projectExpr(`date_trunc('${group.unit}', ${column}, ${ZONE})`, "timestamptz");
|
|
166
|
+
const at = wrapped.indexOf(ZONE);
|
|
167
|
+
sink.text(wrapped.slice(0, at));
|
|
168
|
+
sink.param(group.tz ?? "");
|
|
169
|
+
sink.text(wrapped.slice(at + ZONE.length));
|
|
170
|
+
}
|
|
171
|
+
function emitCalendar(sink, group) {
|
|
172
|
+
const column = `t.${quote(group.column.sql)}`;
|
|
173
|
+
sink.text("(date_trunc('");
|
|
174
|
+
sink.text(group.unit);
|
|
175
|
+
sink.text("', ");
|
|
176
|
+
emitLocal(sink, column, group.tz);
|
|
177
|
+
if (group.shift === 0) {
|
|
178
|
+
sink.text("))::date");
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
const days = group.shift === 1 ? "1 day" : "2 days";
|
|
182
|
+
sink.text(" + interval '");
|
|
183
|
+
sink.text(days);
|
|
184
|
+
sink.text("') - interval '");
|
|
185
|
+
sink.text(days);
|
|
186
|
+
sink.text("')::date");
|
|
187
|
+
}
|
|
188
|
+
function emitLocal(sink, column, tz) {
|
|
189
|
+
if (tz === undefined) {
|
|
190
|
+
sink.text(column);
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
sink.text("(");
|
|
194
|
+
sink.text(column);
|
|
195
|
+
sink.text(" AT TIME ZONE ");
|
|
196
|
+
sink.param(tz);
|
|
197
|
+
sink.text(")");
|
|
198
|
+
}
|
|
199
|
+
function ordinals(count) {
|
|
200
|
+
const parts = [];
|
|
201
|
+
for (let index = 1;index <= count; index += 1)
|
|
202
|
+
parts.push(String(index));
|
|
203
|
+
return parts.join(", ");
|
|
204
|
+
}
|
|
205
|
+
export {
|
|
206
|
+
readBucket,
|
|
207
|
+
writeBucket,
|
|
208
|
+
writeGrouped
|
|
209
|
+
};
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import {
|
|
2
|
+
stack2
|
|
3
|
+
} from "./35zemrmf.js";
|
|
4
|
+
import {
|
|
5
|
+
OkmError,
|
|
6
|
+
isOperator,
|
|
7
|
+
operatorValue
|
|
8
|
+
} from "./hfq3n3bd.js";
|
|
9
|
+
|
|
10
|
+
// src/dialects/pg/preset.ts
|
|
11
|
+
var RESERVED_PRESETS = new Set([
|
|
12
|
+
"find",
|
|
13
|
+
"one",
|
|
14
|
+
"page",
|
|
15
|
+
"aggregate",
|
|
16
|
+
"count",
|
|
17
|
+
"exists",
|
|
18
|
+
"insert",
|
|
19
|
+
"update",
|
|
20
|
+
"delete",
|
|
21
|
+
"descendants",
|
|
22
|
+
"ancestors",
|
|
23
|
+
"sync",
|
|
24
|
+
"archive",
|
|
25
|
+
"restore",
|
|
26
|
+
"withArchived",
|
|
27
|
+
"onlyArchived",
|
|
28
|
+
"then",
|
|
29
|
+
"lock",
|
|
30
|
+
"watch",
|
|
31
|
+
"subscribe",
|
|
32
|
+
"stream",
|
|
33
|
+
"inspect",
|
|
34
|
+
"explain",
|
|
35
|
+
"with",
|
|
36
|
+
"for",
|
|
37
|
+
"as"
|
|
38
|
+
]);
|
|
39
|
+
function checkPresetNames(names, where) {
|
|
40
|
+
for (const name of names) {
|
|
41
|
+
if (!RESERVED_PRESETS.has(name))
|
|
42
|
+
continue;
|
|
43
|
+
throw new OkmError("OKM1040", `Preset ${name} on ${where} is a client method or a reserved name. Reserved: ${[...RESERVED_PRESETS].join(", ")}.`, { fix: { summary: `Rename the preset ${name}.` } });
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// src/runtime/presets.ts
|
|
48
|
+
function checkSchema(schema) {
|
|
49
|
+
for (const name in schema.model) {
|
|
50
|
+
const presets = schema.model[name]?.presets;
|
|
51
|
+
if (presets !== undefined)
|
|
52
|
+
checkPresetNames(Object.keys(presets), `table ${name}`);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
function resolve(schema, table, uses) {
|
|
56
|
+
const model = schema.model[table];
|
|
57
|
+
const known = model?.presets ?? {};
|
|
58
|
+
const chain = [];
|
|
59
|
+
for (let use = uses;use !== undefined; use = use[0])
|
|
60
|
+
chain.unshift(use);
|
|
61
|
+
const wheres = [];
|
|
62
|
+
const rules = [];
|
|
63
|
+
for (const [, name, args] of chain) {
|
|
64
|
+
const preset = known[name];
|
|
65
|
+
if (typeof preset !== "function") {
|
|
66
|
+
throw new OkmError("OKM1120", `Table ${table} has no preset ${name}.`, {
|
|
67
|
+
fix: { summary: `Use one of: ${Object.keys(known).join(", ")}.` }
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
const added = [];
|
|
71
|
+
const q = {
|
|
72
|
+
where(filter) {
|
|
73
|
+
added.push(filter);
|
|
74
|
+
return q;
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
if (preset(q, ...args) !== q) {
|
|
78
|
+
throw new OkmError("OKM1121", `Preset ${name} on ${table} must return the builder it was given.`, { fix: { summary: "Return q, as in (q) => q.where({ ... })." } });
|
|
79
|
+
}
|
|
80
|
+
for (const filter of added)
|
|
81
|
+
wheres.push(filter);
|
|
82
|
+
rules.push({
|
|
83
|
+
rule: "preset",
|
|
84
|
+
contribution: `${name} filters ${[...new Set(added.flatMap(fieldsOf))].join(", ") || "nothing"}`,
|
|
85
|
+
provenance: ownerOf(model, name, table),
|
|
86
|
+
...model?.source !== undefined ? { source: model.source } : {}
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
return { wheres, rules };
|
|
90
|
+
}
|
|
91
|
+
function refine(schema, call, uses) {
|
|
92
|
+
const { wheres, rules } = resolve(schema, call.table, uses);
|
|
93
|
+
return {
|
|
94
|
+
...call,
|
|
95
|
+
where: stack2(call.where, wheres),
|
|
96
|
+
ruleLines: [...call.ruleLines ?? [], ...rules]
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
function ownerOf(model, name, table) {
|
|
100
|
+
for (const trait of model?.traits ?? []) {
|
|
101
|
+
const presets = trait.presets;
|
|
102
|
+
if (presets !== undefined && Object.hasOwn(presets, name))
|
|
103
|
+
return `trait ${trait.name}`;
|
|
104
|
+
}
|
|
105
|
+
return `table ${table}`;
|
|
106
|
+
}
|
|
107
|
+
function fieldsOf(filter) {
|
|
108
|
+
if (isOperator(filter)) {
|
|
109
|
+
const branches = operatorValue(filter);
|
|
110
|
+
return Array.isArray(branches) ? branches.flatMap(fieldsOf) : [];
|
|
111
|
+
}
|
|
112
|
+
return typeof filter === "object" && filter !== null ? Object.keys(filter) : [];
|
|
113
|
+
}
|
|
114
|
+
export {
|
|
115
|
+
checkSchema,
|
|
116
|
+
refine,
|
|
117
|
+
resolve
|
|
118
|
+
};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import {
|
|
2
|
+
open6,
|
|
3
|
+
seal2
|
|
4
|
+
} from "./t6zjeake.js";
|
|
5
|
+
import {
|
|
6
|
+
createClient
|
|
7
|
+
} from "./jr2nmr81.js";
|
|
8
|
+
|
|
9
|
+
// src/runtime/pg/pglite.ts
|
|
10
|
+
async function connect(target, options) {
|
|
11
|
+
if (typeof target === "object" && !isPool(target)) {
|
|
12
|
+
return import("./kbqnrs1x.js").then((mod) => mod.default(target, options, open6));
|
|
13
|
+
}
|
|
14
|
+
const pool = isPool(target) ? seal2(target) : await open6({
|
|
15
|
+
...target !== undefined ? { dataDir: target } : {},
|
|
16
|
+
...options.timeouts !== undefined ? { timeouts: options.timeouts } : {}
|
|
17
|
+
});
|
|
18
|
+
return createClient(options.schema, pool, {
|
|
19
|
+
ownsPool: true,
|
|
20
|
+
http: options.errors?.http,
|
|
21
|
+
includeValues: options.errors?.includeValues,
|
|
22
|
+
logger: options.logger,
|
|
23
|
+
signal: options.signal,
|
|
24
|
+
requireMeta: options.requireMeta,
|
|
25
|
+
timeout: options.timeout,
|
|
26
|
+
timeouts: options.timeouts,
|
|
27
|
+
hookm: options.hookm,
|
|
28
|
+
catalog: options.catalog,
|
|
29
|
+
catalogDir: options.catalogDir,
|
|
30
|
+
generators: options.generators
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
function isPool(target) {
|
|
34
|
+
return typeof target === "object" && target !== null && "execute" in target && "capabilities" in target;
|
|
35
|
+
}
|
|
36
|
+
export {
|
|
37
|
+
connect
|
|
38
|
+
};
|