@nmakarov/cli-toolkit 0.39.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.
package/dist/index.js CHANGED
@@ -3936,6 +3936,41 @@ var Db = class _Db {
3936
3936
  }
3937
3937
  return stack.pop();
3938
3938
  };
3939
+ const isTimeoutError = (error) => error?.name === "KnexTimeoutError" || /query timeout|timeout exceeded/i.test(String(error?.message ?? ""));
3940
+ const logQueryEntry = (query, start, { error } = {}) => {
3941
+ let executionTimeMs;
3942
+ if (start) {
3943
+ const [seconds, nanoseconds] = process.hrtime(start);
3944
+ executionTimeMs = (seconds * 1e3 + nanoseconds / 1e6).toFixed(2);
3945
+ } else if (error?.timeout != null) {
3946
+ executionTimeMs = Number(error.timeout).toFixed(2);
3947
+ } else {
3948
+ executionTimeMs = "?";
3949
+ }
3950
+ let status = "ok";
3951
+ if (error) {
3952
+ status = isTimeoutError(error) ? "timeout" : "error";
3953
+ }
3954
+ const logEntry = {
3955
+ sql: query?.sql,
3956
+ bindings: query?.bindings || [],
3957
+ executionTimeMs,
3958
+ status
3959
+ };
3960
+ if (error) {
3961
+ logEntry.error = error.message ?? String(error);
3962
+ }
3963
+ this.queriesLog.push(logEntry);
3964
+ const suffix = error ? ` | ${status}: ${logEntry.error}` : "";
3965
+ this.logger.debug?.(
3966
+ `[Db] Query: ${query?.sql} | Duration: ${executionTimeMs}ms${suffix}`
3967
+ );
3968
+ };
3969
+ const handleQueryError = (error, query) => {
3970
+ const start = popStart(query);
3971
+ logQueryEntry(query, start, { error });
3972
+ this.logger.error?.(`[Db] Query failed: ${query?.sql}`, error);
3973
+ };
3939
3974
  this.knexInstance.on("query", (query) => {
3940
3975
  pushStart(query);
3941
3976
  });
@@ -3944,20 +3979,31 @@ var Db = class _Db {
3944
3979
  if (!start) {
3945
3980
  return;
3946
3981
  }
3947
- const [seconds, nanoseconds] = process.hrtime(start);
3948
- const executionTimeMs = (seconds * 1e3 + nanoseconds / 1e6).toFixed(2);
3949
- const logEntry = {
3950
- sql: query.sql,
3951
- bindings: query.bindings || [],
3952
- executionTimeMs
3953
- };
3954
- this.queriesLog.push(logEntry);
3955
- this.logger.debug?.(`[Db] Query: ${query.sql} | Duration: ${executionTimeMs}ms`);
3982
+ logQueryEntry(query, start);
3956
3983
  });
3957
3984
  this.knexInstance.on("query-error", (error, query) => {
3958
- popStart(query);
3959
- this.logger.error?.(`[Db] Query failed: ${query.sql}`, error);
3985
+ handleQueryError(error, query);
3960
3986
  });
3987
+ const hookBuilder = (builder) => {
3988
+ if (!builder || builder.__dbProfilerHooked) {
3989
+ return;
3990
+ }
3991
+ builder.__dbProfilerHooked = true;
3992
+ builder.on("query", pushStart);
3993
+ builder.on("query-error", handleQueryError);
3994
+ };
3995
+ const client = this.knexInstance.client;
3996
+ if (client && !client.__dbProfilerPatched) {
3997
+ client.__dbProfilerPatched = true;
3998
+ if (typeof client.queryBuilder === "function") {
3999
+ const origQueryBuilder = client.queryBuilder.bind(client);
4000
+ client.queryBuilder = (...args) => {
4001
+ const builder = origQueryBuilder(...args);
4002
+ hookBuilder(builder);
4003
+ return builder;
4004
+ };
4005
+ }
4006
+ }
3961
4007
  }
3962
4008
  getQueryLog() {
3963
4009
  return [...this.queriesLog];