okmodel 0.8.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/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.js +4 -4
- package/dist/dialects/pg/index.d.ts +1 -0
- package/dist/dialects/pg/index.js +16 -11
- package/dist/dialects/pg/view/index.js +5 -5
- package/dist/dialects/pg/view/scratch.d.ts +4 -1
- 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/pg/bun.js +4 -4
- package/dist/runtime/pg/neon.js +4 -4
- 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/server-check.d.ts +36 -6
- package/dist/runtime/traits/index.js +5 -5
- package/dist/runtime/validate/index.js +1 -1
- package/dist/shared/{4m6hskg8.js → 0j6qe91y.js} +4 -4
- package/dist/shared/{14x60fbf.js → 1mxm04fy.js} +5 -5
- package/dist/shared/{b6d6p0zc.js → 2szgv6yj.js} +5 -5
- package/dist/shared/{f4jy1j7x.js → 2zdxkt7b.js} +909 -100
- package/dist/shared/{mnvjvqyp.js → 3456242y.js} +17 -17
- package/dist/shared/3t6kw83m.js +1 -1
- package/dist/shared/{6m78c8ss.js → 5kyb3tqx.js} +1 -1
- package/dist/shared/61bmrmhr.js +35 -0
- package/dist/shared/{zbqbmmrp.js → 62b603gr.js} +8 -8
- package/dist/shared/{jgqj11w4.js → 6f0z3hzh.js} +4 -0
- package/dist/shared/{dpc2r7rb.js → 6nae0spm.js} +150 -89
- package/dist/shared/{0wrea3jq.js → 71n0jmhc.js} +1 -1
- package/dist/shared/7ba5h6pb.js +17 -0
- package/dist/shared/{k86n8vq3.js → 8jesqsrk.js} +1 -1
- package/dist/shared/{qd9hhd8f.js → 8n688yjg.js} +1 -1
- package/dist/shared/{zgex00fd.js → 8qzkd9f3.js} +5 -5
- package/dist/shared/{mkrn359n.js → 90sg0w5a.js} +159 -31
- package/dist/shared/{7ghwamrf.js → 99qp9vtg.js} +1 -1
- package/dist/shared/{bxas03mq.js → 9dtgn1z2.js} +1 -1
- package/dist/shared/{p5nh3r8q.js → aksb89mn.js} +2 -2
- package/dist/shared/{wcrheftc.js → bphrgwz9.js} +2 -2
- package/dist/shared/{wz2a78mp.js → c3mvnq7a.js} +1 -1
- package/dist/shared/{pan0370z.js → ccsmhv9r.js} +24 -6
- package/dist/shared/{n0jmpgj1.js → cexb7z6b.js} +1 -1
- package/dist/shared/{wvdjznah.js → cf8zdz5p.js} +5 -5
- package/dist/shared/{3449rcae.js → ckz05z15.js} +4 -4
- package/dist/shared/{35q0n9tz.js → dd5pmnky.js} +7 -7
- package/dist/shared/{e8zyaqvj.js → ds229x4r.js} +1 -1
- package/dist/{tooling/shared/yw30vnq2.js → shared/e275q7sd.js} +1 -1
- package/dist/shared/{mfedzr74.js → ebhz8vh6.js} +4 -4
- package/dist/shared/{z581d7ny.js → fdsz3w80.js} +2 -2
- package/dist/shared/{sjk86yrx.js → fk9vef30.js} +84 -73
- package/dist/shared/{needfe47.js → g89fmz5s.js} +1 -1
- package/dist/shared/{m4xff2a6.js → ghqzdp94.js} +2 -2
- package/dist/shared/h9811jjm.js +17 -0
- package/dist/shared/{kj9d8maq.js → jpybhf98.js} +1 -1
- package/dist/shared/kzgrnckd.js +29 -0
- package/dist/shared/{agsfb86s.js → mazpbcdj.js} +4 -4
- package/dist/shared/{195pzzzz.js → mgaq2sd3.js} +10 -7
- package/dist/shared/{5mtf6cj3.js → mvmm3xpb.js} +5 -2
- package/dist/shared/{p3xbdcvt.js → myk8fswx.js} +55 -6
- package/dist/shared/{njga14v4.js → n306gqkd.js} +8 -8
- package/dist/shared/n5whk3mt.js +665 -0
- package/dist/shared/{bah9nbg3.js → npdftanb.js} +12 -20
- package/dist/shared/{16kvjebh.js → pg4ezgyf.js} +7 -26
- package/dist/{tooling/shared/08kemxjd.js → shared/pgsak1yw.js} +1 -1
- package/dist/shared/{jp36k7dp.js → qvwwp1mp.js} +61 -40
- package/dist/shared/rbxt795q.js +1766 -0
- package/dist/shared/{g5yc06ka.js → s7he5e5d.js} +1 -1
- package/dist/shared/{hfqmpasr.js → ss2kv2rs.js} +5 -5
- package/dist/shared/stfa9xcw.js +450 -0
- package/dist/{tooling/shared/rs1jb051.js → shared/tr64w5p0.js} +1 -1
- package/dist/shared/{jebgrkeq.js → tw1nvvfj.js} +2 -2
- package/dist/shared/{v1wad97q.js → wvtjg5m3.js} +1 -1
- package/dist/shared/xqacq37x.js +61 -0
- package/dist/shared/{n05z9nwz.js → xtsydxjg.js} +1 -1
- package/dist/shared/{jx2m62cs.js → xv8qj97g.js} +5 -5
- package/dist/shared/{63f4ksbz.js → xwhdcrm4.js} +1 -1
- package/dist/shared/{hm5f8jby.js → yvabyg9k.js} +6 -6
- package/dist/shared/yybhre6n.js +136 -0
- package/dist/shared/zdqf8k5d.js +8 -0
- package/dist/tooling/cli.js +94 -22
- package/dist/tooling/driver-run.js +15 -332
- 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 +2 -1
- 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 +10 -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/{3m1hkg7z.js → 04svnxxj.js} +4 -4
- package/dist/tooling/shared/{ppnnnvj8.js → 0j0jtse1.js} +1 -1
- package/dist/tooling/shared/{z8d7ptht.js → 0r05zhyq.js} +5 -5
- package/dist/tooling/shared/{a6pp9d12.js → 1800eymw.js} +3 -3
- package/dist/tooling/shared/{cmptmb4f.js → 35zemrmf.js} +1 -1
- package/dist/tooling/shared/{w7qe8sj1.js → 3rmaz3pq.js} +1 -1
- package/dist/{shared/q0cqt2sp.js → tooling/shared/3yn79q5m.js} +1 -1
- package/dist/tooling/shared/{2revefqt.js → 57btdkgm.js} +2 -2
- package/dist/tooling/shared/{ma2340rx.js → 5bmksndh.js} +2 -2
- package/dist/tooling/shared/{9xzm7hjg.js → 5p8km5jb.js} +3 -3
- package/dist/tooling/shared/{xhrnjch9.js → 5x3pf400.js} +3 -3
- package/dist/tooling/shared/{wwnekq5n.js → 5zrzffnk.js} +1 -1
- package/dist/tooling/shared/{p7f08ktn.js → 62fxpc1p.js} +7 -7
- package/dist/tooling/shared/{7ae7rjmn.js → 68tf18v4.js} +2 -2
- package/dist/tooling/shared/{rf83gr2d.js → 6vk28rjp.js} +7 -3
- package/dist/tooling/shared/76pv6qff.js +9 -0
- package/dist/tooling/shared/{bz2dqhab.js → 7aews1zg.js} +3 -3
- package/dist/tooling/shared/{9kazs5p6.js → 7b3xp9gc.js} +3 -3
- package/dist/tooling/shared/{4d2w25vw.js → 7pxncnzs.js} +2 -2
- package/dist/tooling/shared/{zvnhh1n0.js → 829fjxyw.js} +2 -2
- package/dist/tooling/shared/{dzx85q70.js → 8m5znpdt.js} +1 -1
- package/dist/tooling/shared/{g9ad4n80.js → 8yr7ges1.js} +7 -7
- package/dist/tooling/shared/{tn8gay14.js → 9qr9rkbh.js} +6 -6
- package/dist/tooling/shared/{8sqrsw3s.js → 9sfbtwsk.js} +3 -3
- package/dist/{shared/bnby0esn.js → tooling/shared/9wsnsvbf.js} +1 -1
- package/dist/tooling/shared/{aftb2mwr.js → a2xe04bm.js} +1 -1
- package/dist/tooling/shared/{nf7jmhfa.js → a4pzghha.js} +2 -2
- package/dist/tooling/shared/{y5656f0e.js → am04v8na.js} +5 -5
- package/dist/tooling/shared/{417zdsk7.js → asgr07mp.js} +5 -5
- package/dist/tooling/shared/{pqy21k24.js → c0xevn1h.js} +4 -4
- package/dist/tooling/shared/dhatassp.js +134 -0
- package/dist/tooling/shared/{spk1jg3r.js → ezqn88nj.js} +8 -8
- package/dist/tooling/shared/{6nyfyw70.js → f77nnfqy.js} +2 -2
- package/dist/tooling/shared/{wt7j7nf6.js → fh07z1dt.js} +1 -1
- package/dist/tooling/shared/{3rrw5w8f.js → gqwevek4.js} +1 -1
- package/dist/tooling/shared/{f9mphxej.js → hfq3n3bd.js} +346 -19
- package/dist/tooling/shared/{vs0t6qt8.js → j664qjrt.js} +2 -2
- package/dist/tooling/shared/{733a1tq3.js → jpgmjsx3.js} +3 -3
- package/dist/tooling/shared/{mtrqn2hn.js → jr2nmr81.js} +22 -30
- package/dist/{shared/mtw6jz76.js → tooling/shared/jsjjz875.js} +1 -1
- package/dist/tooling/shared/{2wtvc7rw.js → kbqnrs1x.js} +2 -2
- package/dist/tooling/shared/{gfvtj6d0.js → kz715295.js} +7 -7
- package/dist/tooling/shared/{a7q7t4n3.js → m3xt8b1h.js} +2 -2
- package/dist/tooling/shared/{dw08f3m8.js → m4hhb8aa.js} +2 -2
- package/dist/tooling/shared/{y031jct9.js → mw5npye0.js} +2 -2
- package/dist/tooling/shared/{vraj6d74.js → nxq95eb6.js} +3 -3
- package/dist/tooling/shared/{fb9k2qwd.js → qartgazh.js} +11 -30
- package/dist/tooling/shared/{xjs6zqtc.js → rcywj2w8.js} +9 -9
- package/dist/tooling/shared/{dy672fz6.js → s6qha57m.js} +2 -2
- package/dist/tooling/shared/{vx86qjv8.js → shjs0379.js} +3 -3
- package/dist/tooling/shared/{345tbc2k.js → sjwkt1pg.js} +1 -1
- package/dist/tooling/shared/{rq5kk8j6.js → stebqyyd.js} +3 -3
- package/dist/tooling/shared/{yk9mp7vb.js → t4xveemq.js} +4 -4
- package/dist/tooling/shared/{djydjpqt.js → t6zjeake.js} +5 -5
- package/dist/tooling/shared/{zhwnfyb2.js → tcwpjvxe.js} +1 -1
- package/dist/tooling/shared/{72fyg4zv.js → tpssp7ac.js} +1 -1
- package/dist/tooling/shared/{jne7b93p.js → v6j1ep6w.js} +3 -3
- package/dist/tooling/shared/{ec71zqgt.js → vwr2y833.js} +2 -2
- package/dist/tooling/shared/{j9t5s12a.js → w17sj05d.js} +2 -2
- package/dist/tooling/shared/{3tq8pnb8.js → w5d89rb4.js} +1 -1
- package/dist/tooling/shared/{smn6kz1p.js → wbwq1jsa.js} +2 -2
- package/dist/tooling/shared/{pwsytxm5.js → wezxf3h8.js} +1 -1
- package/dist/tooling/shared/{5bvw2nev.js → yb4kd19s.js} +1 -1
- package/dist/tooling/shared/{gmcf9cxf.js → ybdew6j7.js} +2 -2
- package/dist/tooling/shared/{ep8zh3bk.js → yfedr1zb.js} +1 -1
- package/dist/tooling/shared/{ddjtfgnx.js → yhp829bx.js} +3 -3
- package/dist/tooling/shared/{yfr8hzhm.js → ytk1tjs0.js} +1 -1
- package/dist/tooling/shared/{03k6res6.js → zs9aymcp.js} +1 -1
- package/dist/tooling/shared/zx3v9phv.js +8 -0
- package/dist/tooling/testing/index.js +2 -2
- package/package.json +38 -9
- package/dist/shared/fjpnhqan.js +0 -71
- package/dist/shared/n41w4fbz.js +0 -113
- package/dist/tooling/shared/pw8fgahx.js +0 -69
- /package/dist/shared/{ae4j5f42.js → mfvx6kjx.js} +0 -0
|
@@ -0,0 +1,665 @@
|
|
|
1
|
+
import"./12z7aznj.js";
|
|
2
|
+
import"./fk9vef30.js";
|
|
3
|
+
import"./n442h54c.js";
|
|
4
|
+
import"./dd5pmnky.js";
|
|
5
|
+
import {
|
|
6
|
+
emitPull,
|
|
7
|
+
formatPullReport,
|
|
8
|
+
pullLayout2,
|
|
9
|
+
commitPullFiles2
|
|
10
|
+
} from "./rbxt795q.js";
|
|
11
|
+
import"./myk8fswx.js";
|
|
12
|
+
import {
|
|
13
|
+
loadConfig
|
|
14
|
+
} from "./2zdxkt7b.js";
|
|
15
|
+
import"./6nae0spm.js";
|
|
16
|
+
import {
|
|
17
|
+
catalog,
|
|
18
|
+
enumType
|
|
19
|
+
} from "./ywspezzx.js";
|
|
20
|
+
import {
|
|
21
|
+
quoteLiteral
|
|
22
|
+
} from "./94ez8kzh.js";
|
|
23
|
+
import {
|
|
24
|
+
staticNamespace2,
|
|
25
|
+
table3,
|
|
26
|
+
column2,
|
|
27
|
+
index3,
|
|
28
|
+
constraint2,
|
|
29
|
+
sequence2
|
|
30
|
+
} from "./358zsv4r.js";
|
|
31
|
+
import {
|
|
32
|
+
referentialAction
|
|
33
|
+
} from "./4e4c6vq2.js";
|
|
34
|
+
import {
|
|
35
|
+
OkmError2
|
|
36
|
+
} from "./52szzakm.js";
|
|
37
|
+
|
|
38
|
+
// src/tooling/migrate/import-drizzle.ts
|
|
39
|
+
import { existsSync, statSync } from "node:fs";
|
|
40
|
+
import { createRequire } from "node:module";
|
|
41
|
+
import { join, resolve } from "node:path";
|
|
42
|
+
import { pathToFileURL } from "node:url";
|
|
43
|
+
var ENUM_SYMBOL = Symbol.for("drizzle:isPgEnum");
|
|
44
|
+
async function importDrizzle(cwd, argv) {
|
|
45
|
+
const flags = parseImportArgs(argv);
|
|
46
|
+
const file = readableFile(cwd, flags.path);
|
|
47
|
+
const drizzle = await loadDrizzle(cwd);
|
|
48
|
+
const imported = await loadSchemaModule(file);
|
|
49
|
+
const found = collect(imported, drizzle);
|
|
50
|
+
if (found.tables.length === 0) {
|
|
51
|
+
throw new OkmError2("invalid", `${file} exports no Drizzle tables.`, {
|
|
52
|
+
fix: { summary: "Pass the module that exports pgTable or pgSchema tables." }
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
const namespace = chooseNamespace(found.tables, drizzle);
|
|
56
|
+
const extras = [];
|
|
57
|
+
const objects = catalogOf(found, drizzle, namespace, extras);
|
|
58
|
+
const built = catalog(objects);
|
|
59
|
+
const layout = await layoutFor(cwd, flags.out);
|
|
60
|
+
const emitted = emitPull(built, layout);
|
|
61
|
+
const tables = emitted.files.filter((item) => item.path !== layout.schemaFile);
|
|
62
|
+
if (tables.length === 0) {
|
|
63
|
+
throw new OkmError2("invalid", "The Drizzle schema has no table okmodel can write.", {
|
|
64
|
+
fix: { summary: "See the not-imported list. table() has to be able to say the columns." }
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
const skipped = [...emitted.skipped, ...extras].sort((left, right) => compare(`${left.kind}\x00${left.label}`, `${right.kind}\x00${right.label}`));
|
|
68
|
+
commitPullFiles2(emitted.files, "Import");
|
|
69
|
+
const report = formatPullReport({ tables: tables.length, functions: 0, triggers: 0, views: 0, materializedViews: 0 }, skipped, [], [], "imported");
|
|
70
|
+
return {
|
|
71
|
+
text: `namespace ${namespace}
|
|
72
|
+
${report}`,
|
|
73
|
+
skipped,
|
|
74
|
+
namespace
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
function parseImportArgs(argv) {
|
|
78
|
+
let path;
|
|
79
|
+
let out;
|
|
80
|
+
for (let index = 0;index < argv.length; index += 1) {
|
|
81
|
+
const arg = argv[index] ?? "";
|
|
82
|
+
if (arg === "--out") {
|
|
83
|
+
const value = argv[index + 1];
|
|
84
|
+
if (value === undefined || value.startsWith("--")) {
|
|
85
|
+
throw new OkmError2("invalid", "--out needs a value.", {
|
|
86
|
+
fix: { summary: "Pass --out <directory>." }
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
out = value;
|
|
90
|
+
index += 1;
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
if (arg.startsWith("--out=")) {
|
|
94
|
+
out = arg.slice("--out=".length);
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
if (arg.startsWith("--")) {
|
|
98
|
+
throw new OkmError2("invalid", `Unknown flag ${arg}.`, {
|
|
99
|
+
fix: { summary: "okm import drizzle accepts <path> and --out." }
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
if (path !== undefined) {
|
|
103
|
+
throw new OkmError2("invalid", `Unexpected argument ${arg}.`, {
|
|
104
|
+
fix: { summary: "Pass one schema path." }
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
path = arg;
|
|
108
|
+
}
|
|
109
|
+
if (path === undefined || path.length === 0) {
|
|
110
|
+
throw new OkmError2("invalid", "okm import drizzle needs a path.", {
|
|
111
|
+
fix: { summary: "Pass the Drizzle schema file." }
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
return { path, out };
|
|
115
|
+
}
|
|
116
|
+
function readableFile(cwd, spec) {
|
|
117
|
+
const path = spec.startsWith("/") ? spec : join(cwd, spec);
|
|
118
|
+
let info;
|
|
119
|
+
try {
|
|
120
|
+
info = statSync(path);
|
|
121
|
+
} catch (error) {
|
|
122
|
+
const detail = error instanceof Error ? error.message : "unreadable";
|
|
123
|
+
throw new OkmError2("invalid", `Cannot read ${path}. ${detail}`, {
|
|
124
|
+
fix: { summary: "Pass a Drizzle schema file the process can read." }
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
if (!info.isFile()) {
|
|
128
|
+
throw new OkmError2("invalid", `Cannot read ${path}. It is not a file.`, {
|
|
129
|
+
fix: { summary: "Pass a Drizzle schema file." }
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
return path;
|
|
133
|
+
}
|
|
134
|
+
async function loadDrizzle(project) {
|
|
135
|
+
let pgEntry;
|
|
136
|
+
let ormEntry;
|
|
137
|
+
try {
|
|
138
|
+
const require2 = createRequire(join(resolve(project), "package.json"));
|
|
139
|
+
pgEntry = require2.resolve("drizzle-orm/pg-core");
|
|
140
|
+
ormEntry = require2.resolve("drizzle-orm");
|
|
141
|
+
} catch (error) {
|
|
142
|
+
throw missingDrizzle(error);
|
|
143
|
+
}
|
|
144
|
+
const pg = await import(pathToFileURL(pgEntry).href);
|
|
145
|
+
const orm = await import(pathToFileURL(ormEntry).href);
|
|
146
|
+
const is = orm.is;
|
|
147
|
+
const getTableConfig = pg.getTableConfig;
|
|
148
|
+
const getTableColumns = orm.getTableColumns;
|
|
149
|
+
const PgTable = pg.PgTable;
|
|
150
|
+
const PgView = pg.PgView;
|
|
151
|
+
const PgMaterializedView = pg.PgMaterializedView;
|
|
152
|
+
const PgSequence = pg.PgSequence;
|
|
153
|
+
const Relations = orm.Relations;
|
|
154
|
+
const getViewConfig = pg.getViewConfig;
|
|
155
|
+
if (typeof is !== "function" || typeof getTableConfig !== "function" || typeof getTableColumns !== "function" || typeof PgTable !== "function" || typeof PgView !== "function" || typeof PgMaterializedView !== "function" || typeof PgSequence !== "function" || typeof Relations !== "function" || typeof getViewConfig !== "function") {
|
|
156
|
+
throw new OkmError2("OKM1856", "This project's drizzle-orm has no pg table helpers.", {
|
|
157
|
+
fix: { summary: "Install a current drizzle-orm in the project that owns the schema." }
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
return {
|
|
161
|
+
is,
|
|
162
|
+
PgTable,
|
|
163
|
+
PgView,
|
|
164
|
+
PgMaterializedView,
|
|
165
|
+
PgSequence,
|
|
166
|
+
Relations,
|
|
167
|
+
getTableConfig,
|
|
168
|
+
getTableColumns,
|
|
169
|
+
getViewConfig
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
function missingDrizzle(error) {
|
|
173
|
+
const detail = error instanceof Error && error.message.length > 0 ? ` ${error.message}` : "";
|
|
174
|
+
return new OkmError2("OKM1856", `drizzle-orm is not installed in this project.${detail}`, {
|
|
175
|
+
cause: error,
|
|
176
|
+
fix: { summary: "Install drizzle-orm in the project that owns the schema." }
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
async function loadSchemaModule(file) {
|
|
180
|
+
try {
|
|
181
|
+
const imported = await import(pathToFileURL(file).href);
|
|
182
|
+
if (typeof imported !== "object" || imported === null)
|
|
183
|
+
return {};
|
|
184
|
+
return imported;
|
|
185
|
+
} catch (error) {
|
|
186
|
+
if (error instanceof OkmError2)
|
|
187
|
+
throw error;
|
|
188
|
+
const detail = error instanceof Error && error.message.length > 0 ? error.message : "failed";
|
|
189
|
+
throw new OkmError2("invalid", `Importing ${file} failed. ${detail}`, {
|
|
190
|
+
cause: error,
|
|
191
|
+
fix: { summary: "The schema file is executed. Fix the error it raised." }
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
function collect(module, drizzle) {
|
|
196
|
+
const tables = [];
|
|
197
|
+
const enums = [];
|
|
198
|
+
const views = [];
|
|
199
|
+
const relations = [];
|
|
200
|
+
const sequences = [];
|
|
201
|
+
const seen = new Set;
|
|
202
|
+
const knownTables = new Set;
|
|
203
|
+
const visit = (value, exportName) => {
|
|
204
|
+
if (typeof value === "function") {
|
|
205
|
+
const note = enumNote(value);
|
|
206
|
+
if (note !== undefined && !enums.some((item) => item.name === note.name))
|
|
207
|
+
enums.push(note);
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
if (typeof value !== "object" || value === null)
|
|
211
|
+
return;
|
|
212
|
+
if (seen.has(value))
|
|
213
|
+
return;
|
|
214
|
+
seen.add(value);
|
|
215
|
+
if (drizzle.is(value, drizzle.PgTable)) {
|
|
216
|
+
if (!knownTables.has(value)) {
|
|
217
|
+
knownTables.add(value);
|
|
218
|
+
tables.push({ value, exportName });
|
|
219
|
+
}
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
if (drizzle.is(value, drizzle.Relations)) {
|
|
223
|
+
relations.push(exportName);
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
if (drizzle.is(value, drizzle.PgView) || drizzle.is(value, drizzle.PgMaterializedView)) {
|
|
227
|
+
views.push(viewName(value, drizzle, exportName));
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
if (drizzle.is(value, drizzle.PgSequence)) {
|
|
231
|
+
const name = stringField(value, "seqName");
|
|
232
|
+
if (name !== undefined) {
|
|
233
|
+
sequences.push({ name, schema: stringField(value, "schema") ?? "public" });
|
|
234
|
+
}
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
for (const child of Object.values(value))
|
|
238
|
+
visit(child, exportName);
|
|
239
|
+
};
|
|
240
|
+
for (const key of Object.keys(module).sort())
|
|
241
|
+
visit(module[key], key);
|
|
242
|
+
tables.sort((left, right) => compare(left.exportName, right.exportName));
|
|
243
|
+
return { tables, enums, views, relations, sequences };
|
|
244
|
+
}
|
|
245
|
+
function chooseNamespace(tables, drizzle) {
|
|
246
|
+
const names = new Set(tables.map((item) => drizzle.getTableConfig(item.value).schema ?? "public"));
|
|
247
|
+
if (names.size === 1)
|
|
248
|
+
return [...names][0] ?? "public";
|
|
249
|
+
if (names.has("public"))
|
|
250
|
+
return "public";
|
|
251
|
+
return [...names].sort()[0] ?? "public";
|
|
252
|
+
}
|
|
253
|
+
function catalogOf(found, drizzle, namespaceName, extras) {
|
|
254
|
+
const namespace = staticNamespace2(namespaceName);
|
|
255
|
+
const objects = [];
|
|
256
|
+
const enums = new Map;
|
|
257
|
+
for (const note of found.enums)
|
|
258
|
+
enums.set(note.name, note.labels);
|
|
259
|
+
for (const item of found.relations) {
|
|
260
|
+
extras.push(extra("relation", item, "relations() is not a foreign key"));
|
|
261
|
+
}
|
|
262
|
+
for (const item of found.views) {
|
|
263
|
+
extras.push(extra("view", item, "a later step writes views"));
|
|
264
|
+
}
|
|
265
|
+
for (const item of found.sequences) {
|
|
266
|
+
if (item.schema !== namespaceName) {
|
|
267
|
+
extras.push(extra("sequence", item.name, "table() has no standalone sequence. An identity column writes its own"));
|
|
268
|
+
continue;
|
|
269
|
+
}
|
|
270
|
+
objects.push(sequence2({
|
|
271
|
+
namespace,
|
|
272
|
+
name: item.name,
|
|
273
|
+
provenance: provenanceOf()
|
|
274
|
+
}));
|
|
275
|
+
}
|
|
276
|
+
for (const item of found.tables) {
|
|
277
|
+
const config = drizzle.getTableConfig(item.value);
|
|
278
|
+
const schemaName = config.schema ?? "public";
|
|
279
|
+
if (schemaName !== namespaceName) {
|
|
280
|
+
extras.push(extra("table", config.name, "schema() has one namespace. Import that schema on its own and set namespace"));
|
|
281
|
+
continue;
|
|
282
|
+
}
|
|
283
|
+
const parent = { namespace, name: config.name };
|
|
284
|
+
objects.push(table3({
|
|
285
|
+
namespace,
|
|
286
|
+
name: config.name,
|
|
287
|
+
provenance: provenanceOf(`export:${item.exportName}`)
|
|
288
|
+
}));
|
|
289
|
+
if (config.enableRLS) {
|
|
290
|
+
extras.push(extra("policy", `${config.name}.row level security`, "a later step writes policies"));
|
|
291
|
+
}
|
|
292
|
+
for (const policy of config.policies) {
|
|
293
|
+
const name = policy.name ?? "policy";
|
|
294
|
+
extras.push(extra("policy", `${config.name}.${name}`, "a later step writes policies"));
|
|
295
|
+
}
|
|
296
|
+
const columns = drizzle.getTableColumns(item.value);
|
|
297
|
+
const primaryColumns = primaryColumnsOf(config, columns);
|
|
298
|
+
for (const [property, value] of Object.entries(columns)) {
|
|
299
|
+
const built = columnOf(parent, property, value, primaryColumns, enums, objects, namespace, extras, config.name);
|
|
300
|
+
if (built !== undefined)
|
|
301
|
+
objects.push(built);
|
|
302
|
+
}
|
|
303
|
+
for (const key of config.primaryKeys) {
|
|
304
|
+
const names = key.columns.map((entry) => entry.name);
|
|
305
|
+
if (names.length === 0)
|
|
306
|
+
continue;
|
|
307
|
+
if (sameColumns(names, primaryColumns))
|
|
308
|
+
continue;
|
|
309
|
+
objects.push(constraint2({
|
|
310
|
+
parent,
|
|
311
|
+
constraintKind: "primaryKey",
|
|
312
|
+
columns: names,
|
|
313
|
+
nameKey: keyName(key.name, names),
|
|
314
|
+
provenance: provenanceOf()
|
|
315
|
+
}));
|
|
316
|
+
}
|
|
317
|
+
if (primaryColumns.length > 0) {
|
|
318
|
+
objects.push(constraint2({
|
|
319
|
+
parent,
|
|
320
|
+
constraintKind: "primaryKey",
|
|
321
|
+
columns: primaryColumns,
|
|
322
|
+
nameKey: "pkey",
|
|
323
|
+
provenance: provenanceOf()
|
|
324
|
+
}));
|
|
325
|
+
}
|
|
326
|
+
const uniqueSets = new Set;
|
|
327
|
+
for (const unique of config.uniqueConstraints) {
|
|
328
|
+
const names = unique.columns.map((entry) => entry.name);
|
|
329
|
+
uniqueSets.add(names.join("\x00"));
|
|
330
|
+
objects.push(constraint2({
|
|
331
|
+
parent,
|
|
332
|
+
constraintKind: "unique",
|
|
333
|
+
columns: names,
|
|
334
|
+
nameKey: keyName(unique.name, names),
|
|
335
|
+
nullsNotDistinct: unique.nullsNotDistinct,
|
|
336
|
+
provenance: provenanceOf()
|
|
337
|
+
}));
|
|
338
|
+
}
|
|
339
|
+
for (const [property, value] of Object.entries(columns)) {
|
|
340
|
+
if (!flag(value, "isUnique"))
|
|
341
|
+
continue;
|
|
342
|
+
const name = stringField(value, "name") ?? property;
|
|
343
|
+
if (uniqueSets.has(name))
|
|
344
|
+
continue;
|
|
345
|
+
objects.push(constraint2({
|
|
346
|
+
parent,
|
|
347
|
+
constraintKind: "unique",
|
|
348
|
+
columns: [name],
|
|
349
|
+
nameKey: keyName(stringField(value, "uniqueName"), [name]),
|
|
350
|
+
provenance: provenanceOf()
|
|
351
|
+
}));
|
|
352
|
+
}
|
|
353
|
+
for (const key of config.foreignKeys) {
|
|
354
|
+
const ref = key.reference();
|
|
355
|
+
const local = ref.columns.map((entry) => entry.name);
|
|
356
|
+
const remote = ref.foreignColumns.map((entry) => entry.name);
|
|
357
|
+
const target = drizzle.getTableConfig(ref.foreignTable);
|
|
358
|
+
if ((target.schema ?? "public") !== namespaceName) {
|
|
359
|
+
extras.push(extra("constraint", `${config.name}.${local.join("_")}`, "the referenced table is in another schema"));
|
|
360
|
+
continue;
|
|
361
|
+
}
|
|
362
|
+
const onDelete = action(key.onDelete);
|
|
363
|
+
const onUpdate = action(key.onUpdate);
|
|
364
|
+
objects.push(constraint2({
|
|
365
|
+
parent,
|
|
366
|
+
constraintKind: "foreignKey",
|
|
367
|
+
columns: local,
|
|
368
|
+
nameKey: keyName(undefined, local),
|
|
369
|
+
provenance: provenanceOf(),
|
|
370
|
+
references: {
|
|
371
|
+
parent: { namespace, name: target.name },
|
|
372
|
+
columns: remote,
|
|
373
|
+
...onDelete !== undefined ? { onDelete } : {},
|
|
374
|
+
...onUpdate !== undefined ? { onUpdate } : {}
|
|
375
|
+
}
|
|
376
|
+
}));
|
|
377
|
+
}
|
|
378
|
+
for (const check of config.checks) {
|
|
379
|
+
const rendered = sqlText(check.value);
|
|
380
|
+
if (rendered === undefined) {
|
|
381
|
+
extras.push(extra("constraint", `${config.name}.${check.name}`, "the check expression could not be read"));
|
|
382
|
+
continue;
|
|
383
|
+
}
|
|
384
|
+
objects.push(constraint2({
|
|
385
|
+
parent,
|
|
386
|
+
constraintKind: "check",
|
|
387
|
+
columns: namesIn(check.value),
|
|
388
|
+
nameKey: keyName(check.name, [check.name]),
|
|
389
|
+
expression: rendered,
|
|
390
|
+
provenance: provenanceOf()
|
|
391
|
+
}));
|
|
392
|
+
}
|
|
393
|
+
for (const item of config.indexes) {
|
|
394
|
+
const unsupported = item.config.concurrently === true || item.config.only === true || item.config.method !== undefined && item.config.method !== "btree" || item.config.with !== undefined;
|
|
395
|
+
const label = `${config.name}.${item.config.name ?? "index"}`;
|
|
396
|
+
if (unsupported) {
|
|
397
|
+
extras.push(extra("index", label, "index() cannot store concurrently, only, a non-btree method, or storage parameters"));
|
|
398
|
+
continue;
|
|
399
|
+
}
|
|
400
|
+
const names = item.config.columns.map((entry) => stringField(entry, "name")).filter((name) => name !== undefined);
|
|
401
|
+
const expression = item.config.columns.filter((entry) => stringField(entry, "name") === undefined).map((entry) => sqlText(entry)).find((text) => text !== undefined);
|
|
402
|
+
const predicate = item.config.where === undefined ? undefined : sqlText(item.config.where);
|
|
403
|
+
if (names.length === 0 && expression === undefined) {
|
|
404
|
+
extras.push(extra("index", label, "index() needs a column. This index is an expression"));
|
|
405
|
+
continue;
|
|
406
|
+
}
|
|
407
|
+
if (predicate === undefined && item.config.where !== undefined) {
|
|
408
|
+
extras.push(extra("index", label, "the index predicate could not be read"));
|
|
409
|
+
continue;
|
|
410
|
+
}
|
|
411
|
+
objects.push(index3({
|
|
412
|
+
parent,
|
|
413
|
+
columns: names,
|
|
414
|
+
unique: item.config.unique,
|
|
415
|
+
nameKey: keyName(item.config.name, names.length > 0 ? names : ["expr"]),
|
|
416
|
+
...predicate !== undefined ? { predicate } : {},
|
|
417
|
+
...expression !== undefined ? { expression } : {},
|
|
418
|
+
provenance: provenanceOf()
|
|
419
|
+
}));
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
for (const [name, labels] of enums) {
|
|
423
|
+
objects.push(enumType({
|
|
424
|
+
namespace,
|
|
425
|
+
name,
|
|
426
|
+
labels,
|
|
427
|
+
provenance: provenanceOf()
|
|
428
|
+
}));
|
|
429
|
+
}
|
|
430
|
+
return objects;
|
|
431
|
+
}
|
|
432
|
+
function columnOf(parent, property, value, primaryColumns, enums, objects, namespace, extras, tableName) {
|
|
433
|
+
const sqlName = stringField(value, "name") ?? property;
|
|
434
|
+
const rawType = sqlType(value);
|
|
435
|
+
if (rawType === undefined)
|
|
436
|
+
return;
|
|
437
|
+
const mapped = catalogType(rawType);
|
|
438
|
+
const note = enumNote(recordField(value, "enum"));
|
|
439
|
+
if (note !== undefined)
|
|
440
|
+
enums.set(note.name, note.labels);
|
|
441
|
+
const identity = identityOf(value);
|
|
442
|
+
const generated = generatedOf(value);
|
|
443
|
+
const serial = mapped.serial;
|
|
444
|
+
if (serial) {
|
|
445
|
+
const dataType = mapped.dataType === "smallint" || mapped.dataType === "bigint" ? mapped.dataType : "integer";
|
|
446
|
+
objects.push(sequence2({
|
|
447
|
+
namespace,
|
|
448
|
+
name: `${parent.name}_${sqlName}_seq`,
|
|
449
|
+
dataType,
|
|
450
|
+
provenance: provenanceOf(),
|
|
451
|
+
dependencies: [{ kind: "table", namespace, name: parent.name }]
|
|
452
|
+
}));
|
|
453
|
+
}
|
|
454
|
+
const defaultExpression = serial || identity !== undefined || generated !== undefined ? undefined : readDefault(value, extras, `${tableName}.${sqlName}`);
|
|
455
|
+
const nullable = flag(value, "notNull") ? false : true;
|
|
456
|
+
const dataType = note !== undefined ? note.name : mapped.dataType;
|
|
457
|
+
const solePrimary = identity?.always === true && dataType === "bigint" && !nullable && primaryColumns.length === 1 && primaryColumns[0] === sqlName;
|
|
458
|
+
return column2({
|
|
459
|
+
parent,
|
|
460
|
+
name: sqlName,
|
|
461
|
+
dataType,
|
|
462
|
+
nullable: solePrimary ? false : nullable,
|
|
463
|
+
provenance: provenanceOf(`field:${property}`),
|
|
464
|
+
...defaultExpression !== undefined ? { defaultExpression } : {},
|
|
465
|
+
...identity !== undefined ? { identity } : {},
|
|
466
|
+
...generated !== undefined ? { generated } : {},
|
|
467
|
+
...note !== undefined ? { dependencies: [{ kind: "type", namespace, name: note.name }] } : {}
|
|
468
|
+
});
|
|
469
|
+
}
|
|
470
|
+
function primaryColumnsOf(config, columns) {
|
|
471
|
+
const declared = config.primaryKeys[0];
|
|
472
|
+
if (declared !== undefined && declared.columns.length > 0) {
|
|
473
|
+
return declared.columns.map((entry) => entry.name);
|
|
474
|
+
}
|
|
475
|
+
const names = [];
|
|
476
|
+
for (const value of Object.values(columns)) {
|
|
477
|
+
if (flag(value, "primary")) {
|
|
478
|
+
const name = stringField(value, "name");
|
|
479
|
+
if (name !== undefined)
|
|
480
|
+
names.push(name);
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
return names;
|
|
484
|
+
}
|
|
485
|
+
function catalogType(sqlType) {
|
|
486
|
+
if (sqlType === "serial")
|
|
487
|
+
return { dataType: "integer", serial: true };
|
|
488
|
+
if (sqlType === "bigserial")
|
|
489
|
+
return { dataType: "bigint", serial: true };
|
|
490
|
+
if (sqlType === "smallserial")
|
|
491
|
+
return { dataType: "smallint", serial: true };
|
|
492
|
+
if (sqlType === "timestamp")
|
|
493
|
+
return { dataType: "timestamp without time zone", serial: false };
|
|
494
|
+
if (sqlType === "time")
|
|
495
|
+
return { dataType: "time without time zone", serial: false };
|
|
496
|
+
return { dataType: sqlType, serial: false };
|
|
497
|
+
}
|
|
498
|
+
function identityOf(value) {
|
|
499
|
+
const identity = recordField(value, "generatedIdentity");
|
|
500
|
+
if (typeof identity !== "object" || identity === null)
|
|
501
|
+
return;
|
|
502
|
+
const type = stringField(identity, "type");
|
|
503
|
+
if (type === "always")
|
|
504
|
+
return { always: true };
|
|
505
|
+
if (type === "byDefault")
|
|
506
|
+
return { always: false };
|
|
507
|
+
return;
|
|
508
|
+
}
|
|
509
|
+
function generatedOf(value) {
|
|
510
|
+
const generated = recordField(value, "generated");
|
|
511
|
+
if (typeof generated !== "object" || generated === null)
|
|
512
|
+
return;
|
|
513
|
+
const expression = sqlText(recordField(generated, "as"));
|
|
514
|
+
if (expression === undefined || expression.length === 0)
|
|
515
|
+
return;
|
|
516
|
+
const mode = stringField(generated, "mode");
|
|
517
|
+
return { stored: mode !== "virtual", expression };
|
|
518
|
+
}
|
|
519
|
+
function readDefault(value, extras, label) {
|
|
520
|
+
const hasFn = typeof recordField(value, "defaultFn") === "function";
|
|
521
|
+
const raw = recordField(value, "default");
|
|
522
|
+
if (raw === undefined) {
|
|
523
|
+
if (hasFn)
|
|
524
|
+
extras.push(extra("column", label, "a client default is not a SQL default"));
|
|
525
|
+
return;
|
|
526
|
+
}
|
|
527
|
+
const rendered = sqlText(raw);
|
|
528
|
+
if (rendered === undefined || rendered.length === 0) {
|
|
529
|
+
extras.push(extra("column", label, "the default expression could not be read"));
|
|
530
|
+
return;
|
|
531
|
+
}
|
|
532
|
+
return rendered;
|
|
533
|
+
}
|
|
534
|
+
function sqlType(value) {
|
|
535
|
+
if (!("getSQLType" in value) || typeof value.getSQLType !== "function")
|
|
536
|
+
return;
|
|
537
|
+
const type = value.getSQLType.call(value);
|
|
538
|
+
return typeof type === "string" && type.length > 0 ? type : undefined;
|
|
539
|
+
}
|
|
540
|
+
function sqlText(value) {
|
|
541
|
+
if (typeof value === "string")
|
|
542
|
+
return quoteLiteral(value);
|
|
543
|
+
if (typeof value === "boolean")
|
|
544
|
+
return value ? "true" : "false";
|
|
545
|
+
if (typeof value === "number" && Number.isFinite(value))
|
|
546
|
+
return String(value);
|
|
547
|
+
if (value === null)
|
|
548
|
+
return "null";
|
|
549
|
+
if (typeof value !== "object")
|
|
550
|
+
return;
|
|
551
|
+
const chunks = recordField(value, "queryChunks");
|
|
552
|
+
if (!Array.isArray(chunks))
|
|
553
|
+
return;
|
|
554
|
+
let text = "";
|
|
555
|
+
for (const chunk of chunks) {
|
|
556
|
+
if (typeof chunk !== "object" || chunk === null)
|
|
557
|
+
return;
|
|
558
|
+
const literal = recordField(chunk, "value");
|
|
559
|
+
if (Array.isArray(literal) && typeof literal[0] === "string") {
|
|
560
|
+
text += literal[0];
|
|
561
|
+
continue;
|
|
562
|
+
}
|
|
563
|
+
const name = stringField(chunk, "name");
|
|
564
|
+
if (name !== undefined) {
|
|
565
|
+
text += `"${name.replaceAll('"', '""')}"`;
|
|
566
|
+
continue;
|
|
567
|
+
}
|
|
568
|
+
return;
|
|
569
|
+
}
|
|
570
|
+
return text;
|
|
571
|
+
}
|
|
572
|
+
function namesIn(value) {
|
|
573
|
+
if (typeof value !== "object" || value === null)
|
|
574
|
+
return [];
|
|
575
|
+
const chunks = recordField(value, "queryChunks");
|
|
576
|
+
if (!Array.isArray(chunks))
|
|
577
|
+
return [];
|
|
578
|
+
const names = [];
|
|
579
|
+
for (const chunk of chunks) {
|
|
580
|
+
const name = typeof chunk === "object" && chunk !== null ? stringField(chunk, "name") : undefined;
|
|
581
|
+
if (name !== undefined && !names.includes(name))
|
|
582
|
+
names.push(name);
|
|
583
|
+
}
|
|
584
|
+
return names;
|
|
585
|
+
}
|
|
586
|
+
function enumNote(value) {
|
|
587
|
+
if (typeof value !== "function" && (typeof value !== "object" || value === null))
|
|
588
|
+
return;
|
|
589
|
+
if (typeof value === "function" && (!(ENUM_SYMBOL in value) || value[ENUM_SYMBOL] !== true)) {
|
|
590
|
+
return;
|
|
591
|
+
}
|
|
592
|
+
if (typeof value === "object" && value !== null && !("enumName" in value))
|
|
593
|
+
return;
|
|
594
|
+
const name = stringField(value, "enumName");
|
|
595
|
+
const labels = recordField(value, "enumValues");
|
|
596
|
+
if (name === undefined || !Array.isArray(labels))
|
|
597
|
+
return;
|
|
598
|
+
const strings = labels.filter((label) => typeof label === "string");
|
|
599
|
+
if (strings.length === 0)
|
|
600
|
+
return;
|
|
601
|
+
return { name, labels: strings };
|
|
602
|
+
}
|
|
603
|
+
function action(value) {
|
|
604
|
+
if (value === undefined || value === "no action")
|
|
605
|
+
return;
|
|
606
|
+
return referentialAction(value);
|
|
607
|
+
}
|
|
608
|
+
function keyName(name, columns) {
|
|
609
|
+
if (name !== undefined && /^[A-Za-z_][A-Za-z0-9_]*$/.test(name))
|
|
610
|
+
return name;
|
|
611
|
+
const joined = columns.join("_");
|
|
612
|
+
if (/^[A-Za-z_][A-Za-z0-9_]*$/.test(joined))
|
|
613
|
+
return joined;
|
|
614
|
+
return "key";
|
|
615
|
+
}
|
|
616
|
+
function sameColumns(left, right) {
|
|
617
|
+
if (left.length !== right.length)
|
|
618
|
+
return false;
|
|
619
|
+
return left.every((name, index) => name === right[index]);
|
|
620
|
+
}
|
|
621
|
+
function provenanceOf(source) {
|
|
622
|
+
return source === undefined ? { origin: "file", name: "drizzle" } : { origin: "file", name: "drizzle", source };
|
|
623
|
+
}
|
|
624
|
+
function extra(kind, label, reason) {
|
|
625
|
+
return { key: `import:${kind}:${label}`, kind, label, reason, sql: "" };
|
|
626
|
+
}
|
|
627
|
+
function viewName(value, drizzle, fallback) {
|
|
628
|
+
try {
|
|
629
|
+
const config = drizzle.getViewConfig(value);
|
|
630
|
+
return config.name ?? fallback;
|
|
631
|
+
} catch {
|
|
632
|
+
return fallback;
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
function flag(value, key) {
|
|
636
|
+
return recordField(value, key) === true;
|
|
637
|
+
}
|
|
638
|
+
function stringField(value, key) {
|
|
639
|
+
const field = recordField(value, key);
|
|
640
|
+
return typeof field === "string" && field.length > 0 ? field : undefined;
|
|
641
|
+
}
|
|
642
|
+
function recordField(value, key) {
|
|
643
|
+
if (!Object.prototype.hasOwnProperty.call(value, key))
|
|
644
|
+
return;
|
|
645
|
+
return value[key];
|
|
646
|
+
}
|
|
647
|
+
async function layoutFor(cwd, out) {
|
|
648
|
+
if (out !== undefined)
|
|
649
|
+
return pullLayout2(cwd, "schema.ts", "tables", out);
|
|
650
|
+
const configPath = join(cwd, "okmodel.config.ts");
|
|
651
|
+
if (!existsSync(configPath))
|
|
652
|
+
return pullLayout2(cwd, "schema.ts", "tables", undefined);
|
|
653
|
+
const config = await loadConfig(cwd);
|
|
654
|
+
return pullLayout2(cwd, config.schema, config.tables, undefined);
|
|
655
|
+
}
|
|
656
|
+
function compare(left, right) {
|
|
657
|
+
if (left < right)
|
|
658
|
+
return -1;
|
|
659
|
+
if (left > right)
|
|
660
|
+
return 1;
|
|
661
|
+
return 0;
|
|
662
|
+
}
|
|
663
|
+
export {
|
|
664
|
+
importDrizzle
|
|
665
|
+
};
|