@prisma-next/postgres 0.3.0-dev.44 → 0.3.0-dev.52

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 CHANGED
@@ -13,13 +13,16 @@ Composition-root Postgres helper that builds a Prisma Next runtime client and ex
13
13
  `@prisma-next/postgres/runtime` exposes a single `postgres(...)` helper that composes the Postgres execution stack and returns query/runtime roots:
14
14
 
15
15
  - `db.sql`
16
- - `db.kysely(runtime)`
16
+ - `db.kysely` (lane-owned build-only authoring surface: `build(query)` + `whereExpr(query)`)
17
17
  - `db.schema`
18
18
  - `db.orm`
19
19
  - `db.context`
20
20
  - `db.stack`
21
21
 
22
- Runtime and connection resources are deferred until `db.runtime()` is called.
22
+ `db.kysely` is produced by `@prisma-next/sql-kysely-lane` and intentionally exposes lane behavior, not raw Kysely execution APIs. `build(query)` infers plan row type from `query.compile()`, and `whereExpr(query)` produces `ToWhereExpr` payloads for ORM `.where(...)` interop.
23
+
24
+ Runtime resources are deferred until `db.runtime()` or `db.connect(...)` is called.
25
+ Connection binding can be provided up front (`url`, `pg`, `binding`) or deferred via `db.connect(...)`.
23
26
 
24
27
  When URL binding is used, pool timeouts are configurable via `poolOptions`:
25
28
 
@@ -29,10 +32,11 @@ When URL binding is used, pool timeouts are configurable via `poolOptions`:
29
32
  ## Responsibilities
30
33
 
31
34
  - Build a static Postgres execution stack from target, adapter, and driver descriptors
32
- - Build typed SQL and Kysely lane instances from the same execution context
35
+ - Build typed SQL and a build-only Kysely authoring surface from the same execution context
33
36
  - Build static schema and ORM roots from the execution context
34
37
  - Normalize runtime binding input (`binding`, `url`, `pg`)
35
- - Lazily instantiate runtime resources on first `db.runtime()` call
38
+ - Lazily instantiate runtime resources on first `db.runtime()` or `db.connect(...)` call
39
+ - Connect the internal Postgres driver through `db.connect(...)` or from initial binding options
36
40
  - Memoize runtime so repeated `db.runtime()` calls return one instance
37
41
 
38
42
  ## Dependencies
@@ -43,11 +47,10 @@ When URL binding is used, pool timeouts are configurable via `poolOptions`:
43
47
  - `@prisma-next/adapter-postgres` for adapter descriptor
44
48
  - `@prisma-next/driver-postgres` for driver descriptor
45
49
  - `@prisma-next/sql-lane` for `sql(...)`
46
- - `@prisma-next/integration-kysely` for `KyselyPrismaDialect` and contract-to-Kysely typing
50
+ - `@prisma-next/sql-kysely-lane` for contract-to-Kysely typing and build-only Kysely plan assembly
47
51
  - `@prisma-next/sql-relational-core` for `schema(...)`
48
52
  - `@prisma-next/sql-orm-client` for `orm(...)`
49
53
  - `@prisma-next/sql-contract` for `validateContract(...)` and contract types
50
- - `kysely` for query-builder API surface
51
54
  - `pg` for lazy `Pool` construction when using URL binding
52
55
 
53
56
  ## Architecture
