forge-sql-orm 2.1.11 → 2.1.12

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.
@@ -804,7 +804,14 @@ async function printQueriesWithPlan(forgeSQLORM, timeDiffMs, timeout) {
804
804
  and(
805
805
  isNotNull(statementsTable.digest),
806
806
  not(ilike(statementsTable.digestText, "%information_schema%")),
807
- notInArray(statementsTable.stmtType, ["Use", "Set", "Show", "Commit", "Rollback", "Begin"]),
807
+ notInArray(statementsTable.stmtType, [
808
+ "Use",
809
+ "Set",
810
+ "Show",
811
+ "Commit",
812
+ "Rollback",
813
+ "Begin"
814
+ ]),
808
815
  gte(
809
816
  statementsTable.lastSeen,
810
817
  sql$1`DATE_SUB
@@ -866,9 +873,7 @@ async function slowQueryPerHours(forgeSQLORM, hours, timeout) {
866
873
  const message = `Found SlowQuery SQL: ${result.query} | Memory: ${memMaxMB.toFixed(2)} MB | Time: ${result.queryTime} ms
867
874
  Plan:${result.plan}`;
868
875
  response.push(message);
869
- console.warn(
870
- message
871
- );
876
+ console.warn(message);
872
877
  });
873
878
  return response;
874
879
  } catch (error) {
@@ -876,16 +881,16 @@ async function slowQueryPerHours(forgeSQLORM, hours, timeout) {
876
881
  `Error occurred while retrieving query execution plan: ${error instanceof Error ? error.message : "Unknown error"}. Try again after some time`,
877
882
  error
878
883
  );
879
- return [`Error occurred while retrieving query execution plan: ${error instanceof Error ? error.message : "Unknown error"}`];
884
+ return [
885
+ `Error occurred while retrieving query execution plan: ${error instanceof Error ? error.message : "Unknown error"}`
886
+ ];
880
887
  }
881
888
  }
882
889
  async function withTimeout(promise, message, timeoutMs2) {
883
890
  let timeoutId;
884
891
  const timeoutPromise = new Promise((_, reject) => {
885
892
  timeoutId = setTimeout(() => {
886
- reject(
887
- new Error(message)
888
- );
893
+ reject(new Error(message));
889
894
  }, timeoutMs2);
890
895
  });
891
896
  try {
@@ -917,6 +922,17 @@ function nowPlusSeconds(secondsToAdd) {
917
922
  const dt = DateTime.now().plus({ seconds: secondsToAdd });
918
923
  return Math.floor(dt.toSeconds());
919
924
  }
925
+ function extractBacktickedValues(sql2) {
926
+ const regex = /`([^`]+)`/g;
927
+ const matches = /* @__PURE__ */ new Set();
928
+ let match;
929
+ while ((match = regex.exec(sql2.toLowerCase())) !== null) {
930
+ if (!match[1].startsWith("a_")) {
931
+ matches.add(`\`${match[1]}\``);
932
+ }
933
+ }
934
+ return Array.from(matches).sort().join(",");
935
+ }
920
936
  function hashKey(query) {
921
937
  const h = crypto.createHash("sha256");
922
938
  h.update(query.sql.toLowerCase());
@@ -1060,7 +1076,7 @@ async function getFromCache(query, options) {
1060
1076
  }
1061
1077
  try {
1062
1078
  const cacheResult = await kvs.entity(options.cacheEntityName).get(key);
1063
- if (cacheResult && cacheResult[expirationName] >= getCurrentTime() && sqlQuery.sql.toLowerCase() === cacheResult[entityQueryName]) {
1079
+ if (cacheResult && cacheResult[expirationName] >= getCurrentTime() && extractBacktickedValues(sqlQuery.sql) === cacheResult[entityQueryName]) {
1064
1080
  if (options.logCache) {
1065
1081
  console.warn(`Get value from cache, cacheKey: ${key}`);
1066
1082
  }
@@ -1091,7 +1107,7 @@ async function setCacheResult(query, options, results, cacheTtl) {
1091
1107
  await kvs.transact().set(
1092
1108
  key,
1093
1109
  {
1094
- [entityQueryName]: sqlQuery.sql.toLowerCase(),
1110
+ [entityQueryName]: extractBacktickedValues(sqlQuery.sql),
1095
1111
  [expirationName]: nowPlusSeconds(cacheTtl),
1096
1112
  [dataName]: JSON.stringify(results)
1097
1113
  },
@@ -1594,10 +1610,7 @@ async function saveMetaDataToContext(metadata) {
1594
1610
  if (process.env.NODE_ENV !== "test") {
1595
1611
  await new Promise((r) => setTimeout(r, 200));
1596
1612
  }
1597
- await printQueriesWithPlan(
1598
- context.forgeSQLORM,
1599
- Date.now() - context.beginTime.getTime()
1600
- );
1613
+ await printQueriesWithPlan(context.forgeSQLORM, Date.now() - context.beginTime.getTime());
1601
1614
  };
1602
1615
  if (metadata) {
1603
1616
  context.totalResponseSize += metadata.responseSize;
@@ -1664,7 +1677,11 @@ async function processAllMethod(query, params) {
1664
1677
  if (params) {
1665
1678
  await sqlStatement.bindParams(...params);
1666
1679
  }
1667
- const result = await withTimeout(sqlStatement.execute(), timeoutMessage, timeoutMs);
1680
+ const result = await withTimeout(
1681
+ sqlStatement.execute(),
1682
+ timeoutMessage,
1683
+ timeoutMs
1684
+ );
1668
1685
  await saveMetaDataToContext(result.metadata);
1669
1686
  if (!result.rows) {
1670
1687
  return { rows: [] };
@@ -1675,7 +1692,11 @@ async function processAllMethod(query, params) {
1675
1692
  const forgeDriver = async (query, params, method) => {
1676
1693
  const operationType = await getOperationType();
1677
1694
  if (operationType === "DDL") {
1678
- const result = await withTimeout(sql.executeDDL(inlineParams(query, params)), timeoutMessage, timeoutMs);
1695
+ const result = await withTimeout(
1696
+ sql.executeDDL(inlineParams(query, params)),
1697
+ timeoutMessage,
1698
+ timeoutMs
1699
+ );
1679
1700
  return await processDDLResult(method, result);
1680
1701
  }
1681
1702
  if (method === "execute") {
@@ -3814,7 +3835,12 @@ const clearCacheSchedulerTrigger = async (options) => {
3814
3835
  };
3815
3836
  async function slowQuerySchedulerTrigger(forgeSQLORM, options) {
3816
3837
  try {
3817
- return getHttpResponse(200, JSON.stringify(await slowQueryPerHours(forgeSQLORM, options?.hours ?? 1, options?.timeout ?? 3e3)));
3838
+ return getHttpResponse(
3839
+ 200,
3840
+ JSON.stringify(
3841
+ await slowQueryPerHours(forgeSQLORM, options?.hours ?? 1, options?.timeout ?? 3e3)
3842
+ )
3843
+ );
3818
3844
  } catch (error) {
3819
3845
  const errorMessage = error?.debug?.sqlMessage ?? error?.debug?.message ?? error.message ?? "Unknown error occurred";
3820
3846
  console.error(errorMessage);