• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

vzakharchenko / forge-sql-orm / 17648728887

11 Sep 2025 03:02PM UTC coverage: 68.742% (-18.1%) from 86.81%
17648728887

Pull #627

github

web-flow
Merge ce989a6a5 into 67ed05a12
Pull Request #627: added kvs cache

287 of 357 branches covered (80.39%)

Branch coverage included in aggregate %.

294 of 825 new or added lines in 9 files covered. (35.64%)

1 existing line in 1 file now uncovered.

1336 of 2004 relevant lines covered (66.67%)

8.61 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

29.63
/src/utils/cacheContextUtils.ts
1
import { AsyncLocalStorage } from "node:async_hooks";
1✔
2
import { AnyMySqlTable } from "drizzle-orm/mysql-core";
3
import { getTableName } from "drizzle-orm/table";
1✔
4
import { ForgeSqlOrmOptions } from "../core/ForgeSQLQueryBuilder";
5

6
/**
7
 * Interface representing the cache application context.
8
 * Stores information about tables that are being processed within a cache context.
9
 */
10
export interface CacheApplicationContext {
11
  /** Set of table names (in lowercase) that are being processed within the cache context */
12
  tables: Set<string>;
13
}
14

15
/**
16
 * AsyncLocalStorage instance for managing cache context across async operations.
17
 * This allows tracking which tables are being processed within a cache context
18
 * without explicitly passing context through function parameters.
19
 */
20
export const cacheApplicationContext = new AsyncLocalStorage<CacheApplicationContext>();
1✔
21

22
/**
23
 * Saves a table name to the current cache context if one exists.
24
 * This function is used to track which tables are being processed within
25
 * a cache context for proper cache invalidation.
26
 * 
27
 * @param table - The Drizzle table schema to track
28
 * @returns Promise that resolves when the table is saved to context
29
 * 
30
 * @example
31
 * ```typescript
32
 * await saveTableIfInsideCacheContext(usersTable);
33
 * ```
34
 */
35
export async function saveTableIfInsideCacheContext<T extends AnyMySqlTable>(table: T): Promise<void> {
41✔
36
  const context = cacheApplicationContext.getStore();
41✔
37
  if (context) {
41!
NEW
38
    const tableName = getTableName(table).toLowerCase();
×
NEW
39
    context.tables.add(tableName);
×
NEW
40
  }
×
41
}
41✔
42

43
/**
44
 * Checks if the given SQL query contains any tables that are currently being processed
45
 * within a cache context. This is used to determine if cache should be bypassed
46
 * for queries that affect tables being modified within the context.
47
 * 
48
 * @param sql - The SQL query string to check
49
 * @param options - ForgeSQL ORM options containing cache configuration
50
 * @returns Promise that resolves to true if the SQL contains tables in cache context
51
 * 
52
 * @example
53
 * ```typescript
54
 * const shouldBypassCache = await isTableContainsTableInCacheContext(
55
 *   "SELECT * FROM users WHERE id = 1",
56
 *   forgeSqlOptions
57
 * );
58
 * ```
59
 */
NEW
60
export async function isTableContainsTableInCacheContext(
×
NEW
61
  sql: string,
×
NEW
62
  options: ForgeSqlOrmOptions,
×
NEW
63
): Promise<boolean> {
×
NEW
64
  const context = cacheApplicationContext.getStore();
×
NEW
65
  if (!context) {
×
NEW
66
    return false;
×
NEW
67
  }
×
68

NEW
69
  const tables = Array.from(context.tables);
×
NEW
70
  const lowerSql = sql.toLowerCase();
×
71
  
NEW
72
  return tables.some((table) => {
×
NEW
73
    const tablePattern = options.cacheWrapTable ? `\`${table}\`` : table;
×
NEW
74
    return lowerSql.includes(tablePattern);
×
NEW
75
  });
×
NEW
76
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc