@chainfuse/helpers 4.4.7 → 4.4.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.
package/dist/db.d.mts CHANGED
@@ -20,6 +20,7 @@ export declare class SQLCache<C extends CacheStorageLike> extends DrizzleCache {
20
20
  private globalTtl;
21
21
  private ttlCutoff;
22
22
  private _strategy;
23
+ private logging;
23
24
  private usedTablesPerKey;
24
25
  static constructorArgs: z.ZodMiniObject<{
25
26
  dbName: z.ZodMiniPipe<z.ZodMiniString<string>, z.ZodMiniTransform<string, string>>;
@@ -30,6 +31,7 @@ export declare class SQLCache<C extends CacheStorageLike> extends DrizzleCache {
30
31
  all: "all";
31
32
  explicit: "explicit";
32
33
  }>>;
34
+ logging: z.ZodMiniDefault<z.ZodMiniBoolean<boolean>>;
33
35
  }, z.core.$strip>;
34
36
  /**
35
37
  * Creates an instance of the class with the specified database name, type, and cache TTL.
package/dist/db.mjs CHANGED
@@ -21,14 +21,16 @@ export class SQLCache extends DrizzleCache {
21
21
  globalTtl;
22
22
  ttlCutoff;
23
23
  _strategy;
24
+ logging;
24
25
  // 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
26
  usedTablesPerKey = {};
26
27
  static constructorArgs = z.object({
27
- dbName: z.pipe(z.string().check(z.minLength(1)), z.transform((val) => encodeURIComponent(val))),
28
- dbType: z.pipe(z.string().check(z.minLength(1)), z.transform((val) => encodeURIComponent(val))),
28
+ dbName: z.pipe(z.string().check(z.trim(), z.minLength(1)), z.transform((val) => encodeURIComponent(val))),
29
+ dbType: z.pipe(z.string().check(z.trim(), z.minLength(1)), z.transform((val) => encodeURIComponent(val))),
29
30
  cacheTTL: z._default(z.int().check(z.nonnegative()), 5 * 60),
30
31
  cachePurge: z._default(z.union([z.boolean(), z.date()]), false),
31
32
  strategy: z._default(z.enum(['explicit', 'all']), 'explicit'),
33
+ logging: z._default(z.boolean(), false),
32
34
  });
33
35
  /**
34
36
  * Creates an instance of the class with the specified database name, type, and cache TTL.
@@ -43,7 +45,7 @@ export class SQLCache extends DrizzleCache {
43
45
  */
44
46
  constructor(args, cacheStore) {
45
47
  super();
46
- const { dbName, dbType, cacheTTL, cachePurge, strategy } = SQLCache.constructorArgs.parse(args);
48
+ const { dbName, dbType, cacheTTL, cachePurge, strategy, logging } = SQLCache.constructorArgs.parse(args);
47
49
  this.dbName = dbName;
48
50
  this.dbType = dbType;
49
51
  cacheStore ??= globalThis.caches;
@@ -56,6 +58,7 @@ export class SQLCache extends DrizzleCache {
56
58
  this.globalTtl = cacheTTL;
57
59
  this.ttlCutoff = cachePurge;
58
60
  this._strategy = strategy;
61
+ this.logging = logging;
59
62
  }
60
63
  /**
61
64
  * For the strategy, we have two options:
@@ -86,11 +89,13 @@ export class SQLCache extends DrizzleCache {
86
89
  async get(key, _tables, isTag) {
87
90
  const cacheKey = this.getCacheKey(isTag ? { tag: key } : { key });
88
91
  const response = await this.cache.then(async (cache) => cache.match(cacheKey));
89
- console.debug('SQLCache.get', isTag ? 'tag' : 'key', key, response?.ok ? 'HIT' : 'MISS');
92
+ if (this.logging)
93
+ console.debug('SQLCache.get', isTag ? 'tag' : 'key', key, response?.ok ? 'HIT' : 'MISS');
90
94
  if (response) {
91
95
  // Check if cache should be purged
92
96
  if (this.ttlCutoff === true) {
93
- console.debug('SQLCache.get', 'cache purged', this.ttlCutoff);
97
+ if (this.logging)
98
+ console.debug('SQLCache.get', 'cache purged', this.ttlCutoff);
94
99
  await this.cache.then((cache) => cache.delete(cacheKey));
95
100
  return undefined;
96
101
  }
@@ -99,18 +104,19 @@ export class SQLCache extends DrizzleCache {
99
104
  if (responseDate) {
100
105
  const cachedDate = new Date(responseDate);
101
106
  if (cachedDate < this.ttlCutoff) {
102
- console.debug('SQLCache.get', 'cache purged', { cachedDate, cutoff: this.ttlCutoff });
107
+ if (this.logging)
108
+ console.debug('SQLCache.get', 'cache purged', { cachedDate, cutoff: this.ttlCutoff });
103
109
  await this.cache.then((cache) => cache.delete(cacheKey));
104
110
  return undefined;
105
111
  }
106
112
  }
107
113
  else {
108
- console.debug('SQLCache.get', 'cache purged', { responseDate });
114
+ if (this.logging)
115
+ console.debug('SQLCache.get', 'cache purged', { responseDate });
109
116
  await this.cache.then((cache) => cache.delete(cacheKey));
110
117
  return undefined;
111
118
  }
112
119
  }
113
- // eslint-disable-next-line @typescript-eslint/no-unsafe-return
114
120
  return response.json();
115
121
  }
116
122
  else {
@@ -147,7 +153,12 @@ export class SQLCache extends DrizzleCache {
147
153
  },
148
154
  });
149
155
  cacheResponse.headers.set('ETag', await CryptoHelpers.generateETag(cacheResponse));
150
- await this.cache.then(async (cache) => cache.put(this.getCacheKey(isTag ? { tag: hashedQuery } : { key: hashedQuery }), cacheResponse)).then(() => console.debug('SQLCache.put', isTag ? 'tag' : 'key', hashedQuery, 'SUCCESS'));
156
+ await this.cache
157
+ .then(async (cache) => cache.put(this.getCacheKey(isTag ? { tag: hashedQuery } : { key: hashedQuery }), cacheResponse))
158
+ .then(() => {
159
+ if (this.logging)
160
+ console.debug('SQLCache.put', isTag ? 'tag' : 'key', hashedQuery, 'SUCCESS');
161
+ });
151
162
  for (const table of tables) {
152
163
  const keys = this.usedTablesPerKey[table];
153
164
  if (keys === undefined) {
@@ -177,10 +188,20 @@ export class SQLCache extends DrizzleCache {
177
188
  }
178
189
  if (keysToDelete.size > 0 || tagsArray.length > 0) {
179
190
  for (const tag of tagsArray) {
180
- await this.cache.then(async (cache) => cache.delete(this.getCacheKey({ tag }))).then(() => console.debug('SQLCache.delete', 'tag', tag, 'SUCCESS'));
191
+ await this.cache
192
+ .then(async (cache) => cache.delete(this.getCacheKey({ tag })))
193
+ .then(() => {
194
+ if (this.logging)
195
+ console.debug('SQLCache.delete', 'tag', tag, 'SUCCESS');
196
+ });
181
197
  }
182
198
  for (const key of keysToDelete) {
183
- await this.cache.then(async (cache) => cache.delete(this.getCacheKey({ key }))).then(() => console.debug('SQLCache.delete', 'key', key, 'SUCCESS'));
199
+ await this.cache
200
+ .then(async (cache) => cache.delete(this.getCacheKey({ key })))
201
+ .then(() => {
202
+ if (this.logging)
203
+ console.debug('SQLCache.delete', 'key', key, 'SUCCESS');
204
+ });
184
205
  for (const table of tablesArray) {
185
206
  const tableName = is(table, Table) ? getTableName(table) : table;
186
207
  this.usedTablesPerKey[tableName] = [];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chainfuse/helpers",
3
- "version": "4.4.7",
3
+ "version": "4.4.8",
4
4
  "description": "",
5
5
  "author": "ChainFuse",
6
6
  "homepage": "https://github.com/ChainFuse/packages/tree/main/packages/helpers#readme",
@@ -80,19 +80,19 @@
80
80
  },
81
81
  "prettier": "@demosjarco/prettier-config",
82
82
  "dependencies": {
83
- "@chainfuse/types": "^4.2.21",
83
+ "@chainfuse/types": "^4.2.22",
84
84
  "@discordjs/rest": "^2.6.1",
85
85
  "chalk": "^5.6.2",
86
86
  "cloudflare": "^5.2.0",
87
87
  "dns-packet": "^5.6.1",
88
- "drizzle-orm": "^0.45.1",
88
+ "drizzle-orm": "^0.45.2",
89
89
  "strip-ansi": "^7.2.0",
90
90
  "uuid": "^13.0.0",
91
91
  "zod": "^4.3.6"
92
92
  },
93
93
  "devDependencies": {
94
- "@cloudflare/workers-types": "^4.20260317.1",
94
+ "@cloudflare/workers-types": "^4.20260405.1",
95
95
  "@types/dns-packet": "^5.6.5"
96
96
  },
97
- "gitHead": "dbc6f594a818b5c982269fe8271161972cce4b03"
97
+ "gitHead": "b0bcea1728f8c8e629219b670d3778dbe2cb4aae"
98
98
  }