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.
Files changed (147) hide show
  1. package/README.md +246 -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 +385 -0
  100. package/dist/runtime/write.d.ts +48 -0
  101. package/dist/shared/05w44d8g.js +1524 -0
  102. package/dist/shared/523ft3e8.js +171 -0
  103. package/dist/shared/8b04d28c.js +14 -0
  104. package/dist/shared/cn0n68ft.js +16 -0
  105. package/dist/shared/dkn3y60a.js +374 -0
  106. package/dist/shared/dynq16vb.js +81 -0
  107. package/dist/shared/fgqgsb5z.js +281 -0
  108. package/dist/shared/g5fhqmwe.js +188 -0
  109. package/dist/shared/gk7cs7sp.js +566 -0
  110. package/dist/shared/hadrfb9h.js +398 -0
  111. package/dist/shared/hd293j9g.js +98 -0
  112. package/dist/shared/j5cn2963.js +701 -0
  113. package/dist/shared/jjb7yht9.js +37 -0
  114. package/dist/shared/kprcwaha.js +30 -0
  115. package/dist/shared/m3ypagh5.js +347 -0
  116. package/dist/shared/mkjmqjv4.js +456 -0
  117. package/dist/shared/mtkwv8wx.js +18 -0
  118. package/dist/shared/ngk8gj61.js +40 -0
  119. package/dist/shared/p0cbr3g2.js +79 -0
  120. package/dist/shared/qk3hxfja.js +83 -0
  121. package/dist/shared/rgs0st28.js +1066 -0
  122. package/dist/shared/w8samvk1.js +18 -0
  123. package/dist/shared/x5x59srr.js +566 -0
  124. package/dist/shared/xqbhg6cn.js +119 -0
  125. package/dist/shared/xy79j5hq.js +177 -0
  126. package/dist/shared/y9m15dfv.js +888 -0
  127. package/dist/shared/ycwz42az.js +412 -0
  128. package/dist/shared/yd1kzwn2.js +148 -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 +109 -7
  147. package/dist/index.js +0 -0
