@prisma-next/driver-postgres 0.3.0-dev.15 → 0.3.0-dev.162
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/LICENSE +201 -0
- package/README.md +18 -18
- package/dist/control.d.mts +26 -0
- package/dist/control.d.mts.map +1 -0
- package/dist/control.mjs +64 -0
- package/dist/control.mjs.map +1 -0
- package/dist/normalize-error-BU6yV-XB.mjs +125 -0
- package/dist/normalize-error-BU6yV-XB.mjs.map +1 -0
- package/dist/runtime.d.mts +36 -0
- package/dist/runtime.d.mts.map +1 -0
- package/dist/runtime.mjs +339 -0
- package/dist/runtime.mjs.map +1 -0
- package/package.json +26 -25
- package/src/exports/control.ts +4 -7
- package/src/exports/runtime.ts +145 -21
- package/src/postgres-driver.ts +272 -104
- package/dist/callback-to-promise.d.ts +0 -7
- package/dist/callback-to-promise.d.ts.map +0 -1
- package/dist/chunk-M4WUUVS5.js +0 -116
- package/dist/chunk-M4WUUVS5.js.map +0 -1
- package/dist/core/descriptor-meta.d.ts +0 -9
- package/dist/core/descriptor-meta.d.ts.map +0 -1
- package/dist/exports/control.d.ts +0 -26
- package/dist/exports/control.d.ts.map +0 -1
- package/dist/exports/control.js +0 -66
- package/dist/exports/control.js.map +0 -1
- package/dist/exports/runtime.d.ts +0 -17
- package/dist/exports/runtime.d.ts.map +0 -1
- package/dist/exports/runtime.js +0 -189
- package/dist/exports/runtime.js.map +0 -1
- package/dist/normalize-error.d.ts +0 -60
- package/dist/normalize-error.d.ts.map +0 -1
- package/dist/postgres-driver.d.ts +0 -22
- package/dist/postgres-driver.d.ts.map +0 -1
package/src/exports/runtime.ts
CHANGED
|
@@ -1,38 +1,162 @@
|
|
|
1
1
|
import type {
|
|
2
2
|
RuntimeDriverDescriptor,
|
|
3
3
|
RuntimeDriverInstance,
|
|
4
|
-
} from '@prisma-next/
|
|
5
|
-
import type {
|
|
4
|
+
} from '@prisma-next/framework-components/execution';
|
|
5
|
+
import type {
|
|
6
|
+
SqlConnection,
|
|
7
|
+
SqlDriver,
|
|
8
|
+
SqlExecuteRequest,
|
|
9
|
+
SqlExplainResult,
|
|
10
|
+
SqlQueryResult,
|
|
11
|
+
} from '@prisma-next/sql-relational-core/ast';
|
|
6
12
|
import { postgresDriverDescriptorMeta } from '../core/descriptor-meta';
|
|
7
|
-
import
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
13
|
+
import {
|
|
14
|
+
createBoundDriverFromBinding,
|
|
15
|
+
type PostgresBinding,
|
|
16
|
+
type PostgresDriverCreateOptions,
|
|
17
|
+
} from '../postgres-driver';
|
|
18
|
+
|
|
19
|
+
export type PostgresRuntimeDriver = RuntimeDriverInstance<'sql', 'postgres'> &
|
|
20
|
+
SqlDriver<PostgresBinding>;
|
|
21
|
+
|
|
22
|
+
const USE_BEFORE_CONNECT_MESSAGE =
|
|
23
|
+
'Postgres driver not connected. Call connect(binding) before acquireConnection or execute.';
|
|
24
|
+
const ALREADY_CONNECTED_MESSAGE =
|
|
25
|
+
'Postgres driver already connected. Call close() before reconnecting with a new binding.';
|
|
26
|
+
|
|
27
|
+
interface DriverRuntimeError extends Error {
|
|
28
|
+
readonly code: 'DRIVER.NOT_CONNECTED' | 'DRIVER.ALREADY_CONNECTED';
|
|
29
|
+
readonly category: 'RUNTIME';
|
|
30
|
+
readonly severity: 'error';
|
|
31
|
+
readonly details?: Record<string, unknown>;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function driverError(
|
|
35
|
+
code: DriverRuntimeError['code'],
|
|
36
|
+
message: string,
|
|
37
|
+
details?: Record<string, unknown>,
|
|
38
|
+
): DriverRuntimeError {
|
|
39
|
+
const error = new Error(message) as DriverRuntimeError;
|
|
40
|
+
Object.defineProperty(error, 'name', {
|
|
41
|
+
value: 'RuntimeError',
|
|
42
|
+
configurable: true,
|
|
43
|
+
});
|
|
44
|
+
return Object.assign(error, {
|
|
45
|
+
code,
|
|
46
|
+
category: 'RUNTIME' as const,
|
|
47
|
+
severity: 'error' as const,
|
|
48
|
+
message,
|
|
49
|
+
details,
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function unboundExecute<Row>(): AsyncIterable<Row> {
|
|
54
|
+
return {
|
|
55
|
+
[Symbol.asyncIterator]() {
|
|
56
|
+
return {
|
|
57
|
+
async next() {
|
|
58
|
+
throw driverError('DRIVER.NOT_CONNECTED', USE_BEFORE_CONNECT_MESSAGE);
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
class PostgresUnboundDriverImpl implements PostgresRuntimeDriver {
|
|
66
|
+
readonly familyId = 'sql' as const;
|
|
67
|
+
readonly targetId = 'postgres' as const;
|
|
68
|
+
|
|
69
|
+
#delegate: SqlDriver<PostgresBinding> | null = null;
|
|
70
|
+
#closed = false;
|
|
71
|
+
#cursorOpts: PostgresDriverCreateOptions['cursor'];
|
|
72
|
+
|
|
73
|
+
constructor(cursorOpts?: PostgresDriverCreateOptions['cursor']) {
|
|
74
|
+
this.#cursorOpts = cursorOpts;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
get state(): 'unbound' | 'connected' | 'closed' {
|
|
78
|
+
if (this.#delegate !== null) {
|
|
79
|
+
return 'connected';
|
|
80
|
+
}
|
|
81
|
+
if (this.#closed) {
|
|
82
|
+
return 'closed';
|
|
83
|
+
}
|
|
84
|
+
return 'unbound';
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
#requireDelegate(): SqlDriver<PostgresBinding> {
|
|
88
|
+
const delegate = this.#delegate;
|
|
89
|
+
if (delegate === null) {
|
|
90
|
+
throw driverError('DRIVER.NOT_CONNECTED', USE_BEFORE_CONNECT_MESSAGE);
|
|
91
|
+
}
|
|
92
|
+
return delegate;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
async connect(binding: PostgresBinding): Promise<void> {
|
|
96
|
+
if (this.#delegate !== null) {
|
|
97
|
+
throw driverError('DRIVER.ALREADY_CONNECTED', ALREADY_CONNECTED_MESSAGE, {
|
|
98
|
+
bindingKind: binding.kind,
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
this.#delegate = createBoundDriverFromBinding(binding, this.#cursorOpts);
|
|
102
|
+
this.#closed = false;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
async acquireConnection(): Promise<SqlConnection> {
|
|
106
|
+
const delegate = this.#requireDelegate();
|
|
107
|
+
return delegate.acquireConnection();
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
async close(): Promise<void> {
|
|
111
|
+
const delegate = this.#delegate;
|
|
112
|
+
if (delegate !== null) {
|
|
113
|
+
this.#delegate = null;
|
|
114
|
+
await delegate.close();
|
|
115
|
+
}
|
|
116
|
+
this.#closed = true;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
execute<Row = Record<string, unknown>>(request: SqlExecuteRequest): AsyncIterable<Row> {
|
|
120
|
+
const delegate = this.#delegate;
|
|
121
|
+
if (delegate === null) {
|
|
122
|
+
return unboundExecute<Row>();
|
|
123
|
+
}
|
|
124
|
+
return delegate.execute<Row>(request);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
async explain(request: SqlExecuteRequest): Promise<SqlExplainResult> {
|
|
128
|
+
const delegate = this.#requireDelegate();
|
|
129
|
+
const explain = delegate.explain;
|
|
130
|
+
if (explain === undefined) {
|
|
131
|
+
throw driverError('DRIVER.NOT_CONNECTED', USE_BEFORE_CONNECT_MESSAGE);
|
|
132
|
+
}
|
|
133
|
+
return explain.call(delegate, request);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
async query<Row = Record<string, unknown>>(
|
|
137
|
+
sql: string,
|
|
138
|
+
params?: readonly unknown[],
|
|
139
|
+
): Promise<SqlQueryResult<Row>> {
|
|
140
|
+
const delegate = this.#requireDelegate();
|
|
141
|
+
return delegate.query<Row>(sql, params);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
21
145
|
const postgresRuntimeDriverDescriptor: RuntimeDriverDescriptor<
|
|
22
146
|
'sql',
|
|
23
147
|
'postgres',
|
|
148
|
+
PostgresDriverCreateOptions,
|
|
24
149
|
PostgresRuntimeDriver
|
|
25
150
|
> = {
|
|
26
151
|
...postgresDriverDescriptorMeta,
|
|
27
|
-
create(options
|
|
28
|
-
return
|
|
152
|
+
create(options?: PostgresDriverCreateOptions): PostgresRuntimeDriver {
|
|
153
|
+
return new PostgresUnboundDriverImpl(options?.cursor);
|
|
29
154
|
},
|
|
30
155
|
};
|
|
31
156
|
|
|
32
157
|
export default postgresRuntimeDriverDescriptor;
|
|
33
158
|
export type {
|
|
34
|
-
|
|
35
|
-
|
|
159
|
+
PostgresBinding,
|
|
160
|
+
PostgresDriverCreateOptions,
|
|
36
161
|
QueryResult,
|
|
37
162
|
} from '../postgres-driver';
|
|
38
|
-
export { createPostgresDriver, createPostgresDriverFromOptions } from '../postgres-driver';
|
package/src/postgres-driver.ts
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
import type {
|
|
2
|
+
SqlConnection,
|
|
2
3
|
SqlDriver,
|
|
4
|
+
SqlDriverState,
|
|
3
5
|
SqlExecuteRequest,
|
|
4
6
|
SqlExplainResult,
|
|
7
|
+
SqlQueryable,
|
|
5
8
|
SqlQueryResult,
|
|
9
|
+
SqlTransaction,
|
|
6
10
|
} from '@prisma-next/sql-relational-core/ast';
|
|
7
11
|
import type {
|
|
8
12
|
Client,
|
|
@@ -18,47 +22,69 @@ import { isAlreadyConnectedError, isPostgresError, normalizePgError } from './no
|
|
|
18
22
|
|
|
19
23
|
export type QueryResult<T extends QueryResultRow = QueryResultRow> = PgQueryResult<T>;
|
|
20
24
|
|
|
21
|
-
export
|
|
25
|
+
export type PostgresBinding =
|
|
26
|
+
| { readonly kind: 'url'; readonly url: string }
|
|
27
|
+
| { readonly kind: 'pgPool'; readonly pool: PoolType }
|
|
28
|
+
| { readonly kind: 'pgClient'; readonly client: Client };
|
|
29
|
+
|
|
30
|
+
export interface PostgresCursorOptions {
|
|
31
|
+
readonly batchSize?: number;
|
|
32
|
+
readonly disabled?: boolean;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
interface PostgresDriverOptions {
|
|
22
36
|
readonly connect: { client: Client } | { pool: PoolType };
|
|
23
|
-
readonly cursor?:
|
|
24
|
-
| {
|
|
25
|
-
readonly batchSize?: number;
|
|
26
|
-
readonly disabled?: boolean;
|
|
27
|
-
}
|
|
28
|
-
| undefined;
|
|
37
|
+
readonly cursor?: PostgresCursorOptions | undefined;
|
|
29
38
|
}
|
|
30
39
|
|
|
40
|
+
export type PostgresDriverCreateOptions = Omit<PostgresDriverOptions, 'connect'>;
|
|
41
|
+
|
|
31
42
|
const DEFAULT_BATCH_SIZE = 100;
|
|
32
43
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
constructor(options: PostgresDriverOptions) {
|
|
40
|
-
if ('client' in options.connect) {
|
|
41
|
-
this.directClient = options.connect.client;
|
|
42
|
-
} else if ('pool' in options.connect) {
|
|
43
|
-
this.pool = options.connect.pool;
|
|
44
|
-
} else {
|
|
45
|
-
throw new Error('PostgresDriver requires a pool or client');
|
|
46
|
-
}
|
|
44
|
+
type ConnectionOptions =
|
|
45
|
+
| { readonly cursorDisabled: true }
|
|
46
|
+
| { readonly cursorBatchSize: number; readonly cursorDisabled: false };
|
|
47
|
+
|
|
48
|
+
class AsyncMutex {
|
|
49
|
+
#queue = Promise.resolve();
|
|
47
50
|
|
|
48
|
-
|
|
49
|
-
|
|
51
|
+
async lock(): Promise<() => void> {
|
|
52
|
+
const previous = this.#queue;
|
|
53
|
+
let releaseLock: (() => void) | undefined;
|
|
54
|
+
this.#queue = new Promise<void>((resolve) => {
|
|
55
|
+
releaseLock = resolve;
|
|
56
|
+
});
|
|
57
|
+
await previous;
|
|
58
|
+
return () => {
|
|
59
|
+
releaseLock?.();
|
|
60
|
+
releaseLock = undefined;
|
|
61
|
+
};
|
|
50
62
|
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
abstract class PostgresQueryable<C extends PoolClient | Client = PoolClient | Client>
|
|
66
|
+
implements SqlQueryable
|
|
67
|
+
{
|
|
68
|
+
abstract acquireClient(): Promise<C>;
|
|
69
|
+
abstract releaseClient(client: C): Promise<void>;
|
|
51
70
|
|
|
52
|
-
|
|
53
|
-
|
|
71
|
+
protected readonly options: ConnectionOptions;
|
|
72
|
+
|
|
73
|
+
constructor(options: ConnectionOptions) {
|
|
74
|
+
this.options = options;
|
|
54
75
|
}
|
|
55
76
|
|
|
56
77
|
async *execute<Row = Record<string, unknown>>(request: SqlExecuteRequest): AsyncIterable<Row> {
|
|
57
78
|
const client = await this.acquireClient();
|
|
58
79
|
try {
|
|
59
|
-
if (!this.cursorDisabled) {
|
|
80
|
+
if (!this.options.cursorDisabled) {
|
|
60
81
|
try {
|
|
61
|
-
for await (const row of this.executeWithCursor(
|
|
82
|
+
for await (const row of this.executeWithCursor(
|
|
83
|
+
client,
|
|
84
|
+
request.sql,
|
|
85
|
+
request.params,
|
|
86
|
+
this.options.cursorBatchSize,
|
|
87
|
+
)) {
|
|
62
88
|
yield row as Row;
|
|
63
89
|
}
|
|
64
90
|
return;
|
|
@@ -86,13 +112,15 @@ class PostgresDriverImpl implements SqlDriver {
|
|
|
86
112
|
}
|
|
87
113
|
|
|
88
114
|
async explain(request: SqlExecuteRequest): Promise<SqlExplainResult> {
|
|
115
|
+
// SQL is generated by Prisma Next planners (or validated raw paths), so
|
|
116
|
+
// EXPLAIN prefixing preserves the same statement semantics.
|
|
89
117
|
const text = `EXPLAIN (FORMAT JSON) ${request.sql}`;
|
|
90
118
|
const client = await this.acquireClient();
|
|
91
119
|
try {
|
|
92
|
-
const result = await client
|
|
120
|
+
const result = await client
|
|
121
|
+
.query(text, request.params as unknown[] | undefined)
|
|
122
|
+
.catch(rethrowNormalizedError);
|
|
93
123
|
return { rows: result.rows as ReadonlyArray<Record<string, unknown>> };
|
|
94
|
-
} catch (error) {
|
|
95
|
-
throw normalizePgError(error);
|
|
96
124
|
} finally {
|
|
97
125
|
await this.releaseClient(client);
|
|
98
126
|
}
|
|
@@ -104,80 +132,26 @@ class PostgresDriverImpl implements SqlDriver {
|
|
|
104
132
|
): Promise<SqlQueryResult<Row>> {
|
|
105
133
|
const client = await this.acquireClient();
|
|
106
134
|
try {
|
|
107
|
-
const result = await client
|
|
135
|
+
const result = await client
|
|
136
|
+
.query(sql, params as unknown[] | undefined)
|
|
137
|
+
.catch(rethrowNormalizedError);
|
|
108
138
|
return result as unknown as SqlQueryResult<Row>;
|
|
109
|
-
} catch (error) {
|
|
110
|
-
throw normalizePgError(error);
|
|
111
139
|
} finally {
|
|
112
140
|
await this.releaseClient(client);
|
|
113
141
|
}
|
|
114
142
|
}
|
|
115
143
|
|
|
116
|
-
async close(): Promise<void> {
|
|
117
|
-
if (this.pool) {
|
|
118
|
-
// Check if pool is already closed to avoid "Called end on pool more than once" error
|
|
119
|
-
// pg Pool has an 'ended' property that indicates if the pool has been closed
|
|
120
|
-
if (!(this.pool as { ended?: boolean }).ended) {
|
|
121
|
-
await this.pool.end();
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
if (this.directClient) {
|
|
125
|
-
const client = this.directClient as Client & { _ending?: boolean };
|
|
126
|
-
if (!client._ending) {
|
|
127
|
-
await client.end();
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
private async acquireClient(): Promise<PoolClient | Client> {
|
|
133
|
-
if (this.pool) {
|
|
134
|
-
return this.pool.connect();
|
|
135
|
-
}
|
|
136
|
-
if (this.directClient) {
|
|
137
|
-
// Check if client is already connected before attempting to connect
|
|
138
|
-
// This prevents hanging when the database only supports a single connection
|
|
139
|
-
// pg's Client has internal connection state that we can check
|
|
140
|
-
const client = this.directClient as Client & {
|
|
141
|
-
_ending?: boolean;
|
|
142
|
-
_connection?: unknown;
|
|
143
|
-
};
|
|
144
|
-
const isConnected =
|
|
145
|
-
client._connection !== undefined && client._connection !== null && !client._ending;
|
|
146
|
-
|
|
147
|
-
// Only connect if not already connected
|
|
148
|
-
// If caller provided a connected client (e.g., in tests), use it as-is
|
|
149
|
-
if (!isConnected) {
|
|
150
|
-
try {
|
|
151
|
-
await this.directClient.connect();
|
|
152
|
-
} catch (error: unknown) {
|
|
153
|
-
// If already connected, pg throws an error - ignore it and proceed
|
|
154
|
-
// Re-throw other errors (actual connection failures)
|
|
155
|
-
if (!isAlreadyConnectedError(error)) {
|
|
156
|
-
throw error;
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
return this.directClient;
|
|
161
|
-
}
|
|
162
|
-
throw new Error('PostgresDriver requires a pool or client');
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
private async releaseClient(client: PoolClient | Client): Promise<void> {
|
|
166
|
-
if (this.pool) {
|
|
167
|
-
(client as PoolClient).release();
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
|
|
171
144
|
private async *executeWithCursor(
|
|
172
145
|
client: PoolClient | Client,
|
|
173
146
|
sql: string,
|
|
174
147
|
params: readonly unknown[] | undefined,
|
|
148
|
+
cursorBatchSize: number,
|
|
175
149
|
): AsyncIterable<Record<string, unknown>> {
|
|
176
150
|
const cursor = client.query(new Cursor(sql, params as unknown[] | undefined));
|
|
177
151
|
|
|
178
152
|
try {
|
|
179
153
|
while (true) {
|
|
180
|
-
const rows = await readCursor(cursor,
|
|
154
|
+
const rows = await readCursor(cursor, cursorBatchSize);
|
|
181
155
|
if (rows.length === 0) {
|
|
182
156
|
break;
|
|
183
157
|
}
|
|
@@ -203,25 +177,215 @@ class PostgresDriverImpl implements SqlDriver {
|
|
|
203
177
|
}
|
|
204
178
|
}
|
|
205
179
|
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
180
|
+
class PostgresConnectionImpl extends PostgresQueryable implements SqlConnection {
|
|
181
|
+
#connection: PoolClient | Client;
|
|
182
|
+
#onRelease: (() => void) | undefined;
|
|
183
|
+
|
|
184
|
+
constructor(connection: PoolClient | Client, options: ConnectionOptions, onRelease?: () => void) {
|
|
185
|
+
super(options);
|
|
186
|
+
this.#connection = connection;
|
|
187
|
+
this.#onRelease = onRelease;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
override acquireClient(): Promise<PoolClient | Client> {
|
|
191
|
+
return Promise.resolve(this.#connection);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
override releaseClient(_client: PoolClient | Client): Promise<void> {
|
|
195
|
+
return Promise.resolve();
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
async beginTransaction(): Promise<SqlTransaction> {
|
|
199
|
+
await this.#connection.query('BEGIN').catch(rethrowNormalizedError);
|
|
200
|
+
return new PostgresTransactionImpl(this.#connection, this.options);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
async release(): Promise<void> {
|
|
204
|
+
if ('release' in this.#connection) {
|
|
205
|
+
this.#connection.release();
|
|
206
|
+
}
|
|
207
|
+
this.#onRelease?.();
|
|
208
|
+
this.#onRelease = undefined;
|
|
209
|
+
}
|
|
209
210
|
}
|
|
210
211
|
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
212
|
+
class PostgresTransactionImpl extends PostgresQueryable implements SqlTransaction {
|
|
213
|
+
#connection: PoolClient | Client;
|
|
214
|
+
|
|
215
|
+
constructor(connection: PoolClient | Client, options: ConnectionOptions) {
|
|
216
|
+
super(options);
|
|
217
|
+
this.#connection = connection;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
override acquireClient(): Promise<PoolClient | Client> {
|
|
221
|
+
return Promise.resolve(this.#connection);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
override releaseClient(_client: PoolClient | Client): Promise<void> {
|
|
225
|
+
return Promise.resolve();
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
async commit(): Promise<void> {
|
|
229
|
+
await this.#connection.query('COMMIT').catch(rethrowNormalizedError);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
async rollback(): Promise<void> {
|
|
233
|
+
await this.#connection.query('ROLLBACK').catch(rethrowNormalizedError);
|
|
234
|
+
}
|
|
221
235
|
}
|
|
222
236
|
|
|
223
|
-
|
|
224
|
-
|
|
237
|
+
class PostgresPoolDriverImpl
|
|
238
|
+
extends PostgresQueryable<PoolClient>
|
|
239
|
+
implements SqlDriver<PostgresBinding>
|
|
240
|
+
{
|
|
241
|
+
private readonly pool: PoolType;
|
|
242
|
+
#closed = false;
|
|
243
|
+
|
|
244
|
+
constructor(options: PostgresDriverOptions & { connect: { pool: PoolType } }) {
|
|
245
|
+
super({
|
|
246
|
+
cursorBatchSize: options.cursor?.batchSize ?? DEFAULT_BATCH_SIZE,
|
|
247
|
+
cursorDisabled: options.cursor?.disabled ?? false,
|
|
248
|
+
});
|
|
249
|
+
this.pool = options.connect.pool;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
get state(): SqlDriverState {
|
|
253
|
+
return this.#closed ? 'closed' : 'connected';
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
// Bound drivers are created via createBoundDriverFromBinding with pool already
|
|
257
|
+
// configured; connect is intentionally no-op.
|
|
258
|
+
async connect(_binding: PostgresBinding): Promise<void> {}
|
|
259
|
+
|
|
260
|
+
async acquireConnection(): Promise<SqlConnection> {
|
|
261
|
+
const client = await this.acquireClient();
|
|
262
|
+
return new PostgresConnectionImpl(client, this.options);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
async close(): Promise<void> {
|
|
266
|
+
if (this.#closed) {
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
this.#closed = true;
|
|
270
|
+
await this.pool.end();
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
async acquireClient(): Promise<PoolClient> {
|
|
274
|
+
return this.pool.connect();
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
async releaseClient(client: PoolClient): Promise<void> {
|
|
278
|
+
client.release();
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
class PostgresDirectDriverImpl
|
|
283
|
+
extends PostgresQueryable<Client>
|
|
284
|
+
implements SqlDriver<PostgresBinding>
|
|
285
|
+
{
|
|
286
|
+
private readonly directClient: Client;
|
|
287
|
+
readonly #connectionMutex = new AsyncMutex();
|
|
288
|
+
#closed = false;
|
|
289
|
+
#connected = false;
|
|
290
|
+
#connectPromise: Promise<void> | undefined;
|
|
291
|
+
|
|
292
|
+
constructor(options: PostgresDriverOptions & { connect: { client: Client } }) {
|
|
293
|
+
super({
|
|
294
|
+
cursorBatchSize: options.cursor?.batchSize ?? DEFAULT_BATCH_SIZE,
|
|
295
|
+
cursorDisabled: options.cursor?.disabled ?? false,
|
|
296
|
+
});
|
|
297
|
+
this.directClient = options.connect.client;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
get state(): SqlDriverState {
|
|
301
|
+
return this.#closed ? 'closed' : 'connected';
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
// Bound drivers are created via createBoundDriverFromBinding with client already
|
|
305
|
+
// configured; connect is intentionally no-op.
|
|
306
|
+
async connect(_binding: PostgresBinding): Promise<void> {}
|
|
307
|
+
|
|
308
|
+
async acquireConnection(): Promise<SqlConnection> {
|
|
309
|
+
const releaseLease = await this.#connectionMutex.lock();
|
|
310
|
+
try {
|
|
311
|
+
const client = await this.acquireClient();
|
|
312
|
+
return new PostgresConnectionImpl(client, this.options, releaseLease);
|
|
313
|
+
} catch (error) {
|
|
314
|
+
releaseLease();
|
|
315
|
+
throw error;
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
async close(): Promise<void> {
|
|
320
|
+
const releaseLease = await this.#connectionMutex.lock();
|
|
321
|
+
try {
|
|
322
|
+
if (this.#closed) {
|
|
323
|
+
return;
|
|
324
|
+
}
|
|
325
|
+
this.#closed = true;
|
|
326
|
+
await this.directClient.end();
|
|
327
|
+
this.#connected = false;
|
|
328
|
+
} finally {
|
|
329
|
+
releaseLease();
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
async acquireClient(): Promise<Client> {
|
|
334
|
+
if (this.#connected) {
|
|
335
|
+
return this.directClient;
|
|
336
|
+
}
|
|
337
|
+
if (this.#connectPromise !== undefined) {
|
|
338
|
+
await this.#connectPromise;
|
|
339
|
+
return this.directClient;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
this.#connectPromise = (async () => {
|
|
343
|
+
try {
|
|
344
|
+
await this.directClient.connect();
|
|
345
|
+
} catch (error: unknown) {
|
|
346
|
+
if (!isAlreadyConnectedError(error)) {
|
|
347
|
+
throw error;
|
|
348
|
+
}
|
|
349
|
+
} finally {
|
|
350
|
+
this.#connectPromise = undefined;
|
|
351
|
+
}
|
|
352
|
+
this.#connected = true;
|
|
353
|
+
})();
|
|
354
|
+
await this.#connectPromise;
|
|
355
|
+
|
|
356
|
+
return this.directClient;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
async releaseClient(_client: Client): Promise<void> {}
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
export function createBoundDriverFromBinding(
|
|
363
|
+
binding: PostgresBinding,
|
|
364
|
+
cursorOpts: PostgresDriverCreateOptions['cursor'],
|
|
365
|
+
): SqlDriver<PostgresBinding> {
|
|
366
|
+
switch (binding.kind) {
|
|
367
|
+
case 'url': {
|
|
368
|
+
const pool = new Pool({
|
|
369
|
+
connectionString: binding.url,
|
|
370
|
+
connectionTimeoutMillis: 20_000,
|
|
371
|
+
idleTimeoutMillis: 30_000,
|
|
372
|
+
});
|
|
373
|
+
return new PostgresPoolDriverImpl({
|
|
374
|
+
connect: { pool },
|
|
375
|
+
cursor: cursorOpts,
|
|
376
|
+
});
|
|
377
|
+
}
|
|
378
|
+
case 'pgPool':
|
|
379
|
+
return new PostgresPoolDriverImpl({
|
|
380
|
+
connect: { pool: binding.pool },
|
|
381
|
+
cursor: cursorOpts,
|
|
382
|
+
});
|
|
383
|
+
case 'pgClient':
|
|
384
|
+
return new PostgresDirectDriverImpl({
|
|
385
|
+
connect: { client: binding.client },
|
|
386
|
+
cursor: cursorOpts,
|
|
387
|
+
});
|
|
388
|
+
}
|
|
225
389
|
}
|
|
226
390
|
|
|
227
391
|
function readCursor<Row>(cursor: Cursor<Row>, size: number): Promise<Row[]> {
|
|
@@ -233,3 +397,7 @@ function readCursor<Row>(cursor: Cursor<Row>, size: number): Promise<Row[]> {
|
|
|
233
397
|
function closeCursor(cursor: Cursor<unknown>): Promise<void> {
|
|
234
398
|
return callbackToPromise((cb) => cursor.close(cb));
|
|
235
399
|
}
|
|
400
|
+
|
|
401
|
+
function rethrowNormalizedError(error: unknown): never {
|
|
402
|
+
throw normalizePgError(error);
|
|
403
|
+
}
|
|
@@ -1,7 +0,0 @@
|
|
|
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
|
|
@@ -1 +0,0 @@
|
|
|
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"}
|