okmodel 0.0.0 → 0.1.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.
Files changed (147) hide show
  1. package/README.md +44 -1
  2. package/dist/adapters/capabilities.d.ts +62 -0
  3. package/dist/adapters/error.d.ts +91 -0
  4. package/dist/adapters/failure.d.ts +39 -0
  5. package/dist/adapters/pg/batch.d.ts +36 -0
  6. package/dist/adapters/pg/call.d.ts +59 -0
  7. package/dist/adapters/pg/pglite.d.ts +21 -0
  8. package/dist/adapters/pg/postgres-extra.d.ts +43 -0
  9. package/dist/adapters/pg/postgresjs.d.ts +95 -0
  10. package/dist/adapters/pg/result.d.ts +30 -0
  11. package/dist/contracts/catalog/build.d.ts +18 -0
  12. package/dist/contracts/catalog/document.d.ts +79 -0
  13. package/dist/contracts/catalog/enum.d.ts +35 -0
  14. package/dist/contracts/catalog/identifier.d.ts +71 -0
  15. package/dist/contracts/catalog/identity.d.ts +98 -0
  16. package/dist/contracts/catalog/index.d.ts +11 -0
  17. package/dist/contracts/catalog/json.d.ts +17 -0
  18. package/dist/contracts/catalog/object.d.ts +142 -0
  19. package/dist/contracts/catalog/order.d.ts +43 -0
  20. package/dist/contracts/catalog/rewrite.d.ts +18 -0
  21. package/dist/contracts/catalog/types.d.ts +292 -0
  22. package/dist/contracts/catalog/words.d.ts +13 -0
  23. package/dist/contracts/connection.d.ts +33 -0
  24. package/dist/contracts/driver.d.ts +163 -0
  25. package/dist/contracts/error.d.ts +298 -0
  26. package/dist/contracts/index.d.ts +11 -0
  27. package/dist/contracts/index.js +16 -0
  28. package/dist/contracts/internal.d.ts +15 -0
  29. package/dist/contracts/internal.js +68 -0
  30. package/dist/contracts/nearest.d.ts +15 -0
  31. package/dist/contracts/rows.d.ts +84 -0
  32. package/dist/contracts/sha256.d.ts +14 -0
  33. package/dist/contracts/utf8.d.ts +33 -0
  34. package/dist/dialects/pg/array-literal.d.ts +25 -0
  35. package/dist/dialects/pg/bool.d.ts +46 -0
  36. package/dist/dialects/pg/column.d.ts +386 -0
  37. package/dist/dialects/pg/compile.d.ts +81 -0
  38. package/dist/dialects/pg/custom.d.ts +21 -0
  39. package/dist/dialects/pg/ddl.d.ts +70 -0
  40. package/dist/dialects/pg/decimal.d.ts +62 -0
  41. package/dist/dialects/pg/declarations.d.ts +34 -0
  42. package/dist/dialects/pg/emit.d.ts +18 -0
  43. package/dist/dialects/pg/enum.d.ts +30 -0
  44. package/dist/dialects/pg/errors.d.ts +30 -0
  45. package/dist/dialects/pg/finite.d.ts +15 -0
  46. package/dist/dialects/pg/geometry.d.ts +55 -0
  47. package/dist/dialects/pg/index.d.ts +45 -0
  48. package/dist/dialects/pg/index.js +1821 -0
  49. package/dist/dialects/pg/integer.d.ts +96 -0
  50. package/dist/dialects/pg/introspect.d.ts +43 -0
  51. package/dist/dialects/pg/json.d.ts +43 -0
  52. package/dist/dialects/pg/keys.d.ts +32 -0
  53. package/dist/dialects/pg/misuse.d.ts +31 -0
  54. package/dist/dialects/pg/model.d.ts +71 -0
  55. package/dist/dialects/pg/namespace.d.ts +63 -0
  56. package/dist/dialects/pg/network.d.ts +28 -0
  57. package/dist/dialects/pg/operators.d.ts +71 -0
  58. package/dist/dialects/pg/ops/between.d.ts +10 -0
  59. package/dist/dialects/pg/ops/contains.d.ts +8 -0
  60. package/dist/dialects/pg/ops/endsWith.d.ts +8 -0
  61. package/dist/dialects/pg/ops/eq.d.ts +9 -0
  62. package/dist/dialects/pg/ops/every.d.ts +9 -0
  63. package/dist/dialects/pg/ops/gt.d.ts +9 -0
  64. package/dist/dialects/pg/ops/gte.d.ts +9 -0
  65. package/dist/dialects/pg/ops/has.d.ts +9 -0
  66. package/dist/dialects/pg/ops/ilike.d.ts +8 -0
  67. package/dist/dialects/pg/ops/inList.d.ts +9 -0
  68. package/dist/dialects/pg/ops/inc.d.ts +13 -0
  69. package/dist/dialects/pg/ops/like.d.ts +8 -0
  70. package/dist/dialects/pg/ops/lt.d.ts +9 -0
  71. package/dist/dialects/pg/ops/lte.d.ts +9 -0
  72. package/dist/dialects/pg/ops/none.d.ts +9 -0
  73. package/dist/dialects/pg/ops/not.d.ts +9 -0
  74. package/dist/dialects/pg/ops/notIn.d.ts +9 -0
  75. package/dist/dialects/pg/ops/or.d.ts +9 -0
  76. package/dist/dialects/pg/ops/startsWith.d.ts +8 -0
  77. package/dist/dialects/pg/quote.d.ts +24 -0
  78. package/dist/dialects/pg/range.d.ts +50 -0
  79. package/dist/dialects/pg/relations.d.ts +45 -0
  80. package/dist/dialects/pg/schema.d.ts +77 -0
  81. package/dist/dialects/pg/search.d.ts +23 -0
  82. package/dist/dialects/pg/table.d.ts +201 -0
  83. package/dist/dialects/pg/temporal.d.ts +211 -0
  84. package/dist/dialects/pg/text.d.ts +44 -0
  85. package/dist/dialects/pg/time.d.ts +46 -0
  86. package/dist/okm.js +2 -0
  87. package/dist/okmodel.js +2 -0
  88. package/dist/runtime/client.d.ts +47 -0
  89. package/dist/runtime/conflict.d.ts +38 -0
  90. package/dist/runtime/drift-detail.d.ts +32 -0
  91. package/dist/runtime/drift.d.ts +29 -0
  92. package/dist/runtime/include.d.ts +15 -0
  93. package/dist/runtime/pg/pglite.d.ts +25 -0
  94. package/dist/runtime/pg/pglite.js +48 -0
  95. package/dist/runtime/pg/postgresjs.d.ts +29 -0
  96. package/dist/runtime/pg/postgresjs.js +62 -0
  97. package/dist/runtime/plan.d.ts +167 -0
  98. package/dist/runtime/tx.d.ts +29 -0
  99. package/dist/runtime/types.d.ts +369 -0
  100. package/dist/runtime/write.d.ts +48 -0
  101. package/dist/shared/4j72449d.js +37 -0
  102. package/dist/shared/79ggp945.js +148 -0
  103. package/dist/shared/7ck0dtry.js +259 -0
  104. package/dist/shared/8b04d28c.js +14 -0
  105. package/dist/shared/ax16zpvm.js +171 -0
  106. package/dist/shared/ckb1p0a4.js +566 -0
  107. package/dist/shared/cn0n68ft.js +16 -0
  108. package/dist/shared/dynq16vb.js +81 -0
  109. package/dist/shared/f5thc1s0.js +1050 -0
  110. package/dist/shared/fgqgsb5z.js +281 -0
  111. package/dist/shared/g2v6gnc8.js +347 -0
  112. package/dist/shared/g5fhqmwe.js +188 -0
  113. package/dist/shared/gk7cs7sp.js +566 -0
  114. package/dist/shared/hd293j9g.js +98 -0
  115. package/dist/shared/j5cn2963.js +701 -0
  116. package/dist/shared/kynx7vbm.js +1490 -0
  117. package/dist/shared/mkjmqjv4.js +456 -0
  118. package/dist/shared/mtkwv8wx.js +18 -0
  119. package/dist/shared/ngk8gj61.js +40 -0
  120. package/dist/shared/p0cbr3g2.js +79 -0
  121. package/dist/shared/px90pf9p.js +396 -0
  122. package/dist/shared/qk3hxfja.js +83 -0
  123. package/dist/shared/tb4ze1tr.js +119 -0
  124. package/dist/shared/vkm3q4x1.js +30 -0
  125. package/dist/shared/w8samvk1.js +18 -0
  126. package/dist/shared/xy79j5hq.js +177 -0
  127. package/dist/shared/y9m15dfv.js +888 -0
  128. package/dist/shared/ycwz42az.js +412 -0
  129. package/dist/shared/zhc5vmgf.js +257 -0
  130. package/dist/tooling/cli.d.ts +1 -0
  131. package/dist/tooling/cli.js +193 -0
  132. package/dist/tooling/errors/registry.d.ts +33 -0
  133. package/dist/tooling/migrate/apply.d.ts +86 -0
  134. package/dist/tooling/migrate/commands.d.ts +21 -0
  135. package/dist/tooling/migrate/config.d.ts +48 -0
  136. package/dist/tooling/migrate/dev.d.ts +16 -0
  137. package/dist/tooling/migrate/equal.d.ts +15 -0
  138. package/dist/tooling/migrate/files.d.ts +19 -0
  139. package/dist/tooling/migrate/index.d.ts +7 -0
  140. package/dist/tooling/migrate/index.js +7 -0
  141. package/dist/tooling/migrate/plan.d.ts +76 -0
  142. package/dist/tooling/migrate/policy.d.ts +78 -0
  143. package/dist/tooling/migrate/project.d.ts +69 -0
  144. package/dist/tooling/migrate/status.d.ts +74 -0
  145. package/dist/tooling/migrate/values.d.ts +29 -0
  146. package/package.json +94 -7
  147. package/dist/index.js +0 -0
