okmodel 0.1.1 → 0.3.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 +727 -37
- package/dist/adapters/capabilities.d.ts +34 -0
- package/dist/adapters/pg/bunsql-extra.d.ts +17 -0
- package/dist/adapters/pg/bunsql.d.ts +60 -0
- package/dist/adapters/pg/call.d.ts +2 -12
- package/dist/adapters/pg/nodepostgres-extra.d.ts +18 -0
- package/dist/adapters/pg/nodepostgres.d.ts +57 -0
- package/dist/adapters/pg/postgres-extra.d.ts +50 -1
- package/dist/adapters/pg/postgresjs.d.ts +0 -4
- package/dist/adapters/pg/session.d.ts +94 -0
- package/dist/adapters/pg/watch.d.ts +18 -0
- package/dist/contracts/catalog/enum.d.ts +31 -4
- package/dist/contracts/catalog/extension.d.ts +61 -0
- package/dist/contracts/catalog/object.d.ts +1 -0
- package/dist/contracts/catalog/privilege.d.ts +64 -0
- package/dist/contracts/catalog/routine.d.ts +56 -0
- package/dist/contracts/catalog/types.d.ts +179 -18
- package/dist/contracts/catalog/view.d.ts +73 -0
- package/dist/contracts/connection.d.ts +13 -0
- package/dist/contracts/driver.d.ts +18 -5
- package/dist/contracts/error.d.ts +17 -1
- package/dist/contracts/generator.d.ts +63 -0
- package/dist/contracts/index.d.ts +2 -2
- package/dist/contracts/index.js +1 -1
- package/dist/contracts/internal.js +11 -9
- package/dist/contracts/location.d.ts +24 -0
- package/dist/contracts/rows.d.ts +15 -0
- package/dist/dialects/pg/archive-bind.d.ts +22 -0
- package/dist/dialects/pg/column.d.ts +129 -10
- package/dist/dialects/pg/compile.d.ts +1 -0
- package/dist/dialects/pg/custom.d.ts +6 -0
- package/dist/dialects/pg/ddl.d.ts +65 -1
- package/dist/dialects/pg/enum.d.ts +10 -8
- package/dist/dialects/pg/ext/citext.d.ts +14 -0
- package/dist/dialects/pg/ext/citext.js +11 -0
- package/dist/dialects/pg/ext/index.d.ts +52 -0
- package/dist/dialects/pg/ext/index.js +6 -0
- package/dist/dialects/pg/ext/pg-trgm.d.ts +53 -0
- package/dist/dialects/pg/ext/pg-trgm.js +42 -0
- package/dist/dialects/pg/filter-parse.d.ts +19 -0
- package/dist/dialects/pg/filters.d.ts +19 -0
- package/dist/dialects/pg/fn/index.d.ts +107 -0
- package/dist/dialects/pg/fn/index.js +175 -0
- package/dist/dialects/pg/fn/touch.d.ts +14 -0
- package/dist/dialects/pg/index.d.ts +11 -2
- package/dist/dialects/pg/index.js +243 -1062
- package/dist/dialects/pg/introspect.d.ts +10 -1
- package/dist/dialects/pg/keys.d.ts +19 -3
- package/dist/dialects/pg/model.d.ts +223 -2
- package/dist/dialects/pg/operator-fit.d.ts +86 -0
- package/dist/dialects/pg/operators.d.ts +46 -1
- package/dist/dialects/pg/ops/arr-append.d.ts +9 -0
- package/dist/dialects/pg/ops/arr-ns.d.ts +7 -0
- package/dist/dialects/pg/ops/arr-remove.d.ts +9 -0
- package/dist/dialects/pg/ops/containedBy.d.ts +9 -0
- package/dist/dialects/pg/ops/contains.d.ts +8 -4
- package/dist/dialects/pg/ops/hasAnyKey.d.ts +8 -0
- package/dist/dialects/pg/ops/hasKey.d.ts +8 -0
- package/dist/dialects/pg/ops/json-ns.d.ts +6 -0
- package/dist/dialects/pg/ops/json-set.d.ts +12 -0
- package/dist/dialects/pg/ops/matches.d.ts +16 -0
- package/dist/dialects/pg/ops/overlaps.d.ts +9 -0
- package/dist/dialects/pg/ops/path.d.ts +13 -0
- package/dist/dialects/pg/preset.d.ts +76 -0
- package/dist/dialects/pg/relations.d.ts +59 -2
- package/dist/dialects/pg/role/check.d.ts +45 -0
- package/dist/dialects/pg/role/index.d.ts +50 -0
- package/dist/dialects/pg/role/sql.d.ts +44 -0
- package/dist/dialects/pg/schema.d.ts +109 -5
- package/dist/dialects/pg/table.d.ts +137 -11
- package/dist/dialects/pg/temporal.d.ts +9 -7
- package/dist/dialects/pg/tenancy.d.ts +103 -0
- package/dist/dialects/pg/through.d.ts +21 -0
- package/dist/dialects/pg/trait.d.ts +141 -0
- package/dist/dialects/pg/view/client.d.ts +30 -0
- package/dist/dialects/pg/view/depend.d.ts +35 -0
- package/dist/dialects/pg/view/index.d.ts +114 -0
- package/dist/dialects/pg/view/index.js +289 -0
- package/dist/dialects/pg/view/scratch.d.ts +21 -0
- package/dist/dialects/pg/where.d.ts +37 -0
- package/dist/runtime/aggregate.d.ts +25 -0
- package/dist/runtime/archive-handle.d.ts +18 -0
- package/dist/runtime/archive-rules.d.ts +12 -0
- package/dist/runtime/archive.d.ts +73 -0
- package/dist/runtime/batch.d.ts +20 -0
- package/dist/runtime/client.d.ts +114 -5
- package/dist/runtime/conflict.d.ts +2 -1
- package/dist/runtime/exposure.d.ts +27 -0
- package/dist/runtime/hookm.d.ts +39 -0
- package/dist/runtime/ids/index.d.ts +11 -0
- package/dist/runtime/ids/index.js +193 -0
- package/dist/runtime/ids/okid-extended.d.ts +19 -0
- package/dist/runtime/ids/okid-generator.d.ts +21 -0
- package/dist/runtime/ids/okid-shared.d.ts +89 -0
- package/dist/runtime/ids/okid.d.ts +24 -0
- package/dist/runtime/ids/uuid.d.ts +21 -0
- package/dist/runtime/lock.d.ts +22 -0
- package/dist/runtime/operator-sql.d.ts +8 -0
- package/dist/runtime/page.d.ts +25 -0
- package/dist/runtime/pg/bun.d.ts +30 -0
- package/dist/runtime/pg/bun.js +52 -0
- package/dist/runtime/pg/pg.d.ts +29 -0
- package/dist/runtime/pg/pg.js +52 -0
- package/dist/runtime/pg/pglite.js +16 -17
- package/dist/runtime/pg/postgresjs.js +14 -24
- package/dist/runtime/plan.d.ts +146 -7
- package/dist/runtime/preset-stack.d.ts +17 -0
- package/dist/runtime/presets.d.ts +48 -0
- package/dist/runtime/read-stream.d.ts +26 -0
- package/dist/runtime/safety/index.d.ts +159 -0
- package/dist/runtime/safety/index.js +294 -0
- package/dist/runtime/safety-hook.d.ts +42 -0
- package/dist/runtime/server-check.d.ts +34 -0
- package/dist/runtime/tenancy/index.d.ts +37 -0
- package/dist/runtime/tenancy/index.js +368 -0
- package/dist/runtime/trait-read.d.ts +30 -0
- package/dist/runtime/traits/index.d.ts +133 -0
- package/dist/runtime/traits/index.js +646 -0
- package/dist/runtime/transaction.d.ts +19 -0
- package/dist/runtime/tx.d.ts +22 -7
- package/dist/runtime/types.d.ts +376 -27
- package/dist/runtime/validate/bind.d.ts +11 -0
- package/dist/runtime/validate/closed.d.ts +16 -0
- package/dist/runtime/validate/engine.d.ts +41 -0
- package/dist/runtime/validate/hook.d.ts +17 -0
- package/dist/runtime/validate/index.d.ts +21 -0
- package/dist/runtime/validate/index.js +175 -0
- package/dist/runtime/validate/load.d.ts +13 -0
- package/dist/runtime/validate/places.d.ts +41 -0
- package/dist/runtime/validate/rules.d.ts +108 -0
- package/dist/runtime/validate/surface.d.ts +102 -0
- package/dist/runtime/write.d.ts +30 -5
- package/dist/shared/01yr20s5.js +149 -0
- package/dist/shared/0jc3ffzf.js +154 -0
- package/dist/shared/0v1g424p.js +26 -0
- package/dist/shared/1g0drakv.js +225 -0
- package/dist/shared/{kprcwaha.js → 1tfdfvhh.js} +5 -3
- package/dist/shared/{05w44d8g.js → 2ssqrv66.js} +767 -715
- package/dist/shared/3ekj9x92.js +210 -0
- package/dist/shared/49ypnfwt.js +1122 -0
- package/dist/shared/4e4c6vq2.js +62 -0
- package/dist/shared/4m9fn52z.js +70 -0
- package/dist/shared/{ycwz42az.js → 52szzakm.js} +6 -26
- package/dist/shared/5nmdenb6.js +1229 -0
- package/dist/shared/672t5vym.js +12 -0
- package/dist/shared/6bazvqwm.js +86 -0
- package/dist/shared/6w5j844a.js +201 -0
- package/dist/shared/{fgqgsb5z.js → 75xkjv0t.js} +2 -2
- package/dist/shared/7fvtrkwr.js +84 -0
- package/dist/shared/7m88x3e9.js +45 -0
- package/dist/shared/{mtkwv8wx.js → 7p2wbz7f.js} +1 -1
- package/dist/shared/83eaefwr.js +10 -0
- package/dist/shared/94ez8kzh.js +19 -0
- package/dist/shared/96ps4g1z.js +32 -0
- package/dist/shared/{xqbhg6cn.js → 999ade2q.js} +95 -10
- package/dist/shared/9m80mkac.js +158 -0
- package/dist/shared/9x25fxhc.js +150 -0
- package/dist/shared/a5sxz52y.js +13 -0
- package/dist/shared/axhk06q1.js +156 -0
- package/dist/shared/{dynq16vb.js → b066g80h.js} +8 -6
- package/dist/shared/bqha5cg6.js +229 -0
- package/dist/shared/bryqk9k9.js +31 -0
- package/dist/shared/c8aghh05.js +442 -0
- package/dist/shared/c9z5g0fm.js +309 -0
- package/dist/shared/{m3ypagh5.js → dbjej1tw.js} +124 -15
- package/dist/shared/dyj86cmj.js +69 -0
- package/dist/shared/e6wg00wd.js +164 -0
- package/dist/shared/{hadrfb9h.js → edwqqryd.js} +17 -14
- package/dist/shared/{qk3hxfja.js → fptpmvxp.js} +5 -5
- package/dist/shared/fs0ex00p.js +76 -0
- package/dist/shared/{jjb7yht9.js → g4f5kcx9.js} +3 -3
- package/dist/shared/gjthds9j.js +48 -0
- package/dist/shared/hcmdtwgh.js +102 -0
- package/dist/shared/{g5fhqmwe.js → jkc6ws1p.js} +6 -56
- package/dist/shared/{yd1kzwn2.js → jns2wx4t.js} +55 -12
- package/dist/shared/k34e0xf2.js +450 -0
- package/dist/shared/k3ted12y.js +33 -0
- package/dist/shared/k7dwkhvq.js +31 -0
- package/dist/shared/{8b04d28c.js → kgnc8grt.js} +2 -2
- package/dist/shared/{hd293j9g.js → kyxb62mc.js} +14 -2
- package/dist/shared/m78s7898.js +376 -0
- package/dist/shared/m9tv7xn7.js +404 -0
- package/dist/shared/meck53jv.js +99 -0
- package/dist/shared/mt5wswjz.js +90 -0
- package/dist/shared/n0dgs0pb.js +75 -0
- package/dist/shared/n442h54c.js +14 -0
- package/dist/shared/n4jqhpqz.js +27 -0
- package/dist/shared/n7mcc1gk.js +132 -0
- package/dist/shared/ne02ng5c.js +68 -0
- package/dist/shared/{mkjmqjv4.js → nfzc5z0w.js} +2 -104
- package/dist/shared/nsva2a6b.js +42 -0
- package/dist/shared/p24x2334.js +195 -0
- package/dist/shared/qbxzvwwc.js +88 -0
- package/dist/shared/{w8samvk1.js → qybh48nd.js} +1 -1
- package/dist/shared/r6nwkxnt.js +86 -0
- package/dist/shared/{xy79j5hq.js → rrstp3kb.js} +25 -36
- package/dist/shared/rwmrejjz.js +26 -0
- package/dist/shared/{zhc5vmgf.js → s6gqfn5j.js} +2 -2
- package/dist/shared/{523ft3e8.js → scwxrpx2.js} +7 -5
- package/dist/shared/{j5cn2963.js → t4tbsy9h.js} +13 -64
- package/dist/shared/{y9m15dfv.js → t616ynfn.js} +544 -32
- package/dist/shared/{ngk8gj61.js → tbszx8mz.js} +15 -1
- package/dist/shared/th14ygv2.js +69 -0
- package/dist/shared/tmbdywx0.js +178 -0
- package/dist/shared/ty3v3hfk.js +64 -0
- package/dist/shared/wekze34d.js +246 -0
- package/dist/shared/x6wysh83.js +1132 -0
- package/dist/shared/{rgs0st28.js → xrfnq5k8.js} +237 -434
- package/dist/shared/{x5x59srr.js → xva8v7qk.js} +416 -83
- package/dist/shared/{dkn3y60a.js → xw1d69vd.js} +76 -112
- package/dist/shared/y005b761.js +112 -0
- package/dist/shared/ydaz1sv3.js +55 -0
- package/dist/shared/{p0cbr3g2.js → zwmqxb0g.js} +1 -1
- package/dist/tooling/cli.js +40 -12
- package/dist/tooling/migrate/apply.d.ts +16 -0
- package/dist/tooling/migrate/commands.d.ts +12 -1
- package/dist/tooling/migrate/config.d.ts +8 -0
- package/dist/tooling/migrate/doctor.d.ts +25 -0
- package/dist/tooling/migrate/drift.d.ts +19 -0
- package/dist/tooling/migrate/engine.d.ts +36 -0
- package/dist/tooling/migrate/ext.d.ts +17 -0
- package/dist/tooling/migrate/extensions.d.ts +51 -0
- package/dist/tooling/migrate/managed.d.ts +17 -0
- package/dist/tooling/migrate/plan.d.ts +14 -1
- package/dist/tooling/migrate/project.d.ts +25 -1
- package/dist/tooling/migrate/type-name.d.ts +19 -0
- package/package.json +61 -4
- package/dist/shared/gk7cs7sp.js +0 -566
|
@@ -30,6 +30,20 @@ function isConnectionFailure(error) {
|
|
|
30
30
|
const message = error.message;
|
|
31
31
|
return message.includes("ECONNREFUSED") || message.includes("ECONNRESET") || message.includes("CONNECTION_CLOSED") || message.includes("terminating connection") || message.includes("The pool is closed");
|
|
32
32
|
}
|
|
33
|
+
function isConnectionLost(error) {
|
|
34
|
+
if (isConnectionFailure(error))
|
|
35
|
+
return true;
|
|
36
|
+
if (typeof error !== "object" || error === null)
|
|
37
|
+
return false;
|
|
38
|
+
if (Reflect.get(error, "kind") === "outcome_unknown")
|
|
39
|
+
return true;
|
|
40
|
+
if (readCode(error, "code") === "ERR_POSTGRES_CONNECTION_CLOSED")
|
|
41
|
+
return true;
|
|
42
|
+
if (error instanceof Error && error.message.includes("Connection terminated"))
|
|
43
|
+
return true;
|
|
44
|
+
const cause = Reflect.get(error, "cause");
|
|
45
|
+
return cause !== undefined && cause !== error && isConnectionLost(cause);
|
|
46
|
+
}
|
|
33
47
|
function readCode(error, key) {
|
|
34
48
|
if (typeof error !== "object" || error === null || !(key in error))
|
|
35
49
|
return;
|
|
@@ -37,4 +51,4 @@ function readCode(error, key) {
|
|
|
37
51
|
return typeof value === "string" && value.length > 0 ? value : undefined;
|
|
38
52
|
}
|
|
39
53
|
|
|
40
|
-
export { isConnectionSqlstate, isConnectionFailure };
|
|
54
|
+
export { isConnectionSqlstate, isConnectionFailure, isConnectionLost };
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import {
|
|
2
|
+
normaliseEdges,
|
|
3
|
+
assertProvenance
|
|
4
|
+
} from "./t4tbsy9h.js";
|
|
5
|
+
import {
|
|
6
|
+
assertIdentifier2
|
|
7
|
+
} from "./nfzc5z0w.js";
|
|
8
|
+
import {
|
|
9
|
+
catalogError2
|
|
10
|
+
} from "./52szzakm.js";
|
|
11
|
+
|
|
12
|
+
// src/contracts/catalog/extension.ts
|
|
13
|
+
function extensionObject(input) {
|
|
14
|
+
assertIdentifier2(input.name, "extension");
|
|
15
|
+
assertIdentifier2(input.schema, "extension schema");
|
|
16
|
+
assertProvenance(input.provenance);
|
|
17
|
+
assertVersion(input.version);
|
|
18
|
+
const definition = {
|
|
19
|
+
schema: input.schema,
|
|
20
|
+
relocatable: input.relocatable ?? true,
|
|
21
|
+
...input.version !== undefined ? { version: input.version } : {}
|
|
22
|
+
};
|
|
23
|
+
return {
|
|
24
|
+
kind: "extension",
|
|
25
|
+
identity: { kind: "extension", name: input.name },
|
|
26
|
+
owner: input.owner ?? "managed",
|
|
27
|
+
definition,
|
|
28
|
+
dependencies: normaliseEdges(input.dependencies ?? []),
|
|
29
|
+
provenance: input.provenance
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
function isExactVersion(version) {
|
|
33
|
+
return version !== undefined && /^\d+(?:\.\d+)*$/.test(version);
|
|
34
|
+
}
|
|
35
|
+
function compareExtensionVersions(left, right) {
|
|
36
|
+
const a = parts(left);
|
|
37
|
+
const b = parts(right);
|
|
38
|
+
const length = Math.max(a.length, b.length);
|
|
39
|
+
for (let index = 0;index < length; index += 1) {
|
|
40
|
+
const diff = (a[index] ?? 0) - (b[index] ?? 0);
|
|
41
|
+
if (diff !== 0)
|
|
42
|
+
return diff < 0 ? -1 : 1;
|
|
43
|
+
}
|
|
44
|
+
return 0;
|
|
45
|
+
}
|
|
46
|
+
function sameExtensionDefinition(before, after) {
|
|
47
|
+
if (before.definition.schema !== after.definition.schema)
|
|
48
|
+
return false;
|
|
49
|
+
if (before.definition.relocatable !== after.definition.relocatable)
|
|
50
|
+
return false;
|
|
51
|
+
return sameVersion(before.definition.version, after.definition.version);
|
|
52
|
+
}
|
|
53
|
+
function sameVersion(before, after) {
|
|
54
|
+
if (!isExactVersion(after) || !isExactVersion(before))
|
|
55
|
+
return true;
|
|
56
|
+
return before === after;
|
|
57
|
+
}
|
|
58
|
+
function assertVersion(version) {
|
|
59
|
+
if (version === undefined)
|
|
60
|
+
return;
|
|
61
|
+
if (isExactVersion(version) || /^>=\d+(?:\.\d+)*$/.test(version))
|
|
62
|
+
return;
|
|
63
|
+
catalogError2("OKM1020", `Extension version ${String(version)} must be a dotted version or a >= floor.`);
|
|
64
|
+
}
|
|
65
|
+
function parts(version) {
|
|
66
|
+
return version.split(".").map((part) => Number(part));
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export { extensionObject, isExactVersion, compareExtensionVersions, sameExtensionDefinition };
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import {
|
|
2
|
+
arrayElement
|
|
3
|
+
} from "./94ez8kzh.js";
|
|
4
|
+
import {
|
|
5
|
+
OkmError2
|
|
6
|
+
} from "./52szzakm.js";
|
|
7
|
+
|
|
8
|
+
// src/dialects/pg/misuse.ts
|
|
9
|
+
var COLUMN_DEFINITION_CODE = "OKM1060";
|
|
10
|
+
var RESERVED_OPTION_CODE = "OKM1061";
|
|
11
|
+
var CODEC_VALUE_CODE = "OKM1210";
|
|
12
|
+
function definition(message) {
|
|
13
|
+
throw new OkmError2(COLUMN_DEFINITION_CODE, message);
|
|
14
|
+
}
|
|
15
|
+
function unavailable(message) {
|
|
16
|
+
throw new OkmError2(RESERVED_OPTION_CODE, message);
|
|
17
|
+
}
|
|
18
|
+
function rejected(message) {
|
|
19
|
+
throw new OkmError2(CODEC_VALUE_CODE, message);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// src/dialects/pg/array-literal.ts
|
|
23
|
+
function writeArray(value, dims, encode, raw) {
|
|
24
|
+
return writeLevel(value, dims, encode, raw);
|
|
25
|
+
}
|
|
26
|
+
function readArray(wire, dims, decode) {
|
|
27
|
+
const cursor = { text: wire, index: 0 };
|
|
28
|
+
const parsed = parseValue(cursor);
|
|
29
|
+
skipSpace(cursor);
|
|
30
|
+
if (cursor.index !== cursor.text.length) {
|
|
31
|
+
rejected("Array literal has trailing text.");
|
|
32
|
+
}
|
|
33
|
+
if (!Array.isArray(parsed)) {
|
|
34
|
+
rejected("Array literal must start with `{`.");
|
|
35
|
+
}
|
|
36
|
+
return interpret(parsed, dims, decode);
|
|
37
|
+
}
|
|
38
|
+
function writeLevel(value, dims, encode, raw) {
|
|
39
|
+
if (!Array.isArray(value)) {
|
|
40
|
+
rejected("Array column expected an array.");
|
|
41
|
+
}
|
|
42
|
+
const parts = [];
|
|
43
|
+
if (dims === 1) {
|
|
44
|
+
for (const item of value) {
|
|
45
|
+
if (item === null || item === undefined) {
|
|
46
|
+
rejected("Array elements cannot be null.");
|
|
47
|
+
}
|
|
48
|
+
parts.push(arrayElement(encode(item), raw));
|
|
49
|
+
}
|
|
50
|
+
} else {
|
|
51
|
+
for (const item of value) {
|
|
52
|
+
parts.push(writeLevel(item, dims - 1, encode, raw));
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return `{${parts.join(",")}}`;
|
|
56
|
+
}
|
|
57
|
+
function interpret(nodes, dims, decode) {
|
|
58
|
+
if (dims === 1) {
|
|
59
|
+
return nodes.map((cell) => {
|
|
60
|
+
if (typeof cell !== "string") {
|
|
61
|
+
rejected("Array rank does not match dims.");
|
|
62
|
+
}
|
|
63
|
+
return decode(cell);
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
return nodes.map((cell) => {
|
|
67
|
+
if (!Array.isArray(cell)) {
|
|
68
|
+
rejected("Array rank does not match dims.");
|
|
69
|
+
}
|
|
70
|
+
return interpret(cell, dims - 1, decode);
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
function parseValue(cursor) {
|
|
74
|
+
skipSpace(cursor);
|
|
75
|
+
if (peek(cursor) === "{") {
|
|
76
|
+
return parseArray(cursor);
|
|
77
|
+
}
|
|
78
|
+
return parseElement(cursor);
|
|
79
|
+
}
|
|
80
|
+
function parseArray(cursor) {
|
|
81
|
+
expectChar(cursor, "{");
|
|
82
|
+
skipSpace(cursor);
|
|
83
|
+
if (peek(cursor) === "}") {
|
|
84
|
+
cursor.index += 1;
|
|
85
|
+
return [];
|
|
86
|
+
}
|
|
87
|
+
const items = [];
|
|
88
|
+
for (;; ) {
|
|
89
|
+
items.push(parseValue(cursor));
|
|
90
|
+
skipSpace(cursor);
|
|
91
|
+
if (peek(cursor) === ",") {
|
|
92
|
+
cursor.index += 1;
|
|
93
|
+
skipSpace(cursor);
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
expectChar(cursor, "}");
|
|
97
|
+
return items;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
function parseElement(cursor) {
|
|
101
|
+
if (peek(cursor) === '"') {
|
|
102
|
+
return parseQuoted(cursor);
|
|
103
|
+
}
|
|
104
|
+
const start = cursor.index;
|
|
105
|
+
while (cursor.index < cursor.text.length) {
|
|
106
|
+
const char = cursor.text[cursor.index];
|
|
107
|
+
if (char === "," || char === "}" || char === undefined || isSpace(char)) {
|
|
108
|
+
break;
|
|
109
|
+
}
|
|
110
|
+
cursor.index += 1;
|
|
111
|
+
}
|
|
112
|
+
const token = cursor.text.slice(start, cursor.index);
|
|
113
|
+
if (token.length === 0) {
|
|
114
|
+
rejected("Array literal is missing an element.");
|
|
115
|
+
}
|
|
116
|
+
if (token === "NULL") {
|
|
117
|
+
return null;
|
|
118
|
+
}
|
|
119
|
+
return token;
|
|
120
|
+
}
|
|
121
|
+
function parseQuoted(cursor) {
|
|
122
|
+
expectChar(cursor, '"');
|
|
123
|
+
let out = "";
|
|
124
|
+
for (;; ) {
|
|
125
|
+
const char = next(cursor);
|
|
126
|
+
if (char === undefined) {
|
|
127
|
+
rejected("Array literal ended inside a quote.");
|
|
128
|
+
}
|
|
129
|
+
if (char === '"') {
|
|
130
|
+
if (peek(cursor) === '"') {
|
|
131
|
+
cursor.index += 1;
|
|
132
|
+
out += '"';
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
return out;
|
|
136
|
+
}
|
|
137
|
+
if (char === "\\") {
|
|
138
|
+
const escaped = next(cursor);
|
|
139
|
+
if (escaped === undefined) {
|
|
140
|
+
rejected("Array literal ended inside an escape.");
|
|
141
|
+
}
|
|
142
|
+
out += escaped;
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
out += char;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
function expectChar(cursor, expected) {
|
|
149
|
+
if (peek(cursor) !== expected) {
|
|
150
|
+
rejected(`Array literal expected ${expected}.`);
|
|
151
|
+
}
|
|
152
|
+
cursor.index += 1;
|
|
153
|
+
}
|
|
154
|
+
function peek(cursor) {
|
|
155
|
+
return cursor.text[cursor.index];
|
|
156
|
+
}
|
|
157
|
+
function next(cursor) {
|
|
158
|
+
const char = cursor.text[cursor.index];
|
|
159
|
+
if (char !== undefined) {
|
|
160
|
+
cursor.index += 1;
|
|
161
|
+
}
|
|
162
|
+
return char;
|
|
163
|
+
}
|
|
164
|
+
function skipSpace(cursor) {
|
|
165
|
+
while (cursor.index < cursor.text.length) {
|
|
166
|
+
const char = cursor.text[cursor.index];
|
|
167
|
+
if (char === undefined || !isSpace(char)) {
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
cursor.index += 1;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
function isSpace(char) {
|
|
174
|
+
return char === " " || char === `
|
|
175
|
+
` || char === "\t";
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export { definition, unavailable, rejected, writeArray, readArray };
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import {
|
|
2
|
+
withHttp,
|
|
3
|
+
attachHttp
|
|
4
|
+
} from "./k34e0xf2.js";
|
|
5
|
+
import {
|
|
6
|
+
OkmError2
|
|
7
|
+
} from "./52szzakm.js";
|
|
8
|
+
|
|
9
|
+
// src/runtime/server-check.ts
|
|
10
|
+
var versionQuery = "select current_setting('server_version_num'), version(), (select (xpath('//catalog_hash/text()', query_to_xml('select catalog_hash from okm_meta where id = ''head''', true, false, '')))[1]::text where to_regclass('okm_meta') is not null)";
|
|
11
|
+
async function acceptServer(pool, schema, rows, http, requireMeta, options, source) {
|
|
12
|
+
for (const name in schema.model) {
|
|
13
|
+
if (schema.model[name]?.presets !== undefined) {
|
|
14
|
+
(await import("./r6nwkxnt.js")).checkSchema(schema);
|
|
15
|
+
break;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
const version = rows[0]?.[1];
|
|
19
|
+
if (version === null || version === undefined || !version.startsWith("PostgreSQL")) {
|
|
20
|
+
throw new OkmError2("OKM1801", "The server is not PostgreSQL. The schema dialect is postgres.", withHttp(http, {
|
|
21
|
+
fix: { summary: "Open the schema with the Postgres driver that matches it." }
|
|
22
|
+
}));
|
|
23
|
+
}
|
|
24
|
+
const recorded = rows[0]?.[2];
|
|
25
|
+
if (recorded === null || recorded === undefined || recorded.length === 0) {
|
|
26
|
+
if (requireMeta) {
|
|
27
|
+
throw new OkmError2("OKM1520", "okm_meta has no catalog hash.", withHttp(http, {
|
|
28
|
+
fix: {
|
|
29
|
+
summary: "Apply migrations so okm_meta records the catalog, or omit requireMeta to adopt this database."
|
|
30
|
+
}
|
|
31
|
+
}));
|
|
32
|
+
}
|
|
33
|
+
} else {
|
|
34
|
+
const { assertCompatible } = await import("./fptpmvxp.js");
|
|
35
|
+
try {
|
|
36
|
+
await assertCompatible(pool, schema, recorded, source, options);
|
|
37
|
+
} catch (error) {
|
|
38
|
+
if (error instanceof OkmError2)
|
|
39
|
+
throw attachHttp(http, error);
|
|
40
|
+
throw error;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
const major = Math.floor(Number(rows[0]?.[0] ?? "0") / 1e4);
|
|
44
|
+
const requires = schema.requires?.postgres;
|
|
45
|
+
if (requires === undefined) {
|
|
46
|
+
if (major < 15) {
|
|
47
|
+
throw new OkmError2("OKM1803", `The server is PostgreSQL ${String(major)}. PostgreSQL 15 is the oldest supported.`, withHttp(http, {
|
|
48
|
+
fix: { summary: "Upgrade to PostgreSQL 15, or set schema({ requires }) to this major." }
|
|
49
|
+
}));
|
|
50
|
+
}
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
const match = /^>=(\d+)$/.exec(requires.trim());
|
|
54
|
+
const need = match?.[1] === undefined ? undefined : Number(match[1]);
|
|
55
|
+
if (need === undefined || major < need) {
|
|
56
|
+
throw new OkmError2("OKM1802", `The server is PostgreSQL ${String(major)}. schema({ requires }) asks for ${requires}.`, withHttp(http, {
|
|
57
|
+
fix: { summary: "Upgrade the server, or lower requires to a version the server meets." }
|
|
58
|
+
}));
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
export {
|
|
62
|
+
acceptServer,
|
|
63
|
+
versionQuery
|
|
64
|
+
};
|
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
import {
|
|
2
|
+
isDomain
|
|
3
|
+
} from "./qbxzvwwc.js";
|
|
4
|
+
import {
|
|
5
|
+
quoteLiteral
|
|
6
|
+
} from "./94ez8kzh.js";
|
|
7
|
+
import {
|
|
8
|
+
identityKey2
|
|
9
|
+
} from "./t4tbsy9h.js";
|
|
10
|
+
import {
|
|
11
|
+
fitIdentifier2
|
|
12
|
+
} from "./nfzc5z0w.js";
|
|
13
|
+
|
|
14
|
+
// src/dialects/pg/ddl.ts
|
|
15
|
+
function quoteIdent(name) {
|
|
16
|
+
return `"${name.replaceAll('"', '""')}"`;
|
|
17
|
+
}
|
|
18
|
+
function ownedByView(object) {
|
|
19
|
+
if (object.kind !== "index")
|
|
20
|
+
return false;
|
|
21
|
+
return object.dependencies.some((edge) => edge.target.kind === "view" || edge.target.kind === "materializedView");
|
|
22
|
+
}
|
|
23
|
+
function dropObjectSql(object, schema) {
|
|
24
|
+
if (object.owner === "ignored")
|
|
25
|
+
return;
|
|
26
|
+
switch (object.kind) {
|
|
27
|
+
case "table":
|
|
28
|
+
return `drop table ${qualify(schema, object.identity.name)}`;
|
|
29
|
+
case "column":
|
|
30
|
+
return `alter table ${qualify(schema, object.identity.parent.name)} drop column ${quoteIdent(object.identity.name)}`;
|
|
31
|
+
case "index":
|
|
32
|
+
return `drop index ${qualify(schema, object.identity.name)}`;
|
|
33
|
+
case "constraint":
|
|
34
|
+
return `alter table ${qualify(schema, object.identity.parent.name)} drop constraint ${quoteIdent(object.identity.name)}`;
|
|
35
|
+
case "sequence":
|
|
36
|
+
return `drop sequence ${qualify(schema, object.identity.name)}`;
|
|
37
|
+
case "type":
|
|
38
|
+
return `drop type ${qualify(schema, object.identity.name)}`;
|
|
39
|
+
case "extension":
|
|
40
|
+
return `drop extension ${quoteIdent(object.identity.name)}`;
|
|
41
|
+
case "function":
|
|
42
|
+
return `drop function ${qualify(schema, object.identity.name)}(${object.identity.argTypes.join(", ")})`;
|
|
43
|
+
case "trigger":
|
|
44
|
+
return `drop trigger ${quoteIdent(object.identity.name)} on ${qualify(schema, object.identity.parent.name)}`;
|
|
45
|
+
case "view":
|
|
46
|
+
return `drop view ${qualify(schema, object.identity.name)}`;
|
|
47
|
+
case "materializedView":
|
|
48
|
+
return `drop materialized view ${qualify(schema, object.identity.name)}`;
|
|
49
|
+
default:
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
function createObjectSql(object, schema) {
|
|
54
|
+
switch (object.kind) {
|
|
55
|
+
case "column":
|
|
56
|
+
return `alter table ${qualify(schema, object.identity.parent.name)} add column ${columnSql(object)}`;
|
|
57
|
+
case "index":
|
|
58
|
+
return createIndexSql(object, schema);
|
|
59
|
+
case "constraint":
|
|
60
|
+
return `alter table ${qualify(schema, object.identity.parent.name)} add constraint ${quoteIdent(object.identity.name)} ${constraintBody(object, schema)}`;
|
|
61
|
+
case "sequence":
|
|
62
|
+
return createSequenceSql(object, schema);
|
|
63
|
+
case "type":
|
|
64
|
+
return createTypeSql(object, schema);
|
|
65
|
+
case "extension":
|
|
66
|
+
return createExtensionSql(object);
|
|
67
|
+
case "function":
|
|
68
|
+
return functionSql(object, schema, false);
|
|
69
|
+
case "trigger":
|
|
70
|
+
return triggerSql(object, schema);
|
|
71
|
+
case "view":
|
|
72
|
+
return viewSql(object, schema, false);
|
|
73
|
+
case "materializedView":
|
|
74
|
+
return materializedViewSql(object, schema);
|
|
75
|
+
case "table":
|
|
76
|
+
return;
|
|
77
|
+
default:
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
function functionSql(object, schema, replace) {
|
|
82
|
+
const definition = object.definition;
|
|
83
|
+
const args = definition.arguments.map((argument) => `${quoteIdent(argument.name)} ${argument.type}`).join(", ");
|
|
84
|
+
const head = `${replace ? "create or replace" : "create"} function ${qualify(schema, object.identity.name)}(${args})`;
|
|
85
|
+
const security = ` security ${definition.security}`;
|
|
86
|
+
const path = definition.searchPath === undefined ? "" : ` set search_path to ${definition.searchPath}`;
|
|
87
|
+
const tail = `returns ${definition.returns} language ${definition.language} ${definition.volatility}${security}${path}`;
|
|
88
|
+
if (definition.atomic)
|
|
89
|
+
return `${head} ${tail} ${definition.body}`;
|
|
90
|
+
return `${head} ${tail} as ${dollarQuote(definition.body)}`;
|
|
91
|
+
}
|
|
92
|
+
function viewSql(object, schema, replace) {
|
|
93
|
+
const verb = replace ? "create or replace view" : "create view";
|
|
94
|
+
return `${verb} ${qualify(schema, object.identity.name)} as ${object.definition.query}`;
|
|
95
|
+
}
|
|
96
|
+
function materializedViewSql(object, schema) {
|
|
97
|
+
return `create materialized view ${qualify(schema, object.identity.name)} as ${object.definition.query} with no data`;
|
|
98
|
+
}
|
|
99
|
+
function refreshMaterializedViewSql(object, schema, populated = false) {
|
|
100
|
+
const concurrently = populated && object.definition.refresh === "concurrently" ? " concurrently" : "";
|
|
101
|
+
return `refresh materialized view${concurrently} ${qualify(schema, object.identity.name)}`;
|
|
102
|
+
}
|
|
103
|
+
function triggerSql(object, schema) {
|
|
104
|
+
const definition = object.definition;
|
|
105
|
+
const timing = definition.timing === "instead" ? "instead of" : definition.timing;
|
|
106
|
+
const events = definition.events.map((event) => {
|
|
107
|
+
if (event !== "update" || definition.updateOf === undefined)
|
|
108
|
+
return event;
|
|
109
|
+
return `update of ${definition.updateOf.map((column) => quoteIdent(column)).join(", ")}`;
|
|
110
|
+
}).join(" or ");
|
|
111
|
+
const when = definition.when === undefined ? "" : ` when (${definition.when})`;
|
|
112
|
+
const args = definition.calls.argTypes.join(", ");
|
|
113
|
+
const fn = `${qualify(schema, definition.calls.name)}(${args})`;
|
|
114
|
+
return `create trigger ${quoteIdent(object.identity.name)} ${timing} ${events} on ${qualify(schema, object.identity.parent.name)} for each ${definition.level}${when} execute function ${fn}`;
|
|
115
|
+
}
|
|
116
|
+
function dollarQuote(body) {
|
|
117
|
+
let tag = "okm";
|
|
118
|
+
while (body.includes(`$${tag}$`))
|
|
119
|
+
tag = `${tag}_`;
|
|
120
|
+
return `$${tag}$${body}$${tag}$`;
|
|
121
|
+
}
|
|
122
|
+
function alterColumnSql(before, after, schema) {
|
|
123
|
+
const statements = [];
|
|
124
|
+
const table = qualify(schema, after.identity.parent.name);
|
|
125
|
+
const name = quoteIdent(after.identity.name);
|
|
126
|
+
const typeChanged = before.definition.dataType !== after.definition.dataType;
|
|
127
|
+
const collationChanged = before.definition.collation !== after.definition.collation;
|
|
128
|
+
if (typeChanged || collationChanged) {
|
|
129
|
+
const using = typeChanged ? ` using ${name}::${after.definition.dataType}` : "";
|
|
130
|
+
statements.push(`alter table ${table} alter column ${name} set data type ${after.definition.dataType}${collateSql(after.definition.collation)}${using}`);
|
|
131
|
+
}
|
|
132
|
+
if (before.definition.nullable !== after.definition.nullable) {
|
|
133
|
+
statements.push(`alter table ${table} alter column ${name} ${after.definition.nullable ? "drop not null" : "set not null"}`);
|
|
134
|
+
}
|
|
135
|
+
if (before.definition.defaultExpression !== after.definition.defaultExpression) {
|
|
136
|
+
statements.push(after.definition.defaultExpression === undefined ? `alter table ${table} alter column ${name} drop default` : `alter table ${table} alter column ${name} set default ${after.definition.defaultExpression}`);
|
|
137
|
+
}
|
|
138
|
+
return statements;
|
|
139
|
+
}
|
|
140
|
+
function identitySequence(sequence, source) {
|
|
141
|
+
const key = identityKey2(sequence.identity);
|
|
142
|
+
return source.objects.some((object) => {
|
|
143
|
+
if (object.kind !== "column" || object.definition.identity === undefined)
|
|
144
|
+
return false;
|
|
145
|
+
return object.dependencies.some((edge) => identityKey2(edge.target) === key);
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
function createTableSql(tableObject, source, schema) {
|
|
149
|
+
const parent = tableObject.identity.name;
|
|
150
|
+
const columns = source.objects.filter((object) => object.kind === "column" && object.identity.parent.name === parent);
|
|
151
|
+
const primary = source.objects.find((object) => object.kind === "constraint" && object.definition.constraintKind === "primaryKey" && object.identity.parent.name === parent);
|
|
152
|
+
const lines = columns.map((column) => columnSql(column));
|
|
153
|
+
if (primary !== undefined) {
|
|
154
|
+
const cols = primary.definition.columns.map((name) => quoteIdent(name)).join(", ");
|
|
155
|
+
lines.push(`constraint ${quoteIdent(primary.identity.name)} primary key (${cols})`);
|
|
156
|
+
}
|
|
157
|
+
const partition = tableObject.definition.partition;
|
|
158
|
+
const tail = partition === undefined ? "" : ` partition by ${partition.method} (${partition.columns.map((name) => quoteIdent(name)).join(", ")})`;
|
|
159
|
+
return `create table ${qualify(schema, parent)} (
|
|
160
|
+
${lines.join(`,
|
|
161
|
+
`)}
|
|
162
|
+
)${tail}`;
|
|
163
|
+
}
|
|
164
|
+
function collateSql(collation) {
|
|
165
|
+
if (collation === undefined)
|
|
166
|
+
return "";
|
|
167
|
+
return ` collate ${quoteIdent(collation)}`;
|
|
168
|
+
}
|
|
169
|
+
function createTypeSql(object, schema) {
|
|
170
|
+
if (isDomain(object.definition)) {
|
|
171
|
+
const name = object.identity.name;
|
|
172
|
+
const constraint = quoteIdent(fitIdentifier2(`${name}_check`));
|
|
173
|
+
return `create domain ${qualify(schema, name)} as ${object.definition.base} constraint ${constraint} check (${object.definition.check})`;
|
|
174
|
+
}
|
|
175
|
+
const labels = object.definition.labels.map((label) => quoteLiteral(label)).join(", ");
|
|
176
|
+
return `create type ${qualify(schema, object.identity.name)} as enum (${labels})`;
|
|
177
|
+
}
|
|
178
|
+
function columnSql(column) {
|
|
179
|
+
const definition = column.definition;
|
|
180
|
+
let sql = `${quoteIdent(column.identity.name)} ${definition.dataType}${collateSql(definition.collation)}`;
|
|
181
|
+
if (definition.identity !== undefined) {
|
|
182
|
+
sql += definition.identity.always ? " generated always as identity" : " generated by default as identity";
|
|
183
|
+
} else if (definition.generated !== undefined) {
|
|
184
|
+
sql += ` generated always as (${definition.generated.expression}) stored`;
|
|
185
|
+
}
|
|
186
|
+
if (!definition.nullable)
|
|
187
|
+
sql += " not null";
|
|
188
|
+
if (definition.defaultExpression !== undefined)
|
|
189
|
+
sql += ` default ${definition.defaultExpression}`;
|
|
190
|
+
return sql;
|
|
191
|
+
}
|
|
192
|
+
function createIndexSql(object, schema) {
|
|
193
|
+
const unique = object.definition.unique ? "unique " : "";
|
|
194
|
+
const where = object.definition.predicate === undefined ? "" : ` where ${object.definition.predicate}`;
|
|
195
|
+
const expression = object.definition.expression;
|
|
196
|
+
const name = quoteIdent(object.identity.name);
|
|
197
|
+
const on = qualify(schema, object.identity.parent.name);
|
|
198
|
+
if (expression !== undefined && expression.startsWith("using ")) {
|
|
199
|
+
return `create ${unique}index ${name} on ${on} ${expression}${where}`;
|
|
200
|
+
}
|
|
201
|
+
const target = expression !== undefined ? `(${expression})` : object.definition.columns.map((column) => quoteIdent(column)).join(", ");
|
|
202
|
+
return `create ${unique}index ${name} on ${on} (${target})${where}`;
|
|
203
|
+
}
|
|
204
|
+
function createExtensionSql(object) {
|
|
205
|
+
const version = object.definition.version;
|
|
206
|
+
const pinned = version !== undefined && /^\d+(?:\.\d+)*$/.test(version) ? ` version ${quoteLiteral(version)}` : "";
|
|
207
|
+
return `create extension ${quoteIdent(object.identity.name)} schema ${quoteIdent(object.definition.schema)}${pinned}`;
|
|
208
|
+
}
|
|
209
|
+
function createSequenceSql(object, schema) {
|
|
210
|
+
const definition = object.definition;
|
|
211
|
+
const cycle = definition.cycle ? " cycle" : "";
|
|
212
|
+
return `create sequence ${qualify(schema, object.identity.name)} as ${definition.dataType} start ${definition.start} increment ${definition.increment}${cycle}`;
|
|
213
|
+
}
|
|
214
|
+
function constraintBody(object, schema) {
|
|
215
|
+
const definition = object.definition;
|
|
216
|
+
const columns = definition.columns.map((name) => quoteIdent(name)).join(", ");
|
|
217
|
+
switch (definition.constraintKind) {
|
|
218
|
+
case "primaryKey":
|
|
219
|
+
return `primary key (${columns})`;
|
|
220
|
+
case "unique": {
|
|
221
|
+
const nulls = definition.nullsNotDistinct ? " nulls not distinct" : "";
|
|
222
|
+
return `unique${nulls} (${columns})${deferClause(definition)}`;
|
|
223
|
+
}
|
|
224
|
+
case "check":
|
|
225
|
+
return `check (${definition.expression ?? "true"})${deferClause(definition)}`;
|
|
226
|
+
case "foreignKey": {
|
|
227
|
+
const references = definition.references;
|
|
228
|
+
const target = references === undefined ? "" : ` references ${qualify(schema, references.parent.name)} (${references.columns.map((name) => quoteIdent(name)).join(", ")})`;
|
|
229
|
+
const onDelete = references?.onDelete === undefined ? "" : ` on delete ${references.onDelete}`;
|
|
230
|
+
const onUpdate = references?.onUpdate === undefined ? "" : ` on update ${references.onUpdate}`;
|
|
231
|
+
return `foreign key (${columns})${target}${onDelete}${onUpdate}${deferClause(definition)}`;
|
|
232
|
+
}
|
|
233
|
+
default:
|
|
234
|
+
return definition.constraintKind;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
function deferClause(definition) {
|
|
238
|
+
if (!definition.deferrable)
|
|
239
|
+
return "";
|
|
240
|
+
return ` deferrable initially ${definition.initially}`;
|
|
241
|
+
}
|
|
242
|
+
function qualify(schema, name) {
|
|
243
|
+
return `${quoteIdent(schema)}.${quoteIdent(name)}`;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
export { quoteIdent, ownedByView, dropObjectSql, createObjectSql, functionSql, viewSql, refreshMaterializedViewSql, alterColumnSql, identitySequence, createTableSql, qualify };
|