@nmakarov/cli-toolkit 0.37.0 → 0.40.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.
@@ -3263,23 +3263,90 @@ var Db = class _Db {
3263
3263
  }
3264
3264
  this.queriesLog = [];
3265
3265
  this.knexInstance.queriesLog = this.queriesLog;
3266
- this.knexInstance.on("query", (query) => {
3267
- query.__startTime = process.hrtime();
3268
- });
3269
- this.knexInstance.on("query-response", (_response, query) => {
3270
- const [seconds, nanoseconds] = process.hrtime(query.__startTime);
3271
- const executionTimeMs = (seconds * 1e3 + nanoseconds / 1e6).toFixed(2);
3266
+ const startsByConnection = /* @__PURE__ */ new Map();
3267
+ const pushStart = (query) => {
3268
+ const uid = query?.__knexUid ?? "";
3269
+ if (!startsByConnection.has(uid)) {
3270
+ startsByConnection.set(uid, []);
3271
+ }
3272
+ startsByConnection.get(uid).push(process.hrtime());
3273
+ };
3274
+ const popStart = (query) => {
3275
+ const uid = query?.__knexUid ?? "";
3276
+ const stack = startsByConnection.get(uid);
3277
+ if (!stack?.length) {
3278
+ return null;
3279
+ }
3280
+ return stack.pop();
3281
+ };
3282
+ const isTimeoutError = (error) => error?.name === "KnexTimeoutError" || /query timeout|timeout exceeded/i.test(String(error?.message ?? ""));
3283
+ const logQueryEntry = (query, start, { error } = {}) => {
3284
+ let executionTimeMs;
3285
+ if (start) {
3286
+ const [seconds, nanoseconds] = process.hrtime(start);
3287
+ executionTimeMs = (seconds * 1e3 + nanoseconds / 1e6).toFixed(2);
3288
+ } else if (error?.timeout != null) {
3289
+ executionTimeMs = Number(error.timeout).toFixed(2);
3290
+ } else {
3291
+ executionTimeMs = "?";
3292
+ }
3293
+ let status = "ok";
3294
+ if (error) {
3295
+ status = isTimeoutError(error) ? "timeout" : "error";
3296
+ }
3272
3297
  const logEntry = {
3273
- sql: query.sql,
3274
- bindings: query.bindings || [],
3275
- executionTimeMs
3298
+ sql: query?.sql,
3299
+ bindings: query?.bindings || [],
3300
+ executionTimeMs,
3301
+ status
3276
3302
  };
3303
+ if (error) {
3304
+ logEntry.error = error.message ?? String(error);
3305
+ }
3277
3306
  this.queriesLog.push(logEntry);
3278
- this.logger.debug?.(`[Db] Query: ${query.sql} | Duration: ${executionTimeMs}ms`);
3307
+ const suffix = error ? ` | ${status}: ${logEntry.error}` : "";
3308
+ this.logger.debug?.(
3309
+ `[Db] Query: ${query?.sql} | Duration: ${executionTimeMs}ms${suffix}`
3310
+ );
3311
+ };
3312
+ const handleQueryError = (error, query) => {
3313
+ const start = popStart(query);
3314
+ logQueryEntry(query, start, { error });
3315
+ this.logger.error?.(`[Db] Query failed: ${query?.sql}`, error);
3316
+ };
3317
+ this.knexInstance.on("query", (query) => {
3318
+ pushStart(query);
3319
+ });
3320
+ this.knexInstance.on("query-response", (_response, query) => {
3321
+ const start = popStart(query);
3322
+ if (!start) {
3323
+ return;
3324
+ }
3325
+ logQueryEntry(query, start);
3279
3326
  });
3280
3327
  this.knexInstance.on("query-error", (error, query) => {
3281
- this.logger.error?.(`[Db] Query failed: ${query.sql}`, error);
3328
+ handleQueryError(error, query);
3282
3329
  });
3330
+ const hookBuilder = (builder) => {
3331
+ if (!builder || builder.__dbProfilerHooked) {
3332
+ return;
3333
+ }
3334
+ builder.__dbProfilerHooked = true;
3335
+ builder.on("query", pushStart);
3336
+ builder.on("query-error", handleQueryError);
3337
+ };
3338
+ const client = this.knexInstance.client;
3339
+ if (client && !client.__dbProfilerPatched) {
3340
+ client.__dbProfilerPatched = true;
3341
+ if (typeof client.queryBuilder === "function") {
3342
+ const origQueryBuilder = client.queryBuilder.bind(client);
3343
+ client.queryBuilder = (...args) => {
3344
+ const builder = origQueryBuilder(...args);
3345
+ hookBuilder(builder);
3346
+ return builder;
3347
+ };
3348
+ }
3349
+ }
3283
3350
  }
3284
3351
  getQueryLog() {
3285
3352
  return [...this.queriesLog];