@@ -55,7 +58,7 @@ When URL binding is used, pool timeouts are configurable via `poolOptions`:
55
58
  ```mermaid
56
59
  flowchart TD
57
60
  App[App Code] --> Client[postgres(...)]
58
- Client --> Static[Roots: sql kysely(runtime) schema orm context stack]
61
+ Client --> Static[Roots: sql kysely(build-only) schema orm context stack]
59
62
  Client --> Lazy[runtime()]
60
63
 
61
64
  Lazy --> Instantiate[instantiateExecutionStack]
@@ -1,9 +1,8 @@
1
- import { KyselifyContract } from "@prisma-next/integration-kysely";
1
+ import { KyselyQueryLane, KyselyQueryLane as KyselyQueryLane$1 } from "@prisma-next/sql-kysely-lane";
2
2
  import { SelectBuilder } from "@prisma-next/sql-lane";
3
3
  import { orm } from "@prisma-next/sql-orm-client";
4
4
  import { SchemaHandle } from "@prisma-next/sql-relational-core/schema";
5
5
  import { ExecutionContext, Plugin, Runtime, RuntimeVerifyOptions, SqlExecutionStackWithDriver, SqlRuntimeExtensionDescriptor } from "@prisma-next/sql-runtime";
6
- import { Kysely } from "kysely";
7
6
  import { Client, Pool } from "pg";
8
7
  import { ExtractCodecTypes, ExtractOperationTypes, SqlContract, SqlStorage } from "@prisma-next/sql-contract/types";
9
8
  import { OperationTypeSignature, OperationTypes } from "@prisma-next/sql-relational-core/types";
@@ -40,11 +39,12 @@ type PostgresTargetId = 'postgres';
40
39
  type OrmClient<TContract extends SqlContract<SqlStorage>> = ReturnType<typeof orm<TContract>>;
41
40
  interface PostgresClient<TContract extends SqlContract<SqlStorage>> {
42
41
  readonly sql: SelectBuilder<TContract, unknown, ExtractCodecTypes<TContract>, ExtractOperationTypes<TContract>>;
43
- kysely(runtime: Runtime): Kysely<KyselifyContract<TContract>>;
42
+ readonly kysely: KyselyQueryLane$1<TContract>;
44
43
  readonly schema: SchemaHandle<TContract, ExtractCodecTypes<TContract>, ToSchemaOperationTypes<ExtractOperationTypes<TContract>>>;
45
44
  readonly orm: OrmClient<TContract>;
46
45
  readonly context: ExecutionContext<TContract>;
47
46
  readonly stack: SqlExecutionStackWithDriver<PostgresTargetId>;
47
+ connect(bindingInput?: PostgresBindingInput): Promise<Runtime>;
48
48
  runtime(): Runtime;
49
49
  }
50
50
  interface PostgresOptionsBase<TContract extends SqlContract<SqlStorage>> {
@@ -56,11 +56,16 @@ interface PostgresOptionsBase<TContract extends SqlContract<SqlStorage>> {
56
56
  readonly idleTimeoutMillis?: number;
57
57
  };
58
58
  }
59
- type PostgresOptionsWithContract<TContract extends SqlContract<SqlStorage>> = PostgresBindingInput & PostgresOptionsBase<TContract> & {
59
+ interface PostgresBindingOptions {
60
+ readonly binding?: PostgresBinding;
61
+ readonly url?: string;
62
+ readonly pg?: Pool | Client;
63
+ }
64
+ type PostgresOptionsWithContract<TContract extends SqlContract<SqlStorage>> = PostgresBindingOptions & PostgresOptionsBase<TContract> & {
60
65
  readonly contract: TContract;
61
66
  readonly contractJson?: never;
62
67
  };
63
- type PostgresOptionsWithContractJson<TContract extends SqlContract<SqlStorage>> = PostgresBindingInput & PostgresOptionsBase<TContract> & {
68
+ type PostgresOptionsWithContractJson<TContract extends SqlContract<SqlStorage>> = PostgresBindingOptions & PostgresOptionsBase<TContract> & {
64
69
  readonly contractJson: unknown;
65
70
  readonly contract?: never;
66
71
  };
@@ -72,5 +77,5 @@ type PostgresOptions<TContract extends SqlContract<SqlStorage>> = PostgresOption
72
77
  declare function postgres<TContract extends SqlContract<SqlStorage>>(options: PostgresOptionsWithContract<TContract>): PostgresClient<TContract>;
73
78
  declare function postgres<TContract extends SqlContract<SqlStorage>>(options: PostgresOptionsWithContractJson<TContract>): PostgresClient<TContract>;
74
79
  //#endregion
75
- export { type PostgresBinding, type PostgresClient, type PostgresOptions, type PostgresOptionsBase, type PostgresOptionsWithContract, type PostgresOptionsWithContractJson, postgres as default };
80
+ export { type KyselyQueryLane, type PostgresBinding, type PostgresClient, type PostgresOptions, type PostgresOptionsBase, type PostgresOptionsWithContract, type PostgresOptionsWithContractJson, postgres as default };
76
81
  //# sourceMappingURL=runtime.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"runtime.d.mts","names":[],"sources":["../src/runtime/binding.ts","../src/runtime/postgres.ts"],"sourcesContent":[],"mappings":";;;;;;;;;;;KAGY,eAAA;;;;;iBAEkC;;;mBACI;;AAHtC,KAKA,oBAAA,GAHkC;EAGlC,SAAA,OAAA,EAEY,eAFQ;EAER,SAAA,GAAA,CAAA,EAAA,KAAA;EAUL,SAAA,EAAA,CAAA,EAAA,KAAA;CAAO,GAAA;EAAM,SAAA,GAAA,EAAA,MAAA;;;;ECkB3B,SAAA,EAAA,EDlBc,ICkBd,GDlBqB,MCkBE;EACT,SAAA,OAAA,CAAA,EAAA,KAAA;EACE,SAAA,GAAA,CAAA,EAAA,KAAA;CAAE;;;KAFlB,gDACc,iBDpCP,MCqCS,CDrCM,CCqCJ,MDnCuB,CAAA,GCmCb,CDnCa,CCmCX,MDlCe,CAAM,CCkCb,MDlCa,CAAA,SCkCG,sBDlCH,GCmChD,CDnCgD,CCmC9C,MDnC8C,CAAA,CCmCtC,MDnCsC,CAAA,GCoChD,sBDpCgD,EAE5C,EAEY;KCoCnB,sBD1Bc,CAAA,CAAA,CAAA,GC0Bc,CD1Bd,SC0BwB,cD1BxB,GC0ByC,CD1BzC,GC0B6C,uBD1B7C,CC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@ -1,14 +1,13 @@
1
1
  import postgresAdapter from "@prisma-next/adapter-postgres/runtime";
2
2
  import { instantiateExecutionStack } from "@prisma-next/core-execution-plane/stack";
3
3
  import postgresDriver from "@prisma-next/driver-postgres/runtime";
4
- import { KyselyPrismaDialect } from "@prisma-next/integration-kysely";
5
4
  import { validateContract } from "@prisma-next/sql-contract/validate";
5
+ import { createKyselyLane } from "@prisma-next/sql-kysely-lane";
6
6
  import { sql } from "@prisma-next/sql-lane";
7
7
  import { orm } from "@prisma-next/sql-orm-client";
8
8
  import { schema } from "@prisma-next/sql-relational-core/schema";
9
9
  import { createExecutionContext, createRuntime, createSqlExecutionStack } from "@prisma-next/sql-runtime";
10
10
  import postgresTarget from "@prisma-next/target-postgres/runtime";
11
- import { Kysely } from "kysely";
12
11
  import { Client, Pool } from "pg";
13
12
 
14
13
  //#region src/runtime/binding.ts
@@ -43,6 +42,10 @@ function resolvePostgresBinding(options) {
43
42
  };
44
43
  throw new Error("Unable to determine pg binding type from pg input; use binding with explicit kind");
45
44
  }
45
+ function resolveOptionalPostgresBinding(options) {
46
+ if (Number(options.binding !== void 0) + Number(options.url !== void 0) + Number(options.pg !== void 0) === 0) return;
47
+ return resolvePostgresBinding(options);
48
+ }
46
49
 
47
50
  //#endregion
48
51
  //#region src/runtime/postgres.ts
@@ -52,9 +55,20 @@ function hasContractJson(options) {
52
55
  function resolveContract(options) {
53
56
  return validateContract(hasContractJson(options) ? options.contractJson : options.contract);
54
57
  }
58
+ function toRuntimeBinding(binding, options) {
59
+ if (binding.kind !== "url") return binding;
60
+ return {
61
+ kind: "pgPool",
62
+ pool: new Pool({
63
+ connectionString: binding.url,
64
+ connectionTimeoutMillis: options.poolOptions?.connectionTimeoutMillis ?? 2e4,
65
+ idleTimeoutMillis: options.poolOptions?.idleTimeoutMillis ?? 3e4
66
+ })
67
+ };
68
+ }
55
69
  function postgres(options) {
56
70
  const contract = resolveContract(options);
57
- const binding = resolvePostgresBinding(options);
71
+ let binding = resolveOptionalPostgresBinding(options);
58
72
  const stack = createSqlExecutionStack({
59
73
  target: postgresTarget,
60
74
  adapter: postgresAdapter,
@@ -68,23 +82,38 @@ function postgres(options) {
68
82
  const schema$1 = schema(context);
69
83
  const sql$1 = sql({ context });
70
84
  let runtimeInstance;
85
+ let runtimeDriver;
86
+ let driverConnected = false;
87
+ let connectPromise;
88
+ let backgroundConnectError;
89
+ const connectDriver = async (resolvedBinding) => {
90
+ if (driverConnected) return;
91
+ if (!runtimeDriver) throw new Error("Postgres runtime driver missing");
92
+ if (connectPromise) return connectPromise;
93
+ const runtimeBinding = toRuntimeBinding(resolvedBinding, options);
94
+ connectPromise = runtimeDriver.connect(runtimeBinding).then(() => {
95
+ driverConnected = true;
96
+ }).catch(async (err) => {
97
+ backgroundConnectError = err;
98
+ connectPromise = void 0;
99
+ if (resolvedBinding.kind === "url" && runtimeBinding.kind === "pgPool") await runtimeBinding.pool.end().catch(() => void 0);
100
+ throw err;
101
+ });
102
+ return connectPromise;
103
+ };
71
104
  const getRuntime = () => {
105
+ if (backgroundConnectError !== void 0) throw backgroundConnectError;
72
106
  if (runtimeInstance) return runtimeInstance;
73
107
  const stackInstance = instantiateExecutionStack(stack);
74
108
  const driverDescriptor = stack.driver;
75
109
  if (!driverDescriptor) throw new Error("Driver descriptor missing from execution stack");
76
- const connect = binding.kind === "url" ? { pool: new Pool({
77
- connectionString: binding.url,
78
- connectionTimeoutMillis: options.poolOptions?.connectionTimeoutMillis ?? 2e4,
79
- idleTimeoutMillis: options.poolOptions?.idleTimeoutMillis ?? 3e4
80
- }) } : binding.kind === "pgPool" ? { pool: binding.pool } : { client: binding.client };
110
+ const driver = driverDescriptor.create({ cursor: { disabled: true } });
111
+ runtimeDriver = driver;
112
+ if (binding !== void 0) connectDriver(binding).catch(() => void 0);
81
113
  runtimeInstance = createRuntime({
82
114
  stackInstance,
83
115
  context,
84
- driver: driverDescriptor.create({
85
- connect,
86
- cursor: { disabled: true }
87
- }),
116
+ driver,
88
117
  verify: options.verify ?? {
89
118
  mode: "onFirstUse",
90
119
  requireMarker: false
@@ -93,28 +122,33 @@ function postgres(options) {
93
122
  });
94
123
  return runtimeInstance;
95
124
  };
125
+ const orm$1 = orm({
126
+ contract,
127
+ runtime: {
128
+ execute(plan) {
129
+ return getRuntime().execute(plan);
130
+ },
131
+ connection() {
132
+ return getRuntime().connection();
133
+ }
134
+ }
135
+ });
96
136
  return {
97
137
  sql: sql$1,
98
- kysely(runtime) {
99
- return new Kysely({ dialect: new KyselyPrismaDialect({
100
- runtime,
101
- contract
102
- }) });
103
- },
138
+ kysely: createKyselyLane(contract),
104
139
  schema: schema$1,
105
- orm: orm({
106
- contract,
107
- runtime: {
108
- execute(plan) {
109
- return getRuntime().execute(plan);
110
- },
111
- connection() {
112
- return getRuntime().connection();
113
- }
114
- }
115
- }),
140
+ orm: orm$1,
116
141
  context,
117
142
  stack,
143
+ async connect(bindingInput) {
144
+ if (driverConnected || connectPromise) throw new Error("Postgres client already connected");
145
+ if (bindingInput !== void 0) binding = resolvePostgresBinding(bindingInput);
146
+ if (binding === void 0) throw new Error("Postgres binding not configured. Pass url/pg/binding to postgres(...) or call db.connect({ ... }).");
147
+ const runtime = getRuntime();
148
+ if (driverConnected) return runtime;
149
+ await connectDriver(binding);
150
+ return runtime;
151
+ },
118
152
  runtime() {
119
153
  return getRuntime();
120
154
  }
@@ -1 +1 @@
1
- {"version":3,"file":"runtime.mjs","names":["parsed: URL","PgPool","PgClient","schema: PostgresClient<TContract>['schema']","schemaBuilder","sql","sqlBuilder","runtimeInstance: Runtime | undefined","ormBuilder"],"sources":["../src/runtime/binding.ts","../src/runtime/postgres.ts"],"sourcesContent":["import type { Client, Pool } from 'pg';\nimport { Client as PgClient, Pool as PgPool } from 'pg';\n\nexport type PostgresBinding =\n | { readonly kind: 'url'; readonly url: string }\n | { readonly kind: 'pgPool'; readonly pool: Pool }\n | { readonly kind: 'pgClient'; readonly client: Client };\n\nexport type PostgresBindingInput =\n | {\n readonly binding: PostgresBinding;\n readonly url?: never;\n readonly pg?: never;\n }\n | {\n readonly url: string;\n readonly binding?: never;\n readonly pg?: never;\n }\n | {\n readonly pg: Pool | Client;\n readonly binding?: never;\n readonly url?: never;\n };\n\nfunction validatePostgresUrl(url: string): string {\n const trimmed = url.trim();\n if (trimmed.length === 0) {\n throw new Error('Postgres URL must be a non-empty string');\n }\n\n let parsed: URL;\n try {\n parsed = new URL(trimmed);\n } catch {\n throw new Error('Postgres URL must be a valid URL');\n }\n\n if (parsed.protocol !== 'postgres:' && parsed.protocol !== 'postgresql:') {\n throw new Error('Postgres URL must use postgres:// or postgresql://');\n }\n\n return trimmed;\n}\n\nexport function resolvePostgresBinding(options: PostgresBindingInput): PostgresBinding {\n const providedCount =\n Number(options.binding !== undefined) +\n Number(options.url !== undefined) +\n Number(options.pg !== undefined);\n\n if (providedCount !== 1) {\n throw new Error('Provide one binding input: binding, url, or pg');\n }\n\n if (options.binding !== undefined) {\n return options.binding;\n }\n\n if (options.url !== undefined) {\n return { kind: 'url', url: validatePostgresUrl(options.url) };\n }\n\n const pgBinding = options.pg;\n if (pgBinding === undefined) {\n throw new Error('Invariant violation: expected pg binding after validation');\n }\n\n if (pgBinding instanceof PgPool) {\n return { kind: 'pgPool', pool: pgBinding };\n }\n\n if (pgBinding instanceof PgClient) {\n return { kind: 'pgClient', client: pgBinding };\n }\n\n throw new Error(\n 'Unable to determine pg binding type from pg input; use binding with explicit kind',\n );\n}\n","import postgresAdapter from '@prisma-next/adapter-postgres/runtime';\nimport { instantiateExecutionStack } from '@prisma-next/core-execution-plane/stack';\nimport postgresDriver from '@prisma-next/driver-postgres/runtime';\nimport { type KyselifyContract, KyselyPrismaDialect } from '@prisma-next/integration-kysely';\nimport type {\n ExtractCodecTypes,\n ExtractOperationTypes,\n SqlContract,\n SqlStorage,\n} from '@prisma-next/sql-contract/types';\nimport { validateContract } from '@prisma-next/sql-contract/validate';\nimport type { SelectBuilder } from '@prisma-next/sql-lane';\nimport { sql as sqlBuilder } from '@prisma-next/sql-lane';\nimport { orm as ormBuilder } from '@prisma-next/sql-orm-client';\nimport type { SchemaHandle } from '@prisma-next/sql-relational-core/schema';\nimport { schema as schemaBuilder } from '@prisma-next/sql-relational-core/schema';\nimport type {\n OperationTypeSignature,\n OperationTypes,\n} from '@prisma-next/sql-relational-core/types';\nimport type {\n ExecutionContext,\n Plugin,\n Runtime,\n RuntimeVerifyOptions,\n SqlExecutionStackWithDriver,\n SqlRuntimeExtensionDescriptor,\n} from '@prisma-next/sql-runtime';\nimport {\n createExecutionContext,\n createRuntime,\n createSqlExecutionStack,\n} from '@prisma-next/sql-runtime';\nimport postgresTarget from '@prisma-next/target-postgres/runtime';\nimport { Kysely } from 'kysely';\nimport { Pool } from 'pg';\nimport { type PostgresBindingInput, resolvePostgresBinding } from './binding';\n\ntype NormalizeOperationTypes<T> = {\n [TypeId in keyof T]: {\n [Method in keyof T[TypeId]]: T[TypeId][Method] extends OperationTypeSignature\n ? T[TypeId][Method]\n : OperationTypeSignature;\n };\n};\n\ntype ToSchemaOperationTypes<T> = T extends OperationTypes ? T : NormalizeOperationTypes<T>;\n\nexport type PostgresTargetId = 'postgres';\ntype OrmClient<TContract extends SqlContract<SqlStorage>> = ReturnType<\n typeof ormBuilder<TContract>\n>;\n\nexport interface PostgresClient<TContract extends SqlContract<SqlStorage>> {\n readonly sql: SelectBuilder<\n TContract,\n unknown,\n ExtractCodecTypes<TContract>,\n ExtractOperationTypes<TContract>\n >;\n kysely(runtime: Runtime): Kysely<KyselifyContract<TContract>>;\n readonly schema: SchemaHandle<\n TContract,\n ExtractCodecTypes<TContract>,\n ToSchemaOperationTypes<ExtractOperationTypes<TContract>>\n >;\n readonly orm: OrmClient<TContract>;\n readonly context: ExecutionContext<TContract>;\n readonly stack: SqlExecutionStackWithDriver<PostgresTargetId>;\n runtime(): Runtime;\n}\n\nexport interface PostgresOptionsBase<TContract extends SqlContract<SqlStorage>> {\n readonly extensions?: readonly SqlRuntimeExtensionDescriptor<PostgresTargetId>[];\n readonly plugins?: readonly Plugin<TContract>[];\n readonly verify?: RuntimeVerifyOptions;\n readonly poolOptions?: {\n readonly connectionTimeoutMillis?: number;\n readonly idleTimeoutMillis?: number;\n };\n}\n\nexport type PostgresOptionsWithContract<TContract extends SqlContract<SqlStorage>> =\n PostgresBindingInput &\n PostgresOptionsBase<TContract> & {\n readonly contract: TContract;\n readonly contractJson?: never;\n };\n\nexport type PostgresOptionsWithContractJson<TContract extends SqlContract<SqlStorage>> =\n PostgresBindingInput &\n PostgresOptionsBase<TContract> & {\n readonly contractJson: unknown;\n readonly contract?: never;\n };\n\nexport type PostgresOptions<TContract extends SqlContract<SqlStorage>> =\n | PostgresOptionsWithContract<TContract>\n | PostgresOptionsWithContractJson<TContract>;\n\nfunction hasContractJson<TContract extends SqlContract<SqlStorage>>(\n options: PostgresOptions<TContract>,\n): options is PostgresOptionsWithContractJson<TContract> {\n return 'contractJson' in options;\n}\n\nfunction resolveContract<TContract extends SqlContract<SqlStorage>>(\n options: PostgresOptions<TContract>,\n): TContract {\n const contractInput = hasContractJson(options) ? options.contractJson : options.contract;\n return validateContract<TContract>(contractInput);\n}\n\n/**\n * Creates a lazy Postgres client from either `contractJson` or a TypeScript-authored `contract`.\n * Static query surfaces are available immediately, while `runtime()` instantiates the driver/pool on first call.\n */\nexport default function postgres<TContract extends SqlContract<SqlStorage>>(\n options: PostgresOptionsWithContract<TContract>,\n): PostgresClient<TContract>;\nexport default function postgres<TContract extends SqlContract<SqlStorage>>(\n options: PostgresOptionsWithContractJson<TContract>,\n): PostgresClient<TContract>;\nexport default function postgres<TContract extends SqlContract<SqlStorage>>(\n options: PostgresOptions<TContract>,\n): PostgresClient<TContract> {\n const contract = resolveContract(options);\n const binding = resolvePostgresBinding(options);\n const stack = createSqlExecutionStack({\n target: postgresTarget,\n adapter: postgresAdapter,\n driver: postgresDriver,\n extensionPacks: options.extensions ?? [],\n });\n\n const context = createExecutionContext({\n contract,\n stack,\n });\n\n const schema: PostgresClient<TContract>['schema'] = schemaBuilder(context);\n const sql = sqlBuilder({ context });\n let runtimeInstance: Runtime | undefined;\n const getRuntime = (): Runtime => {\n if (runtimeInstance) {\n return runtimeInstance;\n }\n\n const stackInstance = instantiateExecutionStack(stack);\n const driverDescriptor = stack.driver;\n if (!driverDescriptor) {\n throw new Error('Driver descriptor missing from execution stack');\n }\n\n const connect =\n binding.kind === 'url'\n ? {\n pool: new Pool({\n connectionString: binding.url,\n connectionTimeoutMillis: options.poolOptions?.connectionTimeoutMillis ?? 20_000,\n idleTimeoutMillis: options.poolOptions?.idleTimeoutMillis ?? 30_000,\n }),\n }\n : binding.kind === 'pgPool'\n ? { pool: binding.pool }\n : { client: binding.client };\n\n const driver = driverDescriptor.create({\n connect,\n cursor: { disabled: true },\n });\n\n runtimeInstance = createRuntime({\n stackInstance,\n context,\n driver,\n verify: options.verify ?? { mode: 'onFirstUse', requireMarker: false },\n ...(options.plugins ? { plugins: options.plugins } : {}),\n });\n\n return runtimeInstance;\n };\n const orm: OrmClient<TContract> = ormBuilder({\n contract,\n runtime: {\n execute(plan) {\n return getRuntime().execute(plan);\n },\n connection() {\n return getRuntime().connection();\n },\n },\n });\n\n return {\n sql,\n kysely(runtime: Runtime) {\n return new Kysely<KyselifyContract<TContract>>({\n dialect: new KyselyPrismaDialect({ runtime, contract }),\n });\n },\n schema,\n orm,\n context,\n stack,\n runtime() {\n return getRuntime();\n },\n };\n}\n"],"mappings":";;;;;;;;;;;;;;AAyBA,SAAS,oBAAoB,KAAqB;CAChD,MAAM,UAAU,IAAI,MAAM;AAC1B,KAAI,QAAQ,WAAW,EACrB,OAAM,IAAI,MAAM,0CAA0C;CAG5D,IAAIA;AACJ,KAAI;AACF,WAAS,IAAI,IAAI,QAAQ;SACnB;AACN,QAAM,IAAI,MAAM,mCAAmC;;AAGrD,KAAI,OAAO,aAAa,eAAe,OAAO,aAAa,cACzD,OAAM,IAAI,MAAM,qDAAqD;AAGvE,QAAO;;AAGT,SAAgB,uBAAuB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AK;GACb,kBAAkB,QAAQ;GAC1B,yBAAyB,QAAQ,aAAa,2BAA2B;GACzE,mBAAmB,QAAQ,aAAa,qBAAqB;GAC9D,CAAC,EACH,GACD,QAAQ,SAAS,WACf,EAAE,MAAM,QAAQ,MAAM,GACtB,EAAE,QAAQ,QAAQ,QAAQ;AAOlC,oBAAkB,cAAc;GAC9B;GACA;GACA,QARa,iBAAiB,OAAO;IACrC;IACA,QAAQ,EAAE,UAAU,MAAM;IAC3B,CAAC;GAMA,QAAQ,QAAQ,UAAU;IAAE,MAAM;IAAc,eAAe;IAAO;GACtE,GAAI,QAAQ,UAAU,EAAE,SAAS,QAAQ,SAAS,GAAG,EAAE;GACxD,CAAC;AAEF,SAAO;;AAcT,QAAO;EACL;EACA,OAAO,SAAkB;AACvB,UAAO,IAAI,OAAoC,EAC7C,SAAS,IAAI,oBAAoB;IAAE;IAAS;IAAU,CAAC,EACxD,CAAC;;EAEJ;EACA,KApBgCC,IAAW;GAC3C;GACA,SAAS;IACP,QAAQ,MAAM;AACZ,YAAO,YAAY,CAAC,QAAQ,KAAK;;IAEnC,aAAa;AACX,YAAO,YAAY,CAAC,YAAY;;IAEnC;GACF,CAAC;EAWA;EACA;EACA,UAAU;AACR,UAAO,YAAY;;EAEtB"}
1
+ {"version":3,"file":"runtime.mjs","names":["parsed: URL","PgPool","PgClient","schema: PostgresClient<TContract>['schema']","schemaBuilder","sql","sqlBuilder","runtimeInstance: Runtime | undefined","runtimeDriver: { connect(binding: unknown): Promise<void> } | undefined","connectPromise: Promise<void> | undefined","backgroundConnectError: unknown","orm: OrmClient<TContract>","ormBuilder"],"sources":["../src/runtime/binding.ts","../src/runtime/postgres.ts"],"sourcesContent":["import type { Client, Pool } from 'pg';\nimport { Client as PgClient, Pool as PgPool } from 'pg';\n\nexport type PostgresBinding =\n | { readonly kind: 'url'; readonly url: string }\n | { readonly kind: 'pgPool'; readonly pool: Pool }\n | { readonly kind: 'pgClient'; readonly client: Client };\n\nexport type PostgresBindingInput =\n | {\n readonly binding: PostgresBinding;\n readonly url?: never;\n readonly pg?: never;\n }\n | {\n readonly url: string;\n readonly binding?: never;\n readonly pg?: never;\n }\n | {\n readonly pg: Pool | Client;\n readonly binding?: never;\n readonly url?: never;\n };\n\ntype PostgresBindingFields = {\n readonly binding?: PostgresBinding;\n readonly url?: string;\n readonly pg?: Pool | Client;\n};\n\nfunction validatePostgresUrl(url: string): string {\n const trimmed = url.trim();\n if (trimmed.length === 0) {\n throw new Error('Postgres URL must be a non-empty string');\n }\n\n let parsed: URL;\n try {\n parsed = new URL(trimmed);\n } catch {\n throw new Error('Postgres URL must be a valid URL');\n }\n\n if (parsed.protocol !== 'postgres:' && parsed.protocol !== 'postgresql:') {\n throw new Error('Postgres URL must use postgres:// or postgresql://');\n }\n\n return trimmed;\n}\n\nexport function resolvePostgresBinding(options: PostgresBindingInput): PostgresBinding {\n const providedCount =\n Number(options.binding !== undefined) +\n Number(options.url !== undefined) +\n Number(options.pg !== undefined);\n\n if (providedCount !== 1) {\n throw new Error('Provide one binding input: binding, url, or pg');\n }\n\n if (options.binding !== undefined) {\n return options.binding;\n }\n\n if (options.url !== undefined) {\n return { kind: 'url', url: validatePostgresUrl(options.url) };\n }\n\n const pgBinding = options.pg;\n if (pgBinding === undefined) {\n throw new Error('Invariant violation: expected pg binding after validation');\n }\n\n if (pgBinding instanceof PgPool) {\n return { kind: 'pgPool', pool: pgBinding };\n }\n\n if (pgBinding instanceof PgClient) {\n return { kind: 'pgClient', client: pgBinding };\n }\n\n throw new Error(\n 'Unable to determine pg binding type from pg input; use binding with explicit kind',\n );\n}\n\nexport function resolveOptionalPostgresBinding(\n options: PostgresBindingFields,\n): PostgresBinding | undefined {\n const providedCount =\n Number(options.binding !== undefined) +\n Number(options.url !== undefined) +\n Number(options.pg !== undefined);\n\n if (providedCount === 0) {\n return undefined;\n }\n\n return resolvePostgresBinding(options as PostgresBindingInput);\n}\n","import postgresAdapter from '@prisma-next/adapter-postgres/runtime';\nimport { instantiateExecutionStack } from '@prisma-next/core-execution-plane/stack';\nimport postgresDriver from '@prisma-next/driver-postgres/runtime';\nimport type {\n ExtractCodecTypes,\n ExtractOperationTypes,\n SqlContract,\n SqlStorage,\n} from '@prisma-next/sql-contract/types';\nimport { validateContract } from '@prisma-next/sql-contract/validate';\nimport { createKyselyLane, type KyselyQueryLane } from '@prisma-next/sql-kysely-lane';\nimport type { SelectBuilder } from '@prisma-next/sql-lane';\nimport { sql as sqlBuilder } from '@prisma-next/sql-lane';\nimport { orm as ormBuilder } from '@prisma-next/sql-orm-client';\nimport type { SchemaHandle } from '@prisma-next/sql-relational-core/schema';\nimport { schema as schemaBuilder } from '@prisma-next/sql-relational-core/schema';\nimport type {\n OperationTypeSignature,\n OperationTypes,\n} from '@prisma-next/sql-relational-core/types';\nimport type {\n ExecutionContext,\n Plugin,\n Runtime,\n RuntimeVerifyOptions,\n SqlExecutionStackWithDriver,\n SqlRuntimeExtensionDescriptor,\n} from '@prisma-next/sql-runtime';\nimport {\n createExecutionContext,\n createRuntime,\n createSqlExecutionStack,\n} from '@prisma-next/sql-runtime';\nimport postgresTarget from '@prisma-next/target-postgres/runtime';\nimport { type Client, Pool } from 'pg';\nimport {\n type PostgresBinding,\n type PostgresBindingInput,\n resolveOptionalPostgresBinding,\n resolvePostgresBinding,\n} from './binding';\n\ntype NormalizeOperationTypes<T> = {\n [TypeId in keyof T]: {\n [Method in keyof T[TypeId]]: T[TypeId][Method] extends OperationTypeSignature\n ? T[TypeId][Method]\n : OperationTypeSignature;\n };\n};\n\ntype ToSchemaOperationTypes<T> = T extends OperationTypes ? T : NormalizeOperationTypes<T>;\n\nexport type PostgresTargetId = 'postgres';\ntype OrmClient<TContract extends SqlContract<SqlStorage>> = ReturnType<\n typeof ormBuilder<TContract>\n>;\n\nexport interface PostgresClient<TContract extends SqlContract<SqlStorage>> {\n readonly sql: SelectBuilder<\n TContract,\n unknown,\n ExtractCodecTypes<TContract>,\n ExtractOperationTypes<TContract>\n >;\n readonly kysely: KyselyQueryLane<TContract>;\n readonly schema: SchemaHandle<\n TContract,\n ExtractCodecTypes<TContract>,\n ToSchemaOperationTypes<ExtractOperationTypes<TContract>>\n >;\n readonly orm: OrmClient<TContract>;\n readonly context: ExecutionContext<TContract>;\n readonly stack: SqlExecutionStackWithDriver<PostgresTargetId>;\n connect(bindingInput?: PostgresBindingInput): Promise<Runtime>;\n runtime(): Runtime;\n}\n\nexport interface PostgresOptionsBase<TContract extends SqlContract<SqlStorage>> {\n readonly extensions?: readonly SqlRuntimeExtensionDescriptor<PostgresTargetId>[];\n readonly plugins?: readonly Plugin<TContract>[];\n readonly verify?: RuntimeVerifyOptions;\n readonly poolOptions?: {\n readonly connectionTimeoutMillis?: number;\n readonly idleTimeoutMillis?: number;\n };\n}\n\nexport interface PostgresBindingOptions {\n readonly binding?: PostgresBinding;\n readonly url?: string;\n readonly pg?: Pool | Client;\n}\n\nexport type PostgresOptionsWithContract<TContract extends SqlContract<SqlStorage>> =\n PostgresBindingOptions &\n PostgresOptionsBase<TContract> & {\n readonly contract: TContract;\n readonly contractJson?: never;\n };\n\nexport type PostgresOptionsWithContractJson<TContract extends SqlContract<SqlStorage>> =\n PostgresBindingOptions &\n PostgresOptionsBase<TContract> & {\n readonly contractJson: unknown;\n readonly contract?: never;\n };\n\nexport type PostgresOptions<TContract extends SqlContract<SqlStorage>> =\n | PostgresOptionsWithContract<TContract>\n | PostgresOptionsWithContractJson<TContract>;\n\nfunction hasContractJson<TContract extends SqlContract<SqlStorage>>(\n options: PostgresOptions<TContract>,\n): options is PostgresOptionsWithContractJson<TContract> {\n return 'contractJson' in options;\n}\n\nfunction resolveContract<TContract extends SqlContract<SqlStorage>>(\n options: PostgresOptions<TContract>,\n): TContract {\n const contractInput = hasContractJson(options) ? options.contractJson : options.contract;\n return validateContract<TContract>(contractInput);\n}\n\nfunction toRuntimeBinding<TContract extends SqlContract<SqlStorage>>(\n binding: PostgresBinding,\n options: PostgresOptions<TContract>,\n) {\n if (binding.kind !== 'url') {\n return binding;\n }\n\n return {\n kind: 'pgPool',\n pool: new Pool({\n connectionString: binding.url,\n connectionTimeoutMillis: options.poolOptions?.connectionTimeoutMillis ?? 20_000,\n idleTimeoutMillis: options.poolOptions?.idleTimeoutMillis ?? 30_000,\n }),\n } as const;\n}\n\n/**\n * Creates a lazy Postgres client from either `contractJson` or a TypeScript-authored `contract`.\n * Static query surfaces are available immediately, while `runtime()` instantiates the driver/pool on first call.\n */\nexport default function postgres<TContract extends SqlContract<SqlStorage>>(\n options: PostgresOptionsWithContract<TContract>,\n): PostgresClient<TContract>;\nexport default function postgres<TContract extends SqlContract<SqlStorage>>(\n options: PostgresOptionsWithContractJson<TContract>,\n): PostgresClient<TContract>;\nexport default function postgres<TContract extends SqlContract<SqlStorage>>(\n options: PostgresOptions<TContract>,\n): PostgresClient<TContract> {\n const contract = resolveContract(options);\n let binding = resolveOptionalPostgresBinding(options);\n const stack = createSqlExecutionStack({\n target: postgresTarget,\n adapter: postgresAdapter,\n driver: postgresDriver,\n extensionPacks: options.extensions ?? [],\n });\n\n const context = createExecutionContext({\n contract,\n stack,\n });\n\n const schema: PostgresClient<TContract>['schema'] = schemaBuilder(context);\n const sql = sqlBuilder({ context });\n let runtimeInstance: Runtime | undefined;\n let runtimeDriver: { connect(binding: unknown): Promise<void> } | undefined;\n let driverConnected = false;\n let connectPromise: Promise<void> | undefined;\n let backgroundConnectError: unknown;\n const connectDriver = async (resolvedBinding: PostgresBinding): Promise<void> => {\n if (driverConnected) return;\n if (!runtimeDriver) throw new Error('Postgres runtime driver missing');\n if (connectPromise) return connectPromise;\n const runtimeBinding = toRuntimeBinding(resolvedBinding, options);\n connectPromise = runtimeDriver\n .connect(runtimeBinding)\n .then(() => {\n driverConnected = true;\n })\n .catch(async (err) => {\n backgroundConnectError = err;\n connectPromise = undefined;\n if (resolvedBinding.kind === 'url' && runtimeBinding.kind === 'pgPool') {\n await runtimeBinding.pool.end().catch(() => undefined);\n }\n throw err;\n });\n return connectPromise;\n };\n const getRuntime = (): Runtime => {\n if (backgroundConnectError !== undefined) {\n throw backgroundConnectError;\n }\n\n if (runtimeInstance) {\n return runtimeInstance;\n }\n\n const stackInstance = instantiateExecutionStack(stack);\n const driverDescriptor = stack.driver;\n if (!driverDescriptor) {\n throw new Error('Driver descriptor missing from execution stack');\n }\n\n const driver = driverDescriptor.create({\n cursor: { disabled: true },\n });\n runtimeDriver = driver;\n if (binding !== undefined) {\n void connectDriver(binding).catch(() => undefined);\n }\n\n runtimeInstance = createRuntime({\n stackInstance,\n context,\n driver,\n verify: options.verify ?? { mode: 'onFirstUse', requireMarker: false },\n ...(options.plugins ? { plugins: options.plugins } : {}),\n });\n\n return runtimeInstance;\n };\n const orm: OrmClient<TContract> = ormBuilder({\n contract,\n runtime: {\n execute(plan) {\n return getRuntime().execute(plan);\n },\n connection() {\n return getRuntime().connection();\n },\n },\n });\n\n return {\n sql,\n kysely: createKyselyLane(contract),\n schema,\n orm,\n context,\n stack,\n async connect(bindingInput) {\n if (driverConnected || connectPromise) {\n throw new Error('Postgres client already connected');\n }\n\n if (bindingInput !== undefined) {\n binding = resolvePostgresBinding(bindingInput);\n }\n\n if (binding === undefined) {\n throw new Error(\n 'Postgres binding not configured. Pass url/pg/binding to postgres(...) or call db.connect({ ... }).',\n );\n }\n\n const runtime = getRuntime();\n if (driverConnected) {\n return runtime;\n }\n\n await connectDriver(binding);\n return runtime;\n },\n runtime() {\n return getRuntime();\n },\n };\n}\n"],"mappings":";;;;;;;;;;;;;AA+BA,SAAS,oBAAoB,KAAqB;CAChD,MAAM,UAAU,IAAI,MAAM;AAC1B,KAAI,QAAQ,WAAW,EACrB,OAAM,IAAI,MAAM,0CAA0C;CAG5D,IAAIA;AACJ,KAAI;AACF,WAAS,IAAI,IAAI,QAAQ;SACnB;AACN,QAAM,IAAI,MAAM,mCAAmC;;AAGrD,KAAI,OAAO,aAAa,eAAe,OAAO,aAAa,cACzD,OAAM,IAAI,MAAM,qDAAqD;AAGvE,QAA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EAC3C;EACA,SAAS;GACP,QAAQ,MAAM;AACZ,WAAO,YAAY,CAAC,QAAQ,KAAK;;GAEnC,aAAa;AACX,WAAO,YAAY,CAAC,YAAY;;GAEnC;EACF,CAAC;AAEF,QAAO;EACL;EACA,QAAQ,iBAAiB,SAAS;EAClC;EACA;EACA;EACA;EACA,MAAM,QAAQ,cAAc;AAC1B,OAAI,mBAAmB,eACrB,OAAM,IAAI,MAAM,oCAAoC;AAGtD,OAAI,iBAAiB,OACnB,WAAU,uBAAuB,aAAa;AAGhD,OAAI,YAAY,OACd,OAAM,IAAI,MACR,qGACD;GAGH,MAAM,UAAU,YAAY;AAC5B,OAAI,gBACF,QAAO;AAGT,SAAM,cAAc,QAAQ;AAC5B,UAAO;;EAET,UAAU;AACR,UAAO,YAAY;;EAEtB"}
package/package.json CHANGED
@@ -1,22 +1,21 @@
1
1
  {
2
2
  "name": "@prisma-next/postgres",
3
- "version": "0.3.0-dev.44",
3
+ "version": "0.3.0-dev.52",
4
4
  "type": "module",
5
5
  "sideEffects": false,
6
6
  "description": "One-liner lazy Postgres client composition for Prisma Next",
7
7
  "dependencies": {
8
- "kysely": "0.28.10",
9
8
  "pg": "8.16.3",
10
- "@prisma-next/core-execution-plane": "0.3.0-dev.44",
11
- "@prisma-next/driver-postgres": "0.3.0-dev.44",
12
- "@prisma-next/integration-kysely": "0.3.0-dev.44",
13
- "@prisma-next/sql-contract": "0.3.0-dev.44",
14
- "@prisma-next/sql-lane": "0.3.0-dev.44",
15
- "@prisma-next/sql-orm-client": "0.3.0-dev.44",
16
- "@prisma-next/sql-relational-core": "0.3.0-dev.44",
17
- "@prisma-next/sql-runtime": "0.3.0-dev.44",
18
- "@prisma-next/target-postgres": "0.3.0-dev.44",
19
- "@prisma-next/adapter-postgres": "0.3.0-dev.44"
9
+ "@prisma-next/adapter-postgres": "0.3.0-dev.52",
10
+ "@prisma-next/core-execution-plane": "0.3.0-dev.52",
11
+ "@prisma-next/driver-postgres": "0.3.0-dev.52",
12
+ "@prisma-next/sql-contract": "0.3.0-dev.52",
13
+ "@prisma-next/sql-kysely-lane": "0.3.0-dev.52",
14
+ "@prisma-next/sql-lane": "0.3.0-dev.52",
15
+ "@prisma-next/sql-orm-client": "0.3.0-dev.52",
16
+ "@prisma-next/sql-relational-core": "0.3.0-dev.52",
17
+ "@prisma-next/sql-runtime": "0.3.0-dev.52",
18
+ "@prisma-next/target-postgres": "0.3.0-dev.52"
20
19
  },
21
20
  "devDependencies": {
22
21
  "@types/pg": "8.16.0",
@@ -1,3 +1,4 @@
1
+ export type { KyselyQueryLane } from '@prisma-next/sql-kysely-lane';
1
2
  export type { PostgresBinding } from '../runtime/binding';
2
3
  export type {
3
4
  PostgresClient,
@@ -23,6 +23,12 @@ export type PostgresBindingInput =
23
23
  readonly url?: never;
24
24
  };
25
25
 
26
+ type PostgresBindingFields = {
27
+ readonly binding?: PostgresBinding;
28
+ readonly url?: string;
29
+ readonly pg?: Pool | Client;
30
+ };
31
+
26
32
  function validatePostgresUrl(url: string): string {
27
33
  const trimmed = url.trim();
28
34
  if (trimmed.length === 0) {
@@ -78,3 +84,18 @@ export function resolvePostgresBinding(options: PostgresBindingInput): PostgresB
78
84
  'Unable to determine pg binding type from pg input; use binding with explicit kind',
79
85
  );
80
86
  }
87
+
88
+ export function resolveOptionalPostgresBinding(
89
+ options: PostgresBindingFields,
90
+ ): PostgresBinding | undefined {
91
+ const providedCount =
92
+ Number(options.binding !== undefined) +
93
+ Number(options.url !== undefined) +
94
+ Number(options.pg !== undefined);
95
+
96
+ if (providedCount === 0) {
97
+ return undefined;
98
+ }
99
+
100
+ return resolvePostgresBinding(options as PostgresBindingInput);
101
+ }
@@ -1,7 +1,6 @@
1
1
  import postgresAdapter from '@prisma-next/adapter-postgres/runtime';
2
2
  import { instantiateExecutionStack } from '@prisma-next/core-execution-plane/stack';
3
3
  import postgresDriver from '@prisma-next/driver-postgres/runtime';
4
- import { type KyselifyContract, KyselyPrismaDialect } from '@prisma-next/integration-kysely';
5
4
  import type {
6
5
  ExtractCodecTypes,
7
6
  ExtractOperationTypes,
@@ -9,6 +8,7 @@ import type {
9
8
  SqlStorage,
10
9
  } from '@prisma-next/sql-contract/types';
11
10
  import { validateContract } from '@prisma-next/sql-contract/validate';
11
+ import { createKyselyLane, type KyselyQueryLane } from '@prisma-next/sql-kysely-lane';
12
12
  import type { SelectBuilder } from '@prisma-next/sql-lane';
13
13
  import { sql as sqlBuilder } from '@prisma-next/sql-lane';
14
14
  import { orm as ormBuilder } from '@prisma-next/sql-orm-client';
@@ -32,9 +32,13 @@ import {
32
32
  createSqlExecutionStack,
33
33
  } from '@prisma-next/sql-runtime';
34
34
  import postgresTarget from '@prisma-next/target-postgres/runtime';
35
- import { Kysely } from 'kysely';
36
- import { Pool } from 'pg';
37
- import { type PostgresBindingInput, resolvePostgresBinding } from './binding';
35
+ import { type Client, Pool } from 'pg';
36
+ import {
37
+ type PostgresBinding,
38
+ type PostgresBindingInput,
39
+ resolveOptionalPostgresBinding,
40
+ resolvePostgresBinding,
41
+ } from './binding';
38
42
 
39
43
  type NormalizeOperationTypes<T> = {
40
44
  [TypeId in keyof T]: {
@@ -58,7 +62,7 @@ export interface PostgresClient<TContract extends SqlContract<SqlStorage>> {
58
62
  ExtractCodecTypes<TContract>,
59
63
  ExtractOperationTypes<TContract>
60
64
  >;
61
- kysely(runtime: Runtime): Kysely<KyselifyContract<TContract>>;
65
+ readonly kysely: KyselyQueryLane<TContract>;
62
66
  readonly schema: SchemaHandle<
63
67
  TContract,
64
68
  ExtractCodecTypes<TContract>,
@@ -67,6 +71,7 @@ export interface PostgresClient<TContract extends SqlContract<SqlStorage>> {
67
71
  readonly orm: OrmClient<TContract>;
68
72
  readonly context: ExecutionContext<TContract>;
69
73
  readonly stack: SqlExecutionStackWithDriver<PostgresTargetId>;
74
+ connect(bindingInput?: PostgresBindingInput): Promise<Runtime>;
70
75
  runtime(): Runtime;
71
76
  }
72
77
 
@@ -80,15 +85,21 @@ export interface PostgresOptionsBase<TContract extends SqlContract<SqlStorage>>
80
85
  };
81
86
  }
82
87
 
88
+ export interface PostgresBindingOptions {
89
+ readonly binding?: PostgresBinding;
90
+ readonly url?: string;
91
+ readonly pg?: Pool | Client;
92
+ }
93
+
83
94
  export type PostgresOptionsWithContract<TContract extends SqlContract<SqlStorage>> =
84
- PostgresBindingInput &
95
+ PostgresBindingOptions &
85
96
  PostgresOptionsBase<TContract> & {
86
97
  readonly contract: TContract;
87
98
  readonly contractJson?: never;
88
99
  };
89
100
 
90
101
  export type PostgresOptionsWithContractJson<TContract extends SqlContract<SqlStorage>> =
91
- PostgresBindingInput &
102
+ PostgresBindingOptions &
92
103
  PostgresOptionsBase<TContract> & {
93
104
  readonly contractJson: unknown;
94
105
  readonly contract?: never;
@@ -111,6 +122,24 @@ function resolveContract<TContract extends SqlContract<SqlStorage>>(
111
122
  return validateContract<TContract>(contractInput);
112
123
  }
113
124
 
125
+ function toRuntimeBinding<TContract extends SqlContract<SqlStorage>>(
126
+ binding: PostgresBinding,
127
+ options: PostgresOptions<TContract>,
128
+ ) {
129
+ if (binding.kind !== 'url') {
130
+ return binding;
131
+ }
132
+
133
+ return {
134
+ kind: 'pgPool',
135
+ pool: new Pool({
136
+ connectionString: binding.url,
137
+ connectionTimeoutMillis: options.poolOptions?.connectionTimeoutMillis ?? 20_000,
138
+ idleTimeoutMillis: options.poolOptions?.idleTimeoutMillis ?? 30_000,
139
+ }),
140
+ } as const;
141
+ }
142
+
114
143
  /**
115
144
  * Creates a lazy Postgres client from either `contractJson` or a TypeScript-authored `contract`.
116
145
  * Static query surfaces are available immediately, while `runtime()` instantiates the driver/pool on first call.
@@ -125,7 +154,7 @@ export default function postgres<TContract extends SqlContract<SqlStorage>>(
125
154
  options: PostgresOptions<TContract>,
126
155
  ): PostgresClient<TContract> {
127
156
  const contract = resolveContract(options);
128
- const binding = resolvePostgresBinding(options);
157
+ let binding = resolveOptionalPostgresBinding(options);
129
158
  const stack = createSqlExecutionStack({
130
159
  target: postgresTarget,
131
160
  adapter: postgresAdapter,
@@ -141,7 +170,35 @@ export default function postgres<TContract extends SqlContract<SqlStorage>>(
141
170
  const schema: PostgresClient<TContract>['schema'] = schemaBuilder(context);
142
171
  const sql = sqlBuilder({ context });
143
172
  let runtimeInstance: Runtime | undefined;
173
+ let runtimeDriver: { connect(binding: unknown): Promise<void> } | undefined;
174
+ let driverConnected = false;
175
+ let connectPromise: Promise<void> | undefined;
176
+ let backgroundConnectError: unknown;
177
+ const connectDriver = async (resolvedBinding: PostgresBinding): Promise<void> => {
178
+ if (driverConnected) return;
179
+ if (!runtimeDriver) throw new Error('Postgres runtime driver missing');
180
+ if (connectPromise) return connectPromise;
181
+ const runtimeBinding = toRuntimeBinding(resolvedBinding, options);
182
+ connectPromise = runtimeDriver
183
+ .connect(runtimeBinding)
184
+ .then(() => {
185
+ driverConnected = true;
186
+ })
187
+ .catch(async (err) => {
188
+ backgroundConnectError = err;
189
+ connectPromise = undefined;
190
+ if (resolvedBinding.kind === 'url' && runtimeBinding.kind === 'pgPool') {
191
+ await runtimeBinding.pool.end().catch(() => undefined);
192
+ }
193
+ throw err;
194
+ });
195
+ return connectPromise;
196
+ };
144
197
  const getRuntime = (): Runtime => {
198
+ if (backgroundConnectError !== undefined) {
199
+ throw backgroundConnectError;
200
+ }
201
+
145
202
  if (runtimeInstance) {
146
203
  return runtimeInstance;
147
204
  }
@@ -152,23 +209,13 @@ export default function postgres<TContract extends SqlContract<SqlStorage>>(
152
209
  throw new Error('Driver descriptor missing from execution stack');
153
210
  }
154
211
 
155
- const connect =
156
- binding.kind === 'url'
157
- ? {
158
- pool: new Pool({
159
- connectionString: binding.url,
160
- connectionTimeoutMillis: options.poolOptions?.connectionTimeoutMillis ?? 20_000,
161
- idleTimeoutMillis: options.poolOptions?.idleTimeoutMillis ?? 30_000,
162
- }),
163
- }
164
- : binding.kind === 'pgPool'
165
- ? { pool: binding.pool }
166
- : { client: binding.client };
167
-
168
212
  const driver = driverDescriptor.create({
169
- connect,
170
213
  cursor: { disabled: true },
171
214
  });
215
+ runtimeDriver = driver;
216
+ if (binding !== undefined) {
217
+ void connectDriver(binding).catch(() => undefined);
218
+ }
172
219
 
173
220
  runtimeInstance = createRuntime({
174
221
  stackInstance,
@@ -194,15 +241,34 @@ export default function postgres<TContract extends SqlContract<SqlStorage>>(
194
241
 
195
242
  return {
196
243
  sql,
197
- kysely(runtime: Runtime) {
198
- return new Kysely<KyselifyContract<TContract>>({
199
- dialect: new KyselyPrismaDialect({ runtime, contract }),
200
- });
201
- },
244
+ kysely: createKyselyLane(contract),
202
245
  schema,
203
246
  orm,
204
247
  context,
205
248
  stack,
249
+ async connect(bindingInput) {
250
+ if (driverConnected || connectPromise) {
251
+ throw new Error('Postgres client already connected');
252
+ }
253
+
254
+ if (bindingInput !== undefined) {
255
+ binding = resolvePostgresBinding(bindingInput);
256
+ }
257
+
258
+ if (binding === undefined) {
259
+ throw new Error(
260
+ 'Postgres binding not configured. Pass url/pg/binding to postgres(...) or call db.connect({ ... }).',
261
+ );
262
+ }
263
+
264
+ const runtime = getRuntime();
265
+ if (driverConnected) {
266
+ return runtime;
267
+ }
268
+
269
+ await connectDriver(binding);
270
+ return runtime;
271
+ },
206
272
  runtime() {
207
273
  return getRuntime();
208
274
  },