@chainfuse/helpers 3.3.2 → 3.4.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.
package/dist/db.d.mts CHANGED
@@ -25,12 +25,14 @@ export declare class SQLCache<C extends CacheStorageLike> extends DrizzleCache {
25
25
  private dbType;
26
26
  private cache;
27
27
  private globalTtl;
28
+ private ttlCutoff;
28
29
  private _strategy;
29
30
  private usedTablesPerKey;
30
31
  static constructorArgs(): z.ZodObject<{
31
32
  dbName: z.ZodPipe<z.ZodString, z.ZodTransform<string, string>>;
32
33
  dbType: z.ZodPipe<z.ZodString, z.ZodTransform<string, string>>;
33
34
  cacheTTL: z.ZodDefault<z.ZodInt>;
35
+ cachePurge: z.ZodDefault<z.ZodUnion<readonly [z.ZodBoolean, z.ZodDate]>>;
34
36
  strategy: z.ZodDefault<z.ZodEnum<{
35
37
  explicit: "explicit";
36
38
  all: "all";
package/dist/db.mjs CHANGED
@@ -19,9 +19,9 @@ export class SQLCache extends DrizzleCache {
19
19
  dbType;
20
20
  cache;
21
21
  globalTtl;
22
+ ttlCutoff;
22
23
  _strategy;
23
- // This object will be used to store which query keys were used
24
- // for a specific table, so we can later use it for invalidation.
24
+ // This object will be used to store which query keys were used for a specific table, so we can later use it for invalidation.
25
25
  usedTablesPerKey = {};
26
26
  static constructorArgs() {
27
27
  return z.object({
@@ -37,6 +37,7 @@ export class SQLCache extends DrizzleCache {
37
37
  .int()
38
38
  .nonnegative()
39
39
  .default(5 * 60),
40
+ cachePurge: z.union([z.boolean(), z.date()]).default(false),
40
41
  strategy: z.enum(['explicit', 'all']).default('explicit'),
41
42
  });
42
43
  }
@@ -53,7 +54,7 @@ export class SQLCache extends DrizzleCache {
53
54
  */
54
55
  constructor(args, cacheStore) {
55
56
  super();
56
- const { dbName, dbType, cacheTTL, strategy } = SQLCache.constructorArgs().parse(args);
57
+ const { dbName, dbType, cacheTTL, cachePurge, strategy } = SQLCache.constructorArgs().parse(args);
57
58
  this.dbName = dbName;
58
59
  this.dbType = dbType;
59
60
  cacheStore ??= globalThis.caches;
@@ -64,6 +65,7 @@ export class SQLCache extends DrizzleCache {
64
65
  throw new Error('Cache store must be a CacheStorage (or subclass/instance of)');
65
66
  }
66
67
  this.globalTtl = cacheTTL;
68
+ this.ttlCutoff = cachePurge;
67
69
  this._strategy = strategy;
68
70
  }
69
71
  /**
@@ -90,14 +92,35 @@ export class SQLCache extends DrizzleCache {
90
92
  * @param key - A hashed query and parameters.
91
93
  */
92
94
  async get(key, _tables, isTag) {
93
- const response = await this.cache.then(async (cache) => cache.match(this.getCacheKey(isTag ? { tag: key } : { key })));
95
+ const cacheKey = this.getCacheKey(isTag ? { tag: key } : { key });
96
+ const response = await this.cache.then(async (cache) => cache.match(cacheKey));
94
97
  console.debug('SQLCache.get', isTag ? 'tag' : 'key', key, response?.ok ? 'HIT' : 'MISS');
95
98
  if (response) {
99
+ // Check if cache should be purged
100
+ if (this.ttlCutoff === true) {
101
+ console.debug('SQLCache.get', 'cache purged', this.ttlCutoff);
102
+ await this.cache.then((cache) => cache.delete(cacheKey));
103
+ return undefined;
104
+ }
105
+ if (this.ttlCutoff instanceof Date) {
106
+ const responseDate = response.headers.get('Date');
107
+ if (responseDate) {
108
+ const cachedDate = new Date(responseDate);
109
+ if (cachedDate < this.ttlCutoff) {
110
+ console.debug('SQLCache.get', 'cache purged', { cachedDate, cutoff: this.ttlCutoff });
111
+ await this.cache.then((cache) => cache.delete(cacheKey));
112
+ return undefined;
113
+ }
114
+ }
115
+ else {
116
+ console.error('SQLCache.get', 'response does not have a Date header, cannot check against ttlCutoff date');
117
+ }
118
+ }
96
119
  // eslint-disable-next-line @typescript-eslint/no-unsafe-return
97
120
  return response.json();
98
121
  }
99
122
  else {
100
- return response;
123
+ return undefined;
101
124
  }
102
125
  }
103
126
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chainfuse/helpers",
3
- "version": "3.3.2",
3
+ "version": "3.4.1",
4
4
  "description": "",
5
5
  "author": "ChainFuse",
6
6
  "homepage": "https://github.com/ChainFuse/packages/tree/main/packages/helpers#readme",
@@ -76,18 +76,18 @@
76
76
  },
77
77
  "prettier": "@demosjarco/prettier-config",
78
78
  "dependencies": {
79
- "@chainfuse/types": "^2.10.21",
79
+ "@chainfuse/types": "^2.10.22",
80
80
  "@discordjs/rest": "^2.5.0",
81
81
  "chalk": "^5.4.1",
82
82
  "cloudflare": "^4.3.0",
83
83
  "drizzle-orm": "^0.44.2",
84
84
  "strip-ansi": "^7.1.0",
85
85
  "uuid": "^11.1.0",
86
- "zod": "^3.25.51"
86
+ "zod": "^3.25.57"
87
87
  },
88
88
  "devDependencies": {
89
- "@cloudflare/workers-types": "^4.20250605.0",
90
- "@types/node": "^22.15.29"
89
+ "@cloudflare/workers-types": "^4.20250610.0",
90
+ "@types/node": "^22.15.31"
91
91
  },
92
- "gitHead": "c7b762988f05d1d4cb641a3073defce0af1b5e32"
92
+ "gitHead": "a36878f122942e7c2495a6b11a240fa7089bed4b"
93
93
  }