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.
@@ -565,6 +565,9 @@ async function setCacheResult(query, options, results, cacheTtl) {
565
565
  console.error(`Error setting cache: ${error.message}`, error);
566
566
  }
567
567
  }
568
+ function isQuery(obj) {
569
+ return typeof obj === "object" && obj !== null && typeof obj.sql === "string" && Array.isArray(obj.params);
570
+ }
568
571
  const cacheApplicationContext = new node_async_hooks.AsyncLocalStorage();
569
572
  const localCacheApplicationContext = new node_async_hooks.AsyncLocalStorage();
570
573
  async function saveTableIfInsideCacheContext(table$1) {
@@ -580,7 +583,12 @@ async function saveQueryLocalCacheQuery(query, rows, options) {
580
583
  if (!context.cache) {
581
584
  context.cache = {};
582
585
  }
583
- const sql2 = query;
586
+ let sql2;
587
+ if (isQuery(query)) {
588
+ sql2 = { toSQL: () => query };
589
+ } else {
590
+ sql2 = query;
591
+ }
584
592
  const key = hashKey(sql2.toSQL());
585
593
  context.cache[key] = {
586
594
  sql: sql2.toSQL().sql.toLowerCase(),
@@ -600,7 +608,12 @@ async function getQueryLocalCacheQuery(query, options) {
600
608
  if (!context.cache) {
601
609
  context.cache = {};
602
610
  }
603
- const sql2 = query;
611
+ let sql2;
612
+ if (isQuery(query)) {
613
+ sql2 = { toSQL: () => query };
614
+ } else {
615
+ sql2 = query;
616
+ }
604
617
  const key = hashKey(sql2.toSQL());
605
618
  if (context.cache[key] && context.cache[key].sql === sql2.toSQL().sql.toLowerCase()) {
606
619
  if (options.logCache) {
@@ -1085,7 +1098,7 @@ async function processDDLResult(method, result) {
1085
1098
  if (result.metadata) {
1086
1099
  await saveMetaDataToContext(result.metadata);
1087
1100
  }
1088
- if (!result.rows) {
1101
+ if (!result?.rows) {
1089
1102
  return { rows: [] };
1090
1103
  }
1091
1104
  if (isUpdateQueryResponse(result.rows)) {
@@ -1109,8 +1122,8 @@ async function processExecuteMethod(query, params) {
1109
1122
  }
1110
1123
  const result = await withTimeout$1(sqlStatement.execute());
1111
1124
  await saveMetaDataToContext(result.metadata);
1112
- if (!result.rows) {
1113
- return { rows: [] };
1125
+ if (!result?.rows || Array.isArray(result.rows) && result.rows.length === 0) {
1126
+ return { rows: [void 0] };
1114
1127
  }
1115
1128
  if (isUpdateQueryResponse(result.rows)) {
1116
1129
  const oneRow = result.rows;
@@ -1125,7 +1138,7 @@ async function processAllMethod(query, params) {
1125
1138
  }
1126
1139
  const result = await withTimeout$1(sqlStatement.execute());
1127
1140
  await saveMetaDataToContext(result.metadata);
1128
- if (!result.rows) {
1141
+ if (!result?.rows) {
1129
1142
  return { rows: [] };
1130
1143
  }
1131
1144
  const rows = result.rows.map((r) => Object.values(r));
@@ -1214,7 +1227,7 @@ async function handleSuccessfulExecution(rows, onfulfilled, table2, options, isC
1214
1227
  if (isCached && !cacheApplicationContext.getStore()) {
1215
1228
  await clearCache(table2, options);
1216
1229
  }
1217
- const result = onfulfilled?.(rows);
1230
+ const result = onfulfilled ? onfulfilled(rows) : rows;
1218
1231
  return result;
1219
1232
  } catch (error) {
1220
1233
  if (shouldClearCacheOnError(error)) {
@@ -1285,11 +1298,11 @@ async function handleCachedQuery(target, options, cacheTtl, selections, aliasMap
1285
1298
  try {
1286
1299
  const localCached = await getQueryLocalCacheQuery(target, options);
1287
1300
  if (localCached) {
1288
- return onfulfilled?.(localCached);
1301
+ return onfulfilled ? onfulfilled(localCached) : localCached;
1289
1302
  }
1290
1303
  const cacheResult = await getFromCache(target, options);
1291
1304
  if (cacheResult) {
1292
- return onfulfilled?.(cacheResult);
1305
+ return onfulfilled ? onfulfilled(cacheResult) : cacheResult;
1293
1306
  }
1294
1307
  const rows = await target.execute();
1295
1308
  const transformed = applyFromDriverTransform(rows, selections, aliasMap);
@@ -1297,23 +1310,29 @@ async function handleCachedQuery(target, options, cacheTtl, selections, aliasMap
1297
1310
  await setCacheResult(target, options, transformed, cacheTtl).catch((cacheError) => {
1298
1311
  console.warn("Cache set error:", cacheError);
1299
1312
  });
1300
- return onfulfilled?.(transformed);
1313
+ return onfulfilled ? onfulfilled(transformed) : transformed;
1301
1314
  } catch (error) {
1302
- return onrejected?.(error);
1315
+ if (onrejected) {
1316
+ return onrejected(error);
1317
+ }
1318
+ throw error;
1303
1319
  }
1304
1320
  }
1305
1321
  async function handleNonCachedQuery(target, options, selections, aliasMap, onfulfilled, onrejected) {
1306
1322
  try {
1307
1323
  const localCached = await getQueryLocalCacheQuery(target, options);
1308
1324
  if (localCached) {
1309
- return onfulfilled?.(localCached);
1325
+ return onfulfilled ? onfulfilled(localCached) : localCached;
1310
1326
  }
1311
1327
  const rows = await target.execute();
1312
1328
  const transformed = applyFromDriverTransform(rows, selections, aliasMap);
1313
1329
  await saveQueryLocalCacheQuery(target, transformed, options);
1314
- return onfulfilled?.(transformed);
1330
+ return onfulfilled ? onfulfilled(transformed) : transformed;
1315
1331
  } catch (error) {
1316
- return onrejected?.(error);
1332
+ if (onrejected) {
1333
+ return onrejected(error);
1334
+ }
1335
+ throw error;
1317
1336
  }
1318
1337
  }
1319
1338
  function createAliasedSelectBuilder(db, fields, selectFn, useCache, options, cacheTtl) {