package/README.md CHANGED
@@ -1 +1,44 @@
1
- okmodel — work in progress
1
+ # OKModel
2
+
3
+ Catalog-first TypeScript ORM for PostgreSQL. Version 0.1.
4
+
5
+ ## Install
6
+
7
+ ```sh
8
+ bun add okmodel
9
+ ```
10
+
11
+ postgres.js and PGlite are optional peers. Install the one you connect with.
12
+
13
+ ```sh
14
+ bun add postgres
15
+ # or
16
+ bun add @electric-sql/pglite
17
+ ```
18
+
19
+ The repository docs have the [quickstart](docs/quickstart.md), the [production checklist](docs/production.md), and the [known limits](docs/known-limits.md). Those files are not in the npm tarball.
20
+
21
+ ```ts
22
+ import { schema, table, t } from "okmodel/pg";
23
+ import { defineConfig } from "okmodel/migrate";
24
+
25
+ export const notes = table("notes", {
26
+ id: t.uuid(),
27
+ title: t.text(),
28
+ });
29
+
30
+ export const app = schema({ tables: [notes] });
31
+
32
+ export default defineConfig({ schema: "./schema.ts" });
33
+ ```
34
+
35
+ ```sh
36
+ bunx okm build
37
+ bunx okm generate init
38
+ ```
39
+
40
+ `table` and `index` for a schema come from `okmodel/pg`. Stable, experimental, and internal exports are listed in `tests/fixtures/api-surface.json` in the repository. `okmodel/internal` has no stability promise.
41
+
42
+ A production target sets `protected: true`. A production `connect` sets `requireMeta: true`. `prepared: "named"` is not for a transaction-mode pooler.
43
+
44
+ Apache-2.0.
@@ -0,0 +1,62 @@
1
+ /**
2
+ * Driver capability registry.
3
+ *
4
+ * The flags are data (spec §4). A helper reads them. Spec section 21 has no
5
+ * OKM11xx for a driver that lacks a flag: OKM1110 is the engine-version gate,
6
+ * and OKM1801 is a dialect mismatch. This module does not invent a code and
7
+ * does not throw for a missing flag.
8
+ */
9
+ import type { DriverCapabilities } from "../contracts/driver.js";
10
+ /** Capability names a caller can read. */
11
+ export declare const CAPABILITY_NAMES: readonly ["transactions", "stream", "listen", "cancel", "prepared", "describe"];
12
+ /** One name in {@link DriverCapabilities}. */
13
+ export type CapabilityName = (typeof CAPABILITY_NAMES)[number];
14
+ /**
15
+ * postgres.js execution flags.
16
+ *
17
+ * Interactive transactions, streaming, listen, cancel, and describe.
18
+ * Statements use the unnamed extended protocol so a plain execute is one
19
+ * round trip. Named prepares are a later opt-in.
20
+ */
21
+ export declare const POSTGRESJS_CAPABILITIES: {
22
+ readonly transactions: "interactive";
23
+ readonly stream: true;
24
+ readonly listen: true;
25
+ readonly cancel: true;
26
+ readonly prepared: "unnamed";
27
+ readonly describe: true;
28
+ };
29
+ /**
30
+ * PGlite execution flags.
31
+ *
32
+ * A pool of one with interactive transactions, listen, and describe.
33
+ * `cancel` is false: an in-flight statement cannot be aborted, and
34
+ * `statement_timeout` is not enforced.
35
+ */
36
+ export declare const PGLITE_CAPABILITIES: {
37
+ readonly transactions: "interactive";
38
+ readonly stream: false;
39
+ readonly listen: true;
40
+ readonly cancel: false;
41
+ readonly prepared: "unnamed";
42
+ readonly describe: true;
43
+ };
44
+ /**
45
+ * Reads one capability flag.
46
+ *
47
+ * @param flags - Declared capabilities
48
+ * @param name - Flag name
49
+ * @returns The flag value
50
+ */
51
+ export declare function readCapability<Name extends CapabilityName>(flags: DriverCapabilities, name: Name): DriverCapabilities[Name];
52
+ /**
53
+ * Reports whether a boolean capability is set.
54
+ *
55
+ * `transactions` and `prepared` are not boolean. Use {@link readCapability}
56
+ * for those.
57
+ *
58
+ * @param flags - Declared capabilities
59
+ * @param name - `stream`, `listen`, `cancel`, or `describe`
60
+ * @returns The flag
61
+ */
62
+ export declare function hasCapability(flags: DriverCapabilities, name: "stream" | "listen" | "cancel" | "describe"): boolean;
@@ -0,0 +1,91 @@
1
+ /**
2
+ * Errors an adapter throws.
3
+ *
4
+ * Database failures stay {@link DriverError}. The Postgres dialect maps that
5
+ * to {@link OkmError}. Acquire exhaustion is OKM1846 on {@link OkmError}:
6
+ * that code is the pool's own deadline (spec §15.2), not a SQLSTATE.
7
+ */
8
+ import { OkmError } from "../contracts/error.js";
9
+ import type { DriverErrorFields, DriverFailureKind } from "../contracts/driver.js";
10
+ /**
11
+ * A driver failure normalised to the contract fields.
12
+ *
13
+ * `batchIndex` is `null` except on a failed batch statement, where it is that
14
+ * statement's index. A commit failure (a deferred constraint) stays `null`.
15
+ */
16
+ export declare class DriverError extends Error {
17
+ /** SQLSTATE, when the server sent one. */
18
+ readonly sqlstate: string | undefined;
19
+ /** Constraint name, when the server sent one. */
20
+ readonly constraint: string | undefined;
21
+ /** Table name, when the server sent one. */
22
+ readonly table: string | undefined;
23
+ /** Column name, when the server sent one. */
24
+ readonly column: string | undefined;
25
+ /** Server detail line. */
26
+ readonly detail: string | undefined;
27
+ /** Driver or server error underneath. */
28
+ readonly cause: unknown;
29
+ /** Statement index inside a batch, or `null` when this is not that case. */
30
+ readonly batchIndex: number | null;
31
+ /** `timeout` or `cancelled`. Absent on a database error. */
32
+ readonly kind: DriverFailureKind | undefined;
33
+ /**
34
+ * @param message - Error text
35
+ * @param fields - Normalised fields
36
+ */
37
+ constructor(message: string, fields?: DriverErrorFields);
38
+ /**
39
+ * Same error with a batch index.
40
+ *
41
+ * @param batchIndex - Statement index, or `null` at commit
42
+ * @returns A new error when the index differs
43
+ */
44
+ at(batchIndex: number | null): DriverError;
45
+ }
46
+ /**
47
+ * An abort from the caller's `signal` (D124).
48
+ *
49
+ * @param cause - Underlying error, when one exists
50
+ * @returns Kind `cancelled`
51
+ */
52
+ export declare function cancelled(cause?: unknown): DriverError;
53
+ /**
54
+ * A statement, transaction, or batch deadline (D124).
55
+ *
56
+ * @param cause - Underlying error, when one exists
57
+ * @returns Kind `timeout`
58
+ */
59
+ export declare function timedOut(cause?: unknown): DriverError;
60
+ /**
61
+ * A commit was sent and its result never arrived (OKM1401 once mapped).
62
+ *
63
+ * @param cause - Underlying error, when one exists
64
+ * @returns Kind `outcome_unknown`, batch index `null`
65
+ */
66
+ export declare function outcomeUnknown(cause?: unknown): DriverError;
67
+ /**
68
+ * The pool did not hand out a connection within `timeouts.acquire`.
69
+ *
70
+ * @returns OKM1846, kind `timeout`
71
+ */
72
+ export declare function acquireTimeout(): OkmError;
73
+ /**
74
+ * Reads a string field from an unknown error object.
75
+ *
76
+ * @param error - Caught value
77
+ * @param key - Field name
78
+ * @returns The string, or `undefined`
79
+ */
80
+ export declare function errorField(error: unknown, key: string): string | undefined;
81
+ /**
82
+ * Maps a driver exception into {@link DriverError}.
83
+ *
84
+ * {@link OkmError} and {@link DriverError} pass through. A batch index on an
85
+ * existing {@link DriverError} is applied with {@link DriverError.at}.
86
+ *
87
+ * @param error - Caught value
88
+ * @param batchIndex - Statement index, or `null` at commit
89
+ * @returns A normalised error
90
+ */
91
+ export declare function mapDriverError(error: unknown, batchIndex?: number | null): unknown;
@@ -0,0 +1,39 @@
1
+ /**
2
+ * Driver-error constructors, loaded on the first failure.
3
+ *
4
+ * A successful `execute` does not import {@link import("./error.js")}.
5
+ */
6
+ type ErrorModule = typeof import("./error.js");
7
+ /**
8
+ * Loads driver-error constructors once.
9
+ *
10
+ * @returns The error module
11
+ */
12
+ export declare function driverErrors(): Promise<ErrorModule>;
13
+ /**
14
+ * Rejects with a closed-pool {@link import("./error.js").DriverError}.
15
+ *
16
+ * @param message - Error text
17
+ * @returns A rejected promise
18
+ */
19
+ export declare function rejectClosed(message?: string): Promise<never>;
20
+ /**
21
+ * Rejects with kind `cancelled`.
22
+ *
23
+ * @returns A rejected promise
24
+ */
25
+ export declare function rejectCancelled(): Promise<never>;
26
+ /**
27
+ * Rejects with kind `timeout`.
28
+ *
29
+ * @returns A rejected promise
30
+ */
31
+ export declare function rejectTimedOut(): Promise<never>;
32
+ /**
33
+ * Maps a caught driver exception.
34
+ *
35
+ * @param error - Caught value
36
+ * @returns A normalised error
37
+ */
38
+ export declare function mapFailure(error: unknown): Promise<unknown>;
39
+ export {};
@@ -0,0 +1,36 @@
1
+ /**
2
+ * Atomic batch for an interactive connection.
3
+ *
4
+ * Statements run one at a time. A failure rolls the unit back. Inside an open
5
+ * transaction the unit is a savepoint and the outer transaction stays open
6
+ * (D124). A timeout stays kind `timeout`. An abort stays kind `cancelled`.
7
+ */
8
+ import type { ExecuteResult, Statement, WireValue } from "../../contracts/driver.js";
9
+ import type { Watch } from "./call.js";
10
+ /** A connection that can run the batch protocol. */
11
+ export type BatchSession = {
12
+ /** This driver can abort an in-flight statement. */
13
+ readonly canCancel: boolean;
14
+ /**
15
+ * Runs one statement.
16
+ *
17
+ * @param text - SQL
18
+ * @param params - Wire parameters
19
+ * @param watch - Shared deadline for the batch, when there is one
20
+ * @returns The statement result
21
+ */
22
+ query(text: string, params: readonly WireValue[] | undefined, watch: Watch | undefined): Promise<ExecuteResult>;
23
+ /** Reports whether a transaction is open on this connection. */
24
+ inTransaction(): boolean;
25
+ /** Forgets a transaction after a rollback that did not complete. */
26
+ abandon(): void;
27
+ };
28
+ /**
29
+ * Runs statements as one atomic unit.
30
+ *
31
+ * @param session - Connection to use
32
+ * @param statements - Statements in order
33
+ * @param watch - Deadline for the whole batch
34
+ * @returns One result per statement
35
+ */
36
+ export declare function runAtomicBatch(session: BatchSession, statements: readonly Statement[], watch: Watch | undefined): Promise<readonly ExecuteResult[]>;
@@ -0,0 +1,59 @@
1
+ /**
2
+ * One deadline for a call.
3
+ *
4
+ * Created only when the caller passed `signal` or `timeout`. A plain execute
5
+ * does not allocate one.
6
+ */
7
+ import type { DriverFailureKind, ExecuteOptions } from "../../contracts/driver.js";
8
+ /** A signal plus the reason it aborted. */
9
+ export type Watch = {
10
+ /** Aborts on the caller's signal or on the deadline. */
11
+ readonly signal: AbortSignal;
12
+ /** Stops the timer and drops the caller's listener. */
13
+ finish(): void;
14
+ /**
15
+ * Why {@link signal} aborted.
16
+ *
17
+ * `undefined` until it aborts.
18
+ */
19
+ reason(): DriverFailureKind | undefined;
20
+ };
21
+ /**
22
+ * Reports whether `options` carries a signal or a timeout.
23
+ *
24
+ * @param options - Per-call options
25
+ * @returns True when a {@link Watch} is required
26
+ */
27
+ export declare function needsWatch(options: ExecuteOptions | undefined): boolean;
28
+ /**
29
+ * Next transaction depth after one statement.
30
+ *
31
+ * Only a whole command matches, so `ROLLBACK TO SAVEPOINT` stays inside the
32
+ * transaction. Kept here so a read does not load the batch runner.
33
+ *
34
+ * @param depth - Depth before the statement
35
+ * @param text - Statement text
36
+ * @returns Depth after a successful statement
37
+ */
38
+ export declare function nextTransactionDepth(depth: number, text: string): number;
39
+ /**
40
+ * Opens a watch for one call.
41
+ *
42
+ * The caller has already rejected a pre-aborted signal and a non-positive
43
+ * timeout.
44
+ *
45
+ * @param options - Options that include a signal or a timeout
46
+ * @returns The watch. The caller must {@link Watch.finish} it
47
+ */
48
+ export declare function openWatch(options: ExecuteOptions): Watch;
49
+ /**
50
+ * Runs `fn` with a watch when the caller set a signal or a timeout.
51
+ *
52
+ * A plain call passes `undefined` and returns `fn`'s promise unchanged.
53
+ *
54
+ * @param closed - The pool has been closed
55
+ * @param options - Per-call options
56
+ * @param fn - Work that receives the watch
57
+ * @returns Whatever `fn` returns
58
+ */
59
+ export declare function runCall<T>(closed: boolean, options: ExecuteOptions | undefined, fn: (watch: Watch | undefined) => Promise<T>): Promise<T>;
@@ -0,0 +1,21 @@
1
+ /**
2
+ * PGlite adapter (`okmodel/pg/pglite`).
3
+ *
4
+ * One database is a pool of one. `cancel` is false: a statement already
5
+ * running cannot be aborted. `@electric-sql/pglite` is a peer of this entry.
6
+ */
7
+ import type { DriverPool, DriverPoolConfig } from "../../contracts/driver.js";
8
+ export { hasCapability, PGLITE_CAPABILITIES as capabilities, readCapability, } from "../capabilities.js";
9
+ export { DriverError } from "../error.js";
10
+ /** Options for {@link open}. The pool size is always one. */
11
+ export type PgliteConfig = DriverPoolConfig & {
12
+ /** Data directory. `memory://` when omitted. */
13
+ readonly dataDir?: string;
14
+ };
15
+ /**
16
+ * Opens a PGlite pool of one.
17
+ *
18
+ * @param config - Data directory and acquire timeout. `max` is ignored
19
+ * @returns The pool
20
+ */
21
+ export declare function open(config?: PgliteConfig): Promise<DriverPool>;
@@ -0,0 +1,43 @@
1
+ /**
2
+ * Pool operations a read does not use: checkout, describe, and stream.
3
+ *
4
+ * Loaded on the first reserve, batch, describe, or stream.
5
+ */
6
+ import type { Sql } from "postgres";
7
+ import type { DescribeResult, DriverConnection, WireValue } from "../../contracts/driver.js";
8
+ import { type Canceller, type Counters, type NoticeBuffer } from "./postgresjs.js";
9
+ /** State `open` shares with a checkout. */
10
+ export type HeldPool = {
11
+ readonly sql: Sql;
12
+ readonly acquireMs: number | undefined;
13
+ readonly counters: Counters;
14
+ readonly isClosed: () => boolean;
15
+ readonly notices: NoticeBuffer;
16
+ readonly named: boolean;
17
+ readonly poolInflight: Set<Canceller>;
18
+ };
19
+ /**
20
+ * Checks out one connection and clears it on release.
21
+ *
22
+ * @param pool - The open pool
23
+ * @returns A connection the caller must release
24
+ */
25
+ export declare function checkout(pool: HeldPool): Promise<DriverConnection>;
26
+ /**
27
+ * Describes a statement.
28
+ *
29
+ * @param sql - Pool client
30
+ * @param text - SQL
31
+ * @param params - Wire parameters
32
+ * @returns Column names and the parameter count
33
+ */
34
+ export declare function describe(sql: Sql, text: string, params?: readonly WireValue[]): Promise<DescribeResult>;
35
+ /**
36
+ * Yields cursor chunks.
37
+ *
38
+ * @param sql - Pool client
39
+ * @param text - SQL
40
+ * @param params - Wire parameters
41
+ * @returns Chunks of wire rows
42
+ */
43
+ export declare function stream(sql: Sql, text: string, params?: readonly WireValue[]): AsyncIterable<readonly (readonly WireValue[])[]>;
@@ -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;