okmodel 0.0.0 → 0.1.1
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 +246 -1
- package/dist/adapters/capabilities.d.ts +62 -0
- package/dist/adapters/error.d.ts +91 -0
- package/dist/adapters/failure.d.ts +39 -0
- package/dist/adapters/pg/batch.d.ts +36 -0
- package/dist/adapters/pg/call.d.ts +59 -0
- package/dist/adapters/pg/pglite.d.ts +21 -0
- package/dist/adapters/pg/postgres-extra.d.ts +43 -0
- package/dist/adapters/pg/postgresjs.d.ts +95 -0
- package/dist/adapters/pg/result.d.ts +30 -0
- package/dist/contracts/catalog/build.d.ts +18 -0
- package/dist/contracts/catalog/document.d.ts +79 -0
- package/dist/contracts/catalog/enum.d.ts +35 -0
- package/dist/contracts/catalog/identifier.d.ts +71 -0
- package/dist/contracts/catalog/identity.d.ts +98 -0
- package/dist/contracts/catalog/index.d.ts +11 -0
- package/dist/contracts/catalog/json.d.ts +17 -0
- package/dist/contracts/catalog/object.d.ts +142 -0
- package/dist/contracts/catalog/order.d.ts +43 -0
- package/dist/contracts/catalog/rewrite.d.ts +18 -0
- package/dist/contracts/catalog/types.d.ts +292 -0
- package/dist/contracts/catalog/words.d.ts +13 -0
- package/dist/contracts/connection.d.ts +33 -0
- package/dist/contracts/driver.d.ts +163 -0
- package/dist/contracts/error.d.ts +298 -0
- package/dist/contracts/index.d.ts +11 -0
- package/dist/contracts/index.js +16 -0
- package/dist/contracts/internal.d.ts +15 -0
- package/dist/contracts/internal.js +68 -0
- package/dist/contracts/nearest.d.ts +15 -0
- package/dist/contracts/rows.d.ts +84 -0
- package/dist/contracts/sha256.d.ts +14 -0
- package/dist/contracts/utf8.d.ts +33 -0
- package/dist/dialects/pg/array-literal.d.ts +25 -0
- package/dist/dialects/pg/bool.d.ts +46 -0
- package/dist/dialects/pg/column.d.ts +386 -0
- package/dist/dialects/pg/compile.d.ts +81 -0
- package/dist/dialects/pg/custom.d.ts +21 -0
- package/dist/dialects/pg/ddl.d.ts +70 -0
- package/dist/dialects/pg/decimal.d.ts +62 -0
- package/dist/dialects/pg/declarations.d.ts +34 -0
- package/dist/dialects/pg/emit.d.ts +18 -0
- package/dist/dialects/pg/enum.d.ts +30 -0
- package/dist/dialects/pg/errors.d.ts +30 -0
- package/dist/dialects/pg/finite.d.ts +15 -0
- package/dist/dialects/pg/geometry.d.ts +55 -0
- package/dist/dialects/pg/index.d.ts +45 -0
- package/dist/dialects/pg/index.js +1821 -0
- package/dist/dialects/pg/integer.d.ts +96 -0
- package/dist/dialects/pg/introspect.d.ts +43 -0
- package/dist/dialects/pg/json.d.ts +43 -0
- package/dist/dialects/pg/keys.d.ts +32 -0
- package/dist/dialects/pg/misuse.d.ts +31 -0
- package/dist/dialects/pg/model.d.ts +71 -0
- package/dist/dialects/pg/namespace.d.ts +63 -0
- package/dist/dialects/pg/network.d.ts +28 -0
- package/dist/dialects/pg/operators.d.ts +71 -0
- package/dist/dialects/pg/ops/between.d.ts +10 -0
- package/dist/dialects/pg/ops/contains.d.ts +8 -0
- package/dist/dialects/pg/ops/endsWith.d.ts +8 -0
- package/dist/dialects/pg/ops/eq.d.ts +9 -0
- package/dist/dialects/pg/ops/every.d.ts +9 -0
- package/dist/dialects/pg/ops/gt.d.ts +9 -0
- package/dist/dialects/pg/ops/gte.d.ts +9 -0
- package/dist/dialects/pg/ops/has.d.ts +9 -0
- package/dist/dialects/pg/ops/ilike.d.ts +8 -0
- package/dist/dialects/pg/ops/inList.d.ts +9 -0
- package/dist/dialects/pg/ops/inc.d.ts +13 -0
- package/dist/dialects/pg/ops/like.d.ts +8 -0
- package/dist/dialects/pg/ops/lt.d.ts +9 -0
- package/dist/dialects/pg/ops/lte.d.ts +9 -0
- package/dist/dialects/pg/ops/none.d.ts +9 -0
- package/dist/dialects/pg/ops/not.d.ts +9 -0
- package/dist/dialects/pg/ops/notIn.d.ts +9 -0
- package/dist/dialects/pg/ops/or.d.ts +9 -0
- package/dist/dialects/pg/ops/startsWith.d.ts +8 -0
- package/dist/dialects/pg/quote.d.ts +24 -0
- package/dist/dialects/pg/range.d.ts +50 -0
- package/dist/dialects/pg/relations.d.ts +45 -0
- package/dist/dialects/pg/schema.d.ts +77 -0
- package/dist/dialects/pg/search.d.ts +23 -0
- package/dist/dialects/pg/table.d.ts +201 -0
- package/dist/dialects/pg/temporal.d.ts +211 -0
- package/dist/dialects/pg/text.d.ts +44 -0
- package/dist/dialects/pg/time.d.ts +46 -0
- package/dist/okm.js +2 -0
- package/dist/okmodel.js +2 -0
- package/dist/runtime/client.d.ts +47 -0
- package/dist/runtime/conflict.d.ts +38 -0
- package/dist/runtime/drift-detail.d.ts +32 -0
- package/dist/runtime/drift.d.ts +29 -0
- package/dist/runtime/include.d.ts +15 -0
- package/dist/runtime/pg/pglite.d.ts +25 -0
- package/dist/runtime/pg/pglite.js +48 -0
- package/dist/runtime/pg/postgresjs.d.ts +29 -0
- package/dist/runtime/pg/postgresjs.js +62 -0
- package/dist/runtime/plan.d.ts +167 -0
- package/dist/runtime/tx.d.ts +29 -0
- package/dist/runtime/types.d.ts +385 -0
- package/dist/runtime/write.d.ts +48 -0
- package/dist/shared/05w44d8g.js +1524 -0
- package/dist/shared/523ft3e8.js +171 -0
- package/dist/shared/8b04d28c.js +14 -0
- package/dist/shared/cn0n68ft.js +16 -0
- package/dist/shared/dkn3y60a.js +374 -0
- package/dist/shared/dynq16vb.js +81 -0
- package/dist/shared/fgqgsb5z.js +281 -0
- package/dist/shared/g5fhqmwe.js +188 -0
- package/dist/shared/gk7cs7sp.js +566 -0
- package/dist/shared/hadrfb9h.js +398 -0
- package/dist/shared/hd293j9g.js +98 -0
- package/dist/shared/j5cn2963.js +701 -0
- package/dist/shared/jjb7yht9.js +37 -0
- package/dist/shared/kprcwaha.js +30 -0
- package/dist/shared/m3ypagh5.js +347 -0
- package/dist/shared/mkjmqjv4.js +456 -0
- package/dist/shared/mtkwv8wx.js +18 -0
- package/dist/shared/ngk8gj61.js +40 -0
- package/dist/shared/p0cbr3g2.js +79 -0
- package/dist/shared/qk3hxfja.js +83 -0
- package/dist/shared/rgs0st28.js +1066 -0
- package/dist/shared/w8samvk1.js +18 -0
- package/dist/shared/x5x59srr.js +566 -0
- package/dist/shared/xqbhg6cn.js +119 -0
- package/dist/shared/xy79j5hq.js +177 -0
- package/dist/shared/y9m15dfv.js +888 -0
- package/dist/shared/ycwz42az.js +412 -0
- package/dist/shared/yd1kzwn2.js +148 -0
- package/dist/shared/zhc5vmgf.js +257 -0
- package/dist/tooling/cli.d.ts +1 -0
- package/dist/tooling/cli.js +193 -0
- package/dist/tooling/errors/registry.d.ts +33 -0
- package/dist/tooling/migrate/apply.d.ts +86 -0
- package/dist/tooling/migrate/commands.d.ts +21 -0
- package/dist/tooling/migrate/config.d.ts +48 -0
- package/dist/tooling/migrate/dev.d.ts +16 -0
- package/dist/tooling/migrate/equal.d.ts +15 -0
- package/dist/tooling/migrate/files.d.ts +19 -0
- package/dist/tooling/migrate/index.d.ts +7 -0
- package/dist/tooling/migrate/index.js +7 -0
- package/dist/tooling/migrate/plan.d.ts +76 -0
- package/dist/tooling/migrate/policy.d.ts +78 -0
- package/dist/tooling/migrate/project.d.ts +69 -0
- package/dist/tooling/migrate/status.d.ts +74 -0
- package/dist/tooling/migrate/values.d.ts +29 -0
- package/package.json +109 -7
- package/dist/index.js +0 -0
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* postgres.js adapter (`okmodel/pg/postgresjs`).
|
|
3
|
+
*
|
|
4
|
+
* One pool per `open` call. Plain `execute` uses the driver's pool directly,
|
|
5
|
+
* so it does not take an extra checkout. `reserve` checks out one connection
|
|
6
|
+
* and clears session state on release. `postgres` is a peer of this entry.
|
|
7
|
+
*/
|
|
8
|
+
import { type Sql } from "postgres";
|
|
9
|
+
import type { DriverPool, DriverPoolConfig, ExecuteResult, Notice, WireValue } from "../../contracts/driver.js";
|
|
10
|
+
import type { BatchSession } from "./batch.js";
|
|
11
|
+
import { type Watch } from "./call.js";
|
|
12
|
+
export { POSTGRESJS_CAPABILITIES as capabilities, hasCapability, readCapability, } from "../capabilities.js";
|
|
13
|
+
export { DriverError } from "../error.js";
|
|
14
|
+
/** Options for {@link open}. */
|
|
15
|
+
export type PostgresJsConfig = DriverPoolConfig & {
|
|
16
|
+
/** Connection URL for this endpoint. */
|
|
17
|
+
readonly url: string;
|
|
18
|
+
/**
|
|
19
|
+
* Named prepared statements, or the unnamed protocol.
|
|
20
|
+
*
|
|
21
|
+
* Unnamed is the default. It stays valid behind a pooler in transaction mode.
|
|
22
|
+
* `prepared: "named"` is not for transaction-mode poolers.
|
|
23
|
+
*/
|
|
24
|
+
readonly prepared?: "named" | "unnamed";
|
|
25
|
+
/** Session search path for every connection in the pool. */
|
|
26
|
+
readonly searchPath?: string;
|
|
27
|
+
/** TLS mode passed to postgres.js. */
|
|
28
|
+
readonly ssl?: boolean | "require" | "allow" | "prefer" | "verify-full" | object;
|
|
29
|
+
};
|
|
30
|
+
/** Something an in-flight query can abort. */
|
|
31
|
+
export type Canceller = {
|
|
32
|
+
cancel(): void;
|
|
33
|
+
};
|
|
34
|
+
/** Pool occupancy. Shared by execute and by a reserved connection. */
|
|
35
|
+
export type Counters = {
|
|
36
|
+
reserved: number;
|
|
37
|
+
busy: number;
|
|
38
|
+
reservedBusy: number;
|
|
39
|
+
waiting: number;
|
|
40
|
+
};
|
|
41
|
+
/** Notice slice shared by the pool. */
|
|
42
|
+
export type NoticeBuffer = {
|
|
43
|
+
start(): number;
|
|
44
|
+
since(start: number): readonly Notice[];
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* Opens a postgres.js pool for one endpoint.
|
|
48
|
+
*
|
|
49
|
+
* @param config - URL, pool size, and acquire timeout
|
|
50
|
+
* @returns The pool
|
|
51
|
+
*/
|
|
52
|
+
export declare function open(config: PostgresJsConfig): DriverPool;
|
|
53
|
+
export declare class PgSession implements BatchSession {
|
|
54
|
+
readonly canCancel = true;
|
|
55
|
+
private transactionDepth;
|
|
56
|
+
private readonly sql;
|
|
57
|
+
private readonly counters;
|
|
58
|
+
private readonly scope;
|
|
59
|
+
private readonly local;
|
|
60
|
+
private readonly isClosed;
|
|
61
|
+
private readonly shared;
|
|
62
|
+
private readonly notices;
|
|
63
|
+
private readonly named;
|
|
64
|
+
/**
|
|
65
|
+
* @param sql - Root client or a reserved connection
|
|
66
|
+
* @param counters - Pool counters
|
|
67
|
+
* @param scope - Pool executes are not checkouts
|
|
68
|
+
* @param local - In-flight queries this connection cancels
|
|
69
|
+
* @param isClosed - Pool closed flag
|
|
70
|
+
* @param shared - Pool-wide in-flight set for a reserved connection
|
|
71
|
+
* @param notices - Notice buffer shared by the pool
|
|
72
|
+
*/
|
|
73
|
+
constructor(sql: Sql, counters: Counters, scope: "pool" | "reserved", local: Set<Canceller>, isClosed: () => boolean, shared: Set<Canceller> | undefined, notices: NoticeBuffer, named: boolean);
|
|
74
|
+
/** @inheritdoc */
|
|
75
|
+
query(text: string, params: readonly WireValue[] | undefined, watch: Watch | undefined): Promise<ExecuteResult>;
|
|
76
|
+
/** @inheritdoc */
|
|
77
|
+
inTransaction(): boolean;
|
|
78
|
+
/** @inheritdoc */
|
|
79
|
+
abandon(): void;
|
|
80
|
+
/**
|
|
81
|
+
* Open transaction depth.
|
|
82
|
+
*
|
|
83
|
+
* @returns The depth. Zero means no transaction
|
|
84
|
+
*/
|
|
85
|
+
depth(): number;
|
|
86
|
+
/** Aborts in-flight statements on this connection. */
|
|
87
|
+
cancel(): void;
|
|
88
|
+
private note;
|
|
89
|
+
private enter;
|
|
90
|
+
private leave;
|
|
91
|
+
private track;
|
|
92
|
+
private untrack;
|
|
93
|
+
private watched;
|
|
94
|
+
private finishWatched;
|
|
95
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Turns a driver result into wire rows.
|
|
3
|
+
*
|
|
4
|
+
* The empty notice list is shared. A call that received none does not allocate one.
|
|
5
|
+
*/
|
|
6
|
+
import type { ExecuteResult, Notice, WireValue } from "../../contracts/driver.js";
|
|
7
|
+
/** Shared notice list for a statement that received none. */
|
|
8
|
+
export declare const EMPTY_NOTICES: readonly Notice[];
|
|
9
|
+
/**
|
|
10
|
+
* Reads one cell as wire text.
|
|
11
|
+
*
|
|
12
|
+
* @param value - A driver cell
|
|
13
|
+
* @returns Wire text, or `null` for SQL NULL
|
|
14
|
+
*/
|
|
15
|
+
export declare function cell(value: unknown): WireValue;
|
|
16
|
+
/**
|
|
17
|
+
* Reads rows as arrays of wire text.
|
|
18
|
+
*
|
|
19
|
+
* @param value - A driver result, or its row list
|
|
20
|
+
* @returns Wire rows
|
|
21
|
+
*/
|
|
22
|
+
export declare function rowsFrom(value: unknown): WireValue[][];
|
|
23
|
+
/**
|
|
24
|
+
* Builds an {@link ExecuteResult}.
|
|
25
|
+
*
|
|
26
|
+
* @param value - A driver result
|
|
27
|
+
* @param notices - Notices observed during the call
|
|
28
|
+
* @returns Rows, count, and notices
|
|
29
|
+
*/
|
|
30
|
+
export declare function resultFrom(value: unknown, notices: readonly Notice[]): ExecuteResult;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Assembles a catalog document.
|
|
3
|
+
*
|
|
4
|
+
* Parsing and serialising stay in the document module. `schema()` needs this
|
|
5
|
+
* check and not the parser.
|
|
6
|
+
*/
|
|
7
|
+
import { type Catalog, type CatalogObject } from "./types.js";
|
|
8
|
+
/**
|
|
9
|
+
* Checks a set of objects and returns them in identity-key order.
|
|
10
|
+
*
|
|
11
|
+
* Duplicate identities are OKM1023. A foreign key whose target is missing is
|
|
12
|
+
* OKM1021. A dependency cycle is OKM1026. A self-edge or a missing target is
|
|
13
|
+
* OKM1020.
|
|
14
|
+
*
|
|
15
|
+
* @param objects - Built objects, in any order
|
|
16
|
+
* @returns A catalog document
|
|
17
|
+
*/
|
|
18
|
+
export declare function catalog(objects: readonly CatalogObject[]): Catalog;
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Catalog documents.
|
|
3
|
+
*
|
|
4
|
+
* `catalog` checks identity, foreign keys, and cycles, then stores objects in
|
|
5
|
+
* identity-key order. The serialized form is the canonical JSON of that
|
|
6
|
+
* document, templates included. The hash is SHA-256 of those bytes.
|
|
7
|
+
*/
|
|
8
|
+
import { catalog } from "./build.js";
|
|
9
|
+
import type { Catalog, CatalogObject, Namespace, ObjectRef } from "./types.js";
|
|
10
|
+
export { catalog };
|
|
11
|
+
/**
|
|
12
|
+
* Create order for a catalog.
|
|
13
|
+
*
|
|
14
|
+
* Ties break by identity key.
|
|
15
|
+
*
|
|
16
|
+
* @param source - Catalog document
|
|
17
|
+
* @returns Dependencies first
|
|
18
|
+
*/
|
|
19
|
+
export declare function creationOrder(source: Catalog): readonly CatalogObject[];
|
|
20
|
+
/**
|
|
21
|
+
* Renames a column without renaming constraints or indexes.
|
|
22
|
+
*
|
|
23
|
+
* Column lists and edges that pointed at the old column move to the new name.
|
|
24
|
+
* `nameKey` and the stored constraint or index name stay as they were.
|
|
25
|
+
*
|
|
26
|
+
* @param source - Catalog document
|
|
27
|
+
* @param change - Parent table, current column name, and the new name
|
|
28
|
+
* @returns A new catalog
|
|
29
|
+
*/
|
|
30
|
+
export declare function renameColumn(source: Catalog, change: {
|
|
31
|
+
readonly parent: ObjectRef;
|
|
32
|
+
readonly from: string;
|
|
33
|
+
readonly to: string;
|
|
34
|
+
}): Catalog;
|
|
35
|
+
/**
|
|
36
|
+
* Renames a table without renaming its constraints or indexes.
|
|
37
|
+
*
|
|
38
|
+
* Column, index, and constraint parents move to the new name. Expression text
|
|
39
|
+
* that names the table is rewritten. Constraint and index names stay.
|
|
40
|
+
*
|
|
41
|
+
* @param source - Catalog document
|
|
42
|
+
* @param change - Namespace, current table name, and the new name
|
|
43
|
+
* @returns A new catalog
|
|
44
|
+
*/
|
|
45
|
+
export declare function renameTable(source: Catalog, change: {
|
|
46
|
+
readonly namespace: Namespace;
|
|
47
|
+
readonly from: string;
|
|
48
|
+
readonly to: string;
|
|
49
|
+
}): Catalog;
|
|
50
|
+
export declare function serializeCatalog(source: Catalog): string;
|
|
51
|
+
/**
|
|
52
|
+
* SHA-256 of {@link serializeCatalog}.
|
|
53
|
+
*
|
|
54
|
+
* The hash covers the normalised structure. It does not cover a separate
|
|
55
|
+
* authoring document.
|
|
56
|
+
*
|
|
57
|
+
* @param source - Catalog document
|
|
58
|
+
* @returns Lowercase hex digest
|
|
59
|
+
*/
|
|
60
|
+
export declare function catalogHash(source: Catalog): string;
|
|
61
|
+
/**
|
|
62
|
+
* Loads a build artifact without validating objects.
|
|
63
|
+
*
|
|
64
|
+
* `okm build` already validated the catalog. Production startup checks the
|
|
65
|
+
* stored hash and the version, then trusts the objects (D133). `okm check`
|
|
66
|
+
* and dev keep {@link parseCatalog}.
|
|
67
|
+
*
|
|
68
|
+
* @param text - Canonical catalog JSON
|
|
69
|
+
* @param hash - SHA-256 of `text`, stored beside the artifact
|
|
70
|
+
* @returns The catalog document
|
|
71
|
+
*/
|
|
72
|
+
export declare function loadTrustedCatalog(text: string, hash: string): Catalog;
|
|
73
|
+
/**
|
|
74
|
+
* Reads canonical catalog JSON.
|
|
75
|
+
*
|
|
76
|
+
* @param text - Serialized catalog
|
|
77
|
+
* @returns A checked catalog
|
|
78
|
+
*/
|
|
79
|
+
export declare function parseCatalog(text: string): Catalog;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Enum catalog objects.
|
|
3
|
+
*
|
|
4
|
+
* Kind `type`, identity `(namespace, name)`. The definition is the ordered
|
|
5
|
+
* label list and nothing else: domains are not catalog objects.
|
|
6
|
+
*/
|
|
7
|
+
import type { Namespace, ObjectIdentity, Owner, Provenance, TypeObject } from "./types.js";
|
|
8
|
+
/** Input for {@link enumType}. */
|
|
9
|
+
export type EnumTypeInput = {
|
|
10
|
+
readonly namespace: Namespace;
|
|
11
|
+
readonly name: string;
|
|
12
|
+
/** Labels in stored order. The order is part of the catalog hash. */
|
|
13
|
+
readonly labels: readonly string[];
|
|
14
|
+
readonly owner?: Owner;
|
|
15
|
+
readonly provenance: Provenance;
|
|
16
|
+
readonly dependencies?: readonly ObjectIdentity[];
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Builds an enum type object.
|
|
20
|
+
*
|
|
21
|
+
* Labels are copied in the order given. An empty list, an empty label, or a
|
|
22
|
+
* repeated label is OKM1020.
|
|
23
|
+
*
|
|
24
|
+
* @param input - Namespace, name, and ordered labels
|
|
25
|
+
* @returns A type envelope
|
|
26
|
+
*/
|
|
27
|
+
export declare function enumType(input: EnumTypeInput): TypeObject;
|
|
28
|
+
/**
|
|
29
|
+
* Reports whether two label lists are the same text in the same order.
|
|
30
|
+
*
|
|
31
|
+
* @param left - First list
|
|
32
|
+
* @param right - Second list
|
|
33
|
+
* @returns `true` when every position matches
|
|
34
|
+
*/
|
|
35
|
+
export declare function sameEnumLabels(left: readonly string[], right: readonly string[]): boolean;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Identifier rules and deterministic constraint and index names.
|
|
3
|
+
*
|
|
4
|
+
* Postgres stores 63 bytes (`NAMEDATALEN` minus the trailing NUL). A generated
|
|
5
|
+
* name that would be longer keeps a hash suffix so two long names stay distinct.
|
|
6
|
+
* The name is a function of a stable key, not of the field's current name, so
|
|
7
|
+
* renaming a field does not rename the constraint.
|
|
8
|
+
*/
|
|
9
|
+
import { utf8ByteLength } from "../utf8.js";
|
|
10
|
+
/** Maximum Postgres identifier size in bytes. `NAMEDATALEN - 1`. */
|
|
11
|
+
export declare const POSTGRES_IDENTIFIER_MAX_BYTES = 63;
|
|
12
|
+
/** Why a constraint or index name is being generated. */
|
|
13
|
+
export type NamePurpose = "primaryKey" | "unique" | "foreignKey" | "check" | "index";
|
|
14
|
+
export { utf8ByteLength };
|
|
15
|
+
/**
|
|
16
|
+
* Reports whether a name is an unquoted reserved word.
|
|
17
|
+
*
|
|
18
|
+
* Comparison is case-insensitive.
|
|
19
|
+
*
|
|
20
|
+
* @param name - Candidate identifier
|
|
21
|
+
* @returns `true` when Postgres would reject the unquoted word
|
|
22
|
+
*/
|
|
23
|
+
export declare function isReservedIdentifier(name: string): boolean;
|
|
24
|
+
/**
|
|
25
|
+
* Rejects a name that fails the identifier rules.
|
|
26
|
+
*
|
|
27
|
+
* The rules are empty, NUL, other control characters, byte length, and an
|
|
28
|
+
* unquoted reserved word (OKM1122).
|
|
29
|
+
*
|
|
30
|
+
* @param name - Candidate identifier
|
|
31
|
+
* @param role - What the name refers to, used in the error
|
|
32
|
+
* @param limit - Dialect byte limit. Defaults to the Postgres limit
|
|
33
|
+
*/
|
|
34
|
+
export declare function assertIdentifier(name: string, role: string, limit?: number): void;
|
|
35
|
+
/**
|
|
36
|
+
* Returns a name the dialect can store without truncating it.
|
|
37
|
+
*
|
|
38
|
+
* Names that already fit are unchanged, including reserved words (those are
|
|
39
|
+
* rejected by {@link assertIdentifier} before a name is stored). Longer names
|
|
40
|
+
* become a UTF-8 prefix, an underscore, and the first 8 hex characters of the
|
|
41
|
+
* SHA-256 of the original name.
|
|
42
|
+
*
|
|
43
|
+
* @param name - Requested identifier
|
|
44
|
+
* @param limit - Dialect byte limit. Defaults to the Postgres limit
|
|
45
|
+
* @returns The name to store
|
|
46
|
+
*/
|
|
47
|
+
export declare function fitIdentifier(name: string, limit?: number): string;
|
|
48
|
+
/**
|
|
49
|
+
* Generates a constraint or index name.
|
|
50
|
+
*
|
|
51
|
+
* Primary keys are `{parent}_pkey`, so the column name is not part of the
|
|
52
|
+
* name. Other purposes are `{parent}_{nameKey}_{tag}`. `nameKey` is chosen
|
|
53
|
+
* once and kept; a later field rename must pass the same key. A result that
|
|
54
|
+
* exceeds `limit` is fitted with a hash suffix.
|
|
55
|
+
*
|
|
56
|
+
* @param input - Parent, purpose, stable key, and dialect limit
|
|
57
|
+
* @returns The name to store on the object
|
|
58
|
+
*/
|
|
59
|
+
export declare function deterministicName(input: {
|
|
60
|
+
readonly parent: string;
|
|
61
|
+
readonly purpose: NamePurpose;
|
|
62
|
+
readonly nameKey?: string;
|
|
63
|
+
readonly limit?: number;
|
|
64
|
+
}): string;
|
|
65
|
+
/**
|
|
66
|
+
* Rejects NUL and other control characters in text that is not an identifier.
|
|
67
|
+
*
|
|
68
|
+
* @param value - Text stored in a definition
|
|
69
|
+
* @param role - What the text refers to
|
|
70
|
+
*/
|
|
71
|
+
export declare function assertStoredText(value: string, role: string): void;
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Namespaces, identity keys, and tenant resolution.
|
|
3
|
+
*
|
|
4
|
+
* The identity key is canonical JSON, so key order and runtime do not change it.
|
|
5
|
+
* Resolving a template never rewrites the identity.
|
|
6
|
+
*/
|
|
7
|
+
import { type Json } from "./json.js";
|
|
8
|
+
import type { Namespace, ObjectIdentity, ObjectKind, ObjectRef } from "./types.js";
|
|
9
|
+
/**
|
|
10
|
+
* A concrete schema name.
|
|
11
|
+
*
|
|
12
|
+
* @param name - Schema name stored in the catalog
|
|
13
|
+
* @returns A namespace that is not a template
|
|
14
|
+
*/
|
|
15
|
+
export declare function staticNamespace(name: string): Namespace;
|
|
16
|
+
/**
|
|
17
|
+
* A namespace template such as `tenant_{id}`.
|
|
18
|
+
*
|
|
19
|
+
* The pattern must contain `{id}`. Every other character is an identifier
|
|
20
|
+
* character. The pattern is stored as written.
|
|
21
|
+
*
|
|
22
|
+
* @param pattern - Pattern containing the `{id}` placeholder
|
|
23
|
+
* @returns A template namespace
|
|
24
|
+
*/
|
|
25
|
+
export declare function templateNamespace(pattern: string): Namespace;
|
|
26
|
+
/**
|
|
27
|
+
* Rejects a namespace that cannot be stored.
|
|
28
|
+
*
|
|
29
|
+
* Static names follow the identifier rules. Templates must contain `{id}`.
|
|
30
|
+
*
|
|
31
|
+
* @param namespace - Namespace to check
|
|
32
|
+
*/
|
|
33
|
+
export declare function assertNamespace(namespace: Namespace): void;
|
|
34
|
+
/**
|
|
35
|
+
* Resolves a namespace for one tenant id.
|
|
36
|
+
*
|
|
37
|
+
* Static namespaces return their name. Templates substitute `{id}`. A
|
|
38
|
+
* hyphenated UUID is written as 32 lowercase hex digits so the result can
|
|
39
|
+
* enter an identifier. The id must be that UUID or `[a-z0-9_]`.
|
|
40
|
+
*
|
|
41
|
+
* @param namespace - Logical namespace
|
|
42
|
+
* @param tenantId - Tenant id. Ignored for a static namespace
|
|
43
|
+
* @returns The schema name to use at execution time
|
|
44
|
+
*/
|
|
45
|
+
export declare function resolveNamespace(namespace: Namespace, tenantId?: string): string;
|
|
46
|
+
/**
|
|
47
|
+
* Stable identity key.
|
|
48
|
+
*
|
|
49
|
+
* @param identity - Object identity
|
|
50
|
+
* @returns Canonical JSON of the identity
|
|
51
|
+
*/
|
|
52
|
+
export declare function identityKey(identity: ObjectIdentity): string;
|
|
53
|
+
/**
|
|
54
|
+
* Reports whether the dialect length limit applies to a kind.
|
|
55
|
+
*
|
|
56
|
+
* Grants and default privileges use a structured key and are exempt (D119).
|
|
57
|
+
*
|
|
58
|
+
* @param kind - Object kind
|
|
59
|
+
* @returns `false` for grants and default privileges
|
|
60
|
+
*/
|
|
61
|
+
export declare function identifierLimitApplies(kind: ObjectKind): boolean;
|
|
62
|
+
/**
|
|
63
|
+
* Short label for an identity, used in error messages.
|
|
64
|
+
*
|
|
65
|
+
* @param identity - Object identity
|
|
66
|
+
* @returns Kind and name
|
|
67
|
+
*/
|
|
68
|
+
export declare function identityLabel(identity: ObjectIdentity): string;
|
|
69
|
+
/**
|
|
70
|
+
* Reports whether two parent refs are the same logical object.
|
|
71
|
+
*
|
|
72
|
+
* @param left - First ref
|
|
73
|
+
* @param right - Second ref
|
|
74
|
+
* @returns `true` when namespace and name match
|
|
75
|
+
*/
|
|
76
|
+
export declare function sameRef(left: ObjectRef, right: ObjectRef): boolean;
|
|
77
|
+
/**
|
|
78
|
+
* Reports whether two namespaces are the same logical namespace.
|
|
79
|
+
*
|
|
80
|
+
* @param left - First namespace
|
|
81
|
+
* @param right - Second namespace
|
|
82
|
+
* @returns `true` when form and text match
|
|
83
|
+
*/
|
|
84
|
+
export declare function sameNamespace(left: Namespace, right: Namespace): boolean;
|
|
85
|
+
/**
|
|
86
|
+
* Canonical JSON for an identity.
|
|
87
|
+
*
|
|
88
|
+
* @param identity - Object identity
|
|
89
|
+
* @returns JSON object
|
|
90
|
+
*/
|
|
91
|
+
export declare function identityToJson(identity: ObjectIdentity): Json;
|
|
92
|
+
/**
|
|
93
|
+
* Canonical JSON for a namespace.
|
|
94
|
+
*
|
|
95
|
+
* @param namespace - Static or template namespace
|
|
96
|
+
* @returns JSON object
|
|
97
|
+
*/
|
|
98
|
+
export declare function namespaceToJson(namespace: Namespace): Json;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Catalog contract for the runtime entry: envelope, identity, and names.
|
|
3
|
+
*
|
|
4
|
+
* Document checks, serialisation, parsing, hashing, and dependency order live
|
|
5
|
+
* in `document.ts` and `order.ts`. They are tooling and are not re-exported
|
|
6
|
+
* from the package entry.
|
|
7
|
+
*/
|
|
8
|
+
export { BUILT_KINDS, CATALOG_VERSION, KIND_OPERATIONS, OBJECT_KINDS, OBJECT_OPERATIONS, OWNERS, type AnchoredIdentity, type BuiltKind, type Catalog, type CatalogEnvelope, type CatalogObject, type ColumnDefinition, type ColumnObject, type ConstraintDefinition, type ConstraintKind, type ConstraintObject, type DefaultPrivilegeIdentity, type DependencyEdge, type ExtensionIdentity, type FunctionIdentity, type GrantIdentity, type GrantObjectRef, type IndexDefinition, type IndexObject, type Namespace, type NamespaceIdentity, type ObjectIdentity, type ObjectKind, type ObjectOperation, type ObjectRef, type Owner, type PartitionMethod, type Provenance, type ReferentialAction, type RoleIdentity, type SequenceDefinition, type SequenceObject, type TableDefinition, type TableObject, type TypeDefinition, type TypeObject, } from "./types.js";
|
|
9
|
+
export { assertNamespace, identityKey, identityLabel, identifierLimitApplies, resolveNamespace, sameNamespace, sameRef, staticNamespace, templateNamespace, } from "./identity.js";
|
|
10
|
+
export { POSTGRES_IDENTIFIER_MAX_BYTES, assertIdentifier, deterministicName, fitIdentifier, isReservedIdentifier, utf8ByteLength, type NamePurpose, } from "./identifier.js";
|
|
11
|
+
export { column, constraint, index, sequence, table, type ColumnInput, type ConstraintInput, type IndexInput, type SequenceInput, type TableInput, } from "./object.js";
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Canonical JSON.
|
|
3
|
+
*
|
|
4
|
+
* Object keys are sorted. Strings are ASCII-only escapes, so the same value
|
|
5
|
+
* produces the same bytes on every runtime. Arrays keep their order.
|
|
6
|
+
*/
|
|
7
|
+
/** JSON values the canonical encoder accepts. Numbers are safe integers. */
|
|
8
|
+
export type Json = null | boolean | number | string | readonly Json[] | {
|
|
9
|
+
readonly [key: string]: Json | undefined;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Encodes a value with object keys sorted at every level.
|
|
13
|
+
*
|
|
14
|
+
* @param value - JSON value
|
|
15
|
+
* @returns Canonical text
|
|
16
|
+
*/
|
|
17
|
+
export declare function canonicalJson(value: Json): string;
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Factories for the built catalog kinds.
|
|
3
|
+
*
|
|
4
|
+
* Each factory stores ownership, a normalised definition, and dependency
|
|
5
|
+
* edges. Constraint and index names are generated from a stable key and then
|
|
6
|
+
* left alone.
|
|
7
|
+
*/
|
|
8
|
+
import { type ColumnObject, type ConstraintKind, type ConstraintObject, type DependencyEdge, type IndexObject, type Namespace, type ObjectIdentity, type ObjectRef, type Owner, type Provenance, type ReferentialAction, type SequenceDefinition, type SequenceObject, type TableDefinition, type TableObject } from "./types.js";
|
|
9
|
+
/** Fields every factory accepts. */
|
|
10
|
+
type CommonInput = {
|
|
11
|
+
readonly owner?: Owner;
|
|
12
|
+
readonly provenance: Provenance;
|
|
13
|
+
readonly dependencies?: readonly ObjectIdentity[];
|
|
14
|
+
};
|
|
15
|
+
/** Input for {@link table}. */
|
|
16
|
+
export type TableInput = CommonInput & {
|
|
17
|
+
readonly namespace: Namespace;
|
|
18
|
+
readonly name: string;
|
|
19
|
+
readonly partition?: TableDefinition["partition"];
|
|
20
|
+
};
|
|
21
|
+
/** Input for {@link column}. */
|
|
22
|
+
export type ColumnInput = CommonInput & {
|
|
23
|
+
readonly parent: ObjectRef;
|
|
24
|
+
readonly name: string;
|
|
25
|
+
readonly dataType: string;
|
|
26
|
+
readonly nullable: boolean;
|
|
27
|
+
readonly defaultExpression?: string;
|
|
28
|
+
readonly identity?: {
|
|
29
|
+
readonly always: boolean;
|
|
30
|
+
};
|
|
31
|
+
readonly generated?: {
|
|
32
|
+
readonly stored: boolean;
|
|
33
|
+
readonly expression: string;
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
/** Input for {@link index}. */
|
|
37
|
+
export type IndexInput = CommonInput & {
|
|
38
|
+
readonly parent: ObjectRef;
|
|
39
|
+
readonly name?: string;
|
|
40
|
+
readonly nameKey?: string;
|
|
41
|
+
readonly columns?: readonly string[];
|
|
42
|
+
readonly unique?: boolean;
|
|
43
|
+
readonly predicate?: string;
|
|
44
|
+
readonly expression?: string;
|
|
45
|
+
};
|
|
46
|
+
/** Input for {@link constraint}. */
|
|
47
|
+
export type ConstraintInput = CommonInput & {
|
|
48
|
+
readonly parent: ObjectRef;
|
|
49
|
+
readonly constraintKind: ConstraintKind;
|
|
50
|
+
readonly name?: string;
|
|
51
|
+
readonly nameKey?: string;
|
|
52
|
+
readonly columns?: readonly string[];
|
|
53
|
+
readonly expression?: string;
|
|
54
|
+
readonly references?: {
|
|
55
|
+
readonly parent: ObjectRef;
|
|
56
|
+
readonly columns: readonly string[];
|
|
57
|
+
readonly onDelete?: ReferentialAction;
|
|
58
|
+
readonly onUpdate?: ReferentialAction;
|
|
59
|
+
};
|
|
60
|
+
readonly deferrable?: boolean;
|
|
61
|
+
readonly initially?: "immediate" | "deferred";
|
|
62
|
+
readonly nullsNotDistinct?: boolean;
|
|
63
|
+
};
|
|
64
|
+
/** Input for {@link sequence}. */
|
|
65
|
+
export type SequenceInput = CommonInput & {
|
|
66
|
+
readonly namespace: Namespace;
|
|
67
|
+
readonly name: string;
|
|
68
|
+
readonly dataType?: SequenceDefinition["dataType"];
|
|
69
|
+
readonly start?: string;
|
|
70
|
+
readonly increment?: string;
|
|
71
|
+
readonly cycle?: boolean;
|
|
72
|
+
};
|
|
73
|
+
/**
|
|
74
|
+
* Builds a table object.
|
|
75
|
+
*
|
|
76
|
+
* A partitioned table sets `partition`. Copied partition primary keys and
|
|
77
|
+
* inherited indexes are not created; they are not catalog objects.
|
|
78
|
+
*
|
|
79
|
+
* @param input - Name, namespace, ownership, and optional partition
|
|
80
|
+
* @returns A table envelope
|
|
81
|
+
*/
|
|
82
|
+
export declare function table(input: TableInput): TableObject;
|
|
83
|
+
/**
|
|
84
|
+
* Builds a column object.
|
|
85
|
+
*
|
|
86
|
+
* The column depends on its parent table. Extra dependencies (a sequence, for
|
|
87
|
+
* example) are kept.
|
|
88
|
+
*
|
|
89
|
+
* @param input - Parent, name, type, and nullability
|
|
90
|
+
* @returns A column envelope
|
|
91
|
+
*/
|
|
92
|
+
export declare function column(input: ColumnInput): ColumnObject;
|
|
93
|
+
/**
|
|
94
|
+
* Builds an index object.
|
|
95
|
+
*
|
|
96
|
+
* The stored name comes from {@link deterministicName} unless `name` is set.
|
|
97
|
+
* Either way a name past the dialect limit is fitted. `nameKey` is what a
|
|
98
|
+
* later rename must keep passing.
|
|
99
|
+
*
|
|
100
|
+
* @param input - Parent, columns, and the stable name key
|
|
101
|
+
* @returns An index envelope
|
|
102
|
+
*/
|
|
103
|
+
export declare function index(input: IndexInput): IndexObject;
|
|
104
|
+
/**
|
|
105
|
+
* Builds a constraint object.
|
|
106
|
+
*
|
|
107
|
+
* Primary key names are `{parent}_pkey` and do not include column names.
|
|
108
|
+
* Other names use `nameKey`, which stays fixed when a field is renamed.
|
|
109
|
+
*
|
|
110
|
+
* @param input - Parent, kind, columns, and the stable name key
|
|
111
|
+
* @returns A constraint envelope
|
|
112
|
+
*/
|
|
113
|
+
export declare function constraint(input: ConstraintInput): ConstraintObject;
|
|
114
|
+
/**
|
|
115
|
+
* Builds a sequence object.
|
|
116
|
+
*
|
|
117
|
+
* @param input - Namespace, name, and sequence counters
|
|
118
|
+
* @returns A sequence envelope
|
|
119
|
+
*/
|
|
120
|
+
export declare function sequence(input: SequenceInput): SequenceObject;
|
|
121
|
+
/**
|
|
122
|
+
* Sorts and dedupes dependency targets.
|
|
123
|
+
*
|
|
124
|
+
* @param targets - Identity targets, structural ones first
|
|
125
|
+
* @returns Edges in identity-key order
|
|
126
|
+
*/
|
|
127
|
+
export declare function normaliseEdges(targets: readonly ObjectIdentity[]): readonly DependencyEdge[];
|
|
128
|
+
/**
|
|
129
|
+
* Compares two strings by UTF-16 code unit, which is stable across runtimes.
|
|
130
|
+
*
|
|
131
|
+
* @param left - First string
|
|
132
|
+
* @param right - Second string
|
|
133
|
+
* @returns A negative, zero, or positive number
|
|
134
|
+
*/
|
|
135
|
+
export declare function compareText(left: string, right: string): number;
|
|
136
|
+
/**
|
|
137
|
+
* Rejects a provenance that cannot be stored.
|
|
138
|
+
*
|
|
139
|
+
* @param provenance - Origin and name
|
|
140
|
+
*/
|
|
141
|
+
export declare function assertProvenance(provenance: Provenance): void;
|
|
142
|
+
export {};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Dependency order.
|
|
3
|
+
*
|
|
4
|
+
* Create order is a topological order: dependencies first, ties broken by
|
|
5
|
+
* identity key. The comparison is UTF-16 code unit order, not a locale.
|
|
6
|
+
* Each object's identity key is computed once per pass.
|
|
7
|
+
*/
|
|
8
|
+
import type { CatalogObject } from "./types.js";
|
|
9
|
+
/** One catalog object and the identity key computed for it. */
|
|
10
|
+
export type IndexedObject = {
|
|
11
|
+
readonly key: string;
|
|
12
|
+
readonly object: CatalogObject;
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* Computes each object's identity key once.
|
|
16
|
+
*
|
|
17
|
+
* A duplicate identity is OKM1023.
|
|
18
|
+
*
|
|
19
|
+
* @param objects - Catalog objects
|
|
20
|
+
* @returns Objects paired with their keys, in input order
|
|
21
|
+
*/
|
|
22
|
+
export declare function indexObjects(objects: readonly CatalogObject[]): readonly IndexedObject[];
|
|
23
|
+
/**
|
|
24
|
+
* Orders objects so each dependency appears before the object that needs it.
|
|
25
|
+
*
|
|
26
|
+
* A cycle is OKM1026. A self-edge, or a target that is not in `objects`, is
|
|
27
|
+
* OKM1020. A duplicate identity is OKM1023.
|
|
28
|
+
*
|
|
29
|
+
* @param objects - Catalog objects to order
|
|
30
|
+
* @returns Create order
|
|
31
|
+
*/
|
|
32
|
+
export declare function dependencyOrder(objects: readonly CatalogObject[]): readonly CatalogObject[];
|
|
33
|
+
/**
|
|
34
|
+
* Topological order of objects whose identity keys are already known.
|
|
35
|
+
*
|
|
36
|
+
* A cycle is OKM1026. A self-edge, or a target that is not in `indexed`, is
|
|
37
|
+
* OKM1020.
|
|
38
|
+
* `indexed` must not contain duplicate keys.
|
|
39
|
+
*
|
|
40
|
+
* @param indexed - Objects paired with identity keys
|
|
41
|
+
* @returns Create order
|
|
42
|
+
*/
|
|
43
|
+
export declare function orderIndexed(indexed: readonly IndexedObject[]): readonly CatalogObject[];
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Rewrites an identifier inside stored expression text.
|
|
3
|
+
*
|
|
4
|
+
* Check expressions and index predicates are text (D128). A declared rename
|
|
5
|
+
* updates references in that text and leaves string literals alone.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Replaces `from` with `to` outside single-quoted strings.
|
|
9
|
+
*
|
|
10
|
+
* Quoted identifiers (`"from"`) and bare words both change. A word inside a
|
|
11
|
+
* SQL string does not.
|
|
12
|
+
*
|
|
13
|
+
* @param text - Expression text
|
|
14
|
+
* @param from - Identifier to replace
|
|
15
|
+
* @param to - Replacement identifier
|
|
16
|
+
* @returns The expression, or the same string when nothing changed
|
|
17
|
+
*/
|
|
18
|
+
export declare function replaceIdentifier(text: string, from: string, to: string): string;
|