@@ -0,0 +1,46 @@
1
+ /**
2
+ * Date and time columns. The timestamp codec is Temporal.
3
+ */
4
+ import { type ColumnBuilder, type PlainFlags } from "./column.js";
5
+ import { type TimeWithOffset } from "./temporal.js";
6
+ /**
7
+ * Timestamp with time zone.
8
+ *
9
+ * @param precision - Fractional seconds, 0 through 6
10
+ * @returns A timestamptz column
11
+ */
12
+ export declare function timestamptz(precision?: number): ColumnBuilder<Temporal.Instant, PlainFlags>;
13
+ /**
14
+ * Timestamp without time zone.
15
+ *
16
+ * @param precision - Fractional seconds, 0 through 6
17
+ * @returns A timestamp column
18
+ */
19
+ export declare function timestamp(precision?: number): ColumnBuilder<Temporal.PlainDateTime, PlainFlags>;
20
+ /**
21
+ * Calendar date.
22
+ *
23
+ * @returns A date column
24
+ */
25
+ export declare function date(): ColumnBuilder<Temporal.PlainDate, PlainFlags>;
26
+ /**
27
+ * Time of day.
28
+ *
29
+ * @param precision - Fractional seconds, 0 through 6
30
+ * @returns A time column
31
+ */
32
+ export declare function time(precision?: number): ColumnBuilder<Temporal.PlainTime, PlainFlags>;
33
+ /**
34
+ * Time of day with a numeric offset.
35
+ *
36
+ * @param precision - Fractional seconds, 0 through 6
37
+ * @returns A timetz column
38
+ */
39
+ export declare function timetz(precision?: number): ColumnBuilder<TimeWithOffset, PlainFlags>;
40
+ /**
41
+ * Interval. `fields` selects a Postgres qualifier such as `day to second`.
42
+ *
43
+ * @param fields - Optional field qualifier
44
+ * @returns An interval column
45
+ */
46
+ export declare function interval(fields?: string): ColumnBuilder<Temporal.Duration, PlainFlags>;
package/dist/okm.js ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ import "./tooling/cli.js";
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ import "./tooling/cli.js";
@@ -0,0 +1,47 @@
1
+ /**
2
+ * Connected client: plan cache, one endpoint, and the read methods.
3
+ *
4
+ * `connect()` starts the dialect and `requires` checks. Queries wait for them.
5
+ * The plan cache is per client and bounded.
6
+ */
7
+ import type { DriverPool } from "../contracts/driver.js";
8
+ import { type ErrorStatuses } from "../contracts/error.js";
9
+ import type { QuerySchema } from "../dialects/pg/model.js";
10
+ import { type Plan } from "./plan.js";
11
+ import type { CatalogArtifact, Connected } from "./types.js";
12
+ type Session = {
13
+ readonly schema: QuerySchema;
14
+ readonly pool: DriverPool;
15
+ readonly ownsPool: boolean;
16
+ readonly http: ErrorStatuses | undefined;
17
+ readonly includeValues: boolean;
18
+ readonly logger: {
19
+ error?(entry: {
20
+ readonly code: string;
21
+ readonly summary: string;
22
+ }): void;
23
+ } | undefined;
24
+ readonly connected: Promise<void>;
25
+ readonly cache: Map<string, Plan>;
26
+ };
27
+ /**
28
+ * Builds a client for one pool.
29
+ *
30
+ * @param schema - Caller schema
31
+ * @param pool - Endpoint pool
32
+ * @param options - Errors, logger, and whether `close` ends the pool
33
+ * @returns The typed client
34
+ */
35
+ export declare function createClient<S extends QuerySchema>(schema: S, pool: DriverPool, options: {
36
+ readonly endpoint?: string;
37
+ readonly ownsPool: boolean;
38
+ readonly http?: ErrorStatuses | undefined;
39
+ readonly includeValues?: boolean | undefined;
40
+ readonly logger?: Session["logger"] | undefined;
41
+ readonly signal?: AbortSignal | undefined;
42
+ readonly timeout?: number | undefined;
43
+ readonly catalog?: CatalogArtifact | undefined;
44
+ readonly catalogDir?: string | undefined;
45
+ readonly requireMeta?: boolean | undefined;
46
+ }): Connected<S>;
47
+ export {};
@@ -0,0 +1,38 @@
1
+ /**
2
+ * `onConflict` planning.
3
+ *
4
+ * Loaded only when a write passes `onConflict` other than `"error"`.
5
+ * A plain insert does not import this module.
6
+ */
7
+ import type { ColumnModel } from "../dialects/pg/model.js";
8
+ import { type Indexed } from "./plan.js";
9
+ /** How a conflict should be written. `"error"` never reaches here. */
10
+ export type ConflictPlan = {
11
+ readonly kind: "ignore" | "update" | "return";
12
+ readonly columns: readonly ColumnModel[];
13
+ readonly update: readonly ColumnModel[];
14
+ };
15
+ /**
16
+ * Reads `onConflict` and checks `on` against unique constraints (OKM1104).
17
+ *
18
+ * @param table - Target table
19
+ * @param value - The option the caller passed
20
+ * @returns The plan, or `undefined` for the default `"error"`
21
+ */
22
+ export declare function readConflict(table: Indexed, value: unknown): ConflictPlan | undefined;
23
+ /**
24
+ * Appends the conflict clause, or wraps a first-or-create insert.
25
+ *
26
+ * @param head - `insert into … values …` without `returning`
27
+ * @param returning - ` returning …`, including the leading space
28
+ * @param params - Insert parameters
29
+ * @param plan - Conflict plan
30
+ * @param table - Target table
31
+ * @param returningColumns - Columns in `returning`
32
+ * @param keys - Encoded conflict-key tuples, one per inserted row. Used by `return`
33
+ * @returns Statement text and parameters
34
+ */
35
+ export declare function finishInsert(head: string, returning: string, params: readonly (string | null)[], plan: ConflictPlan, table: Indexed, returningColumns: readonly ColumnModel[], keys: readonly (readonly (string | null)[])[]): {
36
+ readonly text: string;
37
+ readonly params: (string | null)[];
38
+ };
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Compatibility check used only when the catalog hashes differ.
3
+ *
4
+ * Ahead by expand is compatible. Ahead by a contract step, or behind, fails
5
+ * closed (OKM1520).
6
+ */
7
+ import type { DriverPool, ExecuteOptions } from "../contracts/driver.js";
8
+ /** One applied migration, in history order. */
9
+ export type DriftMigration = {
10
+ readonly id: string;
11
+ readonly catalogHash: string;
12
+ /** True when every step is expand. */
13
+ readonly expand: boolean;
14
+ };
15
+ /**
16
+ * Classifies a hash mismatch.
17
+ *
18
+ * @param codeHash - Hash of the code's catalog
19
+ * @param recordedHash - Hash stored in `okm_meta`
20
+ * @param migrations - Applied migrations, oldest first
21
+ * @returns `ok` when the database is ahead by expand only
22
+ */
23
+ export declare function driftVerdict(codeHash: string, recordedHash: string, migrations: readonly DriftMigration[]): "ok" | "behind" | "contract";
24
+ /**
25
+ * Reads history and throws OKM1520 when the database is not expand-ahead.
26
+ *
27
+ * @param pool - The connection `connect` already opened
28
+ * @param codeHash - Hash of the code's catalog
29
+ * @param recordedHash - Hash from the startup query
30
+ * @param options - The call's cancellation and deadline
31
+ */
32
+ export declare function assertDrift(pool: DriverPool, codeHash: string, recordedHash: string, options: ExecuteOptions | undefined): Promise<void>;
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Startup catalog check.
3
+ *
4
+ * The fast path compares two hashes. History, catalog JSON, and
5
+ * {@link loadTrustedCatalog} load only when those hashes differ.
6
+ */
7
+ import type { DriverPool, ExecuteOptions } from "../contracts/driver.js";
8
+ import type { QuerySchema } from "../dialects/pg/model.js";
9
+ /** Build artifact `okm build` wrote. */
10
+ export type CatalogArtifact = {
11
+ readonly text: string;
12
+ readonly hash: string;
13
+ };
14
+ /**
15
+ * Compares the code's catalog hash with the hash already read from `okm_meta`.
16
+ *
17
+ * Equal hashes return without reading history. A difference loads the
18
+ * compatibility check.
19
+ *
20
+ * @param pool - The pool `connect` is checking
21
+ * @param schema - The caller's schema. Its catalog is read only when no artifact hash is available
22
+ * @param recordedHash - Hash from the startup query
23
+ * @param source - Optional build artifact, or a directory that contains one
24
+ * @param options - Cancellation and deadline for the history query
25
+ */
26
+ export declare function assertCompatible(pool: DriverPool, schema: QuerySchema, recordedHash: string, source: {
27
+ readonly catalog?: CatalogArtifact;
28
+ readonly catalogDir?: string;
29
+ }, options: ExecuteOptions | undefined): Promise<void>;
@@ -0,0 +1,15 @@
1
+ /**
2
+ * LATERAL include planner. Loaded on the first include.
3
+ */
4
+ import { type IncludeHooks, type Plan } from "./plan.js";
5
+ /**
6
+ * Decodes a result that has includes.
7
+ *
8
+ * @param plan - Compiled plan
9
+ * @param rows - Wire rows
10
+ * @param table - Table the call named
11
+ * @returns The call's value
12
+ */
13
+ export declare function decodeIncluded(plan: Plan, rows: readonly (readonly (string | null)[])[], table: string): unknown;
14
+ /** Hooks the read planner calls after this module has loaded. */
15
+ export declare const includeHooks: IncludeHooks;
@@ -0,0 +1,25 @@
1
+ /**
2
+ * `connect` for PGlite (`okmodel/pg/pglite`).
3
+ *
4
+ * Opening a database is async. An existing pool is reused and not closed.
5
+ */
6
+ import type { DriverPool } from "../../contracts/driver.js";
7
+ import type { QuerySchema } from "../../dialects/pg/model.js";
8
+ import { type PgliteConfig } from "../../adapters/pg/pglite.js";
9
+ import type { ConnectOptions, Connected } from "../types.js";
10
+ export { open, type PgliteConfig } from "../../adapters/pg/pglite.js";
11
+ export { capabilities, hasCapability, readCapability } from "../../adapters/pg/pglite.js";
12
+ export { DriverError } from "../../adapters/error.js";
13
+ /** A data directory, a pool from {@link open}, or omitted for memory. */
14
+ export type PgliteTarget = string | DriverPool | undefined;
15
+ /** Driver options `connect` forwards to {@link open}. */
16
+ export type PgliteConnectOptions<S extends QuerySchema> = ConnectOptions<S> & Omit<PgliteConfig, "dataDir">;
17
+ /**
18
+ * Connects one PGlite endpoint and returns a client for `schema`.
19
+ *
20
+ * @typeParam S - Schema
21
+ * @param target - Data directory, existing pool, or omitted for memory
22
+ * @param options - Schema, errors, and logger
23
+ * @returns The client
24
+ */
25
+ export declare function connect<const S extends QuerySchema>(target: PgliteTarget, options: PgliteConnectOptions<S>): Promise<Connected<S>>;
@@ -0,0 +1,48 @@
1
+ import {
2
+ DriverError2
3
+ } from "../../shared/hd293j9g.js";
4
+ import {
5
+ open3
6
+ } from "../../shared/fgqgsb5z.js";
7
+ import {
8
+ createClient
9
+ } from "../../shared/rgs0st28.js";
10
+ import {
11
+ PGLITE_CAPABILITIES,
12
+ readCapability2,
13
+ hasCapability2
14
+ } from "../../shared/xy79j5hq.js";
15
+
16
+ // src/runtime/pg/pglite.ts
17
+ async function connect(target, options) {
18
+ const shared = {
19
+ http: options.errors?.http,
20
+ includeValues: options.errors?.includeValues,
21
+ logger: options.logger,
22
+ signal: options.signal,
23
+ requireMeta: options.requireMeta,
24
+ timeout: options.timeout,
25
+ catalog: options.catalog,
26
+ catalogDir: options.catalogDir
27
+ };
28
+ if (isPool(target)) {
29
+ return createClient(options.schema, target, { ...shared, ownsPool: false });
30
+ }
31
+ const dataDir = target;
32
+ const pool = await open3({
33
+ ...dataDir !== undefined ? { dataDir } : {},
34
+ ...options.timeouts !== undefined ? { timeouts: options.timeouts } : {}
35
+ });
36
+ return createClient(options.schema, pool, { ...shared, ownsPool: true });
37
+ }
38
+ function isPool(target) {
39
+ return typeof target === "object" && target !== null && "execute" in target && "capabilities" in target;
40
+ }
41
+ export {
42
+ DriverError2 as DriverError,
43
+ PGLITE_CAPABILITIES as capabilities,
44
+ connect,
45
+ hasCapability2 as hasCapability,
46
+ open3 as open,
47
+ readCapability2 as readCapability
48
+ };
@@ -0,0 +1,29 @@
1
+ /**
2
+ * `connect` for postgres.js (`okmodel/pg/postgresjs`).
3
+ *
4
+ * One URL or an existing pool is one endpoint. The client is typed by the
5
+ * schema passed in, not by `Register`.
6
+ */
7
+ import type { DriverPool } from "../../contracts/driver.js";
8
+ import type { QuerySchema } from "../../dialects/pg/model.js";
9
+ import { type PostgresJsConfig } from "../../adapters/pg/postgresjs.js";
10
+ import type { ConnectOptions, Connected } from "../types.js";
11
+ export { open, type PostgresJsConfig } from "../../adapters/pg/postgresjs.js";
12
+ export { capabilities, hasCapability, readCapability } from "../../adapters/pg/postgresjs.js";
13
+ export { DriverError } from "../../adapters/error.js";
14
+ /** A connection string or a pool from {@link open}. */
15
+ export type PostgresTarget = string | DriverPool;
16
+ /** Driver options `connect` forwards to {@link open}. */
17
+ export type PostgresConnectOptions<S extends QuerySchema> = ConnectOptions<S> & Omit<PostgresJsConfig, "url">;
18
+ /**
19
+ * Connects one Postgres endpoint and returns a client for `schema`.
20
+ *
21
+ * Named prepared statements are off unless `prepared` is `"named"`.
22
+ * `prepared: "named"` is not for transaction-mode poolers.
23
+ *
24
+ * @typeParam S - Schema
25
+ * @param target - URL or an existing pool
26
+ * @param options - Schema, errors, logger, and driver options
27
+ * @returns The client. Await `connected` for the dialect check
28
+ */
29
+ export declare function connect<const S extends QuerySchema>(target: PostgresTarget, options: PostgresConnectOptions<S>): Connected<S>;
@@ -0,0 +1,62 @@
1
+ import {
2
+ DriverError2
3
+ } from "../../shared/hd293j9g.js";
4
+ import {
5
+ open2
6
+ } from "../../shared/dkn3y60a.js";
7
+ import {
8
+ createClient
9
+ } from "../../shared/rgs0st28.js";
10
+ import {
11
+ POSTGRESJS_CAPABILITIES,
12
+ readCapability2,
13
+ hasCapability2
14
+ } from "../../shared/xy79j5hq.js";
15
+
16
+ // src/runtime/pg/postgresjs.ts
17
+ function connect(target, options) {
18
+ if (isPool(target)) {
19
+ return createClient(options.schema, target, {
20
+ ownsPool: false,
21
+ http: options.errors?.http,
22
+ includeValues: options.errors?.includeValues,
23
+ logger: options.logger,
24
+ signal: options.signal,
25
+ timeout: options.timeout,
26
+ catalog: options.catalog,
27
+ catalogDir: options.catalogDir,
28
+ requireMeta: options.requireMeta
29
+ });
30
+ }
31
+ const url = target;
32
+ const pool = open2({
33
+ url,
34
+ ...options.max !== undefined ? { max: options.max } : {},
35
+ ...options.timeouts !== undefined ? { timeouts: options.timeouts } : {},
36
+ ...options.prepared !== undefined ? { prepared: options.prepared } : {},
37
+ ...options.searchPath !== undefined ? { searchPath: options.searchPath } : {},
38
+ ...options.ssl !== undefined ? { ssl: options.ssl } : {}
39
+ });
40
+ return createClient(options.schema, pool, {
41
+ ownsPool: true,
42
+ http: options.errors?.http,
43
+ includeValues: options.errors?.includeValues,
44
+ logger: options.logger,
45
+ signal: options.signal,
46
+ timeout: options.timeout,
47
+ catalog: options.catalog,
48
+ catalogDir: options.catalogDir,
49
+ requireMeta: options.requireMeta
50
+ });
51
+ }
52
+ function isPool(target) {
53
+ return typeof target === "object" && "execute" in target && "capabilities" in target;
54
+ }
55
+ export {
56
+ DriverError2 as DriverError,
57
+ POSTGRESJS_CAPABILITIES as capabilities,
58
+ connect,
59
+ hasCapability2 as hasCapability,
60
+ open2 as open,
61
+ readCapability2 as readCapability
62
+ };
@@ -0,0 +1,167 @@
1
+ /**
2
+ * Read planning: logical shape, one Postgres statement, decode.
3
+ *
4
+ * A cache hit walks the call once to bind parameters. SQL is built on a miss.
5
+ * Identifiers come from the catalog model. Values are parameters.
6
+ */
7
+ import { type QueryCode } from "../contracts/error.js";
8
+ import { type ColumnModel, type QuerySchema, type RelationModel, type TableModel } from "../dialects/pg/model.js";
9
+ /** A read the caller asked for. */
10
+ export type ReadOp = "find" | "one" | "count" | "exists";
11
+ /** Routing constraint chosen before execute. */
12
+ export type RouteConstraint = "auto" | "primary" | "replica";
13
+ /** One read call. `all` is the `.all(reason)` escape for an unbounded read. */
14
+ export type ReadCall = {
15
+ readonly op: ReadOp;
16
+ readonly table: string;
17
+ readonly where?: unknown;
18
+ readonly select?: unknown;
19
+ readonly orderBy?: unknown;
20
+ readonly limit?: unknown;
21
+ readonly include?: unknown;
22
+ readonly all?: string;
23
+ };
24
+ /** Where a decoded value sits. */
25
+ export type ScalarOut = {
26
+ readonly key: string;
27
+ readonly at: number;
28
+ readonly decode: ((wire: string) => unknown) | undefined;
29
+ };
30
+ /** A nested include. `at` is a root column for `one` and the JSON column for `many`. */
31
+ export type IncludeOut = {
32
+ readonly name: string;
33
+ readonly kind: "one" | "many";
34
+ readonly at: number;
35
+ readonly fields: readonly ScalarOut[];
36
+ readonly nested: readonly IncludeOut[];
37
+ };
38
+ /** Columns and includes in statement order. */
39
+ export type Outputs = {
40
+ readonly fields: readonly ScalarOut[];
41
+ readonly includes: readonly IncludeOut[];
42
+ };
43
+ /** Mutable outputs while a plan is compiled. */
44
+ /** Lateral include compiler. Loaded on the first include, not on a plain read. */
45
+ export type IncludeHooks = {
46
+ read(schema: QuerySchema, table: Indexed, include: unknown, all: string | undefined): readonly ParsedInclude[];
47
+ select(include: ParsedInclude, alias: string, sink: Sink, outputs: Built | undefined, column: number): number;
48
+ join(schema: QuerySchema, include: ParsedInclude, sink: Sink, parentAlias: string, alias: string, depth: number): void;
49
+ };
50
+ export type Built = {
51
+ fields: ScalarOut[];
52
+ includes: IncludeOut[];
53
+ };
54
+ /** A cached physical plan. Parameters are not stored. */
55
+ export type Plan = {
56
+ readonly fingerprint: string;
57
+ readonly text: string;
58
+ readonly mode: ReadOp;
59
+ readonly outputs: Outputs;
60
+ };
61
+ /** Bound parameters plus the cache key for one call. */
62
+ export type Bound = {
63
+ readonly key: string;
64
+ readonly params: readonly (string | null)[];
65
+ };
66
+ /** A rule recorded for inspection. Provenance names who added it. */
67
+ export type AppliedRule = {
68
+ readonly rule: string;
69
+ readonly contribution: string;
70
+ readonly provenance: string;
71
+ };
72
+ export type Indexed = {
73
+ readonly model: TableModel;
74
+ readonly columns: ReadonlyMap<string, ColumnModel>;
75
+ readonly relations: ReadonlyMap<string, RelationModel>;
76
+ readonly names: readonly string[];
77
+ };
78
+ export type Sink = {
79
+ text(value: string): void;
80
+ param(encoded: string): void;
81
+ mark(token: string): void;
82
+ };
83
+ /**
84
+ * Rules the read path enforces before planning.
85
+ *
86
+ * P21 adds the rest of the final safety verification here.
87
+ *
88
+ * @param rules - Rules already checked for this call
89
+ */
90
+ export declare function verifyRead(rules: readonly AppliedRule[]): void;
91
+ /**
92
+ * Binds a call: shape key and parameters, no SQL.
93
+ *
94
+ * @param schema - Connected schema
95
+ * @param call - The read
96
+ * @returns The cache key and the wire parameters
97
+ */
98
+ export declare function bindCall(schema: QuerySchema, call: ReadCall, hooks?: IncludeHooks): Bound;
99
+ /**
100
+ * Compiles a call to one statement.
101
+ *
102
+ * @param schema - Connected schema
103
+ * @param call - The read
104
+ * @param key - Shape key from {@link bindCall}
105
+ * @returns The plan
106
+ */
107
+ export declare function compileCall(schema: QuerySchema, call: ReadCall, key: string, hooks?: IncludeHooks): Plan;
108
+ /**
109
+ * Decodes driver rows into plain objects.
110
+ *
111
+ * @param plan - Compiled plan
112
+ * @param rows - Wire rows
113
+ * @param table - Table name, for `not_unique`
114
+ * @returns Rows, one row or null, a count, or a boolean
115
+ */
116
+ export declare function decodeResult(plan: Plan, rows: readonly (readonly (string | null)[])[], table: string, rowOf?: (outputs: Outputs, row: readonly (string | null)[]) => Record<string, unknown>): unknown;
117
+ /**
118
+ * Logical intent for inspection. Operators become plain tags.
119
+ *
120
+ * @param call - The read
121
+ * @returns A JSON-safe description, without a fingerprint
122
+ */
123
+ export declare function logicalIntent(call: ReadCall): {
124
+ readonly op: ReadOp;
125
+ readonly table: string;
126
+ readonly where: unknown;
127
+ readonly select: unknown;
128
+ readonly orderBy: unknown;
129
+ readonly limit: unknown;
130
+ readonly include: unknown;
131
+ readonly all: string | undefined;
132
+ };
133
+ /**
134
+ * Rules for inspection, including the caller filter when one was passed.
135
+ *
136
+ * @param call - The read
137
+ * @returns Provenance-tagged rules
138
+ */
139
+ export declare function appliedRules(call: ReadCall): readonly AppliedRule[];
140
+ export declare function emitJoin(sink: Sink, parent: string, child: string, relation: RelationModel): void;
141
+ export declare function emitWhere(schema: QuerySchema, table: Indexed, where: unknown, sink: Sink, alias: string, depth: number, appended?: boolean): void;
142
+ export declare function emitOrder(table: Indexed, orderBy: unknown, sink: Sink, alias: string): void;
143
+ export declare function emitPrimaryOrder(table: Indexed, sink: Sink, alias: string): void;
144
+ export type ParsedInclude = {
145
+ readonly name: string;
146
+ readonly relation: RelationModel;
147
+ readonly where: unknown;
148
+ readonly orderBy: unknown;
149
+ readonly limit: number | undefined;
150
+ readonly columns: readonly ColumnModel[];
151
+ readonly presence: ColumnModel | undefined;
152
+ /** Index in the projected list that is null when the related row is missing. */
153
+ readonly anchor: number;
154
+ readonly nested: readonly ParsedInclude[];
155
+ };
156
+ export declare function selectedColumns(table: Indexed, select: unknown): readonly ColumnModel[];
157
+ export declare function rejectKeys(value: Readonly<Record<string, unknown>>, accepted: readonly string[], where: string): void;
158
+ export declare function readLimit(value: unknown): number;
159
+ export declare function projectExpr(sql: string, dataType: string): string;
160
+ export declare function jsonExpr(sql: string, dataType: string): string;
161
+ export declare function decodeRow(outputs: Outputs, row: readonly (string | null)[]): Record<string, unknown>;
162
+ export declare function cell(value: string | null | undefined, decode: ((wire: string) => unknown) | undefined): unknown;
163
+ export declare function indexes(schema: QuerySchema): ReadonlyMap<string, Indexed>;
164
+ export declare function quote(name: string): string;
165
+ export declare function list(names: readonly string[]): string;
166
+ export declare function isRecord(value: unknown): value is Record<string, unknown>;
167
+ export declare function fail(code: QueryCode, message: string): never;
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Internal transaction runner for multi-statement writes.
3
+ *
4
+ * One statement uses the pool. Several statements use `batch`, which reserves
5
+ * a connection, begins, and commits. Inside an open transaction, `batch` is a
6
+ * savepoint and does not commit the outer transaction (D124). A connection
7
+ * lost at commit is `outcome_unknown`. Public `tx` is P29.
8
+ */
9
+ import type { DriverConnection, DriverPool, ExecuteOptions, ExecuteResult, Statement } from "../contracts/driver.js";
10
+ /**
11
+ * Runs write statements as one atomic unit.
12
+ *
13
+ * @param pool - Endpoint pool
14
+ * @param statements - Statements in order
15
+ * @param options - Signal and timeout for the whole unit
16
+ * @returns One result per statement
17
+ */
18
+ export declare function runWrite(pool: DriverPool, statements: readonly Statement[], options: ExecuteOptions | undefined): Promise<readonly ExecuteResult[]>;
19
+ /**
20
+ * Runs statements on a connection the caller already reserved.
21
+ *
22
+ * An open transaction makes this a savepoint. The caller commits.
23
+ *
24
+ * @param connection - Reserved connection
25
+ * @param statements - Statements in order
26
+ * @param options - Signal and timeout
27
+ * @returns One result per statement
28
+ */
29
+ export declare function runWriteOn(connection: DriverConnection, statements: readonly Statement[], options: ExecuteOptions | undefined): Promise<readonly ExecuteResult[]>;