forge-sql-orm 2.1.1 → 2.1.2
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/ForgeSQLORM.js +64 -50
- package/dist/ForgeSQLORM.js.map +1 -1
- package/dist/ForgeSQLORM.mjs +64 -50
- package/dist/ForgeSQLORM.mjs.map +1 -1
- package/dist/core/ForgeSQLORM.d.ts.map +1 -1
- package/dist/core/ForgeSQLQueryBuilder.d.ts +23 -23
- package/dist/core/ForgeSQLQueryBuilder.d.ts.map +1 -1
- package/dist/lib/drizzle/extensions/additionalActions.d.ts.map +1 -1
- package/dist/utils/cacheContextUtils.d.ts +4 -2
- package/dist/utils/cacheContextUtils.d.ts.map +1 -1
- package/dist/utils/sqlUtils.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/core/ForgeSQLORM.ts +29 -23
- package/src/core/ForgeSQLQueryBuilder.ts +172 -119
- package/src/lib/drizzle/extensions/additionalActions.ts +123 -74
- package/src/lib/drizzle/extensions/types.d.ts +16 -11
- package/src/utils/cacheContextUtils.ts +16 -4
- package/src/utils/sqlUtils.ts +27 -28
package/dist/ForgeSQLORM.js
CHANGED
|
@@ -553,7 +553,7 @@ async function saveTableIfInsideCacheContext(table$1) {
|
|
|
553
553
|
context.tables.add(tableName);
|
|
554
554
|
}
|
|
555
555
|
}
|
|
556
|
-
async function saveQueryLocalCacheQuery(query, rows) {
|
|
556
|
+
async function saveQueryLocalCacheQuery(query, rows, options) {
|
|
557
557
|
const context = localCacheApplicationContext.getStore();
|
|
558
558
|
if (context) {
|
|
559
559
|
if (!context.cache) {
|
|
@@ -565,9 +565,15 @@ async function saveQueryLocalCacheQuery(query, rows) {
|
|
|
565
565
|
sql: sql2.toSQL().sql.toLowerCase(),
|
|
566
566
|
data: rows
|
|
567
567
|
};
|
|
568
|
+
if (options.logRawSqlQuery) {
|
|
569
|
+
const q = sql2.toSQL();
|
|
570
|
+
console.log(
|
|
571
|
+
`[forge-sql-orm][local-cache][SAVE] Stored result in cache. sql="${q.sql}", params=${JSON.stringify(q.params)}`
|
|
572
|
+
);
|
|
573
|
+
}
|
|
568
574
|
}
|
|
569
575
|
}
|
|
570
|
-
async function getQueryLocalCacheQuery(query) {
|
|
576
|
+
async function getQueryLocalCacheQuery(query, options) {
|
|
571
577
|
const context = localCacheApplicationContext.getStore();
|
|
572
578
|
if (context) {
|
|
573
579
|
if (!context.cache) {
|
|
@@ -576,6 +582,12 @@ async function getQueryLocalCacheQuery(query) {
|
|
|
576
582
|
const sql2 = query;
|
|
577
583
|
const key = hashKey(sql2.toSQL());
|
|
578
584
|
if (context.cache[key] && context.cache[key].sql === sql2.toSQL().sql.toLowerCase()) {
|
|
585
|
+
if (options.logRawSqlQuery) {
|
|
586
|
+
const q = sql2.toSQL();
|
|
587
|
+
console.log(
|
|
588
|
+
`[forge-sql-orm][local-cache][HIT] Returned cached result. sql="${q.sql}", params=${JSON.stringify(q.params)}`
|
|
589
|
+
);
|
|
590
|
+
}
|
|
579
591
|
return context.cache[key].data;
|
|
580
592
|
}
|
|
581
593
|
}
|
|
@@ -1065,23 +1077,10 @@ function createForgeDriverProxy(options, logRawSqlQuery) {
|
|
|
1065
1077
|
return forgeDriver(modifiedQuery, params, method);
|
|
1066
1078
|
};
|
|
1067
1079
|
}
|
|
1068
|
-
const NON_CACHE_CLEARING_ERROR_CODES = [
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
];
|
|
1072
|
-
const CACHE_CLEARING_ERROR_CODES = [
|
|
1073
|
-
"DEADLOCK",
|
|
1074
|
-
"LOCK_WAIT_TIMEOUT",
|
|
1075
|
-
"CONNECTION_ERROR"
|
|
1076
|
-
];
|
|
1077
|
-
const NON_CACHE_CLEARING_PATTERNS = [
|
|
1078
|
-
/validation/i,
|
|
1079
|
-
/constraint/i
|
|
1080
|
-
];
|
|
1081
|
-
const CACHE_CLEARING_PATTERNS = [
|
|
1082
|
-
/timeout/i,
|
|
1083
|
-
/connection/i
|
|
1084
|
-
];
|
|
1080
|
+
const NON_CACHE_CLEARING_ERROR_CODES = ["VALIDATION_ERROR", "CONSTRAINT_ERROR"];
|
|
1081
|
+
const CACHE_CLEARING_ERROR_CODES = ["DEADLOCK", "LOCK_WAIT_TIMEOUT", "CONNECTION_ERROR"];
|
|
1082
|
+
const NON_CACHE_CLEARING_PATTERNS = [/validation/i, /constraint/i];
|
|
1083
|
+
const CACHE_CLEARING_PATTERNS = [/timeout/i, /connection/i];
|
|
1085
1084
|
function shouldClearCacheOnError(error) {
|
|
1086
1085
|
if (error?.code && NON_CACHE_CLEARING_ERROR_CODES.includes(error.code)) {
|
|
1087
1086
|
return false;
|
|
@@ -1173,7 +1172,7 @@ function deleteAndEvictCacheBuilder(db, table2, options, isCached) {
|
|
|
1173
1172
|
}
|
|
1174
1173
|
async function handleCachedQuery(target, options, cacheTtl, selections, aliasMap, onfulfilled, onrejected) {
|
|
1175
1174
|
try {
|
|
1176
|
-
const localCached = await getQueryLocalCacheQuery(target);
|
|
1175
|
+
const localCached = await getQueryLocalCacheQuery(target, options);
|
|
1177
1176
|
if (localCached) {
|
|
1178
1177
|
return onfulfilled?.(localCached);
|
|
1179
1178
|
}
|
|
@@ -1183,7 +1182,7 @@ async function handleCachedQuery(target, options, cacheTtl, selections, aliasMap
|
|
|
1183
1182
|
}
|
|
1184
1183
|
const rows = await target.execute();
|
|
1185
1184
|
const transformed = applyFromDriverTransform(rows, selections, aliasMap);
|
|
1186
|
-
await saveQueryLocalCacheQuery(target, transformed);
|
|
1185
|
+
await saveQueryLocalCacheQuery(target, transformed, options);
|
|
1187
1186
|
await setCacheResult(target, options, transformed, cacheTtl).catch((cacheError) => {
|
|
1188
1187
|
console.warn("Cache set error:", cacheError);
|
|
1189
1188
|
});
|
|
@@ -1192,15 +1191,15 @@ async function handleCachedQuery(target, options, cacheTtl, selections, aliasMap
|
|
|
1192
1191
|
return onrejected?.(error);
|
|
1193
1192
|
}
|
|
1194
1193
|
}
|
|
1195
|
-
async function handleNonCachedQuery(target, selections, aliasMap, onfulfilled, onrejected) {
|
|
1194
|
+
async function handleNonCachedQuery(target, options, selections, aliasMap, onfulfilled, onrejected) {
|
|
1196
1195
|
try {
|
|
1197
|
-
const localCached = await getQueryLocalCacheQuery(target);
|
|
1196
|
+
const localCached = await getQueryLocalCacheQuery(target, options);
|
|
1198
1197
|
if (localCached) {
|
|
1199
1198
|
return onfulfilled?.(localCached);
|
|
1200
1199
|
}
|
|
1201
1200
|
const rows = await target.execute();
|
|
1202
1201
|
const transformed = applyFromDriverTransform(rows, selections, aliasMap);
|
|
1203
|
-
await saveQueryLocalCacheQuery(target, transformed);
|
|
1202
|
+
await saveQueryLocalCacheQuery(target, transformed, options);
|
|
1204
1203
|
return onfulfilled?.(transformed);
|
|
1205
1204
|
} catch (error) {
|
|
1206
1205
|
return onrejected?.(error);
|
|
@@ -1232,7 +1231,14 @@ function createAliasedSelectBuilder(db, fields, selectFn, useCache, options, cac
|
|
|
1232
1231
|
onrejected
|
|
1233
1232
|
);
|
|
1234
1233
|
} else {
|
|
1235
|
-
return handleNonCachedQuery(
|
|
1234
|
+
return handleNonCachedQuery(
|
|
1235
|
+
target,
|
|
1236
|
+
options,
|
|
1237
|
+
selections,
|
|
1238
|
+
aliasMap,
|
|
1239
|
+
onfulfilled,
|
|
1240
|
+
onrejected
|
|
1241
|
+
);
|
|
1236
1242
|
}
|
|
1237
1243
|
};
|
|
1238
1244
|
}
|
|
@@ -1266,14 +1272,16 @@ function createRawQueryExecutor(db, options, useGlobalCache = false) {
|
|
|
1266
1272
|
let sql$12;
|
|
1267
1273
|
if (sql.isSQLWrapper(query)) {
|
|
1268
1274
|
const sqlWrapper = query;
|
|
1269
|
-
sql$12 = sqlWrapper.getSQL().toQuery(
|
|
1275
|
+
sql$12 = sqlWrapper.getSQL().toQuery(
|
|
1276
|
+
db.dialect
|
|
1277
|
+
);
|
|
1270
1278
|
} else {
|
|
1271
1279
|
sql$12 = {
|
|
1272
1280
|
sql: query,
|
|
1273
1281
|
params: []
|
|
1274
1282
|
};
|
|
1275
1283
|
}
|
|
1276
|
-
const localCacheResult = await getQueryLocalCacheQuery(sql$12);
|
|
1284
|
+
const localCacheResult = await getQueryLocalCacheQuery(sql$12, options);
|
|
1277
1285
|
if (localCacheResult) {
|
|
1278
1286
|
return localCacheResult;
|
|
1279
1287
|
}
|
|
@@ -1284,7 +1292,7 @@ function createRawQueryExecutor(db, options, useGlobalCache = false) {
|
|
|
1284
1292
|
}
|
|
1285
1293
|
}
|
|
1286
1294
|
const results = await db.execute(query);
|
|
1287
|
-
await saveQueryLocalCacheQuery(sql$12, results);
|
|
1295
|
+
await saveQueryLocalCacheQuery(sql$12, results, options);
|
|
1288
1296
|
if (useGlobalCache) {
|
|
1289
1297
|
await setCacheResult(
|
|
1290
1298
|
{ toSQL: () => sql$12 },
|
|
@@ -1835,34 +1843,40 @@ class ForgeSQLORMImpl {
|
|
|
1835
1843
|
*/
|
|
1836
1844
|
async executeWithCacheContextAndReturnValue(cacheContext) {
|
|
1837
1845
|
return await this.executeWithLocalCacheContextAndReturnValue(
|
|
1838
|
-
async () => await cacheApplicationContext.run(
|
|
1839
|
-
|
|
1840
|
-
|
|
1841
|
-
|
|
1842
|
-
|
|
1843
|
-
|
|
1844
|
-
|
|
1845
|
-
|
|
1846
|
+
async () => await cacheApplicationContext.run(
|
|
1847
|
+
cacheApplicationContext.getStore() ?? { tables: /* @__PURE__ */ new Set() },
|
|
1848
|
+
async () => {
|
|
1849
|
+
try {
|
|
1850
|
+
return await cacheContext();
|
|
1851
|
+
} finally {
|
|
1852
|
+
await clearTablesCache(
|
|
1853
|
+
Array.from(cacheApplicationContext.getStore()?.tables ?? []),
|
|
1854
|
+
this.options
|
|
1855
|
+
);
|
|
1856
|
+
}
|
|
1846
1857
|
}
|
|
1847
|
-
|
|
1858
|
+
)
|
|
1848
1859
|
);
|
|
1849
1860
|
}
|
|
1850
1861
|
/**
|
|
1851
1862
|
* Executes operations within a local cache context and returns a value.
|
|
1852
1863
|
* This provides in-memory caching for select queries within a single request scope.
|
|
1853
|
-
*
|
|
1864
|
+
*
|
|
1854
1865
|
* @param cacheContext - Function containing operations that will benefit from local caching
|
|
1855
1866
|
* @returns Promise that resolves to the return value of the cacheContext function
|
|
1856
1867
|
*/
|
|
1857
1868
|
async executeWithLocalCacheContextAndReturnValue(cacheContext) {
|
|
1858
|
-
return await localCacheApplicationContext.run(
|
|
1859
|
-
|
|
1860
|
-
|
|
1869
|
+
return await localCacheApplicationContext.run(
|
|
1870
|
+
localCacheApplicationContext.getStore() ?? { cache: {} },
|
|
1871
|
+
async () => {
|
|
1872
|
+
return await cacheContext();
|
|
1873
|
+
}
|
|
1874
|
+
);
|
|
1861
1875
|
}
|
|
1862
1876
|
/**
|
|
1863
1877
|
* Executes operations within a local cache context.
|
|
1864
1878
|
* This provides in-memory caching for select queries within a single request scope.
|
|
1865
|
-
*
|
|
1879
|
+
*
|
|
1866
1880
|
* @param cacheContext - Function containing operations that will benefit from local caching
|
|
1867
1881
|
* @returns Promise that resolves when all operations are complete
|
|
1868
1882
|
*/
|
|
@@ -2159,7 +2173,7 @@ class ForgeSQLORMImpl {
|
|
|
2159
2173
|
* ```typescript
|
|
2160
2174
|
* // Using SQLWrapper
|
|
2161
2175
|
* const result = await forgeSQL.execute(sql`SELECT * FROM users WHERE id = ${userId}`);
|
|
2162
|
-
*
|
|
2176
|
+
*
|
|
2163
2177
|
* // Using string
|
|
2164
2178
|
* const result = await forgeSQL.execute("SELECT * FROM users WHERE status = 'active'");
|
|
2165
2179
|
* ```
|
|
@@ -2180,7 +2194,7 @@ class ForgeSQLORMImpl {
|
|
|
2180
2194
|
* ```typescript
|
|
2181
2195
|
* // Using SQLWrapper with custom TTL
|
|
2182
2196
|
* const result = await forgeSQL.executeCacheable(sql`SELECT * FROM users WHERE id = ${userId}`, 300);
|
|
2183
|
-
*
|
|
2197
|
+
*
|
|
2184
2198
|
* // Using string with default TTL
|
|
2185
2199
|
* const result = await forgeSQL.executeCacheable("SELECT * FROM users WHERE status = 'active'");
|
|
2186
2200
|
* ```
|
|
@@ -2218,7 +2232,7 @@ class ForgeSQLORMImpl {
|
|
|
2218
2232
|
* .from(users)
|
|
2219
2233
|
* .groupBy(users.id)
|
|
2220
2234
|
* );
|
|
2221
|
-
*
|
|
2235
|
+
*
|
|
2222
2236
|
* const result = await forgeSQL.with(withQuery)
|
|
2223
2237
|
* .select({ userId: withQuery.userId, count: withQuery.count })
|
|
2224
2238
|
* .from(withQuery);
|
|
@@ -2310,7 +2324,7 @@ class ForgeSQLORM {
|
|
|
2310
2324
|
/**
|
|
2311
2325
|
* Executes operations within a local cache context.
|
|
2312
2326
|
* This provides in-memory caching for select queries within a single request scope.
|
|
2313
|
-
*
|
|
2327
|
+
*
|
|
2314
2328
|
* @param cacheContext - Function containing operations that will benefit from local caching
|
|
2315
2329
|
* @returns Promise that resolves when all operations are complete
|
|
2316
2330
|
*/
|
|
@@ -2320,7 +2334,7 @@ class ForgeSQLORM {
|
|
|
2320
2334
|
/**
|
|
2321
2335
|
* Executes operations within a local cache context and returns a value.
|
|
2322
2336
|
* This provides in-memory caching for select queries within a single request scope.
|
|
2323
|
-
*
|
|
2337
|
+
*
|
|
2324
2338
|
* @param cacheContext - Function containing operations that will benefit from local caching
|
|
2325
2339
|
* @returns Promise that resolves to the return value of the cacheContext function
|
|
2326
2340
|
*/
|
|
@@ -2484,7 +2498,7 @@ class ForgeSQLORM {
|
|
|
2484
2498
|
* ```typescript
|
|
2485
2499
|
* // Using SQLWrapper
|
|
2486
2500
|
* const result = await forgeSQL.execute(sql`SELECT * FROM users WHERE id = ${userId}`);
|
|
2487
|
-
*
|
|
2501
|
+
*
|
|
2488
2502
|
* // Using string
|
|
2489
2503
|
* const result = await forgeSQL.execute("SELECT * FROM users WHERE status = 'active'");
|
|
2490
2504
|
* ```
|
|
@@ -2505,7 +2519,7 @@ class ForgeSQLORM {
|
|
|
2505
2519
|
* ```typescript
|
|
2506
2520
|
* // Using SQLWrapper with custom TTL
|
|
2507
2521
|
* const result = await forgeSQL.executeCacheable(sql`SELECT * FROM users WHERE id = ${userId}`, 300);
|
|
2508
|
-
*
|
|
2522
|
+
*
|
|
2509
2523
|
* // Using string with default TTL
|
|
2510
2524
|
* const result = await forgeSQL.executeCacheable("SELECT * FROM users WHERE status = 'active'");
|
|
2511
2525
|
* ```
|
|
@@ -2543,7 +2557,7 @@ class ForgeSQLORM {
|
|
|
2543
2557
|
* .from(users)
|
|
2544
2558
|
* .groupBy(users.id)
|
|
2545
2559
|
* );
|
|
2546
|
-
*
|
|
2560
|
+
*
|
|
2547
2561
|
* const result = await forgeSQL.with(withQuery)
|
|
2548
2562
|
* .select({ userId: withQuery.userId, count: withQuery.count })
|
|
2549
2563
|
* .from(withQuery);
|