@prisma-next/driver-postgres 0.3.0-pr.94.3 → 0.3.0-pr.95.2
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/dist/callback-to-promise.d.ts +7 -0
- package/dist/callback-to-promise.d.ts.map +1 -0
- package/dist/chunk-M4WUUVS5.js +116 -0
- package/dist/chunk-M4WUUVS5.js.map +1 -0
- package/dist/core/descriptor-meta.d.ts +9 -0
- package/dist/core/descriptor-meta.d.ts.map +1 -0
- package/dist/exports/control.d.ts +26 -0
- package/dist/exports/control.d.ts.map +1 -0
- package/dist/exports/control.js +66 -0
- package/dist/exports/control.js.map +1 -0
- package/dist/exports/runtime.d.ts +17 -0
- package/dist/exports/runtime.d.ts.map +1 -0
- package/dist/exports/runtime.js +189 -0
- package/dist/exports/runtime.js.map +1 -0
- package/dist/normalize-error.d.ts +60 -0
- package/dist/normalize-error.d.ts.map +1 -0
- package/dist/postgres-driver.d.ts +22 -0
- package/dist/postgres-driver.d.ts.map +1 -0
- package/package.json +21 -20
- package/dist/control.d.mts +0 -30
- package/dist/control.d.mts.map +0 -1
- package/dist/control.mjs +0 -67
- package/dist/control.mjs.map +0 -1
- package/dist/normalize-error-BU6yV-XB.mjs +0 -125
- package/dist/normalize-error-BU6yV-XB.mjs.map +0 -1
- package/dist/runtime.d.mts +0 -39
- package/dist/runtime.d.mts.map +0 -1
- package/dist/runtime.mjs +0 -147
- package/dist/runtime.mjs.map +0 -1
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wraps a Node-style callback function into a Promise.
|
|
3
|
+
* Supports both (err) => void and (err, result) => void patterns.
|
|
4
|
+
*/
|
|
5
|
+
export declare function callbackToPromise(fn: (callback: (err: Error | null | undefined) => void) => void): Promise<void>;
|
|
6
|
+
export declare function callbackToPromise<T>(fn: (callback: (err: Error | null | undefined, result: T) => void) => void): Promise<T>;
|
|
7
|
+
//# sourceMappingURL=callback-to-promise.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"callback-to-promise.d.ts","sourceRoot":"","sources":["../src/callback-to-promise.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,wBAAgB,iBAAiB,CAC/B,EAAE,EAAE,CAAC,QAAQ,EAAE,CAAC,GAAG,EAAE,KAAK,GAAG,IAAI,GAAG,SAAS,KAAK,IAAI,KAAK,IAAI,GAC9D,OAAO,CAAC,IAAI,CAAC,CAAC;AACjB,wBAAgB,iBAAiB,CAAC,CAAC,EACjC,EAAE,EAAE,CAAC,QAAQ,EAAE,CAAC,GAAG,EAAE,KAAK,GAAG,IAAI,GAAG,SAAS,EAAE,MAAM,EAAE,CAAC,KAAK,IAAI,KAAK,IAAI,GACzE,OAAO,CAAC,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
// src/core/descriptor-meta.ts
|
|
2
|
+
var postgresDriverDescriptorMeta = {
|
|
3
|
+
kind: "driver",
|
|
4
|
+
familyId: "sql",
|
|
5
|
+
targetId: "postgres",
|
|
6
|
+
id: "postgres",
|
|
7
|
+
version: "0.0.1",
|
|
8
|
+
capabilities: {}
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
// src/normalize-error.ts
|
|
12
|
+
import { SqlConnectionError, SqlQueryError } from "@prisma-next/sql-errors";
|
|
13
|
+
function isConnectionError(error) {
|
|
14
|
+
const code = error.code;
|
|
15
|
+
if (code) {
|
|
16
|
+
if (code === "ECONNRESET" || code === "ETIMEDOUT" || code === "ECONNREFUSED" || code === "ENOTFOUND" || code === "EHOSTUNREACH") {
|
|
17
|
+
return true;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
const message = error.message.toLowerCase();
|
|
21
|
+
if (message.includes("connection terminated") || message.includes("connection closed") || message.includes("connection refused") || message.includes("connection timeout") || message.includes("connection reset")) {
|
|
22
|
+
return true;
|
|
23
|
+
}
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
function isTransientConnectionError(error) {
|
|
27
|
+
const code = error.code;
|
|
28
|
+
if (code) {
|
|
29
|
+
if (code === "ETIMEDOUT" || code === "ECONNRESET") {
|
|
30
|
+
return true;
|
|
31
|
+
}
|
|
32
|
+
if (code === "ECONNREFUSED") {
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
const message = error.message.toLowerCase();
|
|
37
|
+
if (message.includes("timeout") || message.includes("connection reset")) {
|
|
38
|
+
return true;
|
|
39
|
+
}
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
var PG_ERROR_PROPERTIES = [
|
|
43
|
+
"constraint",
|
|
44
|
+
"table",
|
|
45
|
+
"column",
|
|
46
|
+
"hint",
|
|
47
|
+
"internalPosition",
|
|
48
|
+
"internalQuery",
|
|
49
|
+
"where",
|
|
50
|
+
"schema",
|
|
51
|
+
"routine"
|
|
52
|
+
];
|
|
53
|
+
function isPostgresError(error) {
|
|
54
|
+
if (!(error instanceof Error)) {
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
const pgError = error;
|
|
58
|
+
if (pgError.code && isPostgresSqlState(pgError.code)) {
|
|
59
|
+
return true;
|
|
60
|
+
}
|
|
61
|
+
return PG_ERROR_PROPERTIES.some((prop) => pgError[prop] !== void 0);
|
|
62
|
+
}
|
|
63
|
+
function isAlreadyConnectedError(error) {
|
|
64
|
+
if (!(error instanceof Error)) {
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
const message = error.message.toLowerCase();
|
|
68
|
+
return message.includes("already") && message.includes("connected");
|
|
69
|
+
}
|
|
70
|
+
function isPostgresSqlState(code) {
|
|
71
|
+
if (!code) {
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
return /^[A-Z0-9]{5}$/.test(code);
|
|
75
|
+
}
|
|
76
|
+
function normalizePgError(error) {
|
|
77
|
+
if (!(error instanceof Error)) {
|
|
78
|
+
return new Error(String(error));
|
|
79
|
+
}
|
|
80
|
+
const pgError = error;
|
|
81
|
+
if (isPostgresSqlState(pgError.code)) {
|
|
82
|
+
const sqlState = pgError.code;
|
|
83
|
+
const options = {
|
|
84
|
+
cause: error,
|
|
85
|
+
sqlState
|
|
86
|
+
};
|
|
87
|
+
if (pgError.constraint !== void 0) {
|
|
88
|
+
options.constraint = pgError.constraint;
|
|
89
|
+
}
|
|
90
|
+
if (pgError.table !== void 0) {
|
|
91
|
+
options.table = pgError.table;
|
|
92
|
+
}
|
|
93
|
+
if (pgError.column !== void 0) {
|
|
94
|
+
options.column = pgError.column;
|
|
95
|
+
}
|
|
96
|
+
if (pgError.detail !== void 0) {
|
|
97
|
+
options.detail = pgError.detail;
|
|
98
|
+
}
|
|
99
|
+
return new SqlQueryError(error.message, options);
|
|
100
|
+
}
|
|
101
|
+
if (isConnectionError(error)) {
|
|
102
|
+
return new SqlConnectionError(error.message, {
|
|
103
|
+
cause: error,
|
|
104
|
+
transient: isTransientConnectionError(error)
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
return error;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export {
|
|
111
|
+
postgresDriverDescriptorMeta,
|
|
112
|
+
isPostgresError,
|
|
113
|
+
isAlreadyConnectedError,
|
|
114
|
+
normalizePgError
|
|
115
|
+
};
|
|
116
|
+
//# sourceMappingURL=chunk-M4WUUVS5.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/core/descriptor-meta.ts","../src/normalize-error.ts"],"sourcesContent":["export const postgresDriverDescriptorMeta = {\n kind: 'driver',\n familyId: 'sql',\n targetId: 'postgres',\n id: 'postgres',\n version: '0.0.1',\n capabilities: {},\n} as const;\n","import { SqlConnectionError, SqlQueryError } from '@prisma-next/sql-errors';\n\n/**\n * Postgres error shape from the pg library.\n *\n * Note: The pg library doesn't export a DatabaseError type or interface, but errors\n * thrown by pg.query() and pg.Client have this shape at runtime. We define this\n * interface to match the actual runtime structure documented in the pg library\n * (https://github.com/brianc/node-postgres/blob/master/packages/pg/lib/errors.js).\n *\n * The @types/pg package also doesn't provide comprehensive error type definitions,\n * so we define our own interface based on the runtime error properties.\n */\ninterface PostgresError extends Error {\n readonly code?: string;\n readonly constraint?: string;\n readonly table?: string;\n readonly column?: string;\n readonly detail?: string;\n readonly hint?: string;\n readonly position?: string;\n readonly internalPosition?: string;\n readonly internalQuery?: string;\n readonly where?: string;\n readonly schema?: string;\n readonly file?: string;\n readonly line?: string;\n readonly routine?: string;\n}\n\n/**\n * Checks if an error is a connection-related error.\n */\nfunction isConnectionError(error: Error): boolean {\n const code = (error as { code?: string }).code;\n if (code) {\n // Node.js error codes for connection issues\n if (\n code === 'ECONNRESET' ||\n code === 'ETIMEDOUT' ||\n code === 'ECONNREFUSED' ||\n code === 'ENOTFOUND' ||\n code === 'EHOSTUNREACH'\n ) {\n return true;\n }\n }\n\n // Check error message for connection-related strings\n const message = error.message.toLowerCase();\n if (\n message.includes('connection terminated') ||\n message.includes('connection closed') ||\n message.includes('connection refused') ||\n message.includes('connection timeout') ||\n message.includes('connection reset')\n ) {\n return true;\n }\n\n return false;\n}\n\n/**\n * Checks if a connection error is transient (might succeed on retry).\n */\nfunction isTransientConnectionError(error: Error): boolean {\n const code = (error as { code?: string }).code;\n if (code) {\n // Timeouts and connection resets are often transient\n if (code === 'ETIMEDOUT' || code === 'ECONNRESET') {\n return true;\n }\n // Connection refused is usually not transient (server is down)\n if (code === 'ECONNREFUSED') {\n return false;\n }\n }\n\n const message = error.message.toLowerCase();\n if (message.includes('timeout') || message.includes('connection reset')) {\n return true;\n }\n\n return false;\n}\n\n/**\n * PostgreSQL-specific error properties that indicate an error originated from pg library.\n * These properties are not present on Node.js system errors.\n * Excludes generic properties like 'detail', 'file', 'line', and 'position' that could appear on any error.\n */\nconst PG_ERROR_PROPERTIES = [\n 'constraint',\n 'table',\n 'column',\n 'hint',\n 'internalPosition',\n 'internalQuery',\n 'where',\n 'schema',\n 'routine',\n] as const;\n\n/**\n * Type predicate to check if an error is a Postgres error from the pg library.\n *\n * Distinguishes pg library errors from Node.js system errors by checking for:\n * - SQLSTATE codes (5-character alphanumeric codes like '23505', '42601')\n * - pg-specific properties (constraint, table, column, hint, etc.) that Node.js errors don't have\n *\n * Node.js system errors (ECONNREFUSED, ETIMEDOUT, etc.) are excluded to prevent false positives.\n */\nexport function isPostgresError(error: unknown): error is PostgresError {\n if (!(error instanceof Error)) {\n return false;\n }\n\n const pgError = error as PostgresError;\n\n // Check for SQLSTATE code (5-character alphanumeric) - primary indicator of pg errors\n if (pgError.code && isPostgresSqlState(pgError.code)) {\n return true;\n }\n\n // Check for pg-specific properties that Node.js system errors don't have\n // These properties indicate the error originated from pg library query execution\n return PG_ERROR_PROPERTIES.some((prop) => pgError[prop] !== undefined);\n}\n\n/**\n * Checks if an error is an \"already connected\" error from pg.Client.connect().\n * When calling connect() on an already-connected client, pg throws an error that can be safely ignored.\n */\nexport function isAlreadyConnectedError(error: unknown): error is Error {\n if (!(error instanceof Error)) {\n return false;\n }\n const message = error.message.toLowerCase();\n return message.includes('already') && message.includes('connected');\n}\n\n/**\n * Checks if an error code is a Postgres SQLSTATE (5-character alphanumeric code).\n * SQLSTATE codes are standardized SQL error codes (e.g., '23505' for unique violation).\n */\nfunction isPostgresSqlState(code: string | undefined): boolean {\n if (!code) {\n return false;\n }\n // Postgres SQLSTATE codes are 5-character alphanumeric strings\n // Examples: '23505' (unique violation), '42501' (insufficient privilege), '42601' (syntax error)\n return /^[A-Z0-9]{5}$/.test(code);\n}\n\n/**\n * Normalizes a Postgres error into a SQL-shared error type.\n *\n * - Postgres SQLSTATE errors (5-char codes like '23505') → SqlQueryError\n * - Connection errors (ECONNRESET, ETIMEDOUT, etc.) → SqlConnectionError\n * - Unknown errors → returns the original error as-is\n *\n * The original error is preserved via Error.cause to maintain stack traces.\n *\n * @param error - The error to normalize (typically from pg library)\n * @returns SqlQueryError for query-related failures\n * @returns SqlConnectionError for connection-related failures\n * @returns The original error if it cannot be normalized\n */\nexport function normalizePgError(error: unknown): SqlQueryError | SqlConnectionError | Error {\n if (!(error instanceof Error)) {\n // Wrap non-Error values in an Error object\n return new Error(String(error));\n }\n\n const pgError = error as PostgresError;\n\n // Check for Postgres SQLSTATE (query errors)\n if (isPostgresSqlState(pgError.code)) {\n // isPostgresSqlState ensures code is defined and is a valid SQLSTATE\n // biome-ignore lint/style/noNonNullAssertion: isPostgresSqlState guarantees code is defined\n const sqlState = pgError.code!;\n const options: {\n cause: Error;\n sqlState: string;\n constraint?: string;\n table?: string;\n column?: string;\n detail?: string;\n } = {\n cause: error,\n sqlState,\n };\n if (pgError.constraint !== undefined) {\n options.constraint = pgError.constraint;\n }\n if (pgError.table !== undefined) {\n options.table = pgError.table;\n }\n if (pgError.column !== undefined) {\n options.column = pgError.column;\n }\n if (pgError.detail !== undefined) {\n options.detail = pgError.detail;\n }\n return new SqlQueryError(error.message, options);\n }\n\n // Check for connection errors\n if (isConnectionError(error)) {\n return new SqlConnectionError(error.message, {\n cause: error,\n transient: isTransientConnectionError(error),\n });\n }\n\n // Unknown error - return as-is to preserve original error and stack trace\n return error;\n}\n"],"mappings":";AAAO,IAAM,+BAA+B;AAAA,EAC1C,MAAM;AAAA,EACN,UAAU;AAAA,EACV,UAAU;AAAA,EACV,IAAI;AAAA,EACJ,SAAS;AAAA,EACT,cAAc,CAAC;AACjB;;;ACPA,SAAS,oBAAoB,qBAAqB;AAiClD,SAAS,kBAAkB,OAAuB;AAChD,QAAM,OAAQ,MAA4B;AAC1C,MAAI,MAAM;AAER,QACE,SAAS,gBACT,SAAS,eACT,SAAS,kBACT,SAAS,eACT,SAAS,gBACT;AACA,aAAO;AAAA,IACT;AAAA,EACF;AAGA,QAAM,UAAU,MAAM,QAAQ,YAAY;AAC1C,MACE,QAAQ,SAAS,uBAAuB,KACxC,QAAQ,SAAS,mBAAmB,KACpC,QAAQ,SAAS,oBAAoB,KACrC,QAAQ,SAAS,oBAAoB,KACrC,QAAQ,SAAS,kB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,GAAG;AAGpC,UAAM,WAAW,QAAQ;AACzB,UAAM,UAOF;AAAA,MACF,OAAO;AAAA,MACP;AAAA,IACF;AACA,QAAI,QAAQ,eAAe,QAAW;AACpC,cAAQ,aAAa,QAAQ;AAAA,IAC/B;AACA,QAAI,QAAQ,UAAU,QAAW;AAC/B,cAAQ,QAAQ,QAAQ;AAAA,IAC1B;AACA,QAAI,QAAQ,WAAW,QAAW;AAChC,cAAQ,SAAS,QAAQ;AAAA,IAC3B;AACA,QAAI,QAAQ,WAAW,QAAW;AAChC,cAAQ,SAAS,QAAQ;AAAA,IAC3B;AACA,WAAO,IAAI,cAAc,MAAM,SAAS,OAAO;AAAA,EACjD;AAGA,MAAI,kBAAkB,KAAK,GAAG;AAC5B,WAAO,IAAI,mBAAmB,MAAM,SAAS;AAAA,MAC3C,OAAO;AAAA,MACP,WAAW,2BAA2B,KAAK;AAAA,IAC7C,CAAC;AAAA,EACH;AAGA,SAAO;AACT;","names":[]}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export declare const postgresDriverDescriptorMeta: {
|
|
2
|
+
readonly kind: "driver";
|
|
3
|
+
readonly familyId: "sql";
|
|
4
|
+
readonly targetId: "postgres";
|
|
5
|
+
readonly id: "postgres";
|
|
6
|
+
readonly version: "0.0.1";
|
|
7
|
+
readonly capabilities: {};
|
|
8
|
+
};
|
|
9
|
+
//# sourceMappingURL=descriptor-meta.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"descriptor-meta.d.ts","sourceRoot":"","sources":["../../src/core/descriptor-meta.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,4BAA4B;;;;;;;CAO/B,CAAC"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { ControlDriverDescriptor, ControlDriverInstance } from '@prisma-next/core-control-plane/types';
|
|
2
|
+
import { Client } from 'pg';
|
|
3
|
+
/**
|
|
4
|
+
* Postgres control driver instance for control-plane operations.
|
|
5
|
+
* Implements ControlDriverInstance<'sql', 'postgres'> for database queries.
|
|
6
|
+
*/
|
|
7
|
+
export declare class PostgresControlDriver implements ControlDriverInstance<'sql', 'postgres'> {
|
|
8
|
+
private readonly client;
|
|
9
|
+
readonly familyId: "sql";
|
|
10
|
+
readonly targetId: "postgres";
|
|
11
|
+
/**
|
|
12
|
+
* @deprecated Use targetId instead
|
|
13
|
+
*/
|
|
14
|
+
readonly target: "postgres";
|
|
15
|
+
constructor(client: Client);
|
|
16
|
+
query<Row = Record<string, unknown>>(sql: string, params?: readonly unknown[]): Promise<{
|
|
17
|
+
readonly rows: Row[];
|
|
18
|
+
}>;
|
|
19
|
+
close(): Promise<void>;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Postgres driver descriptor for CLI config.
|
|
23
|
+
*/
|
|
24
|
+
declare const postgresDriverDescriptor: ControlDriverDescriptor<'sql', 'postgres', PostgresControlDriver>;
|
|
25
|
+
export default postgresDriverDescriptor;
|
|
26
|
+
//# sourceMappingURL=control.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"control.d.ts","sourceRoot":"","sources":["../../src/exports/control.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,uBAAuB,EACvB,qBAAqB,EACtB,MAAM,uCAAuC,CAAC;AAG/C,OAAO,EAAE,MAAM,EAAE,MAAM,IAAI,CAAC;AAI5B;;;GAGG;AACH,qBAAa,qBAAsB,YAAW,qBAAqB,CAAC,KAAK,EAAE,UAAU,CAAC;IAQxE,OAAO,CAAC,QAAQ,CAAC,MAAM;IAPnC,QAAQ,CAAC,QAAQ,EAAG,KAAK,CAAU;IACnC,QAAQ,CAAC,QAAQ,EAAG,UAAU,CAAU;IACxC;;OAEG;IACH,QAAQ,CAAC,MAAM,EAAG,UAAU,CAAU;gBAET,MAAM,EAAE,MAAM;IAErC,KAAK,CAAC,GAAG,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EACvC,GAAG,EAAE,MAAM,EACX,MAAM,CAAC,EAAE,SAAS,OAAO,EAAE,GAC1B,OAAO,CAAC;QAAE,QAAQ,CAAC,IAAI,EAAE,GAAG,EAAE,CAAA;KAAE,CAAC;IAS9B,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;CAG7B;AAED;;GAEG;AACH,QAAA,MAAM,wBAAwB,EAAE,uBAAuB,CAAC,KAAK,EAAE,UAAU,EAAE,qBAAqB,CAkC7F,CAAC;AAEJ,eAAe,wBAAwB,CAAC"}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import {
|
|
2
|
+
normalizePgError,
|
|
3
|
+
postgresDriverDescriptorMeta
|
|
4
|
+
} from "../chunk-M4WUUVS5.js";
|
|
5
|
+
|
|
6
|
+
// src/exports/control.ts
|
|
7
|
+
import { errorRuntime } from "@prisma-next/core-control-plane/errors";
|
|
8
|
+
import { SqlQueryError } from "@prisma-next/sql-errors";
|
|
9
|
+
import { redactDatabaseUrl } from "@prisma-next/utils/redact-db-url";
|
|
10
|
+
import { Client } from "pg";
|
|
11
|
+
var PostgresControlDriver = class {
|
|
12
|
+
constructor(client) {
|
|
13
|
+
this.client = client;
|
|
14
|
+
}
|
|
15
|
+
familyId = "sql";
|
|
16
|
+
targetId = "postgres";
|
|
17
|
+
/**
|
|
18
|
+
* @deprecated Use targetId instead
|
|
19
|
+
*/
|
|
20
|
+
target = "postgres";
|
|
21
|
+
async query(sql, params) {
|
|
22
|
+
try {
|
|
23
|
+
const result = await this.client.query(sql, params);
|
|
24
|
+
return { rows: result.rows };
|
|
25
|
+
} catch (error) {
|
|
26
|
+
throw normalizePgError(error);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
async close() {
|
|
30
|
+
await this.client.end();
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
var postgresDriverDescriptor = {
|
|
34
|
+
...postgresDriverDescriptorMeta,
|
|
35
|
+
async create(url) {
|
|
36
|
+
const client = new Client({ connectionString: url });
|
|
37
|
+
try {
|
|
38
|
+
await client.connect();
|
|
39
|
+
return new PostgresControlDriver(client);
|
|
40
|
+
} catch (error) {
|
|
41
|
+
const normalized = normalizePgError(error);
|
|
42
|
+
const redacted = redactDatabaseUrl(url);
|
|
43
|
+
try {
|
|
44
|
+
await client.end();
|
|
45
|
+
} catch {
|
|
46
|
+
}
|
|
47
|
+
const codeFromSqlState = SqlQueryError.is(normalized) ? normalized.sqlState : void 0;
|
|
48
|
+
const causeCode = "cause" in normalized && normalized.cause ? normalized.cause.code : void 0;
|
|
49
|
+
const code = codeFromSqlState ?? causeCode;
|
|
50
|
+
throw errorRuntime("Database connection failed", {
|
|
51
|
+
why: normalized.message,
|
|
52
|
+
fix: "Verify the database URL, ensure the database is reachable, and confirm credentials/permissions",
|
|
53
|
+
meta: {
|
|
54
|
+
...typeof code !== "undefined" ? { code } : {},
|
|
55
|
+
...redacted
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
var control_default = postgresDriverDescriptor;
|
|
62
|
+
export {
|
|
63
|
+
PostgresControlDriver,
|
|
64
|
+
control_default as default
|
|
65
|
+
};
|
|
66
|
+
//# sourceMappingURL=control.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/exports/control.ts"],"sourcesContent":["import { errorRuntime } from '@prisma-next/core-control-plane/errors';\nimport type {\n ControlDriverDescriptor,\n ControlDriverInstance,\n} from '@prisma-next/core-control-plane/types';\nimport { SqlQueryError } from '@prisma-next/sql-errors';\nimport { redactDatabaseUrl } from '@prisma-next/utils/redact-db-url';\nimport { Client } from 'pg';\nimport { postgresDriverDescriptorMeta } from '../core/descriptor-meta';\nimport { normalizePgError } from '../normalize-error';\n\n/**\n * Postgres control driver instance for control-plane operations.\n * Implements ControlDriverInstance<'sql', 'postgres'> for database queries.\n */\nexport class PostgresControlDriver implements ControlDriverInstance<'sql', 'postgres'> {\n readonly familyId = 'sql' as const;\n readonly targetId = 'postgres' as const;\n /**\n * @deprecated Use targetId instead\n */\n readonly target = 'postgres' as const;\n\n constructor(private readonly client: Client) {}\n\n async query<Row = Record<string, unknown>>(\n sql: string,\n params?: readonly unknown[],\n ): Promise<{ readonly rows: Row[] }> {\n try {\n const result = await this.client.query(sql, params as unknown[] | undefined);\n return { rows: result.rows as Row[] };\n } catch (error) {\n throw normalizePgError(error);\n }\n }\n\n async close(): Promise<void> {\n await this.client.end();\n }\n}\n\n/**\n * Postgres driver descriptor for CLI config.\n */\nconst postgresDriverDescriptor: ControlDriverDescriptor<'sql', 'postgres', PostgresControlDriver> =\n {\n ...postgresDriverDescriptorMeta,\n async create(url: string): Promise<PostgresControlDriver> {\n const client = new Client({ connectionString: url });\n try {\n await client.connect();\n return new PostgresControlDriver(client);\n } catch (error) {\n const normalized = normalizePgError(error);\n const redacted = redactDatabaseUrl(url);\n try {\n await client.end();\n } catch {\n // ignore\n }\n\n const codeFromSqlState = SqlQueryError.is(normalized) ? normalized.sqlState : undefined;\n const causeCode =\n 'cause' in normalized && normalized.cause\n ? (normalized.cause as { code?: unknown }).code\n : undefined;\n const code = codeFromSqlState ?? causeCode;\n\n throw errorRuntime('Database connection failed', {\n why: normalized.message,\n fix: 'Verify the database URL, ensure the database is reachable, and confirm credentials/permissions',\n meta: {\n ...(typeof code !== 'undefined' ? { code } : {}),\n ...redacted,\n },\n });\n }\n },\n };\n\nexport default postgresDriverDescriptor;\n"],"mappings":";;;;;;AAAA,SAAS,oBAAoB;AAK7B,SAAS,qBAAqB;AAC9B,SAAS,yBAAyB;AAClC,SAAS,cAAc;AAQhB,IAAM,wBAAN,MAAgF;AAAA,EAQrF,YAA6B,QAAgB;AAAhB;AAAA,EAAiB;AAAA,EAPrC,WAAW;AAAA,EACX,WAAW;AAAA;AAAA;AAAA;AAAA,EAIX,SAAS;AAAA,EAIlB,MAAM,MACJ,KACA,QACmC;AACnC,QAAI;AACF,YAAM,SAAS,MAAM,KAAK,OAAO,MAAM,KAAK,MAA+B;AAC3E,aAAO,EAAE,MAAM,OAAO,KAAc;AAAA,IACtC,SAAS,OAAO;AACd,YAAM,iBAAiB,KAAK;AAAA,IAC9B;AAAA,EACF;AAAA,EAEA,MAAM,QAAuB;AAC3B,UAAM,KAAK,OAAO,IAAI;AAAA,EACxB;AACF;AAKA,IAAM,2BACJ;AAAA,EACE,GAAG;AAAA,EACH,MAAM,OAAO,KAA6C;AACxD,UAAM,SAAS,IAAI,OAAO,EAAE,kBAAkB,IAAI,CAAC;AACnD,QAAI;AACF,YAAM,OAAO,QAAQ;AACrB,aAAO,IAAI,sBAAsB,MAAM;AAAA,IACzC,SAAS,OAAO;AACd,YAAM,aAAa,iBAAiB,KAAK;AACzC,YAAM,WAAW,kBAAkB,GAAG;AACtC,UAAI;AACF,cAAM,OAAO,IAAI;AAAA,MACnB,QAAQ;AAAA,MAER;AAEA,YAAM,mBAAmB,cAAc,GAAG,UAAU,IAAI,WAAW,WAAW;AAC9E,YAAM,YACJ,WAAW,cAAc,WAAW,QAC/B,WAAW,MAA6B,OACzC;AACN,YAAM,OAAO,oBAAoB;AAEjC,YAAM,aAAa,8BAA8B;AAAA,QAC/C,KAAK,WAAW;AAAA,QAChB,KAAK;AAAA,QACL,MAAM;AAAA,UACJ,GAAI,OAAO,SAAS,cAAc,EAAE,KAAK,IAAI,CAAC;AAAA,UAC9C,GAAG;AAAA,QACL;AAAA,MACF,CAAC;AAAA,IACH;AAAA,EACF;AACF;AAEF,IAAO,kBAAQ;","names":[]}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { RuntimeDriverDescriptor, RuntimeDriverInstance } from '@prisma-next/core-execution-plane/types';
|
|
2
|
+
import type { SqlDriver } from '@prisma-next/sql-relational-core/ast';
|
|
3
|
+
/**
|
|
4
|
+
* Postgres runtime driver instance interface.
|
|
5
|
+
* SqlDriver provides SQL-specific methods (execute, explain, close).
|
|
6
|
+
* RuntimeDriverInstance provides target identification (familyId, targetId).
|
|
7
|
+
* We use intersection type to combine both interfaces.
|
|
8
|
+
*/
|
|
9
|
+
export type PostgresRuntimeDriver = RuntimeDriverInstance<'sql', 'postgres'> & SqlDriver;
|
|
10
|
+
/**
|
|
11
|
+
* Postgres driver descriptor for runtime plane.
|
|
12
|
+
*/
|
|
13
|
+
declare const postgresRuntimeDriverDescriptor: RuntimeDriverDescriptor<'sql', 'postgres', PostgresRuntimeDriver>;
|
|
14
|
+
export default postgresRuntimeDriverDescriptor;
|
|
15
|
+
export type { CreatePostgresDriverOptions, PostgresDriverOptions, QueryResult, } from '../postgres-driver';
|
|
16
|
+
export { createPostgresDriver, createPostgresDriverFromOptions } from '../postgres-driver';
|
|
17
|
+
//# sourceMappingURL=runtime.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"runtime.d.ts","sourceRoot":"","sources":["../../src/exports/runtime.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,uBAAuB,EACvB,qBAAqB,EACtB,MAAM,yCAAyC,CAAC;AACjD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,sCAAsC,CAAC;AAKtE;;;;;GAKG;AACH,MAAM,MAAM,qBAAqB,GAAG,qBAAqB,CAAC,KAAK,EAAE,UAAU,CAAC,GAAG,SAAS,CAAC;AAEzF;;GAEG;AACH,QAAA,MAAM,+BAA+B,EAAE,uBAAuB,CAC5D,KAAK,EACL,UAAU,EACV,qBAAqB,CAMtB,CAAC;AAEF,eAAe,+BAA+B,CAAC;AAC/C,YAAY,EACV,2BAA2B,EAC3B,qBAAqB,EACrB,WAAW,GACZ,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,oBAAoB,EAAE,+BAA+B,EAAE,MAAM,oBAAoB,CAAC"}
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import {
|
|
2
|
+
isAlreadyConnectedError,
|
|
3
|
+
isPostgresError,
|
|
4
|
+
normalizePgError,
|
|
5
|
+
postgresDriverDescriptorMeta
|
|
6
|
+
} from "../chunk-M4WUUVS5.js";
|
|
7
|
+
|
|
8
|
+
// src/postgres-driver.ts
|
|
9
|
+
import { Pool } from "pg";
|
|
10
|
+
import Cursor from "pg-cursor";
|
|
11
|
+
|
|
12
|
+
// src/callback-to-promise.ts
|
|
13
|
+
function callbackToPromise(fn) {
|
|
14
|
+
return new Promise((resolve, reject) => {
|
|
15
|
+
fn((err, result) => {
|
|
16
|
+
if (err) {
|
|
17
|
+
reject(err);
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
resolve(result);
|
|
21
|
+
});
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// src/postgres-driver.ts
|
|
26
|
+
var DEFAULT_BATCH_SIZE = 100;
|
|
27
|
+
var PostgresDriverImpl = class {
|
|
28
|
+
pool;
|
|
29
|
+
directClient;
|
|
30
|
+
cursorBatchSize;
|
|
31
|
+
cursorDisabled;
|
|
32
|
+
constructor(options) {
|
|
33
|
+
if ("client" in options.connect) {
|
|
34
|
+
this.directClient = options.connect.client;
|
|
35
|
+
} else if ("pool" in options.connect) {
|
|
36
|
+
this.pool = options.connect.pool;
|
|
37
|
+
} else {
|
|
38
|
+
throw new Error("PostgresDriver requires a pool or client");
|
|
39
|
+
}
|
|
40
|
+
this.cursorBatchSize = options.cursor?.batchSize ?? DEFAULT_BATCH_SIZE;
|
|
41
|
+
this.cursorDisabled = options.cursor?.disabled ?? false;
|
|
42
|
+
}
|
|
43
|
+
async connect() {
|
|
44
|
+
}
|
|
45
|
+
async *execute(request) {
|
|
46
|
+
const client = await this.acquireClient();
|
|
47
|
+
try {
|
|
48
|
+
if (!this.cursorDisabled) {
|
|
49
|
+
try {
|
|
50
|
+
for await (const row of this.executeWithCursor(client, request.sql, request.params)) {
|
|
51
|
+
yield row;
|
|
52
|
+
}
|
|
53
|
+
return;
|
|
54
|
+
} catch (cursorError) {
|
|
55
|
+
if (!(cursorError instanceof Error)) {
|
|
56
|
+
throw cursorError;
|
|
57
|
+
}
|
|
58
|
+
if (isPostgresError(cursorError)) {
|
|
59
|
+
throw normalizePgError(cursorError);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
for await (const row of this.executeBuffered(client, request.sql, request.params)) {
|
|
64
|
+
yield row;
|
|
65
|
+
}
|
|
66
|
+
} catch (error) {
|
|
67
|
+
throw normalizePgError(error);
|
|
68
|
+
} finally {
|
|
69
|
+
await this.releaseClient(client);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
async explain(request) {
|
|
73
|
+
const text = `EXPLAIN (FORMAT JSON) ${request.sql}`;
|
|
74
|
+
const client = await this.acquireClient();
|
|
75
|
+
try {
|
|
76
|
+
const result = await client.query(text, request.params);
|
|
77
|
+
return { rows: result.rows };
|
|
78
|
+
} catch (error) {
|
|
79
|
+
throw normalizePgError(error);
|
|
80
|
+
} finally {
|
|
81
|
+
await this.releaseClient(client);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
async query(sql, params) {
|
|
85
|
+
const client = await this.acquireClient();
|
|
86
|
+
try {
|
|
87
|
+
const result = await client.query(sql, params);
|
|
88
|
+
return result;
|
|
89
|
+
} catch (error) {
|
|
90
|
+
throw normalizePgError(error);
|
|
91
|
+
} finally {
|
|
92
|
+
await this.releaseClient(client);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
async close() {
|
|
96
|
+
if (this.pool) {
|
|
97
|
+
if (!this.pool.ended) {
|
|
98
|
+
await this.pool.end();
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
if (this.directClient) {
|
|
102
|
+
const client = this.directClient;
|
|
103
|
+
if (!client._ending) {
|
|
104
|
+
await client.end();
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
async acquireClient() {
|
|
109
|
+
if (this.pool) {
|
|
110
|
+
return this.pool.connect();
|
|
111
|
+
}
|
|
112
|
+
if (this.directClient) {
|
|
113
|
+
const client = this.directClient;
|
|
114
|
+
const isConnected = client._connection !== void 0 && client._connection !== null && !client._ending;
|
|
115
|
+
if (!isConnected) {
|
|
116
|
+
try {
|
|
117
|
+
await this.directClient.connect();
|
|
118
|
+
} catch (error) {
|
|
119
|
+
if (!isAlreadyConnectedError(error)) {
|
|
120
|
+
throw error;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return this.directClient;
|
|
125
|
+
}
|
|
126
|
+
throw new Error("PostgresDriver requires a pool or client");
|
|
127
|
+
}
|
|
128
|
+
async releaseClient(client) {
|
|
129
|
+
if (this.pool) {
|
|
130
|
+
client.release();
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
async *executeWithCursor(client, sql, params) {
|
|
134
|
+
const cursor = client.query(new Cursor(sql, params));
|
|
135
|
+
try {
|
|
136
|
+
while (true) {
|
|
137
|
+
const rows = await readCursor(cursor, this.cursorBatchSize);
|
|
138
|
+
if (rows.length === 0) {
|
|
139
|
+
break;
|
|
140
|
+
}
|
|
141
|
+
for (const row of rows) {
|
|
142
|
+
yield row;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
} finally {
|
|
146
|
+
await closeCursor(cursor);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
async *executeBuffered(client, sql, params) {
|
|
150
|
+
const result = await client.query(sql, params);
|
|
151
|
+
for (const row of result.rows) {
|
|
152
|
+
yield row;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
function createPostgresDriver(connectionString, options) {
|
|
157
|
+
const PoolImpl = options?.poolFactory ?? Pool;
|
|
158
|
+
const pool = new PoolImpl({ connectionString });
|
|
159
|
+
return new PostgresDriverImpl({
|
|
160
|
+
connect: { pool },
|
|
161
|
+
cursor: options?.cursor
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
function createPostgresDriverFromOptions(options) {
|
|
165
|
+
return new PostgresDriverImpl(options);
|
|
166
|
+
}
|
|
167
|
+
function readCursor(cursor, size) {
|
|
168
|
+
return callbackToPromise((cb) => {
|
|
169
|
+
cursor.read(size, (err, rows) => cb(err, rows));
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
function closeCursor(cursor) {
|
|
173
|
+
return callbackToPromise((cb) => cursor.close(cb));
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// src/exports/runtime.ts
|
|
177
|
+
var postgresRuntimeDriverDescriptor = {
|
|
178
|
+
...postgresDriverDescriptorMeta,
|
|
179
|
+
create(options) {
|
|
180
|
+
return createPostgresDriverFromOptions(options);
|
|
181
|
+
}
|
|
182
|
+
};
|
|
183
|
+
var runtime_default = postgresRuntimeDriverDescriptor;
|
|
184
|
+
export {
|
|
185
|
+
createPostgresDriver,
|
|
186
|
+
createPostgresDriverFromOptions,
|
|
187
|
+
runtime_default as default
|
|
188
|
+
};
|
|
189
|
+
//# sourceMappingURL=runtime.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/postgres-driver.ts","../../src/callback-to-promise.ts","../../src/exports/runtime.ts"],"sourcesContent":["import type {\n SqlDriver,\n SqlExecuteRequest,\n SqlExplainResult,\n SqlQueryResult,\n} from '@prisma-next/sql-relational-core/ast';\nimport type {\n Client,\n QueryResult as PgQueryResult,\n PoolClient,\n Pool as PoolType,\n QueryResultRow,\n} from 'pg';\nimport { Pool } from 'pg';\nimport Cursor from 'pg-cursor';\nimport { callbackToPromise } from './callback-to-promise';\nimport { isAlreadyConnectedError, isPostgresError, normalizePgError } from './normalize-error';\n\nexport type QueryResult<T extends QueryResultRow = QueryResultRow> = PgQueryResult<T>;\n\nexport interface PostgresDriverOptions {\n readonly connect: { client: Client } | { pool: PoolType };\n readonly cursor?:\n | {\n readonly batchSize?: number;\n readonly disabled?: boolean;\n }\n | undefined;\n}\n\nconst DEFAULT_BATCH_SIZE = 100;\n\nclass PostgresDriverImpl implements SqlDriver {\n private readonly pool: PoolType | undefined;\n private readonly directClient: Client | undefined;\n private readonly cursorBatchSize: number;\n private readonly cursorDisabled: boolean;\n\n constructor(options: PostgresDriverOptions) {\n if ('client' in options.connect) {\n this.directClient = options.connect.client;\n } else if ('pool' in options.connect) {\n this.pool = options.connect.pool;\n } else {\n throw new Error('PostgresDriver requires a pool or client');\n }\n\n this.cursorBatchSize = options.cursor?.batchSize ?? DEFAULT_BATCH_SIZE;\n this.cursorDisabled = options.cursor?.disabled ?? false;\n }\n\n async connect(): Promise<void> {\n // No-op: caller controls connecting the underlying client or pool\n }\n\n async *execute<Row = Record<string, unknown>>(request: SqlExecuteRequest): AsyncIterable<Row> {\n const client = await this.acquireClient();\n try {\n if (!this.cursorDisabled) {\n try {\n for await (const row of this.executeWithCursor(client, request.sql, request.params)) {\n yield row as Row;\n }\n return;\n } catch (cursorError) {\n if (!(cursorError instanceof Error)) {\n throw cursorError;\n }\n // Check if this is a pg error - if so, normalize and throw\n // Otherwise, fall back to buffered mode for cursor-specific errors\n if (isPostgresError(cursorError)) {\n throw normalizePgError(cursorError);\n }\n // Not a pg error - cursor-specific error, fall back to buffered mode\n }\n }\n\n for await (const row of this.executeBuffered(client, request.sql, request.params)) {\n yield row as Row;\n }\n } catch (error) {\n throw normalizePgError(error);\n } finally {\n await this.releaseClient(client);\n }\n }\n\n async explain(request: SqlExecuteRequest): Promise<SqlExplainResult> {\n const text = `EXPLAIN (FORMAT JSON) ${request.sql}`;\n const client = await this.acquireClient();\n try {\n const result = await client.query(text, request.params as unknown[] | undefined);\n return { rows: result.rows as ReadonlyArray<Record<string, unknown>> };\n } catch (error) {\n throw normalizePgError(error);\n } finally {\n await this.releaseClient(client);\n }\n }\n\n async query<Row = Record<string, unknown>>(\n sql: string,\n params?: readonly unknown[],\n ): Promise<SqlQueryResult<Row>> {\n const client = await this.acquireClient();\n try {\n const result = await client.query(sql, params as unknown[] | undefined);\n return result as unknown as SqlQueryResult<Row>;\n } catch (error) {\n throw normalizePgError(error);\n } finally {\n await this.releaseClient(client);\n }\n }\n\n async close(): Promise<void> {\n if (this.pool) {\n // Check if pool is already closed to avoid \"Called end on pool more than once\" error\n // pg Pool has an 'ended' property that indicates if the pool has been closed\n if (!(this.pool as { ended?: boolean }).ended) {\n await this.pool.end();\n }\n }\n if (this.directClient) {\n const client = this.directClient as Client & { _ending?: boolean };\n if (!client._ending) {\n await client.end();\n }\n }\n }\n\n private async acquireClient(): Promise<PoolClient | Client> {\n if (this.pool) {\n return this.pool.connect();\n }\n if (this.directClient) {\n // Check if client is already connected before attempting to connect\n // This prevents hanging when the database only supports a single connection\n // pg's Client has internal connection state that we can check\n const client = this.directClient as Client & {\n _ending?: boolean;\n _connection?: unknown;\n };\n const isConnected =\n client._connection !== undefined && client._connection !== null && !client._ending;\n\n // Only connect if not already connected\n // If caller provided a connected client (e.g., in tests), use it as-is\n if (!isConnected) {\n try {\n await this.directClient.connect();\n } catch (error: unknown) {\n // If already connected, pg throws an error - ignore it and proceed\n // Re-throw other errors (actual connection failures)\n if (!isAlreadyConnectedError(error)) {\n throw error;\n }\n }\n }\n return this.directClient;\n }\n throw new Error('PostgresDriver requires a pool or client');\n }\n\n private async releaseClient(client: PoolClient | Client): Promise<void> {\n if (this.pool) {\n (client as PoolClient).release();\n }\n }\n\n private async *executeWithCursor(\n client: PoolClient | Client,\n sql: string,\n params: readonly unknown[] | undefined,\n ): AsyncIterable<Record<string, unknown>> {\n const cursor = client.query(new Cursor(sql, params as unknown[] | undefined));\n\n try {\n while (true) {\n const rows = await readCursor(cursor, this.cursorBatchSize);\n if (rows.length === 0) {\n break;\n }\n\n for (const row of rows) {\n yield row;\n }\n }\n } finally {\n await closeCursor(cursor);\n }\n }\n\n private async *executeBuffered(\n client: PoolClient | Client,\n sql: string,\n params: readonly unknown[] | undefined,\n ): AsyncIterable<Record<string, unknown>> {\n const result = await client.query(sql, params as unknown[] | undefined);\n for (const row of result.rows as Record<string, unknown>[]) {\n yield row;\n }\n }\n}\n\nexport interface CreatePostgresDriverOptions {\n readonly cursor?: PostgresDriverOptions['cursor'];\n readonly poolFactory?: typeof Pool;\n}\n\nexport function createPostgresDriver(\n connectionString: string,\n options?: CreatePostgresDriverOptions,\n): SqlDriver {\n const PoolImpl: typeof Pool = options?.poolFactory ?? Pool;\n const pool = new PoolImpl({ connectionString });\n return new PostgresDriverImpl({\n connect: { pool },\n cursor: options?.cursor,\n });\n}\n\nexport function createPostgresDriverFromOptions(options: PostgresDriverOptions): SqlDriver {\n return new PostgresDriverImpl(options);\n}\n\nfunction readCursor<Row>(cursor: Cursor<Row>, size: number): Promise<Row[]> {\n return callbackToPromise<Row[]>((cb) => {\n cursor.read(size, (err, rows) => cb(err, rows));\n });\n}\n\nfunction closeCursor(cursor: Cursor<unknown>): Promise<void> {\n return callbackToPromise((cb) => cursor.close(cb));\n}\n","/**\n * Wraps a Node-style callback function into a Promise.\n * Supports both (err) => void and (err, result) => void patterns.\n */\nexport function callbackToPromise(\n fn: (callback: (err: Error | null | undefined) => void) => void,\n): Promise<void>;\nexport function callbackToPromise<T>(\n fn: (callback: (err: Error | null | undefined, result: T) => void) => void,\n): Promise<T>;\nexport function callbackToPromise<T = void>(\n fn: (callback: (err: Error | null | undefined, result?: T) => void) => void,\n): Promise<T> {\n return new Promise<T>((resolve, reject) => {\n fn((err, result) => {\n if (err) {\n reject(err);\n return;\n }\n resolve(result as T);\n });\n });\n}\n","import type {\n RuntimeDriverDescriptor,\n RuntimeDriverInstance,\n} from '@prisma-next/core-execution-plane/types';\nimport type { SqlDriver } from '@prisma-next/sql-relational-core/ast';\nimport { postgresDriverDescriptorMeta } from '../core/descriptor-meta';\nimport type { PostgresDriverOptions } from '../postgres-driver';\nimport { createPostgresDriverFromOptions } from '../postgres-driver';\n\n/**\n * Postgres runtime driver instance interface.\n * SqlDriver provides SQL-specific methods (execute, explain, close).\n * RuntimeDriverInstance provides target identification (familyId, targetId).\n * We use intersection type to combine both interfaces.\n */\nexport type PostgresRuntimeDriver = RuntimeDriverInstance<'sql', 'postgres'> & SqlDriver;\n\n/**\n * Postgres driver descriptor for runtime plane.\n */\nconst postgresRuntimeDriverDescriptor: RuntimeDriverDescriptor<\n 'sql',\n 'postgres',\n PostgresRuntimeDriver\n> = {\n ...postgresDriverDescriptorMeta,\n create(options: PostgresDriverOptions): PostgresRuntimeDriver {\n return createPostgresDriverFromOptions(options) as PostgresRuntimeDriver;\n },\n};\n\nexport default postgresRuntimeDriverDescriptor;\nexport type {\n CreatePostgresDriverOptions,\n PostgresDriverOptions,\n QueryResult,\n} from '../postgres-driver';\nexport { createPostgresDriver, createPostgresDriverFromOptions } from '../postgres-driver';\n"],"mappings":";;;;;;;;AAaA,SAAS,YAAY;AACrB,OAAO,YAAY;;;ACJZ,SAAS,kBACd,IACY;AACZ,SAAO,IAAI,QAAW,CAAC,SAAS,WAAW;AACzC,OAAG,CAAC,KAAK,WAAW;AAClB,UAAI,KAAK;AACP,eAAO,GAAG;AACV;AAAA,MACF;AACA,cAAQ,MAAW;AAAA,IACrB,CAAC;AAAA,EACH,CAAC;AACH;;;ADQA,IAAM,qBAAqB;AAE3B,IAAM,qBAAN,MAA8C;AAAA,EAC3B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EAEjB,YAAY,SAAgC;AAC1C,QAAI,YAAY,QAAQ,SAAS;AAC/B,WAAK,eAAe,QAAQ,QAAQ;AAAA,IACtC,WAAW,UAAU,QAAQ,SAAS;AACpC,WAAK,OAAO,QAAQ,QAAQ;AAAA,IAC9B,OAAO;AACL,YAAM,IAAI,MAAM,0CAA0C;AAAA,IAC5D;AAEA,SAAK,kBAAkB,QAAQ,QAAQ,aAAa;AACpD,SAAK,iBAAiB,QAAQ,QAAQ,YAAY;AAAA,EACpD;AAAA,EAEA,MAAM,UAAyB;AAAA,EAE/B;AAAA,EAEA,OAAO,QAAuC,SAAgD;AAC5F,UAAM,SAAS,MAAM,KAAK,cAAc;AACxC,QAAI;AACF,UAAI,CAAC,KAAK,gBAAgB;AACxB,YAAI;AACF,2BAAiB,OAAO,KAAK,kBAAkB,QAAQ,QAAQ,KAAK,QAA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M,OAAO,IAAI,SAAS,EAAE,iBAAiB,CAAC;AAC9C,SAAO,IAAI,mBAAmB;AAAA,IAC5B,SAAS,EAAE,KAAK;AAAA,IAChB,QAAQ,SAAS;AAAA,EACnB,CAAC;AACH;AAEO,SAAS,gCAAgC,SAA2C;AACzF,SAAO,IAAI,mBAAmB,OAAO;AACvC;AAEA,SAAS,WAAgB,QAAqB,MAA8B;AAC1E,SAAO,kBAAyB,CAAC,OAAO;AACtC,WAAO,KAAK,MAAM,CAAC,KAAK,SAAS,GAAG,KAAK,IAAI,CAAC;AAAA,EAChD,CAAC;AACH;AAEA,SAAS,YAAY,QAAwC;AAC3D,SAAO,kBAAkB,CAAC,OAAO,OAAO,MAAM,EAAE,CAAC;AACnD;;;AEtNA,IAAM,kCAIF;AAAA,EACF,GAAG;AAAA,EACH,OAAO,SAAuD;AAC5D,WAAO,gCAAgC,OAAO;AAAA,EAChD;AACF;AAEA,IAAO,kBAAQ;","names":[]}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { SqlConnectionError, SqlQueryError } from '@prisma-next/sql-errors';
|
|
2
|
+
/**
|
|
3
|
+
* Postgres error shape from the pg library.
|
|
4
|
+
*
|
|
5
|
+
* Note: The pg library doesn't export a DatabaseError type or interface, but errors
|
|
6
|
+
* thrown by pg.query() and pg.Client have this shape at runtime. We define this
|
|
7
|
+
* interface to match the actual runtime structure documented in the pg library
|
|
8
|
+
* (https://github.com/brianc/node-postgres/blob/master/packages/pg/lib/errors.js).
|
|
9
|
+
*
|
|
10
|
+
* The @types/pg package also doesn't provide comprehensive error type definitions,
|
|
11
|
+
* so we define our own interface based on the runtime error properties.
|
|
12
|
+
*/
|
|
13
|
+
interface PostgresError extends Error {
|
|
14
|
+
readonly code?: string;
|
|
15
|
+
readonly constraint?: string;
|
|
16
|
+
readonly table?: string;
|
|
17
|
+
readonly column?: string;
|
|
18
|
+
readonly detail?: string;
|
|
19
|
+
readonly hint?: string;
|
|
20
|
+
readonly position?: string;
|
|
21
|
+
readonly internalPosition?: string;
|
|
22
|
+
readonly internalQuery?: string;
|
|
23
|
+
readonly where?: string;
|
|
24
|
+
readonly schema?: string;
|
|
25
|
+
readonly file?: string;
|
|
26
|
+
readonly line?: string;
|
|
27
|
+
readonly routine?: string;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Type predicate to check if an error is a Postgres error from the pg library.
|
|
31
|
+
*
|
|
32
|
+
* Distinguishes pg library errors from Node.js system errors by checking for:
|
|
33
|
+
* - SQLSTATE codes (5-character alphanumeric codes like '23505', '42601')
|
|
34
|
+
* - pg-specific properties (constraint, table, column, hint, etc.) that Node.js errors don't have
|
|
35
|
+
*
|
|
36
|
+
* Node.js system errors (ECONNREFUSED, ETIMEDOUT, etc.) are excluded to prevent false positives.
|
|
37
|
+
*/
|
|
38
|
+
export declare function isPostgresError(error: unknown): error is PostgresError;
|
|
39
|
+
/**
|
|
40
|
+
* Checks if an error is an "already connected" error from pg.Client.connect().
|
|
41
|
+
* When calling connect() on an already-connected client, pg throws an error that can be safely ignored.
|
|
42
|
+
*/
|
|
43
|
+
export declare function isAlreadyConnectedError(error: unknown): error is Error;
|
|
44
|
+
/**
|
|
45
|
+
* Normalizes a Postgres error into a SQL-shared error type.
|
|
46
|
+
*
|
|
47
|
+
* - Postgres SQLSTATE errors (5-char codes like '23505') → SqlQueryError
|
|
48
|
+
* - Connection errors (ECONNRESET, ETIMEDOUT, etc.) → SqlConnectionError
|
|
49
|
+
* - Unknown errors → returns the original error as-is
|
|
50
|
+
*
|
|
51
|
+
* The original error is preserved via Error.cause to maintain stack traces.
|
|
52
|
+
*
|
|
53
|
+
* @param error - The error to normalize (typically from pg library)
|
|
54
|
+
* @returns SqlQueryError for query-related failures
|
|
55
|
+
* @returns SqlConnectionError for connection-related failures
|
|
56
|
+
* @returns The original error if it cannot be normalized
|
|
57
|
+
*/
|
|
58
|
+
export declare function normalizePgError(error: unknown): SqlQueryError | SqlConnectionError | Error;
|
|
59
|
+
export {};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G,aAAa,GAAG,kBAAkB,GAAG,KAAK,CAiD3F"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { SqlDriver } from '@prisma-next/sql-relational-core/ast';
|
|
2
|
+
import type { Client, QueryResult as PgQueryResult, Pool as PoolType, QueryResultRow } from 'pg';
|
|
3
|
+
import { Pool } from 'pg';
|
|
4
|
+
export type QueryResult<T extends QueryResultRow = QueryResultRow> = PgQueryResult<T>;
|
|
5
|
+
export interface PostgresDriverOptions {
|
|
6
|
+
readonly connect: {
|
|
7
|
+
client: Client;
|
|
8
|
+
} | {
|
|
9
|
+
pool: PoolType;
|
|
10
|
+
};
|
|
11
|
+
readonly cursor?: {
|
|
12
|
+
readonly batchSize?: number;
|
|
13
|
+
readonly disabled?: boolean;
|
|
14
|
+
} | undefined;
|
|
15
|
+
}
|
|
16
|
+
export interface CreatePostgresDriverOptions {
|
|
17
|
+
readonly cursor?: PostgresDriverOptions['cursor'];
|
|
18
|
+
readonly poolFactory?: typeof Pool;
|
|
19
|
+
}
|
|
20
|
+
export declare function createPostgresDriver(connectionString: string, options?: CreatePostgresDriverOptions): SqlDriver;
|
|
21
|
+
export declare function createPostgresDriverFromOptions(options: PostgresDriverOptions): SqlDriver;
|
|
22
|
+
//# sourceMappingURL=postgres-driver.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"postgres-driver.d.ts","sourceRoot":"","sources":["../src/postgres-driver.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,SAAS,EAIV,MAAM,sCAAsC,CAAC;AAC9C,OAAO,KAAK,EACV,MAAM,EACN,WAAW,IAAI,aAAa,EAE5B,IAAI,IAAI,QAAQ,EAChB,cAAc,EACf,MAAM,IAAI,CAAC;AACZ,OAAO,EAAE,IAAI,EAAE,MAAM,IAAI,CAAC;AAK1B,MAAM,MAAM,WAAW,CAAC,CAAC,SAAS,cAAc,GAAG,cAAc,IAAI,aAAa,CAAC,CAAC,CAAC,CAAC;AAEtF,MAAM,WAAW,qBAAqB;IACpC,QAAQ,CAAC,OAAO,EAAE;QAAE,MAAM,EAAE,MAAM,CAAA;KAAE,GAAG;QAAE,IAAI,EAAE,QAAQ,CAAA;KAAE,CAAC;IAC1D,QAAQ,CAAC,MAAM,CAAC,EACZ;QACE,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;QAC5B,QAAQ,CAAC,QAAQ,CAAC,EAAE,OAAO,CAAC;KAC7B,GACD,SAAS,CAAC;CACf;AAiLD,MAAM,WAAW,2BAA2B;IAC1C,QAAQ,CAAC,MAAM,CAAC,EAAE,qBAAqB,CAAC,QAAQ,CAAC,CAAC;IAClD,QAAQ,CAAC,WAAW,CAAC,EAAE,OAAO,IAAI,CAAC;CACpC;AAED,wBAAgB,oBAAoB,CAClC,gBAAgB,EAAE,MAAM,EACxB,OAAO,CAAC,EAAE,2BAA2B,GACpC,SAAS,CAOX;AAED,wBAAgB,+BAA+B,CAAC,OAAO,EAAE,qBAAqB,GAAG,SAAS,CAEzF"}
|
package/package.json
CHANGED
|
@@ -1,49 +1,50 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prisma-next/driver-postgres",
|
|
3
|
-
"version": "0.3.0-pr.
|
|
3
|
+
"version": "0.3.0-pr.95.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"sideEffects": false,
|
|
6
|
-
"engines": {
|
|
7
|
-
"node": ">=20"
|
|
8
|
-
},
|
|
9
6
|
"dependencies": {
|
|
10
7
|
"arktype": "^2.0.0",
|
|
11
8
|
"pg": "8.16.3",
|
|
12
9
|
"pg-cursor": "^2.10.5",
|
|
13
|
-
"@prisma-next/contract": "0.3.0-pr.
|
|
14
|
-
"@prisma-next/core-control-plane": "0.3.0-pr.
|
|
15
|
-
"@prisma-next/core-execution-plane": "0.3.0-pr.
|
|
16
|
-
"@prisma-next/sql-contract": "0.3.0-pr.
|
|
17
|
-
"@prisma-next/sql-errors": "0.3.0-pr.
|
|
18
|
-
"@prisma-next/sql-
|
|
19
|
-
"@prisma-next/sql-
|
|
20
|
-
"@prisma-next/utils": "0.3.0-pr.
|
|
10
|
+
"@prisma-next/contract": "0.3.0-pr.95.2",
|
|
11
|
+
"@prisma-next/core-control-plane": "0.3.0-pr.95.2",
|
|
12
|
+
"@prisma-next/core-execution-plane": "0.3.0-pr.95.2",
|
|
13
|
+
"@prisma-next/sql-contract": "0.3.0-pr.95.2",
|
|
14
|
+
"@prisma-next/sql-errors": "0.3.0-pr.95.2",
|
|
15
|
+
"@prisma-next/sql-operations": "0.3.0-pr.95.2",
|
|
16
|
+
"@prisma-next/sql-relational-core": "0.3.0-pr.95.2",
|
|
17
|
+
"@prisma-next/utils": "0.3.0-pr.95.2"
|
|
21
18
|
},
|
|
22
19
|
"devDependencies": {
|
|
23
20
|
"@types/pg": "8.16.0",
|
|
24
21
|
"@types/pg-cursor": "^2.4.6",
|
|
25
22
|
"pg-mem": "^3.0.5",
|
|
26
|
-
"
|
|
23
|
+
"tsup": "8.5.1",
|
|
27
24
|
"typescript": "5.9.3",
|
|
28
25
|
"vitest": "4.0.16",
|
|
29
26
|
"@prisma-next/test-utils": "0.0.1",
|
|
30
|
-
"@prisma-next/tsconfig": "0.0.0"
|
|
31
|
-
"@prisma-next/tsdown": "0.0.0"
|
|
27
|
+
"@prisma-next/tsconfig": "0.0.0"
|
|
32
28
|
},
|
|
33
29
|
"files": [
|
|
34
30
|
"dist",
|
|
35
31
|
"src"
|
|
36
32
|
],
|
|
37
33
|
"exports": {
|
|
38
|
-
"./control":
|
|
39
|
-
|
|
40
|
-
|
|
34
|
+
"./control": {
|
|
35
|
+
"types": "./dist/exports/control.d.ts",
|
|
36
|
+
"import": "./dist/exports/control.js"
|
|
37
|
+
},
|
|
38
|
+
"./runtime": {
|
|
39
|
+
"types": "./dist/exports/runtime.d.ts",
|
|
40
|
+
"import": "./dist/exports/runtime.js"
|
|
41
|
+
}
|
|
41
42
|
},
|
|
42
43
|
"scripts": {
|
|
43
|
-
"build": "
|
|
44
|
+
"build": "tsup --config tsup.config.ts && tsc --project tsconfig.build.json",
|
|
44
45
|
"test": "vitest run",
|
|
45
46
|
"test:coverage": "vitest run --coverage",
|
|
46
|
-
"typecheck": "tsc --noEmit",
|
|
47
|
+
"typecheck": "tsc --project tsconfig.json --noEmit",
|
|
47
48
|
"lint": "biome check . --error-on-warnings",
|
|
48
49
|
"lint:fix": "biome check --write .",
|
|
49
50
|
"lint:fix:unsafe": "biome check --write --unsafe .",
|
package/dist/control.d.mts
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { Client } from "pg";
|
|
2
|
-
import { ControlDriverDescriptor, ControlDriverInstance } from "@prisma-next/core-control-plane/types";
|
|
3
|
-
|
|
4
|
-
//#region src/exports/control.d.ts
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Postgres control driver instance for control-plane operations.
|
|
8
|
-
* Implements ControlDriverInstance<'sql', 'postgres'> for database queries.
|
|
9
|
-
*/
|
|
10
|
-
declare class PostgresControlDriver implements ControlDriverInstance<'sql', 'postgres'> {
|
|
11
|
-
private readonly client;
|
|
12
|
-
readonly familyId: "sql";
|
|
13
|
-
readonly targetId: "postgres";
|
|
14
|
-
/**
|
|
15
|
-
* @deprecated Use targetId instead
|
|
16
|
-
*/
|
|
17
|
-
readonly target: "postgres";
|
|
18
|
-
constructor(client: Client);
|
|
19
|
-
query<Row = Record<string, unknown>>(sql: string, params?: readonly unknown[]): Promise<{
|
|
20
|
-
readonly rows: Row[];
|
|
21
|
-
}>;
|
|
22
|
-
close(): Promise<void>;
|
|
23
|
-
}
|
|
24
|
-
/**
|
|
25
|
-
* Postgres driver descriptor for CLI config.
|
|
26
|
-
*/
|
|
27
|
-
declare const postgresDriverDescriptor: ControlDriverDescriptor<'sql', 'postgres', PostgresControlDriver>;
|
|
28
|
-
//#endregion
|
|
29
|
-
export { PostgresControlDriver, postgresDriverDescriptor as default };
|
|
30
|
-
//# sourceMappingURL=control.d.mts.map
|
package/dist/control.d.mts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"control.d.mts","names":[],"sources":["../src/exports/control.ts"],"sourcesContent":[],"mappings":";;;;;;;AAeA;;AAUoB,cAVP,qBAAA,YAAiC,qBAU1B,CAAA,KAAA,EAAA,UAAA,CAAA,CAAA;EAGU,iBAAA,MAAA;EAAzB,SAAA,QAAA,EAAA,KAAA;EASY,SAAA,QAAA,EAAA,UAAA;EAtB6B;;AAyB7C;;sBAjBsC;cAEnB,oEAGf;mBAAyB;;WASb;;;;;cAQX,0BAA0B,2CAA2C"}
|
package/dist/control.mjs
DELETED
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
import { i as postgresDriverDescriptorMeta, r as normalizePgError } from "./normalize-error-BU6yV-XB.mjs";
|
|
2
|
-
import { errorRuntime } from "@prisma-next/core-control-plane/errors";
|
|
3
|
-
import { SqlQueryError } from "@prisma-next/sql-errors";
|
|
4
|
-
import { redactDatabaseUrl } from "@prisma-next/utils/redact-db-url";
|
|
5
|
-
import { Client } from "pg";
|
|
6
|
-
|
|
7
|
-
//#region src/exports/control.ts
|
|
8
|
-
/**
|
|
9
|
-
* Postgres control driver instance for control-plane operations.
|
|
10
|
-
* Implements ControlDriverInstance<'sql', 'postgres'> for database queries.
|
|
11
|
-
*/
|
|
12
|
-
var PostgresControlDriver = class {
|
|
13
|
-
familyId = "sql";
|
|
14
|
-
targetId = "postgres";
|
|
15
|
-
/**
|
|
16
|
-
* @deprecated Use targetId instead
|
|
17
|
-
*/
|
|
18
|
-
target = "postgres";
|
|
19
|
-
constructor(client) {
|
|
20
|
-
this.client = client;
|
|
21
|
-
}
|
|
22
|
-
async query(sql, params) {
|
|
23
|
-
try {
|
|
24
|
-
return { rows: (await this.client.query(sql, params)).rows };
|
|
25
|
-
} catch (error) {
|
|
26
|
-
throw normalizePgError(error);
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
async close() {
|
|
30
|
-
await this.client.end();
|
|
31
|
-
}
|
|
32
|
-
};
|
|
33
|
-
/**
|
|
34
|
-
* Postgres driver descriptor for CLI config.
|
|
35
|
-
*/
|
|
36
|
-
const postgresDriverDescriptor = {
|
|
37
|
-
...postgresDriverDescriptorMeta,
|
|
38
|
-
async create(url) {
|
|
39
|
-
const client = new Client({ connectionString: url });
|
|
40
|
-
try {
|
|
41
|
-
await client.connect();
|
|
42
|
-
return new PostgresControlDriver(client);
|
|
43
|
-
} catch (error) {
|
|
44
|
-
const normalized = normalizePgError(error);
|
|
45
|
-
const redacted = redactDatabaseUrl(url);
|
|
46
|
-
try {
|
|
47
|
-
await client.end();
|
|
48
|
-
} catch {}
|
|
49
|
-
const codeFromSqlState = SqlQueryError.is(normalized) ? normalized.sqlState : void 0;
|
|
50
|
-
const causeCode = "cause" in normalized && normalized.cause ? normalized.cause.code : void 0;
|
|
51
|
-
const code = codeFromSqlState ?? causeCode;
|
|
52
|
-
throw errorRuntime("Database connection failed", {
|
|
53
|
-
why: normalized.message,
|
|
54
|
-
fix: "Verify the database URL, ensure the database is reachable, and confirm credentials/permissions",
|
|
55
|
-
meta: {
|
|
56
|
-
...typeof code !== "undefined" ? { code } : {},
|
|
57
|
-
...redacted
|
|
58
|
-
}
|
|
59
|
-
});
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
};
|
|
63
|
-
var control_default = postgresDriverDescriptor;
|
|
64
|
-
|
|
65
|
-
//#endregion
|
|
66
|
-
export { PostgresControlDriver, control_default as default };
|
|
67
|
-
//# sourceMappingURL=control.mjs.map
|
package/dist/control.mjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"control.mjs","names":["client: Client","postgresDriverDescriptor: ControlDriverDescriptor<'sql', 'postgres', PostgresControlDriver>"],"sources":["../src/exports/control.ts"],"sourcesContent":["import { errorRuntime } from '@prisma-next/core-control-plane/errors';\nimport type {\n ControlDriverDescriptor,\n ControlDriverInstance,\n} from '@prisma-next/core-control-plane/types';\nimport { SqlQueryError } from '@prisma-next/sql-errors';\nimport { redactDatabaseUrl } from '@prisma-next/utils/redact-db-url';\nimport { Client } from 'pg';\nimport { postgresDriverDescriptorMeta } from '../core/descriptor-meta';\nimport { normalizePgError } from '../normalize-error';\n\n/**\n * Postgres control driver instance for control-plane operations.\n * Implements ControlDriverInstance<'sql', 'postgres'> for database queries.\n */\nexport class PostgresControlDriver implements ControlDriverInstance<'sql', 'postgres'> {\n readonly familyId = 'sql' as const;\n readonly targetId = 'postgres' as const;\n /**\n * @deprecated Use targetId instead\n */\n readonly target = 'postgres' as const;\n\n constructor(private readonly client: Client) {}\n\n async query<Row = Record<string, unknown>>(\n sql: string,\n params?: readonly unknown[],\n ): Promise<{ readonly rows: Row[] }> {\n try {\n const result = await this.client.query(sql, params as unknown[] | undefined);\n return { rows: result.rows as Row[] };\n } catch (error) {\n throw normalizePgError(error);\n }\n }\n\n async close(): Promise<void> {\n await this.client.end();\n }\n}\n\n/**\n * Postgres driver descriptor for CLI config.\n */\nconst postgresDriverDescriptor: ControlDriverDescriptor<'sql', 'postgres', PostgresControlDriver> =\n {\n ...postgresDriverDescriptorMeta,\n async create(url: string): Promise<PostgresControlDriver> {\n const client = new Client({ connectionString: url });\n try {\n await client.connect();\n return new PostgresControlDriver(client);\n } catch (error) {\n const normalized = normalizePgError(error);\n const redacted = redactDatabaseUrl(url);\n try {\n await client.end();\n } catch {\n // ignore\n }\n\n const codeFromSqlState = SqlQueryError.is(normalized) ? normalized.sqlState : undefined;\n const causeCode =\n 'cause' in normalized && normalized.cause\n ? (normalized.cause as { code?: unknown }).code\n : undefined;\n const code = codeFromSqlState ?? causeCode;\n\n throw errorRuntime('Database connection failed', {\n why: normalized.message,\n fix: 'Verify the database URL, ensure the database is reachable, and confirm credentials/permissions',\n meta: {\n ...(typeof code !== 'undefined' ? { code } : {}),\n ...redacted,\n },\n });\n }\n },\n };\n\nexport default postgresDriverDescriptor;\n"],"mappings":";;;;;;;;;;;AAeA,IAAa,wBAAb,MAAuF;CACrF,AAAS,WAAW;CACpB,AAAS,WAAW;;;;CAIpB,AAAS,SAAS;CAElB,YAAY,AAAiBA,QAAgB;EAAhB;;CAE7B,MAAM,MACJ,KACA,QACmC;AACnC,MAAI;AAEF,UAAO,EAAE,OADM,MAAM,KAAK,OAAO,MAAM,KAAK,OAAgC,EACtD,MAAe;WAC9B,OAAO;AACd,SAAM,iBAAiB,MAAM;;;CAIjC,MAAM,QAAuB;AAC3B,QAAM,KAAK,OAAO,KAAK;;;;;;AAO3B,MAAMC,2BACJ;CACE,GAAG;CACH,MAAM,OAAO,KAA6C;EACxD,MAAM,SAAS,IAAI,OAAO,EAAE,kBAAkB,KAAK,CAAC;AACpD,MAAI;AACF,SAAM,OAAO,SAAS;AACtB,UAAO,IAAI,sBAAsB,OAAO;WACjC,OAAO;GACd,MAAM,aAAa,iBAAiB,MAAM;GAC1C,MAAM,WAAW,kBAAkB,IAAI;AACvC,OAAI;AACF,UAAM,OAAO,KAAK;WACZ;GAIR,MAAM,mBAAmB,cAAc,GAAG,WAAW,GAAG,WAAW,WAAW;GAC9E,MAAM,YACJ,WAAW,cAAc,WAAW,QAC/B,WAAW,MAA6B,OACzC;GACN,MAAM,OAAO,oBAAoB;AAEjC,SAAM,aAAa,8BAA8B;IAC/C,KAAK,WAAW;IAChB,KAAK;IACL,MAAM;KACJ,GAAI,OAAO,SAAS,cAAc,EAAE,MAAM,GAAG,EAAE;KAC/C,GAAG;KACJ;IACF,CAAC;;;CAGP;AAEH,sBAAe"}
|
|
@@ -1,125 +0,0 @@
|
|
|
1
|
-
import { SqlConnectionError, SqlQueryError } from "@prisma-next/sql-errors";
|
|
2
|
-
|
|
3
|
-
//#region src/core/descriptor-meta.ts
|
|
4
|
-
const postgresDriverDescriptorMeta = {
|
|
5
|
-
kind: "driver",
|
|
6
|
-
familyId: "sql",
|
|
7
|
-
targetId: "postgres",
|
|
8
|
-
id: "postgres",
|
|
9
|
-
version: "0.0.1",
|
|
10
|
-
capabilities: {}
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
//#endregion
|
|
14
|
-
//#region src/normalize-error.ts
|
|
15
|
-
/**
|
|
16
|
-
* Checks if an error is a connection-related error.
|
|
17
|
-
*/
|
|
18
|
-
function isConnectionError(error) {
|
|
19
|
-
const code = error.code;
|
|
20
|
-
if (code) {
|
|
21
|
-
if (code === "ECONNRESET" || code === "ETIMEDOUT" || code === "ECONNREFUSED" || code === "ENOTFOUND" || code === "EHOSTUNREACH") return true;
|
|
22
|
-
}
|
|
23
|
-
const message = error.message.toLowerCase();
|
|
24
|
-
if (message.includes("connection terminated") || message.includes("connection closed") || message.includes("connection refused") || message.includes("connection timeout") || message.includes("connection reset")) return true;
|
|
25
|
-
return false;
|
|
26
|
-
}
|
|
27
|
-
/**
|
|
28
|
-
* Checks if a connection error is transient (might succeed on retry).
|
|
29
|
-
*/
|
|
30
|
-
function isTransientConnectionError(error) {
|
|
31
|
-
const code = error.code;
|
|
32
|
-
if (code) {
|
|
33
|
-
if (code === "ETIMEDOUT" || code === "ECONNRESET") return true;
|
|
34
|
-
if (code === "ECONNREFUSED") return false;
|
|
35
|
-
}
|
|
36
|
-
const message = error.message.toLowerCase();
|
|
37
|
-
if (message.includes("timeout") || message.includes("connection reset")) return true;
|
|
38
|
-
return false;
|
|
39
|
-
}
|
|
40
|
-
/**
|
|
41
|
-
* PostgreSQL-specific error properties that indicate an error originated from pg library.
|
|
42
|
-
* These properties are not present on Node.js system errors.
|
|
43
|
-
* Excludes generic properties like 'detail', 'file', 'line', and 'position' that could appear on any error.
|
|
44
|
-
*/
|
|
45
|
-
const PG_ERROR_PROPERTIES = [
|
|
46
|
-
"constraint",
|
|
47
|
-
"table",
|
|
48
|
-
"column",
|
|
49
|
-
"hint",
|
|
50
|
-
"internalPosition",
|
|
51
|
-
"internalQuery",
|
|
52
|
-
"where",
|
|
53
|
-
"schema",
|
|
54
|
-
"routine"
|
|
55
|
-
];
|
|
56
|
-
/**
|
|
57
|
-
* Type predicate to check if an error is a Postgres error from the pg library.
|
|
58
|
-
*
|
|
59
|
-
* Distinguishes pg library errors from Node.js system errors by checking for:
|
|
60
|
-
* - SQLSTATE codes (5-character alphanumeric codes like '23505', '42601')
|
|
61
|
-
* - pg-specific properties (constraint, table, column, hint, etc.) that Node.js errors don't have
|
|
62
|
-
*
|
|
63
|
-
* Node.js system errors (ECONNREFUSED, ETIMEDOUT, etc.) are excluded to prevent false positives.
|
|
64
|
-
*/
|
|
65
|
-
function isPostgresError(error) {
|
|
66
|
-
if (!(error instanceof Error)) return false;
|
|
67
|
-
const pgError = error;
|
|
68
|
-
if (pgError.code && isPostgresSqlState(pgError.code)) return true;
|
|
69
|
-
return PG_ERROR_PROPERTIES.some((prop) => pgError[prop] !== void 0);
|
|
70
|
-
}
|
|
71
|
-
/**
|
|
72
|
-
* Checks if an error is an "already connected" error from pg.Client.connect().
|
|
73
|
-
* When calling connect() on an already-connected client, pg throws an error that can be safely ignored.
|
|
74
|
-
*/
|
|
75
|
-
function isAlreadyConnectedError(error) {
|
|
76
|
-
if (!(error instanceof Error)) return false;
|
|
77
|
-
const message = error.message.toLowerCase();
|
|
78
|
-
return message.includes("already") && message.includes("connected");
|
|
79
|
-
}
|
|
80
|
-
/**
|
|
81
|
-
* Checks if an error code is a Postgres SQLSTATE (5-character alphanumeric code).
|
|
82
|
-
* SQLSTATE codes are standardized SQL error codes (e.g., '23505' for unique violation).
|
|
83
|
-
*/
|
|
84
|
-
function isPostgresSqlState(code) {
|
|
85
|
-
if (!code) return false;
|
|
86
|
-
return /^[A-Z0-9]{5}$/.test(code);
|
|
87
|
-
}
|
|
88
|
-
/**
|
|
89
|
-
* Normalizes a Postgres error into a SQL-shared error type.
|
|
90
|
-
*
|
|
91
|
-
* - Postgres SQLSTATE errors (5-char codes like '23505') → SqlQueryError
|
|
92
|
-
* - Connection errors (ECONNRESET, ETIMEDOUT, etc.) → SqlConnectionError
|
|
93
|
-
* - Unknown errors → returns the original error as-is
|
|
94
|
-
*
|
|
95
|
-
* The original error is preserved via Error.cause to maintain stack traces.
|
|
96
|
-
*
|
|
97
|
-
* @param error - The error to normalize (typically from pg library)
|
|
98
|
-
* @returns SqlQueryError for query-related failures
|
|
99
|
-
* @returns SqlConnectionError for connection-related failures
|
|
100
|
-
* @returns The original error if it cannot be normalized
|
|
101
|
-
*/
|
|
102
|
-
function normalizePgError(error) {
|
|
103
|
-
if (!(error instanceof Error)) return new Error(String(error));
|
|
104
|
-
const pgError = error;
|
|
105
|
-
if (isPostgresSqlState(pgError.code)) {
|
|
106
|
-
const options = {
|
|
107
|
-
cause: error,
|
|
108
|
-
sqlState: pgError.code
|
|
109
|
-
};
|
|
110
|
-
if (pgError.constraint !== void 0) options.constraint = pgError.constraint;
|
|
111
|
-
if (pgError.table !== void 0) options.table = pgError.table;
|
|
112
|
-
if (pgError.column !== void 0) options.column = pgError.column;
|
|
113
|
-
if (pgError.detail !== void 0) options.detail = pgError.detail;
|
|
114
|
-
return new SqlQueryError(error.message, options);
|
|
115
|
-
}
|
|
116
|
-
if (isConnectionError(error)) return new SqlConnectionError(error.message, {
|
|
117
|
-
cause: error,
|
|
118
|
-
transient: isTransientConnectionError(error)
|
|
119
|
-
});
|
|
120
|
-
return error;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
//#endregion
|
|
124
|
-
export { postgresDriverDescriptorMeta as i, isPostgresError as n, normalizePgError as r, isAlreadyConnectedError as t };
|
|
125
|
-
//# sourceMappingURL=normalize-error-BU6yV-XB.mjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"normalize-error-BU6yV-XB.mjs","names":["options: {\n cause: Error;\n sqlState: string;\n constraint?: string;\n table?: string;\n column?: string;\n detail?: string;\n }"],"sources":["../src/core/descriptor-meta.ts","../src/normalize-error.ts"],"sourcesContent":["export const postgresDriverDescriptorMeta = {\n kind: 'driver',\n familyId: 'sql',\n targetId: 'postgres',\n id: 'postgres',\n version: '0.0.1',\n capabilities: {},\n} as const;\n","import { SqlConnectionError, SqlQueryError } from '@prisma-next/sql-errors';\n\n/**\n * Postgres error shape from the pg library.\n *\n * Note: The pg library doesn't export a DatabaseError type or interface, but errors\n * thrown by pg.query() and pg.Client have this shape at runtime. We define this\n * interface to match the actual runtime structure documented in the pg library\n * (https://github.com/brianc/node-postgres/blob/master/packages/pg/lib/errors.js).\n *\n * The @types/pg package also doesn't provide comprehensive error type definitions,\n * so we define our own interface based on the runtime error properties.\n */\ninterface PostgresError extends Error {\n readonly code?: string;\n readonly constraint?: string;\n readonly table?: string;\n readonly column?: string;\n readonly detail?: string;\n readonly hint?: string;\n readonly position?: string;\n readonly internalPosition?: string;\n readonly internalQuery?: string;\n readonly where?: string;\n readonly schema?: string;\n readonly file?: string;\n readonly line?: string;\n readonly routine?: string;\n}\n\n/**\n * Checks if an error is a connection-related error.\n */\nfunction isConnectionError(error: Error): boolean {\n const code = (error as { code?: string }).code;\n if (code) {\n // Node.js error codes for connection issues\n if (\n code === 'ECONNRESET' ||\n code === 'ETIMEDOUT' ||\n code === 'ECONNREFUSED' ||\n code === 'ENOTFOUND' ||\n code === 'EHOSTUNREACH'\n ) {\n return true;\n }\n }\n\n // Check error message for connection-related strings\n const message = error.message.toLowerCase();\n if (\n message.includes('connection terminated') ||\n message.includes('connection closed') ||\n message.includes('connection refused') ||\n message.includes('connection timeout') ||\n message.includes('connection reset')\n ) {\n return true;\n }\n\n return false;\n}\n\n/**\n * Checks if a connection error is transient (might succeed on retry).\n */\nfunction isTransientConnectionError(error: Error): boolean {\n const code = (error as { code?: string }).code;\n if (code) {\n // Timeouts and connection resets are often transient\n if (code === 'ETIMEDOUT' || code === 'ECONNRESET') {\n return true;\n }\n // Connection refused is usually not transient (server is down)\n if (code === 'ECONNREFUSED') {\n return false;\n }\n }\n\n const message = error.message.toLowerCase();\n if (message.includes('timeout') || message.includes('connection reset')) {\n return true;\n }\n\n return false;\n}\n\n/**\n * PostgreSQL-specific error properties that indicate an error originated from pg library.\n * These properties are not present on Node.js system errors.\n * Excludes generic properties like 'detail', 'file', 'line', and 'position' that could appear on any error.\n */\nconst PG_ERROR_PROPERTIES = [\n 'constraint',\n 'table',\n 'column',\n 'hint',\n 'internalPosition',\n 'internalQuery',\n 'where',\n 'schema',\n 'routine',\n] as const;\n\n/**\n * Type predicate to check if an error is a Postgres error from the pg library.\n *\n * Distinguishes pg library errors from Node.js system errors by checking for:\n * - SQLSTATE codes (5-character alphanumeric codes like '23505', '42601')\n * - pg-specific properties (constraint, table, column, hint, etc.) that Node.js errors don't have\n *\n * Node.js system errors (ECONNREFUSED, ETIMEDOUT, etc.) are excluded to prevent false positives.\n */\nexport function isPostgresError(error: unknown): error is PostgresError {\n if (!(error instanceof Error)) {\n return false;\n }\n\n const pgError = error as PostgresError;\n\n // Check for SQLSTATE code (5-character alphanumeric) - primary indicator of pg errors\n if (pgError.code && isPostgresSqlState(pgError.code)) {\n return true;\n }\n\n // Check for pg-specific properties that Node.js system errors don't have\n // These properties indicate the error originated from pg library query execution\n return PG_ERROR_PROPERTIES.some((prop) => pgError[prop] !== undefined);\n}\n\n/**\n * Checks if an error is an \"already connected\" error from pg.Client.connect().\n * When calling connect() on an already-connected client, pg throws an error that can be safely ignored.\n */\nexport function isAlreadyConnectedError(error: unknown): error is Error {\n if (!(error instanceof Error)) {\n return false;\n }\n const message = error.message.toLowerCase();\n return message.includes('already') && message.includes('connected');\n}\n\n/**\n * Checks if an error code is a Postgres SQLSTATE (5-character alphanumeric code).\n * SQLSTATE codes are standardized SQL error codes (e.g., '23505' for unique violation).\n */\nfunction isPostgresSqlState(code: string | undefined): boolean {\n if (!code) {\n return false;\n }\n // Postgres SQLSTATE codes are 5-character alphanumeric strings\n // Examples: '23505' (unique violation), '42501' (insufficient privilege), '42601' (syntax error)\n return /^[A-Z0-9]{5}$/.test(code);\n}\n\n/**\n * Normalizes a Postgres error into a SQL-shared error type.\n *\n * - Postgres SQLSTATE errors (5-char codes like '23505') → SqlQueryError\n * - Connection errors (ECONNRESET, ETIMEDOUT, etc.) → SqlConnectionError\n * - Unknown errors → returns the original error as-is\n *\n * The original error is preserved via Error.cause to maintain stack traces.\n *\n * @param error - The error to normalize (typically from pg library)\n * @returns SqlQueryError for query-related failures\n * @returns SqlConnectionError for connection-related failures\n * @returns The original error if it cannot be normalized\n */\nexport function normalizePgError(error: unknown): SqlQueryError | SqlConnectionError | Error {\n if (!(error instanceof Error)) {\n // Wrap non-Error values in an Error object\n return new Error(String(error));\n }\n\n const pgError = error as PostgresError;\n\n // Check for Postgres SQLSTATE (query errors)\n if (isPostgresSqlState(pgError.code)) {\n // isPostgresSqlState ensures code is defined and is a valid SQLSTATE\n // biome-ignore lint/style/noNonNullAssertion: isPostgresSqlState guarantees code is defined\n const sqlState = pgError.code!;\n const options: {\n cause: Error;\n sqlState: string;\n constraint?: string;\n table?: string;\n column?: string;\n detail?: string;\n } = {\n cause: error,\n sqlState,\n };\n if (pgError.constraint !== undefined) {\n options.constraint = pgError.constraint;\n }\n if (pgError.table !== undefined) {\n options.table = pgError.table;\n }\n if (pgError.column !== undefined) {\n options.column = pgError.column;\n }\n if (pgError.detail !== undefined) {\n options.detail = pgError.detail;\n }\n return new SqlQueryError(error.message, options);\n }\n\n // Check for connection errors\n if (isConnectionError(error)) {\n return new SqlConnectionError(error.message, {\n cause: error,\n transient: isTransientConnectionError(error),\n });\n }\n\n // Unknown error - return as-is to preserve original error and stack trace\n return error;\n}\n"],"mappings":";;;AAAA,MAAa,+BAA+B;CAC1C,MAAM;CACN,UAAU;CACV,UAAU;CACV,IAAI;CACJ,SAAS;CACT,cAAc,EAAE;CACjB;;;;;;;AC0BD,SAAS,kBAAkB,OAAuB;CAChD,MAAM,OAAQ,MAA4B;AAC1C,KAAI,MAEF;MACE,SAAS,gBACT,SAAS,eACT,SAAS,kBACT,SAAS,eACT,SAAS,eAET,QAAO;;CAKX,MAAM,UAAU,MAAM,QAAQ,aAAa;AAC3C,KACE,QAAQ,SAAS,wBAAwB,IACzC,QAAQ,SAAS,oBAAoB,IACrC,Q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UAAU,OACpB,SAAQ,QAAQ,QAAQ;AAE1B,MAAI,QAAQ,WAAW,OACrB,SAAQ,SAAS,QAAQ;AAE3B,MAAI,QAAQ,WAAW,OACrB,SAAQ,SAAS,QAAQ;AAE3B,SAAO,IAAI,cAAc,MAAM,SAAS,QAAQ;;AAIlD,KAAI,kBAAkB,MAAM,CAC1B,QAAO,IAAI,mBAAmB,MAAM,SAAS;EAC3C,OAAO;EACP,WAAW,2BAA2B,MAAM;EAC7C,CAAC;AAIJ,QAAO"}
|
package/dist/runtime.d.mts
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import { Client, Pool, QueryResult as QueryResult$1, QueryResultRow } from "pg";
|
|
2
|
-
import { RuntimeDriverDescriptor, RuntimeDriverInstance } from "@prisma-next/core-execution-plane/types";
|
|
3
|
-
import { SqlDriver } from "@prisma-next/sql-relational-core/ast";
|
|
4
|
-
|
|
5
|
-
//#region src/postgres-driver.d.ts
|
|
6
|
-
type QueryResult<T extends QueryResultRow = QueryResultRow> = QueryResult$1<T>;
|
|
7
|
-
interface PostgresDriverOptions {
|
|
8
|
-
readonly connect: {
|
|
9
|
-
client: Client;
|
|
10
|
-
} | {
|
|
11
|
-
pool: Pool;
|
|
12
|
-
};
|
|
13
|
-
readonly cursor?: {
|
|
14
|
-
readonly batchSize?: number;
|
|
15
|
-
readonly disabled?: boolean;
|
|
16
|
-
} | undefined;
|
|
17
|
-
}
|
|
18
|
-
interface CreatePostgresDriverOptions {
|
|
19
|
-
readonly cursor?: PostgresDriverOptions['cursor'];
|
|
20
|
-
readonly poolFactory?: typeof Pool;
|
|
21
|
-
}
|
|
22
|
-
declare function createPostgresDriver(connectionString: string, options?: CreatePostgresDriverOptions): SqlDriver;
|
|
23
|
-
declare function createPostgresDriverFromOptions(options: PostgresDriverOptions): SqlDriver;
|
|
24
|
-
//#endregion
|
|
25
|
-
//#region src/exports/runtime.d.ts
|
|
26
|
-
/**
|
|
27
|
-
* Postgres runtime driver instance interface.
|
|
28
|
-
* SqlDriver provides SQL-specific methods (execute, explain, close).
|
|
29
|
-
* RuntimeDriverInstance provides target identification (familyId, targetId).
|
|
30
|
-
* We use intersection type to combine both interfaces.
|
|
31
|
-
*/
|
|
32
|
-
type PostgresRuntimeDriver = RuntimeDriverInstance<'sql', 'postgres'> & SqlDriver;
|
|
33
|
-
/**
|
|
34
|
-
* Postgres driver descriptor for runtime plane.
|
|
35
|
-
*/
|
|
36
|
-
declare const postgresRuntimeDriverDescriptor: RuntimeDriverDescriptor<'sql', 'postgres', PostgresRuntimeDriver>;
|
|
37
|
-
//#endregion
|
|
38
|
-
export { type CreatePostgresDriverOptions, type PostgresDriverOptions, PostgresRuntimeDriver, type QueryResult, createPostgresDriver, createPostgresDriverFromOptions, postgresRuntimeDriverDescriptor as default };
|
|
39
|
-
//# sourceMappingURL=runtime.d.mts.map
|
package/dist/runtime.d.mts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"runtime.d.mts","names":[],"sources":["../src/postgres-driver.ts","../src/exports/runtime.ts"],"sourcesContent":[],"mappings":";;;;;KAkBY,sBAAsB,iBAAiB,kBAAkB,cAAc;UAElE,qBAAA;EAFL,SAAA,OAAW,EAAA;IAAW,MAAA,EAGJ,MAHI;EAAiB,CAAA,GAAA;IAAgC,IAAA,EAGlC,IAHkC;EAAd,CAAA;EAAa,SAAA,MAAA,CAAA,EAAA;IAEjE,SAAA,SAAA,CAAA,EAAqB,MAAA;IAyLrB,SAAA,QAAA,CAAA,EAAA,OAA2B;EAK5B,CAAA,GAAA,SAAA;AAYhB;UAjBiB,2BAAA;oBACG;gCACY;AChMhC;AAKM,iBD8LU,oBAAA,CCrLf,gBANC,EAAA,MAAA,EAAA,OAH4D,CAAvB,EDgM3B,2BChMkD,CAAA,EDiM3D,SCjM2D;iBD0M9C,+BAAA,UAAyC,wBAAwB;;;;;;AA5MjF;;;AAAmF,KCHvE,qBAAA,GAAwB,qBDG+C,CAAA,KAAA,EAAA,UAAA,CAAA,GCHJ,SDGI;;;AAEnF;AAyLA,cCzLM,+BD0Lc,EC1LmB,uBD2LP,CAAA,KAAI,EAAA,UAAA,ECxLlC,qBDwLkC,CAAA"}
|
package/dist/runtime.mjs
DELETED
|
@@ -1,147 +0,0 @@
|
|
|
1
|
-
import { i as postgresDriverDescriptorMeta, n as isPostgresError, r as normalizePgError, t as isAlreadyConnectedError } from "./normalize-error-BU6yV-XB.mjs";
|
|
2
|
-
import { Pool } from "pg";
|
|
3
|
-
import Cursor from "pg-cursor";
|
|
4
|
-
|
|
5
|
-
//#region src/callback-to-promise.ts
|
|
6
|
-
function callbackToPromise(fn) {
|
|
7
|
-
return new Promise((resolve, reject) => {
|
|
8
|
-
fn((err, result) => {
|
|
9
|
-
if (err) {
|
|
10
|
-
reject(err);
|
|
11
|
-
return;
|
|
12
|
-
}
|
|
13
|
-
resolve(result);
|
|
14
|
-
});
|
|
15
|
-
});
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
//#endregion
|
|
19
|
-
//#region src/postgres-driver.ts
|
|
20
|
-
const DEFAULT_BATCH_SIZE = 100;
|
|
21
|
-
var PostgresDriverImpl = class {
|
|
22
|
-
pool;
|
|
23
|
-
directClient;
|
|
24
|
-
cursorBatchSize;
|
|
25
|
-
cursorDisabled;
|
|
26
|
-
constructor(options) {
|
|
27
|
-
if ("client" in options.connect) this.directClient = options.connect.client;
|
|
28
|
-
else if ("pool" in options.connect) this.pool = options.connect.pool;
|
|
29
|
-
else throw new Error("PostgresDriver requires a pool or client");
|
|
30
|
-
this.cursorBatchSize = options.cursor?.batchSize ?? DEFAULT_BATCH_SIZE;
|
|
31
|
-
this.cursorDisabled = options.cursor?.disabled ?? false;
|
|
32
|
-
}
|
|
33
|
-
async connect() {}
|
|
34
|
-
async *execute(request) {
|
|
35
|
-
const client = await this.acquireClient();
|
|
36
|
-
try {
|
|
37
|
-
if (!this.cursorDisabled) try {
|
|
38
|
-
for await (const row of this.executeWithCursor(client, request.sql, request.params)) yield row;
|
|
39
|
-
return;
|
|
40
|
-
} catch (cursorError) {
|
|
41
|
-
if (!(cursorError instanceof Error)) throw cursorError;
|
|
42
|
-
if (isPostgresError(cursorError)) throw normalizePgError(cursorError);
|
|
43
|
-
}
|
|
44
|
-
for await (const row of this.executeBuffered(client, request.sql, request.params)) yield row;
|
|
45
|
-
} catch (error) {
|
|
46
|
-
throw normalizePgError(error);
|
|
47
|
-
} finally {
|
|
48
|
-
await this.releaseClient(client);
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
async explain(request) {
|
|
52
|
-
const text = `EXPLAIN (FORMAT JSON) ${request.sql}`;
|
|
53
|
-
const client = await this.acquireClient();
|
|
54
|
-
try {
|
|
55
|
-
return { rows: (await client.query(text, request.params)).rows };
|
|
56
|
-
} catch (error) {
|
|
57
|
-
throw normalizePgError(error);
|
|
58
|
-
} finally {
|
|
59
|
-
await this.releaseClient(client);
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
async query(sql, params) {
|
|
63
|
-
const client = await this.acquireClient();
|
|
64
|
-
try {
|
|
65
|
-
return await client.query(sql, params);
|
|
66
|
-
} catch (error) {
|
|
67
|
-
throw normalizePgError(error);
|
|
68
|
-
} finally {
|
|
69
|
-
await this.releaseClient(client);
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
async close() {
|
|
73
|
-
if (this.pool) {
|
|
74
|
-
if (!this.pool.ended) await this.pool.end();
|
|
75
|
-
}
|
|
76
|
-
if (this.directClient) {
|
|
77
|
-
const client = this.directClient;
|
|
78
|
-
if (!client._ending) await client.end();
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
async acquireClient() {
|
|
82
|
-
if (this.pool) return this.pool.connect();
|
|
83
|
-
if (this.directClient) {
|
|
84
|
-
const client = this.directClient;
|
|
85
|
-
if (!(client._connection !== void 0 && client._connection !== null && !client._ending)) try {
|
|
86
|
-
await this.directClient.connect();
|
|
87
|
-
} catch (error) {
|
|
88
|
-
if (!isAlreadyConnectedError(error)) throw error;
|
|
89
|
-
}
|
|
90
|
-
return this.directClient;
|
|
91
|
-
}
|
|
92
|
-
throw new Error("PostgresDriver requires a pool or client");
|
|
93
|
-
}
|
|
94
|
-
async releaseClient(client) {
|
|
95
|
-
if (this.pool) client.release();
|
|
96
|
-
}
|
|
97
|
-
async *executeWithCursor(client, sql, params) {
|
|
98
|
-
const cursor = client.query(new Cursor(sql, params));
|
|
99
|
-
try {
|
|
100
|
-
while (true) {
|
|
101
|
-
const rows = await readCursor(cursor, this.cursorBatchSize);
|
|
102
|
-
if (rows.length === 0) break;
|
|
103
|
-
for (const row of rows) yield row;
|
|
104
|
-
}
|
|
105
|
-
} finally {
|
|
106
|
-
await closeCursor(cursor);
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
async *executeBuffered(client, sql, params) {
|
|
110
|
-
const result = await client.query(sql, params);
|
|
111
|
-
for (const row of result.rows) yield row;
|
|
112
|
-
}
|
|
113
|
-
};
|
|
114
|
-
function createPostgresDriver(connectionString, options) {
|
|
115
|
-
return new PostgresDriverImpl({
|
|
116
|
-
connect: { pool: new (options?.poolFactory ?? Pool)({ connectionString }) },
|
|
117
|
-
cursor: options?.cursor
|
|
118
|
-
});
|
|
119
|
-
}
|
|
120
|
-
function createPostgresDriverFromOptions(options) {
|
|
121
|
-
return new PostgresDriverImpl(options);
|
|
122
|
-
}
|
|
123
|
-
function readCursor(cursor, size) {
|
|
124
|
-
return callbackToPromise((cb) => {
|
|
125
|
-
cursor.read(size, (err, rows) => cb(err, rows));
|
|
126
|
-
});
|
|
127
|
-
}
|
|
128
|
-
function closeCursor(cursor) {
|
|
129
|
-
return callbackToPromise((cb) => cursor.close(cb));
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
//#endregion
|
|
133
|
-
//#region src/exports/runtime.ts
|
|
134
|
-
/**
|
|
135
|
-
* Postgres driver descriptor for runtime plane.
|
|
136
|
-
*/
|
|
137
|
-
const postgresRuntimeDriverDescriptor = {
|
|
138
|
-
...postgresDriverDescriptorMeta,
|
|
139
|
-
create(options) {
|
|
140
|
-
return createPostgresDriverFromOptions(options);
|
|
141
|
-
}
|
|
142
|
-
};
|
|
143
|
-
var runtime_default = postgresRuntimeDriverDescriptor;
|
|
144
|
-
|
|
145
|
-
//#endregion
|
|
146
|
-
export { createPostgresDriver, createPostgresDriverFromOptions, runtime_default as default };
|
|
147
|
-
//# sourceMappingURL=runtime.mjs.map
|
package/dist/runtime.mjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"runtime.mjs","names":["error: unknown","postgresRuntimeDriverDescriptor: RuntimeDriverDescriptor<\n 'sql',\n 'postgres',\n PostgresRuntimeDriver\n>"],"sources":["../src/callback-to-promise.ts","../src/postgres-driver.ts","../src/exports/runtime.ts"],"sourcesContent":["/**\n * Wraps a Node-style callback function into a Promise.\n * Supports both (err) => void and (err, result) => void patterns.\n */\nexport function callbackToPromise(\n fn: (callback: (err: Error | null | undefined) => void) => void,\n): Promise<void>;\nexport function callbackToPromise<T>(\n fn: (callback: (err: Error | null | undefined, result: T) => void) => void,\n): Promise<T>;\nexport function callbackToPromise<T = void>(\n fn: (callback: (err: Error | null | undefined, result?: T) => void) => void,\n): Promise<T> {\n return new Promise<T>((resolve, reject) => {\n fn((err, result) => {\n if (err) {\n reject(err);\n return;\n }\n resolve(result as T);\n });\n });\n}\n","import type {\n SqlDriver,\n SqlExecuteRequest,\n SqlExplainResult,\n SqlQueryResult,\n} from '@prisma-next/sql-relational-core/ast';\nimport type {\n Client,\n QueryResult as PgQueryResult,\n PoolClient,\n Pool as PoolType,\n QueryResultRow,\n} from 'pg';\nimport { Pool } from 'pg';\nimport Cursor from 'pg-cursor';\nimport { callbackToPromise } from './callback-to-promise';\nimport { isAlreadyConnectedError, isPostgresError, normalizePgError } from './normalize-error';\n\nexport type QueryResult<T extends QueryResultRow = QueryResultRow> = PgQueryResult<T>;\n\nexport interface PostgresDriverOptions {\n readonly connect: { client: Client } | { pool: PoolType };\n readonly cursor?:\n | {\n readonly batchSize?: number;\n readonly disabled?: boolean;\n }\n | undefined;\n}\n\nconst DEFAULT_BATCH_SIZE = 100;\n\nclass PostgresDriverImpl implements SqlDriver {\n private readonly pool: PoolType | undefined;\n private readonly directClient: Client | undefined;\n private readonly cursorBatchSize: number;\n private readonly cursorDisabled: boolean;\n\n constructor(options: PostgresDriverOptions) {\n if ('client' in options.connect) {\n this.directClient = options.connect.client;\n } else if ('pool' in options.connect) {\n this.pool = options.connect.pool;\n } else {\n throw new Error('PostgresDriver requires a pool or client');\n }\n\n this.cursorBatchSize = options.cursor?.batchSize ?? DEFAULT_BATCH_SIZE;\n this.cursorDisabled = options.cursor?.disabled ?? false;\n }\n\n async connect(): Promise<void> {\n // No-op: caller controls connecting the underlying client or pool\n }\n\n async *execute<Row = Record<string, unknown>>(request: SqlExecuteRequest): AsyncIterable<Row> {\n const client = await this.acquireClient();\n try {\n if (!this.cursorDisabled) {\n try {\n for await (const row of this.executeWithCursor(client, request.sql, request.params)) {\n yield row as Row;\n }\n return;\n } catch (cursorError) {\n if (!(cursorError instanceof Error)) {\n throw cursorError;\n }\n // Check if this is a pg error - if so, normalize and throw\n // Otherwise, fall back to buffered mode for cursor-specific errors\n if (isPostgresError(cursorError)) {\n throw normalizePgError(cursorError);\n }\n // Not a pg error - cursor-specific error, fall back to buffered mode\n }\n }\n\n for await (const row of this.executeBuffered(client, request.sql, request.params)) {\n yield row as Row;\n }\n } catch (error) {\n throw normalizePgError(error);\n } finally {\n await this.releaseClient(client);\n }\n }\n\n async explain(request: SqlExecuteRequest): Promise<SqlExplainResult> {\n const text = `EXPLAIN (FORMAT JSON) ${request.sql}`;\n const client = await this.acquireClient();\n try {\n const result = await client.query(text, request.params as unknown[] | undefined);\n return { rows: result.rows as ReadonlyArray<Record<string, unknown>> };\n } catch (error) {\n throw normalizePgError(error);\n } finally {\n await this.releaseClient(client);\n }\n }\n\n async query<Row = Record<string, unknown>>(\n sql: string,\n params?: readonly unknown[],\n ): Promise<SqlQueryResult<Row>> {\n const client = await this.acquireClient();\n try {\n const result = await client.query(sql, params as unknown[] | undefined);\n return result as unknown as SqlQueryResult<Row>;\n } catch (error) {\n throw normalizePgError(error);\n } finally {\n await this.releaseClient(client);\n }\n }\n\n async close(): Promise<void> {\n if (this.pool) {\n // Check if pool is already closed to avoid \"Called end on pool more than once\" error\n // pg Pool has an 'ended' property that indicates if the pool has been closed\n if (!(this.pool as { ended?: boolean }).ended) {\n await this.pool.end();\n }\n }\n if (this.directClient) {\n const client = this.directClient as Client & { _ending?: boolean };\n if (!client._ending) {\n await client.end();\n }\n }\n }\n\n private async acquireClient(): Promise<PoolClient | Client> {\n if (this.pool) {\n return this.pool.connect();\n }\n if (this.directClient) {\n // Check if client is already connected before attempting to connect\n // This prevents hanging when the database only supports a single connection\n // pg's Client has internal connection state that we can check\n const client = this.directClient as Client & {\n _ending?: boolean;\n _connection?: unknown;\n };\n const isConnected =\n client._connection !== undefined && client._connection !== null && !client._ending;\n\n // Only connect if not already connected\n // If caller provided a connected client (e.g., in tests), use it as-is\n if (!isConnected) {\n try {\n await this.directClient.connect();\n } catch (error: unknown) {\n // If already connected, pg throws an error - ignore it and proceed\n // Re-throw other errors (actual connection failures)\n if (!isAlreadyConnectedError(error)) {\n throw error;\n }\n }\n }\n return this.directClient;\n }\n throw new Error('PostgresDriver requires a pool or client');\n }\n\n private async releaseClient(client: PoolClient | Client): Promise<void> {\n if (this.pool) {\n (client as PoolClient).release();\n }\n }\n\n private async *executeWithCursor(\n client: PoolClient | Client,\n sql: string,\n params: readonly unknown[] | undefined,\n ): AsyncIterable<Record<string, unknown>> {\n const cursor = client.query(new Cursor(sql, params as unknown[] | undefined));\n\n try {\n while (true) {\n const rows = await readCursor(cursor, this.cursorBatchSize);\n if (rows.length === 0) {\n break;\n }\n\n for (const row of rows) {\n yield row;\n }\n }\n } finally {\n await closeCursor(cursor);\n }\n }\n\n private async *executeBuffered(\n client: PoolClient | Client,\n sql: string,\n params: readonly unknown[] | undefined,\n ): AsyncIterable<Record<string, unknown>> {\n const result = await client.query(sql, params as unknown[] | undefined);\n for (const row of result.rows as Record<string, unknown>[]) {\n yield row;\n }\n }\n}\n\nexport interface CreatePostgresDriverOptions {\n readonly cursor?: PostgresDriverOptions['cursor'];\n readonly poolFactory?: typeof Pool;\n}\n\nexport function createPostgresDriver(\n connectionString: string,\n options?: CreatePostgresDriverOptions,\n): SqlDriver {\n const PoolImpl: typeof Pool = options?.poolFactory ?? Pool;\n const pool = new PoolImpl({ connectionString });\n return new PostgresDriverImpl({\n connect: { pool },\n cursor: options?.cursor,\n });\n}\n\nexport function createPostgresDriverFromOptions(options: PostgresDriverOptions): SqlDriver {\n return new PostgresDriverImpl(options);\n}\n\nfunction readCursor<Row>(cursor: Cursor<Row>, size: number): Promise<Row[]> {\n return callbackToPromise<Row[]>((cb) => {\n cursor.read(size, (err, rows) => cb(err, rows));\n });\n}\n\nfunction closeCursor(cursor: Cursor<unknown>): Promise<void> {\n return callbackToPromise((cb) => cursor.close(cb));\n}\n","import type {\n RuntimeDriverDescriptor,\n RuntimeDriverInstance,\n} from '@prisma-next/core-execution-plane/types';\nimport type { SqlDriver } from '@prisma-next/sql-relational-core/ast';\nimport { postgresDriverDescriptorMeta } from '../core/descriptor-meta';\nimport type { PostgresDriverOptions } from '../postgres-driver';\nimport { createPostgresDriverFromOptions } from '../postgres-driver';\n\n/**\n * Postgres runtime driver instance interface.\n * SqlDriver provides SQL-specific methods (execute, explain, close).\n * RuntimeDriverInstance provides target identification (familyId, targetId).\n * We use intersection type to combine both interfaces.\n */\nexport type PostgresRuntimeDriver = RuntimeDriverInstance<'sql', 'postgres'> & SqlDriver;\n\n/**\n * Postgres driver descriptor for runtime plane.\n */\nconst postgresRuntimeDriverDescriptor: RuntimeDriverDescriptor<\n 'sql',\n 'postgres',\n PostgresRuntimeDriver\n> = {\n ...postgresDriverDescriptorMeta,\n create(options: PostgresDriverOptions): PostgresRuntimeDriver {\n return createPostgresDriverFromOptions(options) as PostgresRuntimeDriver;\n },\n};\n\nexport default postgresRuntimeDriverDescriptor;\nexport type {\n CreatePostgresDriverOptions,\n PostgresDriverOptions,\n QueryResult,\n} from '../postgres-driver';\nexport { createPostgresDriver, createPostgresDriverFromOptions } from '../postgres-driver';\n"],"mappings":";;;;;AAUA,SAAgB,kBACd,IACY;AACZ,QAAO,IAAI,SAAY,SAAS,WAAW;AACzC,MAAI,KAAK,WAAW;AAClB,OAAI,KAAK;AACP,WAAO,IAAI;AACX;;AAEF,WAAQ,OAAY;IACpB;GACF;;;;;ACSJ,MAAM,qBAAqB;AAE3B,IAAM,qBAAN,MAA8C;CAC5C,AAAiB;CACjB,AAAiB;CACjB,AAAiB;CACjB,AAAiB;CAEjB,YAAY,SAAgC;AAC1C,MAAI,YAAY,QAAQ,QACtB,MAAK,eAAe,QAAQ,QAAQ;WAC3B,UAAU,QAAQ,QAC3B,MAAK,OAAO,QAAQ,QAAQ;MAE5B,OAAM,IAAI,MAAM,2CAA2C;AAG7D,OAAK,kBAAkB,QAAQ,QAAQ,aAAa;AACpD,OAAK,iBAAiB,QAAQ,QAAQ,YAAY;;CAGpD,MAAM,UAAyB;CAI/B,OAAO,QAAuC,SAAgD;EAC5F,MAAM,SAAS,MAAM,KAAK,eAAe;AACzC,MAAI;AACF,OAAI,CAAC,KAAK,eACR,KAAI;AACF,eAAW,MAAM,OAAO,KAAK,kBAAkB,QAAQ,QAAQ,KAAK,QAAQ,OAAO,CACjF,OAAM;AAER;YACO,aAAa;AACpB,QAAI,EAAE,uBAAuB,OAC3B,OAAM;AAIR,QAAI,gBAAgB,YAAY,CAC9B,OAAM,iBAAiB,YAAY;;AAMzC,cAAW,MAAM,OAAO,KAAK,gBAAgB,QAAQ,QAAQ,KAAK,QAAQ,OAAO,CAC/E,OAAM;WAED,OAAO;AACd,SAAM,iBAAiB,MAAM;YACrB;AACR,SAAM,KAAK,cAAc,OAAO;;;CAIpC,MAAM,QAAQ,SAAuD;E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