forge-sql-orm 2.0.30 → 2.1.1

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.
Files changed (46) hide show
  1. package/README.md +1410 -81
  2. package/dist/ForgeSQLORM.js +1456 -60
  3. package/dist/ForgeSQLORM.js.map +1 -1
  4. package/dist/ForgeSQLORM.mjs +1440 -61
  5. package/dist/ForgeSQLORM.mjs.map +1 -1
  6. package/dist/core/ForgeSQLAnalyseOperations.d.ts +1 -1
  7. package/dist/core/ForgeSQLAnalyseOperations.d.ts.map +1 -1
  8. package/dist/core/ForgeSQLCacheOperations.d.ts +119 -0
  9. package/dist/core/ForgeSQLCacheOperations.d.ts.map +1 -0
  10. package/dist/core/ForgeSQLCrudOperations.d.ts +38 -22
  11. package/dist/core/ForgeSQLCrudOperations.d.ts.map +1 -1
  12. package/dist/core/ForgeSQLORM.d.ts +248 -13
  13. package/dist/core/ForgeSQLORM.d.ts.map +1 -1
  14. package/dist/core/ForgeSQLQueryBuilder.d.ts +394 -19
  15. package/dist/core/ForgeSQLQueryBuilder.d.ts.map +1 -1
  16. package/dist/index.d.ts +1 -1
  17. package/dist/index.d.ts.map +1 -1
  18. package/dist/lib/drizzle/extensions/additionalActions.d.ts +90 -0
  19. package/dist/lib/drizzle/extensions/additionalActions.d.ts.map +1 -0
  20. package/dist/utils/cacheContextUtils.d.ts +123 -0
  21. package/dist/utils/cacheContextUtils.d.ts.map +1 -0
  22. package/dist/utils/cacheUtils.d.ts +56 -0
  23. package/dist/utils/cacheUtils.d.ts.map +1 -0
  24. package/dist/utils/sqlUtils.d.ts +8 -0
  25. package/dist/utils/sqlUtils.d.ts.map +1 -1
  26. package/dist/webtriggers/clearCacheSchedulerTrigger.d.ts +46 -0
  27. package/dist/webtriggers/clearCacheSchedulerTrigger.d.ts.map +1 -0
  28. package/dist/webtriggers/index.d.ts +1 -0
  29. package/dist/webtriggers/index.d.ts.map +1 -1
  30. package/package.json +15 -12
  31. package/src/core/ForgeSQLAnalyseOperations.ts +1 -1
  32. package/src/core/ForgeSQLCacheOperations.ts +195 -0
  33. package/src/core/ForgeSQLCrudOperations.ts +49 -40
  34. package/src/core/ForgeSQLORM.ts +743 -34
  35. package/src/core/ForgeSQLQueryBuilder.ts +456 -20
  36. package/src/index.ts +1 -1
  37. package/src/lib/drizzle/extensions/additionalActions.ts +852 -0
  38. package/src/lib/drizzle/extensions/types.d.ts +99 -10
  39. package/src/utils/cacheContextUtils.ts +212 -0
  40. package/src/utils/cacheUtils.ts +403 -0
  41. package/src/utils/sqlUtils.ts +42 -0
  42. package/src/webtriggers/clearCacheSchedulerTrigger.ts +79 -0
  43. package/src/webtriggers/index.ts +1 -0
  44. package/dist/lib/drizzle/extensions/selectAliased.d.ts +0 -9
  45. package/dist/lib/drizzle/extensions/selectAliased.d.ts.map +0 -1
  46. package/src/lib/drizzle/extensions/selectAliased.ts +0 -72
