forge-sql-orm 2.1.6 → 2.1.8

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.
@@ -546,6 +546,9 @@ async function setCacheResult(query, options, results, cacheTtl) {
546
546
  console.error(`Error setting cache: ${error.message}`, error);
547
547
  }
548
548
  }
549
+ function isQuery(obj) {
550
+ return typeof obj === "object" && obj !== null && typeof obj.sql === "string" && Array.isArray(obj.params);
551
+ }
549
552
  const cacheApplicationContext = new AsyncLocalStorage();
550
553
  const localCacheApplicationContext = new AsyncLocalStorage();
551
554
  async function saveTableIfInsideCacheContext(table) {
@@ -561,7 +564,12 @@ async function saveQueryLocalCacheQuery(query, rows, options) {
561
564
  if (!context.cache) {
562
565
  context.cache = {};
563
566
  }
564
- const sql2 = query;
567
+ let sql2;
568
+ if (isQuery(query)) {
569
+ sql2 = { toSQL: () => query };
570
+ } else {
571
+ sql2 = query;
572
+ }
565
573
  const key = hashKey(sql2.toSQL());
566
574
  context.cache[key] = {
567
575
  sql: sql2.toSQL().sql.toLowerCase(),
@@ -581,7 +589,12 @@ async function getQueryLocalCacheQuery(query, options) {
581
589
  if (!context.cache) {
582
590
  context.cache = {};
583
591
  }
584
- const sql2 = query;
592
+ let sql2;
593
+ if (isQuery(query)) {
594
+ sql2 = { toSQL: () => query };
595
+ } else {
596
+ sql2 = query;
597
+ }
585
598
  const key = hashKey(sql2.toSQL());
586
599
  if (context.cache[key] && context.cache[key].sql === sql2.toSQL().sql.toLowerCase()) {
587
600
  if (options.logCache) {
@@ -1066,7 +1079,7 @@ async function processDDLResult(method, result) {
1066
1079
  if (result.metadata) {
1067
1080
  await saveMetaDataToContext(result.metadata);
1068
1081
  }
1069
- if (!result.rows) {
1082
+ if (!result?.rows) {
1070
1083
  return { rows: [] };
1071
1084
  }
1072
1085
  if (isUpdateQueryResponse(result.rows)) {
@@ -1090,8 +1103,8 @@ async function processExecuteMethod(query, params) {
1090
1103
  }
1091
1104
  const result = await withTimeout$1(sqlStatement.execute());
1092
1105
  await saveMetaDataToContext(result.metadata);
1093
- if (!result.rows) {
1094
- return { rows: [] };
1106
+ if (!result?.rows || Array.isArray(result.rows) && result.rows.length === 0) {
1107
+ return { rows: [void 0] };
1095
1108
  }
1096
1109
  if (isUpdateQueryResponse(result.rows)) {
1097
1110
  const oneRow = result.rows;
@@ -1106,7 +1119,7 @@ async function processAllMethod(query, params) {
1106
1119
  }
1107
1120
  const result = await withTimeout$1(sqlStatement.execute());
1108
1121
  await saveMetaDataToContext(result.metadata);
1109
- if (!result.rows) {
1122
+ if (!result?.rows) {
1110
1123
  return { rows: [] };
1111
1124
  }
1112
1125
  const rows = result.rows.map((r) => Object.values(r));
@@ -1195,7 +1208,7 @@ async function handleSuccessfulExecution(rows, onfulfilled, table, options, isCa
1195
1208
  if (isCached && !cacheApplicationContext.getStore()) {
1196
1209
  await clearCache(table, options);
1197
1210
  }
1198
- const result = onfulfilled?.(rows);
1211
+ const result = onfulfilled ? onfulfilled(rows) : rows;
1199
1212
  return result;
1200
1213
  } catch (error) {
1201
1214
  if (shouldClearCacheOnError(error)) {
@@ -1266,11 +1279,11 @@ async function handleCachedQuery(target, options, cacheTtl, selections, aliasMap
1266
1279
  try {
1267
1280
  const localCached = await getQueryLocalCacheQuery(target, options);
1268
1281
  if (localCached) {
1269
- return onfulfilled?.(localCached);
1282
+ return onfulfilled ? onfulfilled(localCached) : localCached;
1270
1283
  }
1271
1284
  const cacheResult = await getFromCache(target, options);
1272
1285
  if (cacheResult) {
1273
- return onfulfilled?.(cacheResult);
1286
+ return onfulfilled ? onfulfilled(cacheResult) : cacheResult;
1274
1287
  }
1275
1288
  const rows = await target.execute();
1276
1289
  const transformed = applyFromDriverTransform(rows, selections, aliasMap);
@@ -1278,23 +1291,29 @@ async function handleCachedQuery(target, options, cacheTtl, selections, aliasMap
1278
1291
  await setCacheResult(target, options, transformed, cacheTtl).catch((cacheError) => {
1279
1292
  console.warn("Cache set error:", cacheError);
1280
1293
  });
1281
- return onfulfilled?.(transformed);
1294
+ return onfulfilled ? onfulfilled(transformed) : transformed;
1282
1295
  } catch (error) {
1283
- return onrejected?.(error);
1296
+ if (onrejected) {
1297
+ return onrejected(error);
1298
+ }
1299
+ throw error;
1284
1300
  }
1285
1301
  }
1286
1302
  async function handleNonCachedQuery(target, options, selections, aliasMap, onfulfilled, onrejected) {
1287
1303
  try {
1288
1304
  const localCached = await getQueryLocalCacheQuery(target, options);
1289
1305
  if (localCached) {
1290
- return onfulfilled?.(localCached);
1306
+ return onfulfilled ? onfulfilled(localCached) : localCached;
1291
1307
  }
1292
1308
  const rows = await target.execute();
1293
1309
  const transformed = applyFromDriverTransform(rows, selections, aliasMap);
1294
1310
  await saveQueryLocalCacheQuery(target, transformed, options);
1295
- return onfulfilled?.(transformed);
1311
+ return onfulfilled ? onfulfilled(transformed) : transformed;
1296
1312
  } catch (error) {
1297
- return onrejected?.(error);
1313
+ if (onrejected) {
1314
+ return onrejected(error);
1315
+ }
1316
+ throw error;
1298
1317
  }
1299
1318
  }
1300
1319
  function createAliasedSelectBuilder(db, fields, selectFn, useCache, options, cacheTtl) {