@slonik/driver 48.12.3 → 48.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/Logger.js +2 -2
- package/dist/factories/createDriverFactory.d.ts +17 -17
- package/dist/factories/createDriverFactory.d.ts.map +1 -1
- package/dist/factories/createDriverFactory.js +55 -58
- package/dist/factories/createDriverFactory.js.map +1 -1
- package/dist/factories/createDriverFactory.test.js +2 -2
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/package.json +37 -41
- package/src/Logger.ts +2 -2
- package/src/factories/createDriverFactory.test.ts +2 -2
- package/src/factories/createDriverFactory.ts +77 -89
- package/src/index.ts +1 -1
package/README.md
CHANGED
|
@@ -2,4 +2,4 @@
|
|
|
2
2
|
|
|
3
3
|
Slonik driver is an abstraction layer that provides a consistent API for interacting using different PostgreSQL clients.
|
|
4
4
|
|
|
5
|
-
This package is made available for developers who wish to create their own Slonik driver.
|
|
5
|
+
This package is made available for developers who wish to create their own Slonik driver.
|
package/dist/Logger.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import type { Field } from
|
|
2
|
-
import EventEmitter from
|
|
3
|
-
import type { Readable } from
|
|
4
|
-
import type { ConnectionOptions as TlsConnectionOptions } from
|
|
5
|
-
import type { StrictEventEmitter } from
|
|
1
|
+
import type { Field } from "@slonik/types";
|
|
2
|
+
import EventEmitter from "node:events";
|
|
3
|
+
import type { Readable } from "node:stream";
|
|
4
|
+
import type { ConnectionOptions as TlsConnectionOptions } from "node:tls";
|
|
5
|
+
import type { StrictEventEmitter } from "strict-event-emitter-types";
|
|
6
6
|
export type Driver = {
|
|
7
7
|
createClient: () => Promise<DriverClient>;
|
|
8
8
|
};
|
|
@@ -10,11 +10,11 @@ export type DriverClient = {
|
|
|
10
10
|
acquire: () => void;
|
|
11
11
|
destroy: () => Promise<void>;
|
|
12
12
|
id: () => string;
|
|
13
|
-
off: DriverClientEventEmitter[
|
|
14
|
-
on: DriverClientEventEmitter[
|
|
13
|
+
off: DriverClientEventEmitter["off"];
|
|
14
|
+
on: DriverClientEventEmitter["on"];
|
|
15
15
|
query: (query: string, values?: unknown[], queryOptions?: DriverQueryOptions) => Promise<DriverQueryResult>;
|
|
16
16
|
release: () => Promise<void>;
|
|
17
|
-
removeListener: DriverClientEventEmitter[
|
|
17
|
+
removeListener: DriverClientEventEmitter["removeListener"];
|
|
18
18
|
state: () => DriverClientState;
|
|
19
19
|
stream: (query: string, values?: unknown[]) => DriverStream<DriverStreamResult>;
|
|
20
20
|
};
|
|
@@ -25,20 +25,20 @@ export type DriverClientEventEmitter = StrictEventEmitter<EventEmitter, {
|
|
|
25
25
|
notice: (event: DriverNotice) => void;
|
|
26
26
|
release: () => void;
|
|
27
27
|
}>;
|
|
28
|
-
export type DriverClientState =
|
|
29
|
-
export type DriverCommand =
|
|
28
|
+
export type DriverClientState = "ACQUIRED" | "DESTROYED" | "IDLE" | "PENDING_DESTROY" | "PENDING_RELEASE";
|
|
29
|
+
export type DriverCommand = "COPY" | "DELETE" | "INSERT" | "SELECT" | "UPDATE";
|
|
30
30
|
export type DriverConfiguration = {
|
|
31
|
-
readonly connectionTimeout:
|
|
31
|
+
readonly connectionTimeout: "DISABLE_TIMEOUT" | number;
|
|
32
32
|
readonly connectionUri: string;
|
|
33
33
|
readonly gracefulTerminationTimeout?: number;
|
|
34
|
-
readonly idleInTransactionSessionTimeout:
|
|
35
|
-
readonly idleTimeout?:
|
|
36
|
-
readonly maximumConnectionAge?: number;
|
|
34
|
+
readonly idleInTransactionSessionTimeout: "DISABLE_TIMEOUT" | number;
|
|
35
|
+
readonly idleTimeout?: "DISABLE_TIMEOUT" | number;
|
|
36
|
+
readonly maximumConnectionAge?: "DISABLE_TIMEOUT" | number;
|
|
37
37
|
readonly maximumPoolSize?: number;
|
|
38
38
|
readonly minimumPoolSize?: number;
|
|
39
39
|
readonly resetConnection?: (connection: BasicConnection) => Promise<void>;
|
|
40
40
|
readonly ssl?: TlsConnectionOptions;
|
|
41
|
-
readonly statementTimeout:
|
|
41
|
+
readonly statementTimeout: "DISABLE_TIMEOUT" | number;
|
|
42
42
|
readonly typeParsers: readonly DriverTypeParser[];
|
|
43
43
|
};
|
|
44
44
|
export type DriverEventEmitter = StrictEventEmitter<EventEmitter, {
|
|
@@ -64,9 +64,9 @@ export type DriverQueryResult = {
|
|
|
64
64
|
readonly rowCount: null | number;
|
|
65
65
|
readonly rows: Array<Record<string, unknown>>;
|
|
66
66
|
};
|
|
67
|
-
export interface DriverStream<T> extends Omit<Readable,
|
|
67
|
+
export interface DriverStream<T> extends Omit<Readable, "on" | typeof Symbol.asyncIterator> {
|
|
68
68
|
[Symbol.asyncIterator]: () => AsyncIterableIterator<StreamDataEvent<T>>;
|
|
69
|
-
on: ((event:
|
|
69
|
+
on: ((event: "data", listener: (chunk: StreamDataEvent<T>) => void) => this) & ((event: string | symbol, listener: (...args: any[]) => void) => this);
|
|
70
70
|
}
|
|
71
71
|
export type DriverStreamResult = {
|
|
72
72
|
readonly fields: DriverField[];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createDriverFactory.d.ts","sourceRoot":"","sources":["../../src/factories/createDriverFactory.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AAE3C,OAAO,YAAY,MAAM,aAAa,CAAC;AACvC,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,KAAK,EAAE,iBAAiB,IAAI,oBAAoB,EAAE,MAAM,UAAU,CAAC;AAE1E,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,4BAA4B,CAAC;AAQrE,MAAM,MAAM,MAAM,GAAG;IACnB,YAAY,EAAE,MAAM,OAAO,CAAC,YAAY,CAAC,CAAC;CAC3C,CAAC;AAEF,MAAM,MAAM,YAAY,GAAG;IACzB,OAAO,EAAE,MAAM,IAAI,CAAC;IAC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,CAAC,CAAC;AAEzC;;;;GAIG;AACH,KAAK,kBAAkB,GAAG;IACxB,OAAO,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;IAC7B,GAAG,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;IACzB,KAAK,EAAE,CACL,KAAK,EAAE,MAAM,EACb,MAAM,CAAC,EAAE,OAAO,EAAE,EAClB,YAAY,CAAC,EAAE,kBAAkB,KAC9B,OAAO,CAAC,iBAAiB,CAAC,CAAC;IAChC,MAAM,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,OAAO,EAAE,KAAK,YAAY,CAAC,kBAAkB,CAAC,CAAC;CACjF,CAAC;AAEF,KAAK,yBAAyB,GAAG;IAC/B,gBAAgB,EAAE,CAAC,EACjB,kBAAkB,GACnB,EAAE;QACD,kBAAkB,EAAE,wBAAwB,CAAC;KAC9C,KAAK,OAAO,CAAC,kBAAkB,CAAC,CAAC;CACnC,CAAC;AAEF,KAAK,eAAe,CAAC,CAAC,IAAI;IAAE,IAAI,EAAE,CAAC,CAAC;IAAC,MAAM,EAAE,SAAS,KAAK,EAAE,CAAA;CAAE,CAAC;AAEhE,eAAO,MAAM,mBAAmB,GAAI,OAAO,WAAW,KAAG,aA2SxD,CAAC"}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/consistent-type-definitions */
|
|
2
|
-
import { Logger } from
|
|
3
|
-
import { generateUid } from
|
|
4
|
-
import EventEmitter from
|
|
5
|
-
import { serializeError } from
|
|
2
|
+
import { Logger } from "../Logger.js";
|
|
3
|
+
import { generateUid } from "@slonik/utilities";
|
|
4
|
+
import EventEmitter from "node:events";
|
|
5
|
+
import { serializeError } from "serialize-error";
|
|
6
6
|
const delay = (ms) => new Promise((resolve) => {
|
|
7
7
|
const timer = setTimeout(resolve, ms);
|
|
8
8
|
timer.unref();
|
|
@@ -30,10 +30,10 @@ export const createDriverFactory = (setup) => {
|
|
|
30
30
|
}
|
|
31
31
|
Logger.warn({
|
|
32
32
|
error: serializeError(error),
|
|
33
|
-
namespace:
|
|
34
|
-
},
|
|
33
|
+
namespace: "driverClient",
|
|
34
|
+
}, "unhandled driver client error");
|
|
35
35
|
};
|
|
36
|
-
clientEventEmitter.on(
|
|
36
|
+
clientEventEmitter.on("error", onError);
|
|
37
37
|
const { connect, end, query, stream } = await createPoolClient({
|
|
38
38
|
clientEventEmitter,
|
|
39
39
|
});
|
|
@@ -52,26 +52,26 @@ export const createDriverFactory = (setup) => {
|
|
|
52
52
|
};
|
|
53
53
|
const state = () => {
|
|
54
54
|
if (destroyPromise) {
|
|
55
|
-
return
|
|
55
|
+
return "PENDING_DESTROY";
|
|
56
56
|
}
|
|
57
57
|
if (releasePromise) {
|
|
58
|
-
return
|
|
58
|
+
return "PENDING_RELEASE";
|
|
59
59
|
}
|
|
60
60
|
if (isDestroyed) {
|
|
61
|
-
return
|
|
61
|
+
return "DESTROYED";
|
|
62
62
|
}
|
|
63
63
|
if (isAcquired) {
|
|
64
|
-
return
|
|
64
|
+
return "ACQUIRED";
|
|
65
65
|
}
|
|
66
|
-
return
|
|
66
|
+
return "IDLE";
|
|
67
67
|
};
|
|
68
68
|
const internalDestroy = async () => {
|
|
69
69
|
const currentState = state();
|
|
70
|
-
if (currentState ===
|
|
71
|
-
throw new Error(
|
|
70
|
+
if (currentState === "PENDING_DESTROY") {
|
|
71
|
+
throw new Error("Client is pending destroy.");
|
|
72
72
|
}
|
|
73
|
-
if (currentState ===
|
|
74
|
-
throw new Error(
|
|
73
|
+
if (currentState === "DESTROYED") {
|
|
74
|
+
throw new Error("Client is destroyed.");
|
|
75
75
|
}
|
|
76
76
|
clearIdleTimeout();
|
|
77
77
|
// activeQueryPromise and releasePromise are mutually exclusive
|
|
@@ -87,17 +87,14 @@ export const createDriverFactory = (setup) => {
|
|
|
87
87
|
]);
|
|
88
88
|
}
|
|
89
89
|
isDestroyed = true;
|
|
90
|
-
clientEventEmitter.emit(
|
|
90
|
+
clientEventEmitter.emit("destroy");
|
|
91
91
|
// end() calls pg client.end() which sends a termination message and
|
|
92
92
|
// waits for the TCP connection to close. On broken connections (half-open
|
|
93
93
|
// TCP, network partition) this can hang indefinitely. Apply the same
|
|
94
94
|
// graceful termination timeout to prevent leaked promises that block
|
|
95
95
|
// pool.end() and callers awaiting connection.destroy().
|
|
96
|
-
await Promise.race([
|
|
97
|
-
|
|
98
|
-
delay(driverConfiguration.gracefulTerminationTimeout),
|
|
99
|
-
]);
|
|
100
|
-
clientEventEmitter.off('error', onError);
|
|
96
|
+
await Promise.race([end(), delay(driverConfiguration.gracefulTerminationTimeout)]);
|
|
97
|
+
clientEventEmitter.off("error", onError);
|
|
101
98
|
};
|
|
102
99
|
destroy = async () => {
|
|
103
100
|
if (destroyPromise) {
|
|
@@ -108,17 +105,17 @@ export const createDriverFactory = (setup) => {
|
|
|
108
105
|
};
|
|
109
106
|
const internalRelease = async () => {
|
|
110
107
|
const currentState = state();
|
|
111
|
-
if (currentState ===
|
|
112
|
-
throw new Error(
|
|
108
|
+
if (currentState === "PENDING_DESTROY") {
|
|
109
|
+
throw new Error("Client is pending destroy.");
|
|
113
110
|
}
|
|
114
|
-
if (currentState ===
|
|
115
|
-
throw new Error(
|
|
111
|
+
if (currentState === "DESTROYED") {
|
|
112
|
+
throw new Error("Client is destroyed.");
|
|
116
113
|
}
|
|
117
|
-
if (currentState !==
|
|
118
|
-
throw new Error(
|
|
114
|
+
if (currentState !== "ACQUIRED") {
|
|
115
|
+
throw new Error("Client is not acquired.");
|
|
119
116
|
}
|
|
120
117
|
if (activeQueryPromise) {
|
|
121
|
-
throw new Error(
|
|
118
|
+
throw new Error("Client has an active query.");
|
|
122
119
|
}
|
|
123
120
|
if (resetConnection) {
|
|
124
121
|
await resetConnection({
|
|
@@ -127,7 +124,7 @@ export const createDriverFactory = (setup) => {
|
|
|
127
124
|
},
|
|
128
125
|
});
|
|
129
126
|
}
|
|
130
|
-
if (driverConfiguration.idleTimeout !==
|
|
127
|
+
if (driverConfiguration.idleTimeout !== "DISABLE_TIMEOUT") {
|
|
131
128
|
clearIdleTimeout();
|
|
132
129
|
idleTimeout = setTimeout(() => {
|
|
133
130
|
void destroy();
|
|
@@ -136,7 +133,7 @@ export const createDriverFactory = (setup) => {
|
|
|
136
133
|
}
|
|
137
134
|
isAcquired = false;
|
|
138
135
|
releasePromise = null;
|
|
139
|
-
clientEventEmitter.emit(
|
|
136
|
+
clientEventEmitter.emit("release");
|
|
140
137
|
};
|
|
141
138
|
const release = () => {
|
|
142
139
|
if (destroyPromise) {
|
|
@@ -151,21 +148,21 @@ export const createDriverFactory = (setup) => {
|
|
|
151
148
|
const client = {
|
|
152
149
|
acquire: () => {
|
|
153
150
|
const currentState = state();
|
|
154
|
-
if (currentState ===
|
|
155
|
-
throw new Error(
|
|
151
|
+
if (currentState === "PENDING_DESTROY") {
|
|
152
|
+
throw new Error("Client is pending destroy.");
|
|
156
153
|
}
|
|
157
|
-
if (currentState ===
|
|
158
|
-
throw new Error(
|
|
154
|
+
if (currentState === "PENDING_RELEASE") {
|
|
155
|
+
throw new Error("Client is pending release.");
|
|
159
156
|
}
|
|
160
|
-
if (currentState ===
|
|
161
|
-
throw new Error(
|
|
157
|
+
if (currentState === "DESTROYED") {
|
|
158
|
+
throw new Error("Client is destroyed.");
|
|
162
159
|
}
|
|
163
|
-
if (currentState ===
|
|
164
|
-
throw new Error(
|
|
160
|
+
if (currentState === "ACQUIRED") {
|
|
161
|
+
throw new Error("Client is already acquired.");
|
|
165
162
|
}
|
|
166
163
|
clearIdleTimeout();
|
|
167
164
|
isAcquired = true;
|
|
168
|
-
clientEventEmitter.emit(
|
|
165
|
+
clientEventEmitter.emit("acquire");
|
|
169
166
|
},
|
|
170
167
|
destroy,
|
|
171
168
|
id: () => id,
|
|
@@ -177,23 +174,23 @@ export const createDriverFactory = (setup) => {
|
|
|
177
174
|
},
|
|
178
175
|
query: async (sql, values, queryOptions) => {
|
|
179
176
|
const currentState = state();
|
|
180
|
-
if (currentState ===
|
|
181
|
-
throw new Error(
|
|
177
|
+
if (currentState === "PENDING_DESTROY") {
|
|
178
|
+
throw new Error("Client is pending destroy.");
|
|
182
179
|
}
|
|
183
|
-
if (currentState ===
|
|
184
|
-
throw new Error(
|
|
180
|
+
if (currentState === "PENDING_RELEASE") {
|
|
181
|
+
throw new Error("Client is pending release.");
|
|
185
182
|
}
|
|
186
|
-
if (currentState ===
|
|
187
|
-
throw new Error(
|
|
183
|
+
if (currentState === "DESTROYED") {
|
|
184
|
+
throw new Error("Client is destroyed.");
|
|
188
185
|
}
|
|
189
|
-
if (currentState !==
|
|
190
|
-
throw new Error(
|
|
186
|
+
if (currentState !== "ACQUIRED") {
|
|
187
|
+
throw new Error("Client is not acquired.");
|
|
191
188
|
}
|
|
192
189
|
try {
|
|
193
190
|
activeQueryPromise = query(sql, values, queryOptions);
|
|
194
191
|
const result = await activeQueryPromise;
|
|
195
192
|
if (!activeQueryPromise) {
|
|
196
|
-
throw new Error(
|
|
193
|
+
throw new Error("Expected `activeQueryPromise` to be set.");
|
|
197
194
|
}
|
|
198
195
|
return result;
|
|
199
196
|
}
|
|
@@ -208,17 +205,17 @@ export const createDriverFactory = (setup) => {
|
|
|
208
205
|
state,
|
|
209
206
|
stream: (sql, values) => {
|
|
210
207
|
const currentState = state();
|
|
211
|
-
if (currentState ===
|
|
212
|
-
throw new Error(
|
|
208
|
+
if (currentState === "PENDING_DESTROY") {
|
|
209
|
+
throw new Error("Client is pending destroy.");
|
|
213
210
|
}
|
|
214
|
-
if (currentState ===
|
|
215
|
-
throw new Error(
|
|
211
|
+
if (currentState === "PENDING_RELEASE") {
|
|
212
|
+
throw new Error("Client is pending release.");
|
|
216
213
|
}
|
|
217
|
-
if (currentState ===
|
|
218
|
-
throw new Error(
|
|
214
|
+
if (currentState === "DESTROYED") {
|
|
215
|
+
throw new Error("Client is destroyed.");
|
|
219
216
|
}
|
|
220
|
-
if (currentState !==
|
|
221
|
-
throw new Error(
|
|
217
|
+
if (currentState !== "ACQUIRED") {
|
|
218
|
+
throw new Error("Client is not acquired.");
|
|
222
219
|
}
|
|
223
220
|
// Note: Stream lifecycle management should be handled by the caller.
|
|
224
221
|
// The client can be released while streaming, but the stream will continue to work.
|
|
@@ -229,7 +226,7 @@ export const createDriverFactory = (setup) => {
|
|
|
229
226
|
await connect();
|
|
230
227
|
}
|
|
231
228
|
catch (error) {
|
|
232
|
-
clientEventEmitter.off(
|
|
229
|
+
clientEventEmitter.off("error", onError);
|
|
233
230
|
throw error;
|
|
234
231
|
}
|
|
235
232
|
return client;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createDriverFactory.js","sourceRoot":"","sources":["../../src/factories/createDriverFactory.ts"],"names":[],"mappings":"AAAA,mEAAmE;AACnE,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAEtC,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,YAAY,MAAM,aAAa,CAAC;AAGvC,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAGjD,MAAM,KAAK,GAAG,CAAC,EAAsB,EAAE,EAAE,CACvC,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,EAAE;IAC5B,MAAM,KAAK,GAAG,UAAU,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC;IACtC,KAAK,CAAC,KAAK,EAAE,CAAC;AAChB,CAAC,CAAC,CAAC;
|
|
1
|
+
{"version":3,"file":"createDriverFactory.js","sourceRoot":"","sources":["../../src/factories/createDriverFactory.ts"],"names":[],"mappings":"AAAA,mEAAmE;AACnE,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAEtC,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,YAAY,MAAM,aAAa,CAAC;AAGvC,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAGjD,MAAM,KAAK,GAAG,CAAC,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ClB,CAAC;gBAAC,OAAO,KAAK,EAAE,CAAC;oBACf,kBAAkB,CAAC,GAAG,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;oBAEzC,MAAM,KAAK,CAAC;gBACd,CAAC;gBAED,OAAO,MAAM,CAAC;YAChB,CAAC;SACF,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { createDriverFactory, type Driver, type DriverClient, type DriverClientEventEmitter, type DriverClientState, type DriverCommand, type DriverConfiguration, type DriverEventEmitter, type DriverFactory, type DriverNotice, type DriverQueryOptions, type DriverQueryResult, type DriverStream, type DriverStreamResult, type DriverTypeParser, } from
|
|
1
|
+
export { createDriverFactory, type Driver, type DriverClient, type DriverClientEventEmitter, type DriverClientState, type DriverCommand, type DriverConfiguration, type DriverEventEmitter, type DriverFactory, type DriverNotice, type DriverQueryOptions, type DriverQueryResult, type DriverStream, type DriverStreamResult, type DriverTypeParser, } from "./factories/createDriverFactory.js";
|
|
2
2
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { createDriverFactory, } from
|
|
1
|
+
export { createDriverFactory, } from "./factories/createDriverFactory.js";
|
|
2
2
|
//# sourceMappingURL=index.js.map
|
package/package.json
CHANGED
|
@@ -1,68 +1,64 @@
|
|
|
1
1
|
{
|
|
2
|
+
"name": "@slonik/driver",
|
|
3
|
+
"version": "48.13.0",
|
|
4
|
+
"description": "A Node.js PostgreSQL client with strict types, detailed logging and assertions.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"postgresql",
|
|
7
|
+
"promise",
|
|
8
|
+
"types"
|
|
9
|
+
],
|
|
10
|
+
"license": "BSD-3-Clause",
|
|
2
11
|
"author": {
|
|
3
|
-
"email": "gajus@gajus.com",
|
|
4
12
|
"name": "Gajus Kuizinas",
|
|
13
|
+
"email": "gajus@gajus.com",
|
|
5
14
|
"url": "http://gajus.com"
|
|
6
15
|
},
|
|
7
|
-
"
|
|
8
|
-
"
|
|
9
|
-
|
|
10
|
-
],
|
|
11
|
-
"files": [
|
|
12
|
-
"src/**/*.test.ts"
|
|
13
|
-
],
|
|
14
|
-
"nodeArguments": [
|
|
15
|
-
"--import=tsimp"
|
|
16
|
-
]
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "https://github.com/gajus/slonik"
|
|
17
19
|
},
|
|
20
|
+
"files": [
|
|
21
|
+
"./src",
|
|
22
|
+
"./dist"
|
|
23
|
+
],
|
|
24
|
+
"type": "module",
|
|
25
|
+
"main": "./dist/index.js",
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
18
27
|
"dependencies": {
|
|
19
|
-
"@slonik/types": "^48.
|
|
20
|
-
"@slonik/utilities": "^48.
|
|
28
|
+
"@slonik/types": "^48.13.0",
|
|
29
|
+
"@slonik/utilities": "^48.13.0",
|
|
21
30
|
"roarr": "^7.21.4",
|
|
22
31
|
"serialize-error": "^12.0.0",
|
|
23
32
|
"strict-event-emitter-types": "^2.0.0"
|
|
24
33
|
},
|
|
25
|
-
"description": "A Node.js PostgreSQL client with strict types, detailed logging and assertions.",
|
|
26
34
|
"devDependencies": {
|
|
27
35
|
"@types/node": "^24.10.13",
|
|
28
36
|
"ava": "^6.4.1",
|
|
29
37
|
"cspell": "^9.6.4",
|
|
30
|
-
"eslint": "9.39.2",
|
|
31
38
|
"tsimp": "^2.0.12",
|
|
32
|
-
"typescript": "^5.9.3"
|
|
33
|
-
"typescript-eslint": "^8.55.0",
|
|
34
|
-
"@slonik/eslint-config": "^48.12.3"
|
|
35
|
-
},
|
|
36
|
-
"engines": {
|
|
37
|
-
"node": ">=24"
|
|
39
|
+
"typescript": "^5.9.3"
|
|
38
40
|
},
|
|
39
|
-
"files": [
|
|
40
|
-
"./src",
|
|
41
|
-
"./dist"
|
|
42
|
-
],
|
|
43
|
-
"keywords": [
|
|
44
|
-
"postgresql",
|
|
45
|
-
"promise",
|
|
46
|
-
"types"
|
|
47
|
-
],
|
|
48
|
-
"license": "BSD-3-Clause",
|
|
49
|
-
"main": "./dist/index.js",
|
|
50
|
-
"name": "@slonik/driver",
|
|
51
41
|
"peerDependencies": {
|
|
52
42
|
"zod": "^3.25 || ^4"
|
|
53
43
|
},
|
|
54
|
-
"
|
|
55
|
-
"
|
|
56
|
-
|
|
44
|
+
"ava": {
|
|
45
|
+
"extensions": [
|
|
46
|
+
"ts"
|
|
47
|
+
],
|
|
48
|
+
"files": [
|
|
49
|
+
"src/**/*.test.ts"
|
|
50
|
+
],
|
|
51
|
+
"nodeArguments": [
|
|
52
|
+
"--import=tsimp"
|
|
53
|
+
]
|
|
54
|
+
},
|
|
55
|
+
"engines": {
|
|
56
|
+
"node": ">=24"
|
|
57
57
|
},
|
|
58
|
-
"type": "module",
|
|
59
|
-
"types": "./dist/index.d.ts",
|
|
60
|
-
"version": "48.12.3",
|
|
61
58
|
"scripts": {
|
|
62
59
|
"build": "rm -fr ./dist && tsc --project ./tsconfig.json",
|
|
63
|
-
"lint": "npm run lint:cspell && npm run lint:
|
|
60
|
+
"lint": "npm run lint:cspell && npm run lint:tsc",
|
|
64
61
|
"lint:cspell": "cspell . --no-progress --gitignore",
|
|
65
|
-
"lint:eslint": "eslint --cache ./src",
|
|
66
62
|
"lint:tsc": "tsc --noEmit",
|
|
67
63
|
"test": "ava --verbose --serial"
|
|
68
64
|
}
|
package/src/Logger.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/consistent-type-definitions */
|
|
2
|
-
import { Logger } from
|
|
3
|
-
import type { Field } from
|
|
4
|
-
import { generateUid } from
|
|
5
|
-
import EventEmitter from
|
|
6
|
-
import type { Readable } from
|
|
7
|
-
import type { ConnectionOptions as TlsConnectionOptions } from
|
|
8
|
-
import { serializeError } from
|
|
9
|
-
import type { StrictEventEmitter } from
|
|
2
|
+
import { Logger } from "../Logger.js";
|
|
3
|
+
import type { Field } from "@slonik/types";
|
|
4
|
+
import { generateUid } from "@slonik/utilities";
|
|
5
|
+
import EventEmitter from "node:events";
|
|
6
|
+
import type { Readable } from "node:stream";
|
|
7
|
+
import type { ConnectionOptions as TlsConnectionOptions } from "node:tls";
|
|
8
|
+
import { serializeError } from "serialize-error";
|
|
9
|
+
import type { StrictEventEmitter } from "strict-event-emitter-types";
|
|
10
10
|
|
|
11
11
|
const delay = (ms: number | undefined) =>
|
|
12
12
|
new Promise<void>((resolve) => {
|
|
@@ -22,20 +22,17 @@ export type DriverClient = {
|
|
|
22
22
|
acquire: () => void;
|
|
23
23
|
destroy: () => Promise<void>;
|
|
24
24
|
id: () => string;
|
|
25
|
-
off: DriverClientEventEmitter[
|
|
26
|
-
on: DriverClientEventEmitter[
|
|
25
|
+
off: DriverClientEventEmitter["off"];
|
|
26
|
+
on: DriverClientEventEmitter["on"];
|
|
27
27
|
query: (
|
|
28
28
|
query: string,
|
|
29
29
|
values?: unknown[],
|
|
30
30
|
queryOptions?: DriverQueryOptions,
|
|
31
31
|
) => Promise<DriverQueryResult>;
|
|
32
32
|
release: () => Promise<void>;
|
|
33
|
-
removeListener: DriverClientEventEmitter[
|
|
33
|
+
removeListener: DriverClientEventEmitter["removeListener"];
|
|
34
34
|
state: () => DriverClientState;
|
|
35
|
-
stream: (
|
|
36
|
-
query: string,
|
|
37
|
-
values?: unknown[],
|
|
38
|
-
) => DriverStream<DriverStreamResult>;
|
|
35
|
+
stream: (query: string, values?: unknown[]) => DriverStream<DriverStreamResult>;
|
|
39
36
|
};
|
|
40
37
|
|
|
41
38
|
export type DriverClientEventEmitter = StrictEventEmitter<
|
|
@@ -50,26 +47,26 @@ export type DriverClientEventEmitter = StrictEventEmitter<
|
|
|
50
47
|
>;
|
|
51
48
|
|
|
52
49
|
export type DriverClientState =
|
|
53
|
-
|
|
|
54
|
-
|
|
|
55
|
-
|
|
|
56
|
-
|
|
|
57
|
-
|
|
|
50
|
+
| "ACQUIRED"
|
|
51
|
+
| "DESTROYED"
|
|
52
|
+
| "IDLE"
|
|
53
|
+
| "PENDING_DESTROY"
|
|
54
|
+
| "PENDING_RELEASE";
|
|
58
55
|
|
|
59
|
-
export type DriverCommand =
|
|
56
|
+
export type DriverCommand = "COPY" | "DELETE" | "INSERT" | "SELECT" | "UPDATE";
|
|
60
57
|
|
|
61
58
|
export type DriverConfiguration = {
|
|
62
|
-
readonly connectionTimeout:
|
|
59
|
+
readonly connectionTimeout: "DISABLE_TIMEOUT" | number;
|
|
63
60
|
readonly connectionUri: string;
|
|
64
61
|
readonly gracefulTerminationTimeout?: number;
|
|
65
|
-
readonly idleInTransactionSessionTimeout:
|
|
66
|
-
readonly idleTimeout?:
|
|
67
|
-
readonly maximumConnectionAge?: number;
|
|
62
|
+
readonly idleInTransactionSessionTimeout: "DISABLE_TIMEOUT" | number;
|
|
63
|
+
readonly idleTimeout?: "DISABLE_TIMEOUT" | number;
|
|
64
|
+
readonly maximumConnectionAge?: "DISABLE_TIMEOUT" | number;
|
|
68
65
|
readonly maximumPoolSize?: number;
|
|
69
66
|
readonly minimumPoolSize?: number;
|
|
70
67
|
readonly resetConnection?: (connection: BasicConnection) => Promise<void>;
|
|
71
68
|
readonly ssl?: TlsConnectionOptions;
|
|
72
|
-
readonly statementTimeout:
|
|
69
|
+
readonly statementTimeout: "DISABLE_TIMEOUT" | number;
|
|
73
70
|
readonly typeParsers: readonly DriverTypeParser[];
|
|
74
71
|
};
|
|
75
72
|
|
|
@@ -106,12 +103,9 @@ export type DriverQueryResult = {
|
|
|
106
103
|
readonly rows: Array<Record<string, unknown>>;
|
|
107
104
|
};
|
|
108
105
|
|
|
109
|
-
export interface DriverStream<T> extends Omit<
|
|
110
|
-
Readable,
|
|
111
|
-
'on' | typeof Symbol.asyncIterator
|
|
112
|
-
> {
|
|
106
|
+
export interface DriverStream<T> extends Omit<Readable, "on" | typeof Symbol.asyncIterator> {
|
|
113
107
|
[Symbol.asyncIterator]: () => AsyncIterableIterator<StreamDataEvent<T>>;
|
|
114
|
-
on: ((event:
|
|
108
|
+
on: ((event: "data", listener: (chunk: StreamDataEvent<T>) => void) => this) &
|
|
115
109
|
((event: string | symbol, listener: (...args: any[]) => void) => this);
|
|
116
110
|
}
|
|
117
111
|
|
|
@@ -158,10 +152,7 @@ type InternalPoolClient = {
|
|
|
158
152
|
values?: unknown[],
|
|
159
153
|
queryOptions?: DriverQueryOptions,
|
|
160
154
|
) => Promise<DriverQueryResult>;
|
|
161
|
-
stream: (
|
|
162
|
-
query: string,
|
|
163
|
-
values?: unknown[],
|
|
164
|
-
) => DriverStream<DriverStreamResult>;
|
|
155
|
+
stream: (query: string, values?: unknown[]) => DriverStream<DriverStreamResult>;
|
|
165
156
|
};
|
|
166
157
|
|
|
167
158
|
type InternalPoolClientFactory = {
|
|
@@ -204,13 +195,13 @@ export const createDriverFactory = (setup: DriverSetup): DriverFactory => {
|
|
|
204
195
|
Logger.warn(
|
|
205
196
|
{
|
|
206
197
|
error: serializeError(error),
|
|
207
|
-
namespace:
|
|
198
|
+
namespace: "driverClient",
|
|
208
199
|
},
|
|
209
|
-
|
|
200
|
+
"unhandled driver client error",
|
|
210
201
|
);
|
|
211
202
|
};
|
|
212
203
|
|
|
213
|
-
clientEventEmitter.on(
|
|
204
|
+
clientEventEmitter.on("error", onError);
|
|
214
205
|
|
|
215
206
|
const { connect, end, query, stream } = await createPoolClient({
|
|
216
207
|
clientEventEmitter,
|
|
@@ -236,33 +227,33 @@ export const createDriverFactory = (setup: DriverSetup): DriverFactory => {
|
|
|
236
227
|
|
|
237
228
|
const state = () => {
|
|
238
229
|
if (destroyPromise) {
|
|
239
|
-
return
|
|
230
|
+
return "PENDING_DESTROY";
|
|
240
231
|
}
|
|
241
232
|
|
|
242
233
|
if (releasePromise) {
|
|
243
|
-
return
|
|
234
|
+
return "PENDING_RELEASE";
|
|
244
235
|
}
|
|
245
236
|
|
|
246
237
|
if (isDestroyed) {
|
|
247
|
-
return
|
|
238
|
+
return "DESTROYED";
|
|
248
239
|
}
|
|
249
240
|
|
|
250
241
|
if (isAcquired) {
|
|
251
|
-
return
|
|
242
|
+
return "ACQUIRED";
|
|
252
243
|
}
|
|
253
244
|
|
|
254
|
-
return
|
|
245
|
+
return "IDLE";
|
|
255
246
|
};
|
|
256
247
|
|
|
257
248
|
const internalDestroy = async () => {
|
|
258
249
|
const currentState = state();
|
|
259
250
|
|
|
260
|
-
if (currentState ===
|
|
261
|
-
throw new Error(
|
|
251
|
+
if (currentState === "PENDING_DESTROY") {
|
|
252
|
+
throw new Error("Client is pending destroy.");
|
|
262
253
|
}
|
|
263
254
|
|
|
264
|
-
if (currentState ===
|
|
265
|
-
throw new Error(
|
|
255
|
+
if (currentState === "DESTROYED") {
|
|
256
|
+
throw new Error("Client is destroyed.");
|
|
266
257
|
}
|
|
267
258
|
|
|
268
259
|
clearIdleTimeout();
|
|
@@ -283,19 +274,16 @@ export const createDriverFactory = (setup: DriverSetup): DriverFactory => {
|
|
|
283
274
|
|
|
284
275
|
isDestroyed = true;
|
|
285
276
|
|
|
286
|
-
clientEventEmitter.emit(
|
|
277
|
+
clientEventEmitter.emit("destroy");
|
|
287
278
|
|
|
288
279
|
// end() calls pg client.end() which sends a termination message and
|
|
289
280
|
// waits for the TCP connection to close. On broken connections (half-open
|
|
290
281
|
// TCP, network partition) this can hang indefinitely. Apply the same
|
|
291
282
|
// graceful termination timeout to prevent leaked promises that block
|
|
292
283
|
// pool.end() and callers awaiting connection.destroy().
|
|
293
|
-
await Promise.race([
|
|
294
|
-
end(),
|
|
295
|
-
delay(driverConfiguration.gracefulTerminationTimeout),
|
|
296
|
-
]);
|
|
284
|
+
await Promise.race([end(), delay(driverConfiguration.gracefulTerminationTimeout)]);
|
|
297
285
|
|
|
298
|
-
clientEventEmitter.off(
|
|
286
|
+
clientEventEmitter.off("error", onError);
|
|
299
287
|
};
|
|
300
288
|
|
|
301
289
|
destroy = async () => {
|
|
@@ -311,20 +299,20 @@ export const createDriverFactory = (setup: DriverSetup): DriverFactory => {
|
|
|
311
299
|
const internalRelease = async () => {
|
|
312
300
|
const currentState = state();
|
|
313
301
|
|
|
314
|
-
if (currentState ===
|
|
315
|
-
throw new Error(
|
|
302
|
+
if (currentState === "PENDING_DESTROY") {
|
|
303
|
+
throw new Error("Client is pending destroy.");
|
|
316
304
|
}
|
|
317
305
|
|
|
318
|
-
if (currentState ===
|
|
319
|
-
throw new Error(
|
|
306
|
+
if (currentState === "DESTROYED") {
|
|
307
|
+
throw new Error("Client is destroyed.");
|
|
320
308
|
}
|
|
321
309
|
|
|
322
|
-
if (currentState !==
|
|
323
|
-
throw new Error(
|
|
310
|
+
if (currentState !== "ACQUIRED") {
|
|
311
|
+
throw new Error("Client is not acquired.");
|
|
324
312
|
}
|
|
325
313
|
|
|
326
314
|
if (activeQueryPromise) {
|
|
327
|
-
throw new Error(
|
|
315
|
+
throw new Error("Client has an active query.");
|
|
328
316
|
}
|
|
329
317
|
|
|
330
318
|
if (resetConnection) {
|
|
@@ -335,7 +323,7 @@ export const createDriverFactory = (setup: DriverSetup): DriverFactory => {
|
|
|
335
323
|
});
|
|
336
324
|
}
|
|
337
325
|
|
|
338
|
-
if (driverConfiguration.idleTimeout !==
|
|
326
|
+
if (driverConfiguration.idleTimeout !== "DISABLE_TIMEOUT") {
|
|
339
327
|
clearIdleTimeout();
|
|
340
328
|
|
|
341
329
|
idleTimeout = setTimeout(() => {
|
|
@@ -349,7 +337,7 @@ export const createDriverFactory = (setup: DriverSetup): DriverFactory => {
|
|
|
349
337
|
|
|
350
338
|
releasePromise = null;
|
|
351
339
|
|
|
352
|
-
clientEventEmitter.emit(
|
|
340
|
+
clientEventEmitter.emit("release");
|
|
353
341
|
};
|
|
354
342
|
|
|
355
343
|
const release = () => {
|
|
@@ -370,27 +358,27 @@ export const createDriverFactory = (setup: DriverSetup): DriverFactory => {
|
|
|
370
358
|
acquire: () => {
|
|
371
359
|
const currentState = state();
|
|
372
360
|
|
|
373
|
-
if (currentState ===
|
|
374
|
-
throw new Error(
|
|
361
|
+
if (currentState === "PENDING_DESTROY") {
|
|
362
|
+
throw new Error("Client is pending destroy.");
|
|
375
363
|
}
|
|
376
364
|
|
|
377
|
-
if (currentState ===
|
|
378
|
-
throw new Error(
|
|
365
|
+
if (currentState === "PENDING_RELEASE") {
|
|
366
|
+
throw new Error("Client is pending release.");
|
|
379
367
|
}
|
|
380
368
|
|
|
381
|
-
if (currentState ===
|
|
382
|
-
throw new Error(
|
|
369
|
+
if (currentState === "DESTROYED") {
|
|
370
|
+
throw new Error("Client is destroyed.");
|
|
383
371
|
}
|
|
384
372
|
|
|
385
|
-
if (currentState ===
|
|
386
|
-
throw new Error(
|
|
373
|
+
if (currentState === "ACQUIRED") {
|
|
374
|
+
throw new Error("Client is already acquired.");
|
|
387
375
|
}
|
|
388
376
|
|
|
389
377
|
clearIdleTimeout();
|
|
390
378
|
|
|
391
379
|
isAcquired = true;
|
|
392
380
|
|
|
393
|
-
clientEventEmitter.emit(
|
|
381
|
+
clientEventEmitter.emit("acquire");
|
|
394
382
|
},
|
|
395
383
|
destroy,
|
|
396
384
|
id: () => id,
|
|
@@ -403,20 +391,20 @@ export const createDriverFactory = (setup: DriverSetup): DriverFactory => {
|
|
|
403
391
|
query: async (sql, values, queryOptions) => {
|
|
404
392
|
const currentState = state();
|
|
405
393
|
|
|
406
|
-
if (currentState ===
|
|
407
|
-
throw new Error(
|
|
394
|
+
if (currentState === "PENDING_DESTROY") {
|
|
395
|
+
throw new Error("Client is pending destroy.");
|
|
408
396
|
}
|
|
409
397
|
|
|
410
|
-
if (currentState ===
|
|
411
|
-
throw new Error(
|
|
398
|
+
if (currentState === "PENDING_RELEASE") {
|
|
399
|
+
throw new Error("Client is pending release.");
|
|
412
400
|
}
|
|
413
401
|
|
|
414
|
-
if (currentState ===
|
|
415
|
-
throw new Error(
|
|
402
|
+
if (currentState === "DESTROYED") {
|
|
403
|
+
throw new Error("Client is destroyed.");
|
|
416
404
|
}
|
|
417
405
|
|
|
418
|
-
if (currentState !==
|
|
419
|
-
throw new Error(
|
|
406
|
+
if (currentState !== "ACQUIRED") {
|
|
407
|
+
throw new Error("Client is not acquired.");
|
|
420
408
|
}
|
|
421
409
|
|
|
422
410
|
try {
|
|
@@ -425,7 +413,7 @@ export const createDriverFactory = (setup: DriverSetup): DriverFactory => {
|
|
|
425
413
|
const result = await activeQueryPromise;
|
|
426
414
|
|
|
427
415
|
if (!activeQueryPromise) {
|
|
428
|
-
throw new Error(
|
|
416
|
+
throw new Error("Expected `activeQueryPromise` to be set.");
|
|
429
417
|
}
|
|
430
418
|
|
|
431
419
|
return result;
|
|
@@ -441,20 +429,20 @@ export const createDriverFactory = (setup: DriverSetup): DriverFactory => {
|
|
|
441
429
|
stream: (sql, values) => {
|
|
442
430
|
const currentState = state();
|
|
443
431
|
|
|
444
|
-
if (currentState ===
|
|
445
|
-
throw new Error(
|
|
432
|
+
if (currentState === "PENDING_DESTROY") {
|
|
433
|
+
throw new Error("Client is pending destroy.");
|
|
446
434
|
}
|
|
447
435
|
|
|
448
|
-
if (currentState ===
|
|
449
|
-
throw new Error(
|
|
436
|
+
if (currentState === "PENDING_RELEASE") {
|
|
437
|
+
throw new Error("Client is pending release.");
|
|
450
438
|
}
|
|
451
439
|
|
|
452
|
-
if (currentState ===
|
|
453
|
-
throw new Error(
|
|
440
|
+
if (currentState === "DESTROYED") {
|
|
441
|
+
throw new Error("Client is destroyed.");
|
|
454
442
|
}
|
|
455
443
|
|
|
456
|
-
if (currentState !==
|
|
457
|
-
throw new Error(
|
|
444
|
+
if (currentState !== "ACQUIRED") {
|
|
445
|
+
throw new Error("Client is not acquired.");
|
|
458
446
|
}
|
|
459
447
|
|
|
460
448
|
// Note: Stream lifecycle management should be handled by the caller.
|
|
@@ -467,7 +455,7 @@ export const createDriverFactory = (setup: DriverSetup): DriverFactory => {
|
|
|
467
455
|
try {
|
|
468
456
|
await connect();
|
|
469
457
|
} catch (error) {
|
|
470
|
-
clientEventEmitter.off(
|
|
458
|
+
clientEventEmitter.off("error", onError);
|
|
471
459
|
|
|
472
460
|
throw error;
|
|
473
461
|
}
|
package/src/index.ts
CHANGED