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