@prisma-next/postgres 0.3.0-dev.44 → 0.3.0-dev.45
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 +4 -2
- package/dist/runtime.d.mts +8 -2
- package/dist/runtime.d.mts.map +1 -1
- package/dist/runtime.mjs +49 -10
- package/dist/runtime.mjs.map +1 -1
- package/package.json +11 -11
- package/src/runtime/binding.ts +21 -0
- package/src/runtime/postgres.ts +90 -19
package/README.md
CHANGED
|
@@ -19,7 +19,8 @@ Composition-root Postgres helper that builds a Prisma Next runtime client and ex
|
|
|
19
19
|
- `db.context`
|
|
20
20
|
- `db.stack`
|
|
21
21
|
|
|
22
|
-
Runtime
|
|
22
|
+
Runtime resources are deferred until `db.runtime()` or `db.connect(...)` is called.
|
|
23
|
+
Connection binding can be provided up front (`url`, `pg`, `binding`) or deferred via `db.connect(...)`.
|
|
23
24
|
|
|
24
25
|
When URL binding is used, pool timeouts are configurable via `poolOptions`:
|
|
25
26
|
|
|
@@ -32,7 +33,8 @@ When URL binding is used, pool timeouts are configurable via `poolOptions`:
|
|
|
32
33
|
- Build typed SQL and Kysely lane instances from the same execution context
|
|
33
34
|
- Build static schema and ORM roots from the execution context
|
|
34
35
|
- Normalize runtime binding input (`binding`, `url`, `pg`)
|
|
35
|
-
- Lazily instantiate runtime resources on first `db.runtime()` call
|
|
36
|
+
- Lazily instantiate runtime resources on first `db.runtime()` or `db.connect(...)` call
|
|
37
|
+
- Connect the internal Postgres driver through `db.connect(...)` or from initial binding options
|
|
36
38
|
- Memoize runtime so repeated `db.runtime()` calls return one instance
|
|
37
39
|
|
|
38
40
|
## Dependencies
|
package/dist/runtime.d.mts
CHANGED
|
@@ -45,6 +45,7 @@ interface PostgresClient<TContract extends SqlContract<SqlStorage>> {
|
|
|
45
45
|
readonly orm: OrmClient<TContract>;
|
|
46
46
|
readonly context: ExecutionContext<TContract>;
|
|
47
47
|
readonly stack: SqlExecutionStackWithDriver<PostgresTargetId>;
|
|
48
|
+
connect(bindingInput?: PostgresBindingInput): Promise<Runtime>;
|
|
48
49
|
runtime(): Runtime;
|
|
49
50
|
}
|
|
50
51
|
interface PostgresOptionsBase<TContract extends SqlContract<SqlStorage>> {
|
|
@@ -56,11 +57,16 @@ interface PostgresOptionsBase<TContract extends SqlContract<SqlStorage>> {
|
|
|
56
57
|
readonly idleTimeoutMillis?: number;
|
|
57
58
|
};
|
|
58
59
|
}
|
|
59
|
-
|
|
60
|
+
interface PostgresBindingOptions {
|
|
61
|
+
readonly binding?: PostgresBinding;
|
|
62
|
+
readonly url?: string;
|
|
63
|
+
readonly pg?: Pool | Client;
|
|
64
|
+
}
|
|
65
|
+
type PostgresOptionsWithContract<TContract extends SqlContract<SqlStorage>> = PostgresBindingOptions & PostgresOptionsBase<TContract> & {
|
|
60
66
|
readonly contract: TContract;
|
|
61
67
|
readonly contractJson?: never;
|
|
62
68
|
};
|
|
63
|
-
type PostgresOptionsWithContractJson<TContract extends SqlContract<SqlStorage>> =
|
|
69
|
+
type PostgresOptionsWithContractJson<TContract extends SqlContract<SqlStorage>> = PostgresBindingOptions & PostgresOptionsBase<TContract> & {
|
|
64
70
|
readonly contractJson: unknown;
|
|
65
71
|
readonly contract?: never;
|
|
66
72
|
};
|
package/dist/runtime.d.mts.map
CHANGED
|
@@ -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;;;;
|
|
1
|
+
{"version":3,"file":"runtime.d.mts","names":[],"sources":["../src/runtime/binding.ts","../src/runtime/postg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kBAkDJ,WAlDI,CAkDQ,UAlDR,CAAA,CAAA,GAmD9C,2BAnD8C,CAmDlB,SAnDkB,CAAA,GAoD9C,+BApD8C,CAoDd,SApDc,CAAA;;;;;AAK9C,iBAoFoB,QApFpB,CAAA,kBAoF+C,WApF/C,CAoF2D,UApF3D,CAAA,CAAA,CAAA,OAAA,EAqFO,2BArFP,CAqFmC,SArFnC,CAAA,CAAA,EAsFD,cAtFC,CAsFc,SAtFd,CAAA;AAJY,iBA2FQ,QA3FR,CAAA,kBA2FmC,WA3FnC,CA2F+C,UA3F/C,CAAA,CAAA,CAAA,OAAA,EA4FL,+BA5FK,CA4F2B,SA5F3B,CAAA,CAAA,EA6Fb,cA7Fa,CA6FE,SA7FF,CAAA"}
|
package/dist/runtime.mjs
CHANGED
|
@@ -43,6 +43,10 @@ function resolvePostgresBinding(options) {
|
|
|
43
43
|
};
|
|
44
44
|
throw new Error("Unable to determine pg binding type from pg input; use binding with explicit kind");
|
|
45
45
|
}
|
|
46
|
+
function resolveOptionalPostgresBinding(options) {
|
|
47
|
+
if (Number(options.binding !== void 0) + Number(options.url !== void 0) + Number(options.pg !== void 0) === 0) return;
|
|
48
|
+
return resolvePostgresBinding(options);
|
|
49
|
+
}
|
|
46
50
|
|
|
47
51
|
//#endregion
|
|
48
52
|
//#region src/runtime/postgres.ts
|
|
@@ -52,9 +56,20 @@ function hasContractJson(options) {
|
|
|
52
56
|
function resolveContract(options) {
|
|
53
57
|
return validateContract(hasContractJson(options) ? options.contractJson : options.contract);
|
|
54
58
|
}
|
|
59
|
+
function toRuntimeBinding(binding, options) {
|
|
60
|
+
if (binding.kind !== "url") return binding;
|
|
61
|
+
return {
|
|
62
|
+
kind: "pgPool",
|
|
63
|
+
pool: new Pool({
|
|
64
|
+
connectionString: binding.url,
|
|
65
|
+
connectionTimeoutMillis: options.poolOptions?.connectionTimeoutMillis ?? 2e4,
|
|
66
|
+
idleTimeoutMillis: options.poolOptions?.idleTimeoutMillis ?? 3e4
|
|
67
|
+
})
|
|
68
|
+
};
|
|
69
|
+
}
|
|
55
70
|
function postgres(options) {
|
|
56
71
|
const contract = resolveContract(options);
|
|
57
|
-
|
|
72
|
+
let binding = resolveOptionalPostgresBinding(options);
|
|
58
73
|
const stack = createSqlExecutionStack({
|
|
59
74
|
target: postgresTarget,
|
|
60
75
|
adapter: postgresAdapter,
|
|
@@ -68,23 +83,38 @@ function postgres(options) {
|
|
|
68
83
|
const schema$1 = schema(context);
|
|
69
84
|
const sql$1 = sql({ context });
|
|
70
85
|
let runtimeInstance;
|
|
86
|
+
let runtimeDriver;
|
|
87
|
+
let driverConnected = false;
|
|
88
|
+
let connectPromise;
|
|
89
|
+
let backgroundConnectError;
|
|
90
|
+
const connectDriver = async (resolvedBinding) => {
|
|
91
|
+
if (driverConnected) return;
|
|
92
|
+
if (!runtimeDriver) throw new Error("Postgres runtime driver missing");
|
|
93
|
+
if (connectPromise) return connectPromise;
|
|
94
|
+
const runtimeBinding = toRuntimeBinding(resolvedBinding, options);
|
|
95
|
+
connectPromise = runtimeDriver.connect(runtimeBinding).then(() => {
|
|
96
|
+
driverConnected = true;
|
|
97
|
+
}).catch(async (err) => {
|
|
98
|
+
backgroundConnectError = err;
|
|
99
|
+
connectPromise = void 0;
|
|
100
|
+
if (resolvedBinding.kind === "url" && runtimeBinding.kind === "pgPool") await runtimeBinding.pool.end().catch(() => void 0);
|
|
101
|
+
throw err;
|
|
102
|
+
});
|
|
103
|
+
return connectPromise;
|
|
104
|
+
};
|
|
71
105
|
const getRuntime = () => {
|
|
106
|
+
if (backgroundConnectError !== void 0) throw backgroundConnectError;
|
|
72
107
|
if (runtimeInstance) return runtimeInstance;
|
|
73
108
|
const stackInstance = instantiateExecutionStack(stack);
|
|
74
109
|
const driverDescriptor = stack.driver;
|
|
75
110
|
if (!driverDescriptor) throw new Error("Driver descriptor missing from execution stack");
|
|
76
|
-
const
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
idleTimeoutMillis: options.poolOptions?.idleTimeoutMillis ?? 3e4
|
|
80
|
-
}) } : binding.kind === "pgPool" ? { pool: binding.pool } : { client: binding.client };
|
|
111
|
+
const driver = driverDescriptor.create({ cursor: { disabled: true } });
|
|
112
|
+
runtimeDriver = driver;
|
|
113
|
+
if (binding !== void 0) connectDriver(binding).catch(() => void 0);
|
|
81
114
|
runtimeInstance = createRuntime({
|
|
82
115
|
stackInstance,
|
|
83
116
|
context,
|
|
84
|
-
driver
|
|
85
|
-
connect,
|
|
86
|
-
cursor: { disabled: true }
|
|
87
|
-
}),
|
|
117
|
+
driver,
|
|
88
118
|
verify: options.verify ?? {
|
|
89
119
|
mode: "onFirstUse",
|
|
90
120
|
requireMarker: false
|
|
@@ -115,6 +145,15 @@ function postgres(options) {
|
|
|
115
145
|
}),
|
|
116
146
|
context,
|
|
117
147
|
stack,
|
|
148
|
+
async connect(bindingInput) {
|
|
149
|
+
if (driverConnected || connectPromise) throw new Error("Postgres client already connected");
|
|
150
|
+
if (bindingInput !== void 0) binding = resolvePostgresBinding(bindingInput);
|
|
151
|
+
if (binding === void 0) throw new Error("Postgres binding not configured. Pass url/pg/binding to postgres(...) or call db.connect({ ... }).");
|
|
152
|
+
const runtime = getRuntime();
|
|
153
|
+
if (driverConnected) return runtime;
|
|
154
|
+
await connectDriver(binding);
|
|
155
|
+
return runtime;
|
|
156
|
+
},
|
|
118
157
|
runtime() {
|
|
119
158
|
return getRuntime();
|
|
120
159
|
}
|
package/dist/runtime.mjs.map
CHANGED
|
@@ -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,aA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iBACH,OAAM,IAAI,MAAM,iDAAiD;EAGnE,MAAM,UACJ,QAAQ,SAAS,QACb,EACE,MAAM,IAAI,KAAK;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","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 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 { 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 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 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(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 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;CA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ACA;GACA;GACA,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,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,22 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prisma-next/postgres",
|
|
3
|
-
"version": "0.3.0-dev.
|
|
3
|
+
"version": "0.3.0-dev.45",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"description": "One-liner lazy Postgres client composition for Prisma Next",
|
|
7
7
|
"dependencies": {
|
|
8
8
|
"kysely": "0.28.10",
|
|
9
9
|
"pg": "8.16.3",
|
|
10
|
-
"@prisma-next/
|
|
11
|
-
"@prisma-next/
|
|
12
|
-
"@prisma-next/
|
|
13
|
-
"@prisma-next/
|
|
14
|
-
"@prisma-next/sql-
|
|
15
|
-
"@prisma-next/sql-
|
|
16
|
-
"@prisma-next/sql-
|
|
17
|
-
"@prisma-next/sql-
|
|
18
|
-
"@prisma-next/
|
|
19
|
-
"@prisma-next/
|
|
10
|
+
"@prisma-next/adapter-postgres": "0.3.0-dev.45",
|
|
11
|
+
"@prisma-next/core-execution-plane": "0.3.0-dev.45",
|
|
12
|
+
"@prisma-next/driver-postgres": "0.3.0-dev.45",
|
|
13
|
+
"@prisma-next/integration-kysely": "0.3.0-dev.45",
|
|
14
|
+
"@prisma-next/sql-contract": "0.3.0-dev.45",
|
|
15
|
+
"@prisma-next/sql-lane": "0.3.0-dev.45",
|
|
16
|
+
"@prisma-next/sql-orm-client": "0.3.0-dev.45",
|
|
17
|
+
"@prisma-next/sql-relational-core": "0.3.0-dev.45",
|
|
18
|
+
"@prisma-next/sql-runtime": "0.3.0-dev.45",
|
|
19
|
+
"@prisma-next/target-postgres": "0.3.0-dev.45"
|
|
20
20
|
},
|
|
21
21
|
"devDependencies": {
|
|
22
22
|
"@types/pg": "8.16.0",
|
package/src/runtime/binding.ts
CHANGED
|
@@ -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
|
+
}
|
package/src/runtime/postgres.ts
CHANGED
|
@@ -33,8 +33,13 @@ import {
|
|
|
33
33
|
} from '@prisma-next/sql-runtime';
|
|
34
34
|
import postgresTarget from '@prisma-next/target-postgres/runtime';
|
|
35
35
|
import { Kysely } from 'kysely';
|
|
36
|
-
import { Pool } from 'pg';
|
|
37
|
-
import {
|
|
36
|
+
import { type Client, Pool } from 'pg';
|
|
37
|
+
import {
|
|
38
|
+
type PostgresBinding,
|
|
39
|
+
type PostgresBindingInput,
|
|
40
|
+
resolveOptionalPostgresBinding,
|
|
41
|
+
resolvePostgresBinding,
|
|
42
|
+
} from './binding';
|
|
38
43
|
|
|
39
44
|
type NormalizeOperationTypes<T> = {
|
|
40
45
|
[TypeId in keyof T]: {
|
|
@@ -67,6 +72,7 @@ export interface PostgresClient<TContract extends SqlContract<SqlStorage>> {
|
|
|
67
72
|
readonly orm: OrmClient<TContract>;
|
|
68
73
|
readonly context: ExecutionContext<TContract>;
|
|
69
74
|
readonly stack: SqlExecutionStackWithDriver<PostgresTargetId>;
|
|
75
|
+
connect(bindingInput?: PostgresBindingInput): Promise<Runtime>;
|
|
70
76
|
runtime(): Runtime;
|
|
71
77
|
}
|
|
72
78
|
|
|
@@ -80,15 +86,21 @@ export interface PostgresOptionsBase<TContract extends SqlContract<SqlStorage>>
|
|
|
80
86
|
};
|
|
81
87
|
}
|
|
82
88
|
|
|
89
|
+
export interface PostgresBindingOptions {
|
|
90
|
+
readonly binding?: PostgresBinding;
|
|
91
|
+
readonly url?: string;
|
|
92
|
+
readonly pg?: Pool | Client;
|
|
93
|
+
}
|
|
94
|
+
|
|
83
95
|
export type PostgresOptionsWithContract<TContract extends SqlContract<SqlStorage>> =
|
|
84
|
-
|
|
96
|
+
PostgresBindingOptions &
|
|
85
97
|
PostgresOptionsBase<TContract> & {
|
|
86
98
|
readonly contract: TContract;
|
|
87
99
|
readonly contractJson?: never;
|
|
88
100
|
};
|
|
89
101
|
|
|
90
102
|
export type PostgresOptionsWithContractJson<TContract extends SqlContract<SqlStorage>> =
|
|
91
|
-
|
|
103
|
+
PostgresBindingOptions &
|
|
92
104
|
PostgresOptionsBase<TContract> & {
|
|
93
105
|
readonly contractJson: unknown;
|
|
94
106
|
readonly contract?: never;
|
|
@@ -111,6 +123,24 @@ function resolveContract<TContract extends SqlContract<SqlStorage>>(
|
|
|
111
123
|
return validateContract<TContract>(contractInput);
|
|
112
124
|
}
|
|
113
125
|
|
|
126
|
+
function toRuntimeBinding<TContract extends SqlContract<SqlStorage>>(
|
|
127
|
+
binding: PostgresBinding,
|
|
128
|
+
options: PostgresOptions<TContract>,
|
|
129
|
+
) {
|
|
130
|
+
if (binding.kind !== 'url') {
|
|
131
|
+
return binding;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return {
|
|
135
|
+
kind: 'pgPool',
|
|
136
|
+
pool: new Pool({
|
|
137
|
+
connectionString: binding.url,
|
|
138
|
+
connectionTimeoutMillis: options.poolOptions?.connectionTimeoutMillis ?? 20_000,
|
|
139
|
+
idleTimeoutMillis: options.poolOptions?.idleTimeoutMillis ?? 30_000,
|
|
140
|
+
}),
|
|
141
|
+
} as const;
|
|
142
|
+
}
|
|
143
|
+
|
|
114
144
|
/**
|
|
115
145
|
* Creates a lazy Postgres client from either `contractJson` or a TypeScript-authored `contract`.
|
|
116
146
|
* Static query surfaces are available immediately, while `runtime()` instantiates the driver/pool on first call.
|
|
@@ -125,7 +155,7 @@ export default function postgres<TContract extends SqlContract<SqlStorage>>(
|
|
|
125
155
|
options: PostgresOptions<TContract>,
|
|
126
156
|
): PostgresClient<TContract> {
|
|
127
157
|
const contract = resolveContract(options);
|
|
128
|
-
|
|
158
|
+
let binding = resolveOptionalPostgresBinding(options);
|
|
129
159
|
const stack = createSqlExecutionStack({
|
|
130
160
|
target: postgresTarget,
|
|
131
161
|
adapter: postgresAdapter,
|
|
@@ -141,7 +171,35 @@ export default function postgres<TContract extends SqlContract<SqlStorage>>(
|
|
|
141
171
|
const schema: PostgresClient<TContract>['schema'] = schemaBuilder(context);
|
|
142
172
|
const sql = sqlBuilder({ context });
|
|
143
173
|
let runtimeInstance: Runtime | undefined;
|
|
174
|
+
let runtimeDriver: { connect(binding: unknown): Promise<void> } | undefined;
|
|
175
|
+
let driverConnected = false;
|
|
176
|
+
let connectPromise: Promise<void> | undefined;
|
|
177
|
+
let backgroundConnectError: unknown;
|
|
178
|
+
const connectDriver = async (resolvedBinding: PostgresBinding): Promise<void> => {
|
|
179
|
+
if (driverConnected) return;
|
|
180
|
+
if (!runtimeDriver) throw new Error('Postgres runtime driver missing');
|
|
181
|
+
if (connectPromise) return connectPromise;
|
|
182
|
+
const runtimeBinding = toRuntimeBinding(resolvedBinding, options);
|
|
183
|
+
connectPromise = runtimeDriver
|
|
184
|
+
.connect(runtimeBinding)
|
|
185
|
+
.then(() => {
|
|
186
|
+
driverConnected = true;
|
|
187
|
+
})
|
|
188
|
+
.catch(async (err) => {
|
|
189
|
+
backgroundConnectError = err;
|
|
190
|
+
connectPromise = undefined;
|
|
191
|
+
if (resolvedBinding.kind === 'url' && runtimeBinding.kind === 'pgPool') {
|
|
192
|
+
await runtimeBinding.pool.end().catch(() => undefined);
|
|
193
|
+
}
|
|
194
|
+
throw err;
|
|
195
|
+
});
|
|
196
|
+
return connectPromise;
|
|
197
|
+
};
|
|
144
198
|
const getRuntime = (): Runtime => {
|
|
199
|
+
if (backgroundConnectError !== undefined) {
|
|
200
|
+
throw backgroundConnectError;
|
|
201
|
+
}
|
|
202
|
+
|
|
145
203
|
if (runtimeInstance) {
|
|
146
204
|
return runtimeInstance;
|
|
147
205
|
}
|
|
@@ -152,23 +210,13 @@ export default function postgres<TContract extends SqlContract<SqlStorage>>(
|
|
|
152
210
|
throw new Error('Driver descriptor missing from execution stack');
|
|
153
211
|
}
|
|
154
212
|
|
|
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
213
|
const driver = driverDescriptor.create({
|
|
169
|
-
connect,
|
|
170
214
|
cursor: { disabled: true },
|
|
171
215
|
});
|
|
216
|
+
runtimeDriver = driver;
|
|
217
|
+
if (binding !== undefined) {
|
|
218
|
+
void connectDriver(binding).catch(() => undefined);
|
|
219
|
+
}
|
|
172
220
|
|
|
173
221
|
runtimeInstance = createRuntime({
|
|
174
222
|
stackInstance,
|
|
@@ -203,6 +251,29 @@ export default function postgres<TContract extends SqlContract<SqlStorage>>(
|
|
|
203
251
|
orm,
|
|
204
252
|
context,
|
|
205
253
|
stack,
|
|
254
|
+
async connect(bindingInput) {
|
|
255
|
+
if (driverConnected || connectPromise) {
|
|
256
|
+
throw new Error('Postgres client already connected');
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
if (bindingInput !== undefined) {
|
|
260
|
+
binding = resolvePostgresBinding(bindingInput);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
if (binding === undefined) {
|
|
264
|
+
throw new Error(
|
|
265
|
+
'Postgres binding not configured. Pass url/pg/binding to postgres(...) or call db.connect({ ... }).',
|
|
266
|
+
);
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
const runtime = getRuntime();
|
|
270
|
+
if (driverConnected) {
|
|
271
|
+
return runtime;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
await connectDriver(binding);
|
|
275
|
+
return runtime;
|
|
276
|
+
},
|
|
206
277
|
runtime() {
|
|
207
278
|
return getRuntime();
|
|
208
279
|
},
|