@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.
package/dist/index.js CHANGED
@@ -3920,23 +3920,90 @@ var Db = class _Db {
3920
3920
  }
3921
3921
  this.queriesLog = [];
3922
3922
  this.knexInstance.queriesLog = this.queriesLog;
3923
- this.knexInstance.on("query", (query) => {
3924
- query.__startTime = process.hrtime();
3925
- });
3926
- this.knexInstance.on("query-response", (_response, query) => {
3927
- const [seconds, nanoseconds] = process.hrtime(query.__startTime);
3928
- const executionTimeMs = (seconds * 1e3 + nanoseconds / 1e6).toFixed(2);
3923
+ const startsByConnection = /* @__PURE__ */ new Map();
3924
+ const pushStart = (query) => {
3925
+ const uid = query?.__knexUid ?? "";
3926
+ if (!startsByConnection.has(uid)) {
3927
+ startsByConnection.set(uid, []);
3928
+ }
3929
+ startsByConnection.get(uid).push(process.hrtime());
3930
+ };
3931
+ const popStart = (query) => {
3932
+ const uid = query?.__knexUid ?? "";
3933
+ const stack = startsByConnection.get(uid);
3934
+ if (!stack?.length) {
3935
+ return null;
3936
+ }
3937
+ return stack.pop();
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
+ }
3929
3954
  const logEntry = {
3930
- sql: query.sql,
3931
- bindings: query.bindings || [],
3932
- executionTimeMs
3955
+ sql: query?.sql,
3956
+ bindings: query?.bindings || [],
3957
+ executionTimeMs,
3958
+ status
3933
3959
  };
3960
+ if (error) {
3961
+ logEntry.error = error.message ?? String(error);
3962
+ }
3934
3963
  this.queriesLog.push(logEntry);
3935
- this.logger.debug?.(`[Db] Query: ${query.sql} | Duration: ${executionTimeMs}ms`);
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
+ };
3974
+ this.knexInstance.on("query", (query) => {
3975
+ pushStart(query);
3976
+ });
3977
+ this.knexInstance.on("query-response", (_response, query) => {
3978
+ const start = popStart(query);
3979
+ if (!start) {
3980
+ return;
3981
+ }
3982
+ logQueryEntry(query, start);
3936
3983
  });
3937
3984
  this.knexInstance.on("query-error", (error, query) => {
3938
- this.logger.error?.(`[Db] Query failed: ${query.sql}`, error);
3985
+ handleQueryError(error, query);
3939
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
+ }
3940
4007
  }
3941
4008
  getQueryLog() {
3942
4009
  return [...this.queriesLog];