@@ -0,0 +1 @@
1
+ {"version":3,"file":"additionalActions.d.ts","sourceRoot":"","sources":["../../../../src/lib/drizzle/extensions/additionalActions.ts"],"names":[],"mappings":"AAAA,OAAO,EAEH,mBAAmB,EACnB,2BAA2B,EAC3B,yBAAyB,EAC5B,MAAM,yBAAyB,CAAC;AAEjC,OAAO,EAAC,cAAc,EAAC,MAAM,oDAAoD,CAAC;AAClF,OAAO,EAA2B,kBAAkB,EAA2B,MAAM,UAAU,CAAC;AAChG,OAAO,EAAC,eAAe,EAAE,kBAAkB,EAAC,MAAM,wBAAwB,CAAC;AAC3E,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,8BAA8B,CAAC;AAC7D,OAAO,EAAC,eAAe,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,MAAM,uCAAuC,CAAC;AAS/G,OAAO,EAAiC,UAAU,EAAC,MAAM,qBAAqB,CAAC;AAC/E,OAAO,KAAK,EAAC,oBAAoB,EAAC,MAAM,gCAAgC,CAAC;AAGzE,OAAO,KAAK,EAAC,kBAAkB,EAAE,uBAAuB,E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AAK,EAAE,MAAM,KACV,eAAe,CAAC,MAAM,EAAE,yBAAyB,EAAE,2BAA2B,CAAC,CAAC;AAoarF;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,wBAAwB,CACtC,EAAE,EAAE,mBAAmB,CAAC,GAAG,CAAC,EAC5B,OAAO,CAAC,EAAE,kBAAkB,GAC3B,mBAAmB,CAAC,GAAG,CAAC,GAAG;IAC5B,aAAa,EAAE,iBAAiB,CAAC;IACjC,qBAAqB,EAAE,yBAAyB,CAAC;IACjD,sBAAsB,EAAE,0BAA0B,CAAC;IACnD,8BAA8B,EAAE,kCAAkC,CAAC;IACnE,sBAAsB,EAAE,uBAAuB,CAAC;IAChD,mBAAmB,EAAE,uBAAuB,CAAC;IAC7C,mBAAmB,EAAE,uBAAuB,CAAC;IAC7C,sBAAsB,EAAE,uBAAuB,CAAC;IAChD,mBAAmB,EAAE,uBAAuB,CAAC;IAC7C,sBAAsB,EAAE,uBAAuB,CAAC;CACjD,CA0MA"}
@@ -0,0 +1,123 @@
1
+ import { AsyncLocalStorage } from "node:async_hooks";
2
+ import { AnyMySqlSelectQueryBuilder, AnyMySqlTable } from "drizzle-orm/mysql-core";
3
+ import { ForgeSqlOrmOptions } from "../core/ForgeSQLQueryBuilder";
4
+ import { MySqlSelectDynamic } from "drizzle-orm/mysql-core/query-builders/select.types";
5
+ /**
6
+ * Interface representing the cache application context.
7
+ * Stores information about tables that are being processed within a cache context.
8
+ */
9
+ export interface CacheApplicationContext {
10
+ /** Set of table names (in lowercase) that are being processed within the cache context */
11
+ tables: Set<string>;
12
+ }
13
+ /**
14
+ * Interface representing the local cache application context.
15
+ * Stores cached query results in memory for the duration of a local cache context.
16
+ *
17
+ * @interface LocalCacheApplicationContext
18
+ */
19
+ export interface LocalCacheApplicationContext {
20
+ /**
21
+ * Cache object mapping query hash keys to cached results
22
+ * @property {Record<string, {sql: string, data: unknown[]}>} cache - Map of query keys to cached data
23
+ */
24
+ cache: Record<string, {
25
+ sql: string;
26
+ data: unknown[];
27
+ }>;
28
+ }
29
+ /**
30
+ * AsyncLocalStorage instance for managing cache context across async operations.
31
+ * This allows tracking which tables are being processed within a cache context
32
+ * without explicitly passing context through function parameters.
33
+ */
34
+ export declare const cacheApplicationContext: AsyncLocalStorage<CacheApplicationContext>;
35
+ /**
36
+ * AsyncLocalStorage instance for managing local cache context across async operations.
37
+ * This allows storing and retrieving cached query results within a local cache context
38
+ * without explicitly passing context through function parameters.
39
+ */
40
+ export declare const localCacheApplicationContext: AsyncLocalStorage<LocalCacheApplicationContext>;
41
+ /**
42
+ * Saves a table name to the current cache context if one exists.
43
+ * This function is used to track which tables are being processed within
44
+ * a cache context for proper cache invalidation.
45
+ *
46
+ * @param table - The Drizzle table schema to track
47
+ * @returns Promise that resolves when the table is saved to context
48
+ *
49
+ * @example
50
+ * ```typescript
51
+ * await saveTableIfInsideCacheContext(usersTable);
52
+ * ```
53
+ */
54
+ export declare function saveTableIfInsideCacheContext<T extends AnyMySqlTable>(table: T): Promise<void>;
55
+ /**
56
+ * Saves a query result to the local cache context.
57
+ * This function stores query results in memory for the duration of the local cache context.
58
+ *
59
+ * @param query - The Drizzle query to cache
60
+ * @param rows - The query result data to cache
61
+ * @returns Promise that resolves when the data is saved to local cache
62
+ *
63
+ * @example
64
+ * ```typescript
65
+ * const query = db.select({ id: users.id, name: users.name }).from(users);
66
+ * const results = await query.execute();
67
+ * await saveQueryLocalCacheQuery(query, results);
68
+ * ```
69
+ */
70
+ export declare function saveQueryLocalCacheQuery<T extends MySqlSelectDynamic<AnyMySqlSelectQueryBuilder>>(query: T, rows: unknown[]): Promise<void>;
71
+ /**
72
+ * Retrieves a query result from the local cache context.
73
+ * This function checks if a query result is already cached in memory.
74
+ *
75
+ * @param query - The Drizzle query to check for cached results
76
+ * @returns Promise that resolves to cached data if found, undefined otherwise
77
+ *
78
+ * @example
79
+ * ```typescript
80
+ * const query = db.select({ id: users.id, name: users.name }).from(users);
81
+ * const cachedResult = await getQueryLocalCacheQuery(query);
82
+ * if (cachedResult) {
83
+ * return cachedResult; // Use cached data
84
+ * }
85
+ * // Execute query and cache result
86
+ * ```
87
+ */
88
+ export declare function getQueryLocalCacheQuery<T extends MySqlSelectDynamic<AnyMySqlSelectQueryBuilder>>(query: T): Promise<unknown[] | undefined>;
89
+ /**
90
+ * Evicts cached queries from the local cache context that involve the specified table.
91
+ * This function removes cached query results that contain the given table name.
92
+ *
93
+ * @param table - The Drizzle table schema to evict cache for
94
+ * @param options - ForgeSQL ORM options containing cache configuration
95
+ * @returns Promise that resolves when cache eviction is complete
96
+ *
97
+ * @example
98
+ * ```typescript
99
+ * // After inserting/updating/deleting from users table
100
+ * await evictLocalCacheQuery(usersTable, forgeSqlOptions);
101
+ * // All cached queries involving users table are now removed
102
+ * ```
103
+ */
104
+ export declare function evictLocalCacheQuery<T extends AnyMySqlTable>(table: T, options: ForgeSqlOrmOptions): Promise<void>;
105
+ /**
106
+ * Checks if the given SQL query contains any tables that are currently being processed
107
+ * within a cache context. This is used to determine if cache should be bypassed
108
+ * for queries that affect tables being modified within the context.
109
+ *
110
+ * @param sql - The SQL query string to check
111
+ * @param options - ForgeSQL ORM options containing cache configuration
112
+ * @returns Promise that resolves to true if the SQL contains tables in cache context
113
+ *
114
+ * @example
115
+ * ```typescript
116
+ * const shouldBypassCache = await isTableContainsTableInCacheContext(
117
+ * "SELECT * FROM users WHERE id = 1",
118
+ * forgeSqlOptions
119
+ * );
120
+ * ```
121
+ */
122
+ export declare function isTableContainsTableInCacheContext(sql: string, options: ForgeSqlOrmOptions): Promise<boolean>;
123
+ //# sourceMappingURL=cacheContextUtils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cacheContextUtils.d.ts","sourceRoot":"","sources":["../../src/utils/cacheContextUtils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AACrD,OAAO,EAAE,0BAA0B,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;A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E,MAAM,EACX,OAAO,EAAE,kBAAkB,GAC1B,OAAO,CAAC,OAAO,CAAC,CAalB"}
@@ -0,0 +1,56 @@
1
+ import { Query } from "drizzle-orm";
2
+ import { AnyMySqlTable } from "drizzle-orm/mysql-core";
3
+ import { ForgeSqlOrmOptions } from "../core/ForgeSQLQueryBuilder";
4
+ /**
5
+ * Generates a hash key for a query based on its SQL and parameters.
6
+ *
7
+ * @param query - The Drizzle query object
8
+ * @returns 32-character hexadecimal hash
9
+ */
10
+ export declare function hashKey(query: Query): string;
11
+ /**
12
+ * Clears cache for a specific table.
13
+ * Uses cache context if available, otherwise clears immediately.
14
+ *
15
+ * @param schema - The table schema to clear cache for
16
+ * @param options - ForgeSQL ORM options
17
+ */
18
+ export declare function clearCache<T extends AnyMySqlTable>(schema: T, options: ForgeSqlOrmOptions): Promise<void>;
19
+ /**
20
+ * Clears cache for multiple tables with retry logic and performance logging.
21
+ *
22
+ * @param tables - Array of table names to clear cache for
23
+ * @param options - ForgeSQL ORM options
24
+ * @returns Promise that resolves when cache clearing is complete
25
+ */
26
+ export declare function clearTablesCache(tables: string[], options: ForgeSqlOrmOptions): Promise<void>;
27
+ /**
28
+ * Clears expired cache entries with retry logic and performance logging.
29
+ *
30
+ * @param options - ForgeSQL ORM options
31
+ * @returns Promise that resolves when expired cache clearing is complete
32
+ */
33
+ export declare function clearExpiredCache(options: ForgeSqlOrmOptions): Promise<void>;
34
+ /**
35
+ * Retrieves data from cache if it exists and is not expired.
36
+ *
37
+ * @param query - Query object with toSQL method
38
+ * @param options - ForgeSQL ORM options
39
+ * @returns Cached data if found and valid, undefined otherwise
40
+ */
41
+ export declare function getFromCache<T>(query: {
42
+ toSQL: () => Query;
43
+ }, options: ForgeSqlOrmOptions): Promise<T | undefined>;
44
+ /**
45
+ * Stores query results in cache with specified TTL.
46
+ *
47
+ * @param query - Query object with toSQL method
48
+ * @param options - ForgeSQL ORM options
49
+ * @param results - Data to cache
50
+ * @param cacheTtl - Time to live in seconds
51
+ * @returns Promise that resolves when data is stored in cache
52
+ */
53
+ export declare function setCacheResult(query: {
54
+ toSQL: () => Query;
55
+ }, options: ForgeSqlOrmOptions, results: unknown, cacheTtl: number): Promise<void>;
56
+ //# sourceMappingURL=cacheUtils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cacheUtils.d.ts","sourceRoot":"","sources":["../../src/utils/cacheUtils.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AACpC,OAAO,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AAGvD,OAAO,EAAE,kBAAkB,EAAE,MAAM,8BAA8B,CAAC;AA2ClE;;;;;GAKG;AACH,wBAAgB,OAAO,CAAC,KAAK,EAAE,KAAK,GAAG,MAAM,CAK5C;AA+JD;;;;;;GAMG;AACH,wBAAsB,UAAU,CAAC,CAAC,SAAS,aAAa,EACtD,MAAM,EAAE,CAAC,EACT,OAAO,EAAE,kBAAkB,GAC1B,OAAO,CAAC,IAAI,CAAC,CAOf;AAED;;;;;;GAMG;AACH,wBAAsB,gBAAgB,CACpC,MAAM,EAAE,MAAM,EAAE,EAChB,OAAO,EAAE,kBAAkB,GAC1B,OAAO,CAAC,IAAI,CAAC,CAmBf;AACD;;;;;GAKG;AACH,wBAAsB,iBAAiB,CAAC,OAAO,EAAE,kBAAkB,GAAG,OAAO,CAAC,IAAI,CAAC,CAiBlF;AAED;;;;;;GAMG;AACH,wBAAsB,YAAY,CAAC,CAAC,EAClC,KAAK,EAAE;IAAE,KAAK,EAAE,MAAM,KAAK,CAAA;CAAE,EAC7B,OAAO,EAAE,kBAAkB,GAC1B,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC,CAwCxB;AAED;;;;;;;;GAQG;AACH,wBAAsB,cAAc,CAClC,KAAK,EAAE;IAAE,KAAK,EAAE,MAAM,KAAK,CAAA;CAAE,EAC7B,OAAO,EAAE,kBAAkB,EAC3B,OAAO,EAAE,OAAO,EAChB,QAAQ,EAAE,MAAM,GACf,OAAO,CAAC,IAAI,CAAC,CA2Cf"}
@@ -34,6 +34,14 @@ export interface MetadataInfo {
34
34
  * @returns Date object
35
35
  */
36
36
  export declare const parseDateTime: (value: string | Date, format: string) => Date;
37
+ /**
38
+ * Helper function to validate and format a date-like value using Luxon DateTime.
39
+ * @param value - Date object, ISO/RFC2822/SQL/HTTP string, or timestamp (number|string).
40
+ * @param format - DateTime format string (Luxon format tokens).
41
+ * @returns Formatted date string.
42
+ * @throws Error if value cannot be parsed as a valid date.
43
+ */
44
+ export declare function formatDateTime(value: Date | string | number, format: string): string;
37
45
  /**
38
46
  * Gets primary keys from the schema.
39
47
  * @template T - The type of the table schema
@@ -1 +1 @@
1
- {"version":3,"file":"sqlUtils.d.ts","sourceRoot":"","sources":["../../src/utils/sqlUtils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAA0C,MAAM,aAAa,CAAC;AAChF,OAAO,EAAE,aAAa,EAAqB,MAAM,8BAA8B,CAAC;AAEhF,OAAO,EAAE,iBAAiB,EAAE,MAAM,qCAAqC,CAAC;AACxE,OAAO,EAAE,eAAe,EAAE,MAAM,gCAAgC,CAAC;AACjE,OAAO,EAAE,YAAY,EAAE,MAAM,+BAA+B,CAAC;AAC7D,OAAO,EAAE,iBAAiB,EAAE,MAAM,qCAAqC,CAAC;AACxE,OAAO,EAAE,uBAAuB,EAAE,MAAM,0CAA0C,CAAC;AACnF,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,oDAAoD,CAAC;AAIzF;;GAEG;AACH,MAAM,WAAW,YAAY;IAC3B,4BAA4B;IAC5B,SAAS,EAAE,MAAM,CAAC;IAClB,wEAAwE;IACxE,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC;IACnC,8BAA8B;IAC9B,OAAO,EAAE,eAAe,EAAE,CAAC;IAC3B,yCAAyC;IACzC,MAAM,EAAE,YAAY,EAAE,CAAC;IACvB,oCAAoC;IACpC,WAA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,MAAM,GAAG,MAAM,CAEpD"}
@@ -0,0 +1,46 @@
1
+ import { ForgeSqlOrmOptions } from "../core/ForgeSQLQueryBuilder";
2
+ /**
3
+ * Scheduler trigger for clearing expired cache entries.
4
+ *
5
+ * This trigger should be configured as a Forge scheduler to automatically
6
+ * clean up expired cache entries based on their TTL (Time To Live).
7
+ *
8
+ * @param options - Optional ForgeSQL ORM configuration. If not provided,
9
+ * uses default cache settings with cacheEntityName: "cache"
10
+ * @returns Promise that resolves to HTTP response object
11
+ *
12
+ * @example
13
+ * ```typescript
14
+ * // In your index.ts
15
+ * import { clearCacheSchedulerTrigger } from "forge-sql-orm";
16
+ *
17
+ * export const clearCache = () => {
18
+ * return clearCacheSchedulerTrigger({
19
+ * cacheEntityName: "cache",
20
+ * logRawSqlQuery: true
21
+ * });
22
+ * };
23
+ * ```
24
+ *
25
+ * @example
26
+ * ```yaml
27
+ * # In manifest.yml
28
+ * scheduledTrigger:
29
+ * - key: clear-cache-trigger
30
+ * function: clearCache
31
+ * interval: fiveMinute
32
+ *
33
+ * function:
34
+ * - key: clearCache
35
+ * handler: index.clearCache
36
+ * ```
37
+ */
38
+ export declare const clearCacheSchedulerTrigger: (options?: ForgeSqlOrmOptions) => Promise<{
39
+ headers: {
40
+ "Content-Type": string[];
41
+ };
42
+ statusCode: number;
43
+ statusText: string;
44
+ body: string;
45
+ }>;
46
+ //# sourceMappingURL=clearCacheSchedulerTrigger.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"clearCacheSchedulerTrigger.d.ts","sourceRoot":"","sources":["../../src/webtriggers/clearCacheSchedulerTrigger.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,kBAAkB,EAAE,MAAM,8BAA8B,CAAC;AAElE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,eAAO,MAAM,0BAA0B,GAAU,UAAU,kBAAkB;;;;;;;EAuC5E,CAAC"}
@@ -2,6 +2,7 @@ export * from "./dropMigrationWebTrigger";
2
2
  export * from "./applyMigrationsWebTrigger";
3
3
  export * from "./fetchSchemaWebTrigger";
4
4
  export * from "./dropTablesMigrationWebTrigger";
5
+ export * from "./clearCacheSchedulerTrigger";
5
6
  export interface TriggerResponse<BODY> {
6
7
  body?: BODY;
7
8
  headers?: Record<string, string[]>;
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/webtriggers/index.ts"],"names":[],"mappings":"AAAA,cAAc,2BAA2B,CAAC;AAC1C,cAAc,6BAA6B,CAAC;AAC5C,cAAc,yBAAyB,CAAC;AACxC,cAAc,iCAAiC,CAAC;AAEhD,MAAM,WAAW,eAAe,CAAC,IAAI;IACnC,IAAI,CAAC,EAAE,IAAI,CAAC;IACZ,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,CAAC,CAAC;IACnC,UAAU,EAAE,MAAM,CAAC;IACnB,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAED,eAAO,MAAM,eAAe,GAAI,IAAI,EAAE,YAAY,MAAM,EAAE,MAAM,IAAI,KAAG,eAAe,CAAC,IAAI,CAc1F,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/webtriggers/index.ts"],"names":[],"mappings":"AAAA,cAAc,2BAA2B,CAAC;AAC1C,cAAc,6BAA6B,CAAC;AAC5C,cAAc,yBAAyB,CAAC;AACxC,cAAc,iCAAiC,CAAC;AAChD,cAAc,8BAA8B,CAAC;AAE7C,MAAM,WAAW,eAAe,CAAC,IAAI;IACnC,IAAI,CAAC,EAAE,IAAI,CAAC;IACZ,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,CAAC,CAAC;IACnC,UAAU,EAAE,MAAM,CAAC;IACnB,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAED,eAAO,MAAM,eAAe,GAAI,IAAI,EAAE,YAAY,MAAM,EAAE,MAAM,IAAI,KAAG,eAAe,CAAC,IAAI,CAc1F,CAAC"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "forge-sql-orm",
3
- "version": "2.0.30",
4
- "description": "Drizzle ORM integration for Forge-SQL in Atlassian Forge applications.",
3
+ "version": "2.1.1",
4
+ "description": "Drizzle ORM integration for Atlassian @forge/sql. Provides a custom driver, schema migration, two levels of caching (local and global via @forge/kvs), optimistic locking, and query analysis.",
5
5
  "main": "dist/ForgeSQLORM.js",
6
6
  "module": "dist/ForgeSQLORM.mjs",
7
7
  "types": "dist/index.d.ts",
@@ -32,23 +32,23 @@
32
32
  "database"
33
33
  ],
34
34
  "devDependencies": {
35
- "@eslint/js": "^9.34.0",
35
+ "@eslint/js": "^9.35.0",
36
36
  "@types/luxon": "^3.7.1",
37
- "@types/node": "^24.3.0",
38
- "@typescript-eslint/eslint-plugin": "^8.41.0",
39
- "@typescript-eslint/parser": "^8.41.0",
37
+ "@types/node": "^24.4.0",
38
+ "@typescript-eslint/eslint-plugin": "^8.43.0",
39
+ "@typescript-eslint/parser": "^8.43.0",
40
40
  "@vitest/coverage-v8": "^3.2.4",
41
41
  "@vitest/ui": "^3.2.4",
42
- "eslint": "^9.34.0",
42
+ "eslint": "^9.35.0",
43
43
  "eslint-config-prettier": "^10.1.8",
44
44
  "eslint-plugin-import": "^2.32.0",
45
45
  "eslint-plugin-vitest": "^0.5.4",
46
- "knip": "^5.63.0",
46
+ "knip": "^5.63.1",
47
47
  "prettier": "^3.6.2",
48
48
  "ts-node": "^10.9.2",
49
49
  "typescript": "^5.9.2",
50
- "uuid": "^11.1.0",
51
- "vite": "^7.1.4",
50
+ "uuid": "^13.0.0",
51
+ "vite": "^7.1.5",
52
52
  "vitest": "^3.2.4"
53
53
  },
54
54
  "license": "MIT",
@@ -59,7 +59,7 @@
59
59
  "test:watch": "vitest --watch",
60
60
  "lint": "eslint src --ext .ts,.tsx",
61
61
  "lint:fix": "eslint src --ext .ts,.tsx --fix",
62
- "format": "prettier --write src examples",
62
+ "format": "prettier --write src examples __tests__",
63
63
  "clean": "rm -rf dist",
64
64
  "build": "npm run clean && vite build && npm run build:types",
65
65
  "build:types": "tsc --emitDeclarationOnly",
@@ -78,7 +78,10 @@
78
78
  "@forge/sql": "^3.0.5",
79
79
  "drizzle-orm": "^0.44.5"
80
80
  },
