@maestro-js/db 1.0.0-alpha.19 → 1.0.0-alpha.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/index.d.ts +1 -7
- package/dist/index.js +16 -46
- package/package.json +6 -9
package/dist/index.d.ts
CHANGED
|
@@ -63,7 +63,6 @@ interface MysqlDbDriverConfig {
|
|
|
63
63
|
connectionLimit?: number;
|
|
64
64
|
debug?: boolean;
|
|
65
65
|
multipleStatements?: boolean;
|
|
66
|
-
maxExecutionTime?: number;
|
|
67
66
|
ssl?: string | (tls.SecureContextOptions & {
|
|
68
67
|
rejectUnauthorized?: boolean | undefined;
|
|
69
68
|
}) | undefined;
|
|
@@ -85,10 +84,6 @@ interface MysqlDbDriverConfig {
|
|
|
85
84
|
queryId: string;
|
|
86
85
|
connectDurationMs: number;
|
|
87
86
|
error: unknown;
|
|
88
|
-
} | {
|
|
89
|
-
event: 'poolError';
|
|
90
|
-
activeConnections: number;
|
|
91
|
-
error: unknown;
|
|
92
87
|
}
|
|
93
88
|
]>;
|
|
94
89
|
queryLogger?: Log.LogFunctions<MysqlDbDriverLogMessageData>;
|
|
@@ -98,7 +93,6 @@ declare function mysqlDbDriver(config: MysqlDbDriverConfig): DbDriver;
|
|
|
98
93
|
type KeysWithFallback = keyof Db.Provider.Keys extends never ? {
|
|
99
94
|
default: unknown;
|
|
100
95
|
} : Db.Provider.Keys;
|
|
101
|
-
type DbMysqlLogMessage = MysqlDbDriverLogMessageData;
|
|
102
96
|
/**
|
|
103
97
|
* Creates a new connection pool using the supplied options.
|
|
104
98
|
*/
|
|
@@ -213,7 +207,7 @@ declare namespace Db {
|
|
|
213
207
|
namespace drivers {
|
|
214
208
|
namespace mysql {
|
|
215
209
|
/** Log event data emitted by the MySQL driver for query and transaction lifecycle events */
|
|
216
|
-
type MysqlDbDriverLogMessageData =
|
|
210
|
+
type MysqlDbDriverLogMessageData = MysqlDbDriverLogMessageData;
|
|
217
211
|
}
|
|
218
212
|
}
|
|
219
213
|
namespace Provider {
|
package/dist/index.js
CHANGED
|
@@ -11,9 +11,10 @@ import { randomUUID } from "crypto";
|
|
|
11
11
|
import "tls";
|
|
12
12
|
import { Context } from "@maestro-js/context";
|
|
13
13
|
import { Helpers } from "@maestro-js/helpers";
|
|
14
|
+
import { Log } from "@maestro-js/log";
|
|
14
15
|
function mysqlDbDriver(config) {
|
|
15
|
-
const connectionLogger = config?.connectionLogger;
|
|
16
|
-
const queryLogger = config?.queryLogger;
|
|
16
|
+
const connectionLogger = config?.connectionLogger ?? Log.create();
|
|
17
|
+
const queryLogger = config?.queryLogger ?? Log.create();
|
|
17
18
|
const transactionContext = new AsyncLocalStorage();
|
|
18
19
|
function getPoolConnection() {
|
|
19
20
|
return new Promise((resolve, reject) => {
|
|
@@ -94,13 +95,9 @@ function mysqlDbDriver(config) {
|
|
|
94
95
|
database: config.database,
|
|
95
96
|
port: config.port,
|
|
96
97
|
connectionLimit: config.connectionLimit ?? 10,
|
|
97
|
-
connectTimeout:
|
|
98
|
+
connectTimeout: 60 * 60 * 1e3,
|
|
98
99
|
waitForConnections: true,
|
|
99
|
-
queueLimit:
|
|
100
|
-
maxIdle: 10,
|
|
101
|
-
idleTimeout: 6e4,
|
|
102
|
-
enableKeepAlive: true,
|
|
103
|
-
keepAliveInitialDelay: 1e4,
|
|
100
|
+
queueLimit: 0,
|
|
104
101
|
timezone: "Z",
|
|
105
102
|
debug: false,
|
|
106
103
|
multipleStatements: config.multipleStatements,
|
|
@@ -113,35 +110,13 @@ function mysqlDbDriver(config) {
|
|
|
113
110
|
decimalNumbers: true
|
|
114
111
|
});
|
|
115
112
|
let connectionCount = 0;
|
|
116
|
-
let activeConnections = 0;
|
|
117
|
-
pool.on("acquire", function() {
|
|
118
|
-
activeConnections++;
|
|
119
|
-
});
|
|
120
|
-
pool.on("release", function() {
|
|
121
|
-
activeConnections--;
|
|
122
|
-
});
|
|
123
113
|
pool.on("connection", function(connection) {
|
|
124
114
|
connectionCount++;
|
|
125
|
-
connectionLogger
|
|
115
|
+
connectionLogger.info({
|
|
126
116
|
event: "newConnection",
|
|
127
117
|
connectionCount,
|
|
128
118
|
connectionThreadId: connection.threadId
|
|
129
119
|
});
|
|
130
|
-
if (config.maxExecutionTime) {
|
|
131
|
-
connection.query(mysql.format("SET SESSION max_execution_time = ?", [config.maxExecutionTime]), (err) => {
|
|
132
|
-
if (err) {
|
|
133
|
-
console.error("Failed to set max_execution_time:", err);
|
|
134
|
-
connection.destroy();
|
|
135
|
-
}
|
|
136
|
-
});
|
|
137
|
-
}
|
|
138
|
-
});
|
|
139
|
-
pool.on("error", function(err) {
|
|
140
|
-
connectionLogger?.error({
|
|
141
|
-
event: "poolError",
|
|
142
|
-
activeConnections,
|
|
143
|
-
error: err
|
|
144
|
-
});
|
|
145
120
|
});
|
|
146
121
|
async function transaction(callback) {
|
|
147
122
|
const connection = await _getConnection();
|
|
@@ -204,7 +179,7 @@ function mysqlDbDriver(config) {
|
|
|
204
179
|
const transactionStartTime = (/* @__PURE__ */ new Date()).getTime();
|
|
205
180
|
try {
|
|
206
181
|
await connection.beginTransaction();
|
|
207
|
-
queryLogger
|
|
182
|
+
queryLogger.info({
|
|
208
183
|
event: "transactionStarted",
|
|
209
184
|
transactionId: contextValue.currentTransaction.id
|
|
210
185
|
});
|
|
@@ -216,7 +191,7 @@ function mysqlDbDriver(config) {
|
|
|
216
191
|
}
|
|
217
192
|
await contextValue.commit();
|
|
218
193
|
const duration = (/* @__PURE__ */ new Date()).getTime() - transactionStartTime;
|
|
219
|
-
queryLogger
|
|
194
|
+
queryLogger.info({
|
|
220
195
|
event: "transactionCompleted",
|
|
221
196
|
transactionId: contextValue.currentTransaction.id,
|
|
222
197
|
durationMs: duration
|
|
@@ -229,7 +204,7 @@ function mysqlDbDriver(config) {
|
|
|
229
204
|
);
|
|
230
205
|
await connection.rollback();
|
|
231
206
|
const duration = (/* @__PURE__ */ new Date()).getTime() - transactionStartTime;
|
|
232
|
-
queryLogger
|
|
207
|
+
queryLogger.info({
|
|
233
208
|
event: "transactionFailed",
|
|
234
209
|
transactionId: contextValue.currentTransaction.id,
|
|
235
210
|
durationMs: duration,
|
|
@@ -247,13 +222,13 @@ function mysqlDbDriver(config) {
|
|
|
247
222
|
}
|
|
248
223
|
async function getConnectionForQuery(queryId) {
|
|
249
224
|
const connectStartTime = (/* @__PURE__ */ new Date()).getTime();
|
|
250
|
-
connectionLogger
|
|
225
|
+
connectionLogger.info({
|
|
251
226
|
event: "connectionAcquisitionStarted",
|
|
252
227
|
queryId
|
|
253
228
|
});
|
|
254
229
|
try {
|
|
255
230
|
const connection = await _getConnection();
|
|
256
|
-
connectionLogger
|
|
231
|
+
connectionLogger.info({
|
|
257
232
|
event: "connectionAcquisitionCompleted",
|
|
258
233
|
queryId,
|
|
259
234
|
connectionId: connection.id,
|
|
@@ -261,7 +236,7 @@ function mysqlDbDriver(config) {
|
|
|
261
236
|
});
|
|
262
237
|
return connection;
|
|
263
238
|
} catch (e) {
|
|
264
|
-
connectionLogger
|
|
239
|
+
connectionLogger.error({
|
|
265
240
|
event: "connectionAcquisitionFailed",
|
|
266
241
|
queryId,
|
|
267
242
|
connectDurationMs: (/* @__PURE__ */ new Date()).getTime() - connectStartTime,
|
|
@@ -279,7 +254,7 @@ function mysqlDbDriver(config) {
|
|
|
279
254
|
const formatted = mysql.format(query, params);
|
|
280
255
|
const queryStartTime = (/* @__PURE__ */ new Date()).getTime();
|
|
281
256
|
const context = Context.getStore();
|
|
282
|
-
queryLogger
|
|
257
|
+
queryLogger.info({
|
|
283
258
|
event: "queryStarted",
|
|
284
259
|
queryId,
|
|
285
260
|
formattedQuery: formatted,
|
|
@@ -297,7 +272,7 @@ function mysqlDbDriver(config) {
|
|
|
297
272
|
Context.runWith(context, () => {
|
|
298
273
|
if (!streamError) {
|
|
299
274
|
const duration = (/* @__PURE__ */ new Date()).getTime() - queryStartTime;
|
|
300
|
-
queryLogger
|
|
275
|
+
queryLogger.info({
|
|
301
276
|
event: "queryCompleted",
|
|
302
277
|
queryId,
|
|
303
278
|
durationMs: duration,
|
|
@@ -313,7 +288,7 @@ function mysqlDbDriver(config) {
|
|
|
313
288
|
queryStream.on("error", (error) => {
|
|
314
289
|
Context.runWith(context, () => {
|
|
315
290
|
streamError = error;
|
|
316
|
-
queryLogger
|
|
291
|
+
queryLogger.error({
|
|
317
292
|
event: "queryFailed",
|
|
318
293
|
queryId,
|
|
319
294
|
durationMs: (/* @__PURE__ */ new Date()).getTime() - queryStartTime,
|
|
@@ -417,9 +392,6 @@ function mysqlDbDriver(config) {
|
|
|
417
392
|
}
|
|
418
393
|
};
|
|
419
394
|
}
|
|
420
|
-
function isConnectionError(err) {
|
|
421
|
-
return "code" in err && err.code === "ECONNRESET" || "code" in err && err.code === "ECONNREFUSED" || "code" in err && err.code === "ETIMEDOUT" || "code" in err && err.code === "PROTOCOL_CONNECTION_LOST";
|
|
422
|
-
}
|
|
423
395
|
async function execute(query, params = []) {
|
|
424
396
|
const queryId = randomUUID();
|
|
425
397
|
const isInTransaction = !!transactionContext.getStore();
|
|
@@ -455,9 +427,7 @@ function mysqlDbDriver(config) {
|
|
|
455
427
|
{
|
|
456
428
|
maxAttempts: 3,
|
|
457
429
|
delay: 0,
|
|
458
|
-
shouldRetry: (err) => "code" in err && err.code === "ER_LOCK_DEADLOCK" && !isInTransaction || "code" in err && err.code === "ER_LOCK_WAIT_TIMEOUT" ||
|
|
459
|
-
// Not safe inside a transaction — the whole transaction must be retried by the caller.
|
|
460
|
-
isConnectionError(err) && !isInTransaction || err instanceof Error && err.message.includes("Please retry") || err instanceof Error && err.message.includes("Try restarting transaction") && !isInTransaction
|
|
430
|
+
shouldRetry: (err) => "code" in err && err.code === "ER_LOCK_DEADLOCK" && !isInTransaction || "code" in err && err.code === "ER_LOCK_WAIT_TIMEOUT" || err instanceof Error && err.message.includes("Please retry") || err instanceof Error && err.message.includes("Try restarting transaction") && !isInTransaction
|
|
461
431
|
}
|
|
462
432
|
);
|
|
463
433
|
return results;
|
package/package.json
CHANGED
|
@@ -11,18 +11,15 @@
|
|
|
11
11
|
"dependencies": {
|
|
12
12
|
"iso-fns2": "npm:iso-fns@2.0.0-alpha.26",
|
|
13
13
|
"mysql2": "^3.15.3",
|
|
14
|
-
"@maestro-js/
|
|
15
|
-
"@maestro-js/
|
|
16
|
-
"@maestro-js/
|
|
17
|
-
|
|
18
|
-
"peerDependencies": {
|
|
19
|
-
"@maestro-js/log": "1.0.0-alpha.19"
|
|
14
|
+
"@maestro-js/context": "1.0.0-alpha.2",
|
|
15
|
+
"@maestro-js/helpers": "1.0.0-alpha.2",
|
|
16
|
+
"@maestro-js/log": "1.0.0-alpha.2",
|
|
17
|
+
"@maestro-js/service-registry": "1.0.0-alpha.2"
|
|
20
18
|
},
|
|
21
19
|
"devDependencies": {
|
|
22
|
-
"@types/node": "^22.19.11"
|
|
23
|
-
"@maestro-js/log": "1.0.0-alpha.19"
|
|
20
|
+
"@types/node": "^22.19.11"
|
|
24
21
|
},
|
|
25
|
-
"version": "1.0.0-alpha.
|
|
22
|
+
"version": "1.0.0-alpha.2",
|
|
26
23
|
"publishConfig": {
|
|
27
24
|
"access": "restricted"
|
|
28
25
|
},
|