81
+ "optionalDependencies": {
82
+ "@forge/kvs": "^1.0.5"
83
+ },
81
84
  "dependencies": {
82
- "luxon": "^3.7.1"
85
+ "luxon": "^3.7.2"
83
86
  }
84
87
  }
@@ -6,7 +6,7 @@ import {
6
6
  SlowQueryNormalized,
7
7
  } from "./SystemTables";
8
8
  import { SqlParameters } from "@forge/sql/out/sql-statement";
9
- import { AnyMySqlTable } from "drizzle-orm/mysql-core/index";
9
+ import { AnyMySqlTable } from "drizzle-orm/mysql-core";
10
10
  import { getTableName } from "drizzle-orm/table";
11
11
  import { DateTime } from "luxon";
12
12
 
@@ -0,0 +1,195 @@
1
+ import { MySqlSelectDynamic } from "drizzle-orm/mysql-core/query-builders/select.types";
2
+ import { AnyMySqlSelectQueryBuilder, AnyMySqlTable } from "drizzle-orm/mysql-core";
3
+ import { CacheForgeSQL, ForgeSqlOperation, ForgeSqlOrmOptions } from "./ForgeSQLQueryBuilder";
4
+ import { InferInsertModel, Query, SQL } from "drizzle-orm";
5
+ import { clearCache, getFromCache, setCacheResult, clearTablesCache } from "../utils/cacheUtils";
6
+ import { getTableName } from "drizzle-orm/table";
7
+
8
+ /**
9
+ * Implementation of cache operations for ForgeSQL ORM.
10
+ * Provides methods for cacheable database operations with automatic cache management.
11
+ *
12
+ * ⚠️ **IMPORTANT**: All modification methods in this class use optimistic locking/versioning
13
+ * through `modifyWithVersioning()` internally. This ensures data consistency and prevents
14
+ * concurrent modification conflicts.
15
+ */
16
+ export class ForgeSQLCacheOperations implements CacheForgeSQL {
17
+ private readonly options: ForgeSqlOrmOptions;
18
+ private readonly forgeOperations: ForgeSqlOperation;
19
+
20
+ /**
21
+ * Creates a new instance of ForgeSQLCacheOperations.
22
+ *
23
+ * @param options - Configuration options for the ORM
24
+ * @param forgeOperations - The ForgeSQL operations instance
25
+ */
26
+ constructor(options: ForgeSqlOrmOptions, forgeOperations: ForgeSqlOperation) {
27
+ this.options = options;
28
+ this.forgeOperations = forgeOperations;
29
+ }
30
+
31
+ /**
32
+ * Evicts cache for multiple tables using Drizzle table objects.
33
+ *
34
+ * @param tables - Array of Drizzle table objects to clear cache for
35
+ * @returns Promise that resolves when cache eviction is complete
36
+ * @throws Error if cacheEntityName is not configured
37
+ */
38
+ async evictCacheEntities(tables: AnyMySqlTable[]): Promise<void> {
39
+ if (!this.options.cacheEntityName) {
40
+ throw new Error("cacheEntityName is not configured");
41
+ }
42
+ await this.evictCache(tables.map((t) => getTableName(t)));
43
+ }
44
+
45
+ /**
46
+ * Evicts cache for multiple tables by their names.
47
+ *
48
+ * @param tables - Array of table names to clear cache for
49
+ * @returns Promise that resolves when cache eviction is complete
50
+ * @throws Error if cacheEntityName is not configured
51
+ */
52
+ async evictCache(tables: string[]): Promise<void> {
53
+ if (!this.options.cacheEntityName) {
54
+ throw new Error("cacheEntityName is not configured");
55
+ }
56
+ await clearTablesCache(tables, this.options);
57
+ }
58
+
59
+ /**
60
+ * Inserts records with optimistic locking/versioning and automatically evicts cache.
61
+ *
62
+ * This method uses `modifyWithVersioning().insert()` internally, providing:
63
+ * - Automatic version field initialization
64
+ * - Optimistic locking support
65
+ * - Cache eviction after successful operation
66
+ *
67
+ * @param schema - The table schema
68
+ * @param models - Array of entities to insert
69
+ * @param updateIfExists - Whether to update existing records
70
+ * @returns Promise that resolves to the number of inserted rows
71
+ * @throws Error if cacheEntityName is not configured
72
+ * @throws Error if optimistic locking check fails
73
+ */
74
+ async insert<T extends AnyMySqlTable>(
75
+ schema: T,
76
+ models: InferInsertModel<T>[],
77
+ updateIfExists?: boolean,
78
+ ): Promise<number> {
79
+ this.validateCacheConfiguration();
80
+ const number = await this.forgeOperations
81
+ .modifyWithVersioning()
82
+ .insert(schema, models, updateIfExists);
83
+ await clearCache(schema, this.options);
84
+ return number;
85
+ }
86
+
87
+ /**
88
+ * Deletes a record by ID with optimistic locking/versioning and automatically evicts cache.
89
+ *
90
+ * This method uses `modifyWithVersioning().deleteById()` internally, providing:
91
+ * - Optimistic locking checks before deletion
92
+ * - Version field validation
93
+ * - Cache eviction after successful operation
94
+ *
95
+ * @param id - The ID of the record to delete
96
+ * @param schema - The table schema
97
+ * @returns Promise that resolves to the number of affected rows
98
+ * @throws Error if cacheEntityName is not configured
99
+ * @throws Error if optimistic locking check fails
100
+ */
101
+ async deleteById<T extends AnyMySqlTable>(id: unknown, schema: T): Promise<number> {
102
+ this.validateCacheConfiguration();
103
+ const number = await this.forgeOperations.modifyWithVersioning().deleteById(id, schema);
104
+ await clearCache(schema, this.options);
105
+ return number;
106
+ }
107
+
108
+ /**
109
+ * Updates a record by ID with optimistic locking/versioning and automatically evicts cache.
110
+ *
111
+ * This method uses `modifyWithVersioning().updateById()` internally, providing:
112
+ * - Optimistic locking checks before update
113
+ * - Version field incrementation
114
+ * - Cache eviction after successful operation
115
+ *
116
+ * @param entity - The entity with updated values (must include primary key)
117
+ * @param schema - The table schema
118
+ * @returns Promise that resolves to the number of affected rows
119
+ * @throws Error if cacheEntityName is not configured
120
+ * @throws Error if optimistic locking check fails
121
+ */
122
+ async updateById<T extends AnyMySqlTable>(
123
+ entity: Partial<InferInsertModel<T>>,
124
+ schema: T,
125
+ ): Promise<number> {
126
+ this.validateCacheConfiguration();
127
+ const number = await this.forgeOperations.modifyWithVersioning().updateById(entity, schema);
128
+ await clearCache(schema, this.options);
129
+ return number;
130
+ }
131
+
132
+ /**
133
+ * Updates fields based on conditions with optimistic locking/versioning and automatically evicts cache.
134
+ *
135
+ * This method uses `modifyWithVersioning().updateFields()` internally, providing:
136
+ * - Optimistic locking support (if version field is configured)
137
+ * - Version field validation and incrementation
138
+ * - Cache eviction after successful operation
139
+ *
140
+ * @param updateData - The data to update
141
+ * @param schema - The table schema
142
+ * @param where - Optional WHERE conditions
143
+ * @returns Promise that resolves to the number of affected rows
144
+ * @throws Error if cacheEntityName is not configured
145
+ * @throws Error if optimistic locking check fails
146
+ */
147
+ async updateFields<T extends AnyMySqlTable>(
148
+ updateData: Partial<InferInsertModel<T>>,
149
+ schema: T,
150
+ where?: SQL<unknown>,
151
+ ): Promise<number> {
152
+ this.validateCacheConfiguration();
153
+ const number = await this.forgeOperations
154
+ .modifyWithVersioning()
155
+ .updateFields(updateData, schema, where);
156
+ await clearCache(schema, this.options);
157
+ return number;
158
+ }
159
+
160
+ /**
161
+ * Executes a query with caching support.
162
+ * First checks cache, if not found executes query and stores result in cache.
163
+ *
164
+ * @param query - The Drizzle query to execute
165
+ * @param cacheTtl - Optional cache TTL override
166
+ * @returns Promise that resolves to the query results
167
+ * @throws Error if cacheEntityName is not configured
168
+ */
169
+ async executeQuery<T extends MySqlSelectDynamic<AnyMySqlSelectQueryBuilder>>(
170
+ query: T,
171
+ cacheTtl?: number,
172
+ ): Promise<Awaited<T>> {
173
+ this.validateCacheConfiguration();
174
+ const sqlQuery = query as { toSQL: () => Query };
175
+ const cacheResult = await getFromCache<Awaited<T>>(sqlQuery, this.options);
176
+ if (cacheResult) {
177
+ return cacheResult;
178
+ }
179
+ const results = await query;
180
+ await setCacheResult(sqlQuery, this.options, results, cacheTtl ?? this.options.cacheTTL ?? 60);
181
+ return results;
182
+ }
183
+
184
+ /**
185
+ * Validates that cache configuration is properly set up.
186
+ *
187
+ * @throws Error if cacheEntityName is not configured
188
+ * @private
189
+ */
190
+ private validateCacheConfiguration(): void {
191
+ if (!this.options.cacheEntityName) {
192
+ throw new Error("cacheEntityName is not configured");
193
+ }
194
+ }
195
+ }