drizzle-orm 0.43.1-5fb460b → 0.43.1-8eb999a

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.
@@ -84,9 +84,39 @@ class UpstashCache extends import_core.Cache {
84
84
  };
85
85
  }
86
86
  static [import_entity.entityKind] = "UpstashCache";
87
- static compositeTableSetPrefix = "__ct__";
87
+ /**
88
+ * Prefix for sets which denote the composite table names for each unique table
89
+ *
90
+ * Example: In the composite table set of "table1", you may find
91
+ * `${compositeTablePrefix}table1,table2` and `${compositeTablePrefix}table1,table3`
92
+ */
93
+ static compositeTableSetPrefix = "__CTS__";
94
+ /**
95
+ * Prefix for hashes which map hash or tags to cache values
96
+ */
97
+ static compositeTablePrefix = "__CT__";
98
+ /**
99
+ * Key which holds the mapping of tags to composite table names
100
+ *
101
+ * Using this tagsMapKey, you can find the composite table name for a given tag
102
+ * and get the cache value for that tag:
103
+ *
104
+ * ```ts
105
+ * const compositeTable = redis.hget(tagsMapKey, 'tag1')
106
+ * console.log(compositeTable) // `${compositeTablePrefix}table1,table2`
107
+ *
108
+ * const cachevalue = redis.hget(compositeTable, 'tag1')
109
+ */
88
110
  static tagsMapKey = "__tagsMap__";
89
- globalTtl = 1;
111
+ /**
112
+ * Queries whose auto invalidation is false aren't stored in their respective
113
+ * composite table hashes because those hashes are deleted when a mutation
114
+ * occurs on related tables.
115
+ *
116
+ * Instead, they are stored in a separate hash with the prefix
117
+ * `__nonAutoInvalidate__` to prevent them from being deleted when a mutation
118
+ */
119
+ static nonAutoInvalidateTablePrefix = "__nonAutoInvalidate__";
90
120
  luaScripts;
91
121
  internalConfig;
92
122
  strategy() {
@@ -96,9 +126,15 @@ class UpstashCache extends import_core.Cache {
96
126
  return config ? {
97
127
  seconds: config.ex,
98
128
  hexOptions: config.hexOptions
99
- } : void 0;
129
+ } : {
130
+ seconds: 1
131
+ };
100
132
  }
101
- async get(key, tables, isTag = false) {
133
+ async get(key, tables, isTag = false, isAutoInvalidate) {
134
+ if (!isAutoInvalidate) {
135
+ const result2 = await this.redis.hget(UpstashCache.nonAutoInvalidateTablePrefix, key);
136
+ return result2 === null ? void 0 : result2;
137
+ }
102
138
  if (isTag) {
103
139
  const result2 = await this.luaScripts.getByTagScript.exec([UpstashCache.tagsMapKey], [key]);
104
140
  return result2 === null ? void 0 : result2;
@@ -108,10 +144,17 @@ class UpstashCache extends import_core.Cache {
108
144
  return result === null ? void 0 : result;
109
145
  }
110
146
  async put(key, response, tables, isTag = false, config) {
147
+ const isAutoInvalidate = tables.length !== 0;
111
148
  const pipeline = this.redis.pipeline();
112
- const compositeKey = this.getCompositeKey(tables);
113
- const ttlSeconds = config && config.ex ? config.ex : this.globalTtl;
149
+ const ttlSeconds = config && config.ex ? config.ex : this.internalConfig.seconds;
114
150
  const hexOptions = config && config.hexOptions ? config.hexOptions : this.internalConfig?.hexOptions;
151
+ if (!isAutoInvalidate) {
152
+ pipeline.hset(UpstashCache.nonAutoInvalidateTablePrefix, { [key]: response });
153
+ pipeline.hexpire(UpstashCache.nonAutoInvalidateTablePrefix, key, ttlSeconds, hexOptions);
154
+ await pipeline.exec();
155
+ return;
156
+ }
157
+ const compositeKey = this.getCompositeKey(tables);
115
158
  pipeline.hset(compositeKey, { [key]: response });
116
159
  pipeline.hexpire(compositeKey, key, ttlSeconds, hexOptions);
117
160
  if (isTag) {
@@ -131,7 +174,7 @@ class UpstashCache extends import_core.Cache {
131
174
  await this.luaScripts.onMutateScript.exec([UpstashCache.tagsMapKey, ...compositeTableSets], tags);
132
175
  }
133
176
  addTablePrefix = (table) => `${UpstashCache.compositeTableSetPrefix}${table}`;
134
- getCompositeKey = (tables) => tables.sort().join(",");
177
+ getCompositeKey = (tables) => `${UpstashCache.compositeTablePrefix}${tables.sort().join(",")}`;
135
178
  }
136
179
  function upstashCache({ url, token, config, global = false }) {
137
180
  const redis = new import_redis.Redis({
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/cache/upstash/cache.ts"],"sourcesContent":["import { Redis } from '@upstash/redis';\nimport type { MutationOption } from '~/cache/core/index.ts';\nimport { Cache } from '~/cache/core/index.ts';\nimport { entityKind, is } from '~/entity.ts';\nimport { OriginalName, Table } from '~/index.ts';\nimport type { CacheConfig } from '../core/types.ts';\n\nconst getByTagScript = `\nlocal tagsMapKey = KEYS[1] -- tags map key\nlocal tag = ARGV[1] -- tag\n\nlocal compositeTableName = redis.call('HGET', tagsMapKey, tag)\nif not compositeTableName then\n return nil\nend\n\nlocal value = redis.call('HGET', compositeTableName, tag)\nreturn value\n`;\n\nconst onMutateScript = `\nlocal tagsMapKey = KEYS[1] -- tags map key\nlocal tables = {} -- initialize tables array\nlocal tags = ARGV -- tags array\n\nfor i = 2, #KEYS do\n tables[#tables + 1] = KEYS[i] -- add all keys except the first one to tables\nend\n\nif #tags > 0 then\n for _, tag in ipairs(tags) do\n if tag ~= nil and tag ~= '' then\n local compositeTableName = redis.call('HGET', tagsMapKey, tag)\n if compositeTableName then\n redis.call('HDEL', compositeTableName, tag)\n end\n end\n end\n redis.call('HDEL', tagsMapKey, unpack(tags))\nend\n\nlocal keysToDelete = {}\n\nif #tables > 0 then\n local compositeTableNames = redis.call('SUNION', unpack(tables))\n for _, compositeTableName in ipairs(compositeTableNames) do\n keysToDelete[#keysToDelete + 1] = compositeTableName\n end\n for _, table in ipairs(tables) do\n keysToDelete[#keysToDelete + 1] = table\n end\n redis.call('DEL', unpack(keysToDelete))\nend\n`;\n\ntype Script = ReturnType<Redis['createScript']>;\n\ntype ExpireOptions = 'NX' | 'nx' | 'XX' | 'xx' | 'GT' | 'gt' | 'LT' | 'lt';\n\nexport class UpstashCache extends Cache {\n\tstatic override readonly [entityKind]: string = 'UpstashCache';\n\tprivate static compositeTableSetPrefix = '__ct__';\n\tprivate static tagsMapKey = '__tagsMap__';\n\n\tprivate globalTtl: number = 1;\n\n\tprivate luaScripts: {\n\t\tgetByTagScript: Script;\n\t\tonMutateScript: Script;\n\t};\n\n\tprivate internalConfig?: { seconds: number; hexOptions: ExpireOptions };\n\n\tconstructor(public redis: Redis, config?: CacheConfig, protected useGlobally?: boolean) {\n\t\tsuper();\n\t\tthis.internalConfig = this.toInternalConfig(config);\n\t\tthis.luaScripts = {\n\t\t\tgetByTagScript: this.redis.createScript(getByTagScript, { readonly: true }),\n\t\t\tonMutateScript: this.redis.createScript(onMutateScript),\n\t\t};\n\t}\n\n\tpublic strategy() {\n\t\treturn this.useGlobally ? 'all' : 'explicit';\n\t}\n\n\tprivate toInternalConfig(config?: CacheConfig) {\n\t\treturn config\n\t\t\t? {\n\t\t\t\tseconds: config.ex,\n\t\t\t\thexOptions: config.hexOptions,\n\t\t\t} as { seconds: number; hexOptions: ExpireOptions }\n\t\t\t: undefined;\n\t}\n\n\toverride async get(key: string, tables: string[], isTag: boolean = false): Promise<any[] | undefined> {\n\t\tif (isTag) {\n\t\t\tconst result = await this.luaScripts.getByTagScript.exec([UpstashCache.tagsMapKey], [key]);\n\t\t\treturn result === null ? undefined : result as any[];\n\t\t}\n\n\t\t// Normal cache lookup for the composite key\n\t\tconst compositeKey = this.getCompositeKey(tables);\n\t\tconst result = await this.redis.hget(compositeKey, key) ?? undefined; // Retrieve result for normal query\n\t\treturn result === null ? undefined : result as any[];\n\t}\n\n\toverride async put(\n\t\tkey: string,\n\t\tresponse: any,\n\t\ttables: string[],\n\t\tisTag: boolean = false,\n\t\tconfig?: CacheConfig,\n\t): Promise<void> {\n\t\tconst pipeline = this.redis.pipeline();\n\t\tconst compositeKey = this.getCompositeKey(tables);\n\t\tconst ttlSeconds = config && config.ex ? config.ex : this.globalTtl;\n\t\tconst hexOptions = config && config.hexOptions ? config.hexOptions : this.internalConfig?.hexOptions;\n\n\t\tpipeline.hset(compositeKey, { [key]: response }); // Store the result with the tag under the composite key\n\t\tpipeline.hexpire(compositeKey, key, ttlSeconds, hexOptions); // Set expiration for the composite key\n\n\t\tif (isTag) {\n\t\t\tpipeline.hset(UpstashCache.tagsMapKey, { [key]: compositeKey }); // Store the tag and its composite key in the map\n\t\t\tpipeline.hexpire(UpstashCache.tagsMapKey, key, ttlSeconds, hexOptions); // Set expiration for the tag\n\t\t}\n\n\t\tfor (const table of tables) {\n\t\t\tpipeline.sadd(this.addTablePrefix(table), compositeKey);\n\t\t}\n\n\t\tawait pipeline.exec();\n\t}\n\n\toverride async onMutate(params: MutationOption) {\n\t\tconst tags = Array.isArray(params.tags) ? params.tags : params.tags ? [params.tags] : [];\n\t\tconst tables = Array.isArray(params.tables) ? params.tables : params.tables ? [params.tables] : [];\n\t\tconst tableNames: string[] = tables.map((table) => is(table, Table) ? table[OriginalName] : table as string);\n\n\t\tconst compositeTableSets = tableNames.map((table) => this.addTablePrefix(table));\n\t\tawait this.luaScripts.onMutateScript.exec([UpstashCache.tagsMapKey, ...compositeTableSets], tags);\n\t}\n\n\tprivate addTablePrefix = (table: string) => `${UpstashCache.compositeTableSetPrefix}${table}`;\n\tprivate getCompositeKey = (tables: string[]) => tables.sort().join(',');\n}\n\nexport function upstashCache(\n\t{ url, token, config, global = false }: { url: string; token: string; config?: CacheConfig; global?: boolean },\n): UpstashCache {\n\tconst redis = new Redis({\n\t\turl,\n\t\ttoken,\n\t});\n\n\treturn new UpstashCache(redis, config, global);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAAsB;AAEtB,kBAAsB;AACtB,oBAA+B;AAC/B,eAAoC;AAGpC,MAAM,iBAAiB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAavB,MAAM,iBAAiB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAuChB,MAAM,qBAAqB,kBAAM;AAA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AAC/C,aAAS,QAAQ,cAAc,KAAK,YAAY,UAAU;AAE1D,QAAI,OAAO;AACV,eAAS,KAAK,aAAa,YAAY,EAAE,CAAC,GAAG,GAAG,aAAa,CAAC;AAC9D,eAAS,QAAQ,aAAa,YAAY,KAAK,YAAY,UAAU;AAAA,IACtE;AAEA,eAAW,SAAS,QAAQ;AAC3B,eAAS,KAAK,KAAK,eAAe,KAAK,GAAG,YAAY;AAAA,IACvD;AAEA,UAAM,SAAS,KAAK;AAAA,EACrB;AAAA,EAEA,MAAe,SAAS,QAAwB;AAC/C,UAAM,OAAO,MAAM,QAAQ,OAAO,IAAI,IAAI,OAAO,OAAO,OAAO,OAAO,CAAC,OAAO,IAAI,IAAI,CAAC;AACvF,UAAM,SAAS,MAAM,QAAQ,OAAO,MAAM,IAAI,OAAO,SAAS,OAAO,SAAS,CAAC,OAAO,MAAM,IAAI,CAAC;AACjG,UAAM,aAAuB,OAAO,IAAI,CAAC,cAAU,kBAAG,OAAO,cAAK,IAAI,MAAM,qBAAY,IAAI,KAAe;AAE3G,UAAM,qBAAqB,WAAW,IAAI,CAAC,UAAU,KAAK,eAAe,KAAK,CAAC;AAC/E,UAAM,KAAK,WAAW,eAAe,KAAK,CAAC,aAAa,YAAY,GAAG,kBAAkB,GAAG,IAAI;AAAA,EACjG;AAAA,EAEQ,iBAAiB,CAAC,UAAkB,GAAG,aAAa,uBAAuB,GAAG,KAAK;AAAA,EACnF,kBAAkB,CAAC,WAAqB,OAAO,KAAK,EAAE,KAAK,GAAG;AACvE;AAEO,SAAS,aACf,EAAE,KAAK,OAAO,QAAQ,SAAS,MAAM,GACtB;AACf,QAAM,QAAQ,IAAI,mBAAM;AAAA,IACvB;AAAA,IACA;AAAA,EACD,CAAC;AAED,SAAO,IAAI,aAAa,OAAO,QAAQ,MAAM;AAC9C;","names":["result"]}
1
+ {"version":3,"sources":["../../../src/cache/upstash/cache.ts"],"sourcesContent":["import { Redis } from '@upstash/redis';\nimport type { MutationOption } from '~/cache/core/index.ts';\nimport { Cache } from '~/cache/core/index.ts';\nimport { entityKind, is } from '~/entity.ts';\nimport { OriginalName, Table } from '~/index.ts';\nimport type { CacheConfig } from '../core/types.ts';\n\nconst getByTagScript = `\nlocal tagsMapKey = KEYS[1] -- tags map key\nlocal tag = ARGV[1] -- tag\n\nlocal compositeTableName = redis.call('HGET', tagsMapKey, tag)\nif not compositeTableName then\n return nil\nend\n\nlocal value = redis.call('HGET', compositeTableName, tag)\nreturn value\n`;\n\nconst onMutateScript = `\nlocal tagsMapKey = KEYS[1] -- tags map key\nlocal tables = {} -- initialize tables array\nlocal tags = ARGV -- tags array\n\nfor i = 2, #KEYS do\n tables[#tables + 1] = KEYS[i] -- add all keys except the first one to tables\nend\n\nif #tags > 0 then\n for _, tag in ipairs(tags) do\n if tag ~= nil and tag ~= '' then\n local compositeTableName = redis.call('HGET', tagsMapKey, tag)\n if compositeTableName then\n redis.call('HDEL', compositeTableName, tag)\n end\n end\n end\n redis.call('HDEL', tagsMapKey, unpack(tags))\nend\n\nlocal keysToDelete = {}\n\nif #tables > 0 then\n local compositeTableNames = redis.call('SUNION', unpack(tables))\n for _, compositeTableName in ipairs(compositeTableNames) do\n keysToDelete[#keysToDelete + 1] = compositeTableName\n end\n for _, table in ipairs(tables) do\n keysToDelete[#keysToDelete + 1] = table\n end\n redis.call('DEL', unpack(keysToDelete))\nend\n`;\n\ntype Script = ReturnType<Redis['createScript']>;\n\ntype ExpireOptions = 'NX' | 'nx' | 'XX' | 'xx' | 'GT' | 'gt' | 'LT' | 'lt';\n\nexport class UpstashCache extends Cache {\n\tstatic override readonly [entityKind]: string = 'UpstashCache';\n\t/**\n\t * Prefix for sets which denote the composite table names for each unique table\n\t *\n\t * Example: In the composite table set of \"table1\", you may find\n\t * `${compositeTablePrefix}table1,table2` and `${compositeTablePrefix}table1,table3`\n\t */\n\tprivate static compositeTableSetPrefix = '__CTS__';\n\t/**\n\t * Prefix for hashes which map hash or tags to cache values\n\t */\n\tprivate static compositeTablePrefix = '__CT__';\n\t/**\n\t * Key which holds the mapping of tags to composite table names\n\t *\n\t * Using this tagsMapKey, you can find the composite table name for a given tag\n\t * and get the cache value for that tag:\n\t *\n\t * ```ts\n\t * const compositeTable = redis.hget(tagsMapKey, 'tag1')\n\t * console.log(compositeTable) // `${compositeTablePrefix}table1,table2`\n\t *\n\t * const cachevalue = redis.hget(compositeTable, 'tag1')\n\t */\n\tprivate static tagsMapKey = '__tagsMap__';\n\t/**\n\t * Queries whose auto invalidation is false aren't stored in their respective\n\t * composite table hashes because those hashes are deleted when a mutation\n\t * occurs on related tables.\n\t *\n\t * Instead, they are stored in a separate hash with the prefix\n\t * `__nonAutoInvalidate__` to prevent them from being deleted when a mutation\n\t */\n\tprivate static nonAutoInvalidateTablePrefix = '__nonAutoInvalidate__';\n\n\tprivate luaScripts: {\n\t\tgetByTagScript: Script;\n\t\tonMutateScript: Script;\n\t};\n\n\tprivate internalConfig: { seconds: number; hexOptions?: ExpireOptions };\n\n\tconstructor(public redis: Redis, config?: CacheConfig, protected useGlobally?: boolean) {\n\t\tsuper();\n\t\tthis.internalConfig = this.toInternalConfig(config);\n\t\tthis.luaScripts = {\n\t\t\tgetByTagScript: this.redis.createScript(getByTagScript, { readonly: true }),\n\t\t\tonMutateScript: this.redis.createScript(onMutateScript),\n\t\t};\n\t}\n\n\tpublic strategy() {\n\t\treturn this.useGlobally ? 'all' : 'explicit';\n\t}\n\n\tprivate toInternalConfig(config?: CacheConfig): { seconds: number; hexOptions?: ExpireOptions } {\n\t\treturn config\n\t\t\t? {\n\t\t\t\tseconds: config.ex!,\n\t\t\t\thexOptions: config.hexOptions,\n\t\t\t}\n\t\t\t: {\n\t\t\t\tseconds: 1,\n\t\t\t};\n\t}\n\n\toverride async get(\n\t\tkey: string,\n\t\ttables: string[],\n\t\tisTag: boolean = false,\n\t\tisAutoInvalidate?: boolean,\n\t): Promise<any[] | undefined> {\n\t\tif (!isAutoInvalidate) {\n\t\t\tconst result = await this.redis.hget(UpstashCache.nonAutoInvalidateTablePrefix, key);\n\t\t\treturn result === null ? undefined : result as any[];\n\t\t}\n\n\t\tif (isTag) {\n\t\t\tconst result = await this.luaScripts.getByTagScript.exec([UpstashCache.tagsMapKey], [key]);\n\t\t\treturn result === null ? undefined : result as any[];\n\t\t}\n\n\t\t// Normal cache lookup for the composite key\n\t\tconst compositeKey = this.getCompositeKey(tables);\n\t\tconst result = await this.redis.hget(compositeKey, key) ?? undefined; // Retrieve result for normal query\n\t\treturn result === null ? undefined : result as any[];\n\t}\n\n\toverride async put(\n\t\tkey: string,\n\t\tresponse: any,\n\t\ttables: string[],\n\t\tisTag: boolean = false,\n\t\tconfig?: CacheConfig,\n\t): Promise<void> {\n\t\tconst isAutoInvalidate = tables.length !== 0;\n\n\t\tconst pipeline = this.redis.pipeline();\n\t\tconst ttlSeconds = config && config.ex ? config.ex : this.internalConfig.seconds;\n\t\tconst hexOptions = config && config.hexOptions ? config.hexOptions : this.internalConfig?.hexOptions;\n\n\t\tif (!isAutoInvalidate) {\n\t\t\tpipeline.hset(UpstashCache.nonAutoInvalidateTablePrefix, { [key]: response });\n\t\t\tpipeline.hexpire(UpstashCache.nonAutoInvalidateTablePrefix, key, ttlSeconds, hexOptions);\n\t\t\tawait pipeline.exec();\n\t\t\treturn;\n\t\t}\n\n\t\tconst compositeKey = this.getCompositeKey(tables);\n\n\t\tpipeline.hset(compositeKey, { [key]: response }); // Store the result with the tag under the composite key\n\t\tpipeline.hexpire(compositeKey, key, ttlSeconds, hexOptions); // Set expiration for the composite key\n\n\t\tif (isTag) {\n\t\t\tpipeline.hset(UpstashCache.tagsMapKey, { [key]: compositeKey }); // Store the tag and its composite key in the map\n\t\t\tpipeline.hexpire(UpstashCache.tagsMapKey, key, ttlSeconds, hexOptions); // Set expiration for the tag\n\t\t}\n\n\t\tfor (const table of tables) {\n\t\t\tpipeline.sadd(this.addTablePrefix(table), compositeKey);\n\t\t}\n\n\t\tawait pipeline.exec();\n\t}\n\n\toverride async onMutate(params: MutationOption) {\n\t\tconst tags = Array.isArray(params.tags) ? params.tags : params.tags ? [params.tags] : [];\n\t\tconst tables = Array.isArray(params.tables) ? params.tables : params.tables ? [params.tables] : [];\n\t\tconst tableNames: string[] = tables.map((table) => is(table, Table) ? table[OriginalName] : table as string);\n\n\t\tconst compositeTableSets = tableNames.map((table) => this.addTablePrefix(table));\n\t\tawait this.luaScripts.onMutateScript.exec([UpstashCache.tagsMapKey, ...compositeTableSets], tags);\n\t}\n\n\tprivate addTablePrefix = (table: string) => `${UpstashCache.compositeTableSetPrefix}${table}`;\n\tprivate getCompositeKey = (tables: string[]) => `${UpstashCache.compositeTablePrefix}${tables.sort().join(',')}`;\n}\n\nexport function upstashCache(\n\t{ url, token, config, global = false }: { url: string; token: string; config?: CacheConfig; global?: boolean },\n): UpstashCache {\n\tconst redis = new Redis({\n\t\turl,\n\t\ttoken,\n\t});\n\n\treturn new UpstashCache(redis, config, global);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAAsB;AAEtB,kBAAsB;AACtB,oBAA+B;AAC/B,eAAoC;AAGpC,MAAM,iBAAiB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAavB,MAAM,iBAAiB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAuChB,MAAM,qBAAqB,kBAAM;AAAA,EA2CvC,YAAmB,OAAc,QAAgC,aAAuB;AACvF,UAAM;AADY;AAA8C;AAEhE,SAAK,iBAAiB,KAAK,iBAAiB,MAAM;AAClD,SAAK,aAAa;AAAA,MACjB,gBAAgB,KAAK,MAAM,aAAa,gBAAgB,EAAE,UAAU,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jG;AAAA,EAEQ,iBAAiB,CAAC,UAAkB,GAAG,aAAa,uBAAuB,GAAG,KAAK;AAAA,EACnF,kBAAkB,CAAC,WAAqB,GAAG,aAAa,oBAAoB,GAAG,OAAO,KAAK,EAAE,KAAK,GAAG,CAAC;AAC/G;AAEO,SAAS,aACf,EAAE,KAAK,OAAO,QAAQ,SAAS,MAAM,GACtB;AACf,QAAM,QAAQ,IAAI,mBAAM;AAAA,IACvB;AAAA,IACA;AAAA,EACD,CAAC;AAED,SAAO,IAAI,aAAa,OAAO,QAAQ,MAAM;AAC9C;","names":["result"]}
@@ -7,15 +7,45 @@ export declare class UpstashCache extends Cache {
7
7
  redis: Redis;
8
8
  protected useGlobally?: boolean | undefined;
9
9
  static readonly [entityKind]: string;
10
+ /**
11
+ * Prefix for sets which denote the composite table names for each unique table
12
+ *
13
+ * Example: In the composite table set of "table1", you may find
14
+ * `${compositeTablePrefix}table1,table2` and `${compositeTablePrefix}table1,table3`
15
+ */
10
16
  private static compositeTableSetPrefix;
17
+ /**
18
+ * Prefix for hashes which map hash or tags to cache values
19
+ */
20
+ private static compositeTablePrefix;
21
+ /**
22
+ * Key which holds the mapping of tags to composite table names
23
+ *
24
+ * Using this tagsMapKey, you can find the composite table name for a given tag
25
+ * and get the cache value for that tag:
26
+ *
27
+ * ```ts
28
+ * const compositeTable = redis.hget(tagsMapKey, 'tag1')
29
+ * console.log(compositeTable) // `${compositeTablePrefix}table1,table2`
30
+ *
31
+ * const cachevalue = redis.hget(compositeTable, 'tag1')
32
+ */
11
33
  private static tagsMapKey;
12
- private globalTtl;
34
+ /**
35
+ * Queries whose auto invalidation is false aren't stored in their respective
36
+ * composite table hashes because those hashes are deleted when a mutation
37
+ * occurs on related tables.
38
+ *
39
+ * Instead, they are stored in a separate hash with the prefix
40
+ * `__nonAutoInvalidate__` to prevent them from being deleted when a mutation
41
+ */
42
+ private static nonAutoInvalidateTablePrefix;
13
43
  private luaScripts;
14
- private internalConfig?;
44
+ private internalConfig;
15
45
  constructor(redis: Redis, config?: CacheConfig, useGlobally?: boolean | undefined);
16
46
  strategy(): "all" | "explicit";
17
47
  private toInternalConfig;
18
- get(key: string, tables: string[], isTag?: boolean): Promise<any[] | undefined>;
48
+ get(key: string, tables: string[], isTag?: boolean, isAutoInvalidate?: boolean): Promise<any[] | undefined>;
19
49
  put(key: string, response: any, tables: string[], isTag?: boolean, config?: CacheConfig): Promise<void>;
20
50
  onMutate(params: MutationOption): Promise<void>;
21
51
  private addTablePrefix;
@@ -7,15 +7,45 @@ export declare class UpstashCache extends Cache {
7
7
  redis: Redis;
8
8
  protected useGlobally?: boolean | undefined;
9
9
  static readonly [entityKind]: string;
10
+ /**
11
+ * Prefix for sets which denote the composite table names for each unique table
12
+ *
13
+ * Example: In the composite table set of "table1", you may find
14
+ * `${compositeTablePrefix}table1,table2` and `${compositeTablePrefix}table1,table3`
15
+ */
10
16
  private static compositeTableSetPrefix;
17
+ /**
18
+ * Prefix for hashes which map hash or tags to cache values
19
+ */
20
+ private static compositeTablePrefix;
21
+ /**
22
+ * Key which holds the mapping of tags to composite table names
23
+ *
24
+ * Using this tagsMapKey, you can find the composite table name for a given tag
25
+ * and get the cache value for that tag:
26
+ *
27
+ * ```ts
28
+ * const compositeTable = redis.hget(tagsMapKey, 'tag1')
29
+ * console.log(compositeTable) // `${compositeTablePrefix}table1,table2`
30
+ *
31
+ * const cachevalue = redis.hget(compositeTable, 'tag1')
32
+ */
11
33
  private static tagsMapKey;
12
- private globalTtl;
34
+ /**
35
+ * Queries whose auto invalidation is false aren't stored in their respective
36
+ * composite table hashes because those hashes are deleted when a mutation
37
+ * occurs on related tables.
38
+ *
39
+ * Instead, they are stored in a separate hash with the prefix
40
+ * `__nonAutoInvalidate__` to prevent them from being deleted when a mutation
41
+ */
42
+ private static nonAutoInvalidateTablePrefix;
13
43
  private luaScripts;
14
- private internalConfig?;
44
+ private internalConfig;
15
45
  constructor(redis: Redis, config?: CacheConfig, useGlobally?: boolean | undefined);
16
46
  strategy(): "all" | "explicit";
17
47
  private toInternalConfig;
18
- get(key: string, tables: string[], isTag?: boolean): Promise<any[] | undefined>;
48
+ get(key: string, tables: string[], isTag?: boolean, isAutoInvalidate?: boolean): Promise<any[] | undefined>;
19
49
  put(key: string, response: any, tables: string[], isTag?: boolean, config?: CacheConfig): Promise<void>;
20
50
  onMutate(params: MutationOption): Promise<void>;
21
51
  private addTablePrefix;
@@ -60,9 +60,39 @@ class UpstashCache extends Cache {
60
60
  };
61
61
  }
62
62
  static [entityKind] = "UpstashCache";
63
- static compositeTableSetPrefix = "__ct__";
63
+ /**
64
+ * Prefix for sets which denote the composite table names for each unique table
65
+ *
66
+ * Example: In the composite table set of "table1", you may find
67
+ * `${compositeTablePrefix}table1,table2` and `${compositeTablePrefix}table1,table3`
68
+ */
69
+ static compositeTableSetPrefix = "__CTS__";
70
+ /**
71
+ * Prefix for hashes which map hash or tags to cache values
72
+ */
73
+ static compositeTablePrefix = "__CT__";
74
+ /**
75
+ * Key which holds the mapping of tags to composite table names
76
+ *
77
+ * Using this tagsMapKey, you can find the composite table name for a given tag
78
+ * and get the cache value for that tag:
79
+ *
80
+ * ```ts
81
+ * const compositeTable = redis.hget(tagsMapKey, 'tag1')
82
+ * console.log(compositeTable) // `${compositeTablePrefix}table1,table2`
83
+ *
84
+ * const cachevalue = redis.hget(compositeTable, 'tag1')
85
+ */
64
86
  static tagsMapKey = "__tagsMap__";
65
- globalTtl = 1;
87
+ /**
88
+ * Queries whose auto invalidation is false aren't stored in their respective
89
+ * composite table hashes because those hashes are deleted when a mutation
90
+ * occurs on related tables.
91
+ *
92
+ * Instead, they are stored in a separate hash with the prefix
93
+ * `__nonAutoInvalidate__` to prevent them from being deleted when a mutation
94
+ */
95
+ static nonAutoInvalidateTablePrefix = "__nonAutoInvalidate__";
66
96
  luaScripts;
67
97
  internalConfig;
68
98
  strategy() {
@@ -72,9 +102,15 @@ class UpstashCache extends Cache {
72
102
  return config ? {
73
103
  seconds: config.ex,
74
104
  hexOptions: config.hexOptions
75
- } : void 0;
105
+ } : {
106
+ seconds: 1
107
+ };
76
108
  }
77
- async get(key, tables, isTag = false) {
109
+ async get(key, tables, isTag = false, isAutoInvalidate) {
110
+ if (!isAutoInvalidate) {
111
+ const result2 = await this.redis.hget(UpstashCache.nonAutoInvalidateTablePrefix, key);
112
+ return result2 === null ? void 0 : result2;
113
+ }
78
114
  if (isTag) {
79
115
  const result2 = await this.luaScripts.getByTagScript.exec([UpstashCache.tagsMapKey], [key]);
80
116
  return result2 === null ? void 0 : result2;
@@ -84,10 +120,17 @@ class UpstashCache extends Cache {
84
120
  return result === null ? void 0 : result;
85
121
  }
86
122
  async put(key, response, tables, isTag = false, config) {
123
+ const isAutoInvalidate = tables.length !== 0;
87
124
  const pipeline = this.redis.pipeline();
88
- const compositeKey = this.getCompositeKey(tables);
89
- const ttlSeconds = config && config.ex ? config.ex : this.globalTtl;
125
+ const ttlSeconds = config && config.ex ? config.ex : this.internalConfig.seconds;
90
126
  const hexOptions = config && config.hexOptions ? config.hexOptions : this.internalConfig?.hexOptions;
127
+ if (!isAutoInvalidate) {
128
+ pipeline.hset(UpstashCache.nonAutoInvalidateTablePrefix, { [key]: response });
129
+ pipeline.hexpire(UpstashCache.nonAutoInvalidateTablePrefix, key, ttlSeconds, hexOptions);
130
+ await pipeline.exec();
131
+ return;
132
+ }
133
+ const compositeKey = this.getCompositeKey(tables);
91
134
  pipeline.hset(compositeKey, { [key]: response });
92
135
  pipeline.hexpire(compositeKey, key, ttlSeconds, hexOptions);
93
136
  if (isTag) {
@@ -107,7 +150,7 @@ class UpstashCache extends Cache {
107
150
  await this.luaScripts.onMutateScript.exec([UpstashCache.tagsMapKey, ...compositeTableSets], tags);
108
151
  }
109
152
  addTablePrefix = (table) => `${UpstashCache.compositeTableSetPrefix}${table}`;
110
- getCompositeKey = (tables) => tables.sort().join(",");
153
+ getCompositeKey = (tables) => `${UpstashCache.compositeTablePrefix}${tables.sort().join(",")}`;
111
154
  }
112
155
  function upstashCache({ url, token, config, global = false }) {
113
156
  const redis = new Redis({
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/cache/upstash/cache.ts"],"sourcesContent":["import { Redis } from '@upstash/redis';\nimport type { MutationOption } from '~/cache/core/index.ts';\nimport { Cache } from '~/cache/core/index.ts';\nimport { entityKind, is } from '~/entity.ts';\nimport { OriginalName, Table } from '~/index.ts';\nimport type { CacheConfig } from '../core/types.ts';\n\nconst getByTagScript = `\nlocal tagsMapKey = KEYS[1] -- tags map key\nlocal tag = ARGV[1] -- tag\n\nlocal compositeTableName = redis.call('HGET', tagsMapKey, tag)\nif not compositeTableName then\n return nil\nend\n\nlocal value = redis.call('HGET', compositeTableName, tag)\nreturn value\n`;\n\nconst onMutateScript = `\nlocal tagsMapKey = KEYS[1] -- tags map key\nlocal tables = {} -- initialize tables array\nlocal tags = ARGV -- tags array\n\nfor i = 2, #KEYS do\n tables[#tables + 1] = KEYS[i] -- add all keys except the first one to tables\nend\n\nif #tags > 0 then\n for _, tag in ipairs(tags) do\n if tag ~= nil and tag ~= '' then\n local compositeTableName = redis.call('HGET', tagsMapKey, tag)\n if compositeTableName then\n redis.call('HDEL', compositeTableName, tag)\n end\n end\n end\n redis.call('HDEL', tagsMapKey, unpack(tags))\nend\n\nlocal keysToDelete = {}\n\nif #tables > 0 then\n local compositeTableNames = redis.call('SUNION', unpack(tables))\n for _, compositeTableName in ipairs(compositeTableNames) do\n keysToDelete[#keysToDelete + 1] = compositeTableName\n end\n for _, table in ipairs(tables) do\n keysToDelete[#keysToDelete + 1] = table\n end\n redis.call('DEL', unpack(keysToDelete))\nend\n`;\n\ntype Script = ReturnType<Redis['createScript']>;\n\ntype ExpireOptions = 'NX' | 'nx' | 'XX' | 'xx' | 'GT' | 'gt' | 'LT' | 'lt';\n\nexport class UpstashCache extends Cache {\n\tstatic override readonly [entityKind]: string = 'UpstashCache';\n\tprivate static compositeTableSetPrefix = '__ct__';\n\tprivate static tagsMapKey = '__tagsMap__';\n\n\tprivate globalTtl: number = 1;\n\n\tprivate luaScripts: {\n\t\tgetByTagScript: Script;\n\t\tonMutateScript: Script;\n\t};\n\n\tprivate internalConfig?: { seconds: number; hexOptions: ExpireOptions };\n\n\tconstructor(public redis: Redis, config?: CacheConfig, protected useGlobally?: boolean) {\n\t\tsuper();\n\t\tthis.internalConfig = this.toInternalConfig(config);\n\t\tthis.luaScripts = {\n\t\t\tgetByTagScript: this.redis.createScript(getByTagScript, { readonly: true }),\n\t\t\tonMutateScript: this.redis.createScript(onMutateScript),\n\t\t};\n\t}\n\n\tpublic strategy() {\n\t\treturn this.useGlobally ? 'all' : 'explicit';\n\t}\n\n\tprivate toInternalConfig(config?: CacheConfig) {\n\t\treturn config\n\t\t\t? {\n\t\t\t\tseconds: config.ex,\n\t\t\t\thexOptions: config.hexOptions,\n\t\t\t} as { seconds: number; hexOptions: ExpireOptions }\n\t\t\t: undefined;\n\t}\n\n\toverride async get(key: string, tables: string[], isTag: boolean = false): Promise<any[] | undefined> {\n\t\tif (isTag) {\n\t\t\tconst result = await this.luaScripts.getByTagScript.exec([UpstashCache.tagsMapKey], [key]);\n\t\t\treturn result === null ? undefined : result as any[];\n\t\t}\n\n\t\t// Normal cache lookup for the composite key\n\t\tconst compositeKey = this.getCompositeKey(tables);\n\t\tconst result = await this.redis.hget(compositeKey, key) ?? undefined; // Retrieve result for normal query\n\t\treturn result === null ? undefined : result as any[];\n\t}\n\n\toverride async put(\n\t\tkey: string,\n\t\tresponse: any,\n\t\ttables: string[],\n\t\tisTag: boolean = false,\n\t\tconfig?: CacheConfig,\n\t): Promise<void> {\n\t\tconst pipeline = this.redis.pipeline();\n\t\tconst compositeKey = this.getCompositeKey(tables);\n\t\tconst ttlSeconds = config && config.ex ? config.ex : this.globalTtl;\n\t\tconst hexOptions = config && config.hexOptions ? config.hexOptions : this.internalConfig?.hexOptions;\n\n\t\tpipeline.hset(compositeKey, { [key]: response }); // Store the result with the tag under the composite key\n\t\tpipeline.hexpire(compositeKey, key, ttlSeconds, hexOptions); // Set expiration for the composite key\n\n\t\tif (isTag) {\n\t\t\tpipeline.hset(UpstashCache.tagsMapKey, { [key]: compositeKey }); // Store the tag and its composite key in the map\n\t\t\tpipeline.hexpire(UpstashCache.tagsMapKey, key, ttlSeconds, hexOptions); // Set expiration for the tag\n\t\t}\n\n\t\tfor (const table of tables) {\n\t\t\tpipeline.sadd(this.addTablePrefix(table), compositeKey);\n\t\t}\n\n\t\tawait pipeline.exec();\n\t}\n\n\toverride async onMutate(params: MutationOption) {\n\t\tconst tags = Array.isArray(params.tags) ? params.tags : params.tags ? [params.tags] : [];\n\t\tconst tables = Array.isArray(params.tables) ? params.tables : params.tables ? [params.tables] : [];\n\t\tconst tableNames: string[] = tables.map((table) => is(table, Table) ? table[OriginalName] : table as string);\n\n\t\tconst compositeTableSets = tableNames.map((table) => this.addTablePrefix(table));\n\t\tawait this.luaScripts.onMutateScript.exec([UpstashCache.tagsMapKey, ...compositeTableSets], tags);\n\t}\n\n\tprivate addTablePrefix = (table: string) => `${UpstashCache.compositeTableSetPrefix}${table}`;\n\tprivate getCompositeKey = (tables: string[]) => tables.sort().join(',');\n}\n\nexport function upstashCache(\n\t{ url, token, config, global = false }: { url: string; token: string; config?: CacheConfig; global?: boolean },\n): UpstashCache {\n\tconst redis = new Redis({\n\t\turl,\n\t\ttoken,\n\t});\n\n\treturn new UpstashCache(redis, config, global);\n}\n"],"mappings":"AAAA,SAAS,aAAa;AAEtB,SAAS,aAAa;AACtB,SAAS,YAAY,UAAU;AAC/B,SAAS,cAAc,aAAa;AAGpC,MAAM,iBAAiB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAavB,MAAM,iBAAiB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA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stash/cache.ts"],"sourcesContent":["import { Redis } from '@upstash/redis';\nimport type { MutationOption } from '~/cache/core/index.ts';\nimport { Cache } from '~/cache/core/index.ts';\nimport { entityKind, is } from '~/entity.ts';\nimport { OriginalName, Table } from '~/index.ts';\nimport type { CacheConfig } from '../core/types.ts';\n\nconst getByTagScript = `\nlocal tagsMapKey = KEYS[1] -- tags map key\nlocal tag = ARGV[1] -- tag\n\nlocal compositeTableName = redis.call('HGET', tagsMapKey, tag)\nif not compositeTableName then\n return nil\nend\n\nlocal value = redis.call('HGET', compositeTableName, tag)\nreturn value\n`;\n\nconst onMutateScript = `\nlocal tagsMapKey = KEYS[1] -- tags map key\nlocal tables = {} -- initialize tables array\nlocal tags = ARGV -- tags array\n\nfor i = 2, #KEYS do\n tables[#tables + 1] = KEYS[i] -- add all keys except the first one to tables\nend\n\nif #tags > 0 then\n for _, tag in ipairs(tags) do\n if tag ~= nil and tag ~= '' then\n local compositeTableName = redis.call('HGET', tagsMapKey, tag)\n if compositeTableName then\n redis.call('HDEL', compositeTableName, tag)\n end\n end\n end\n redis.call('HDEL', tagsMapKey, unpack(tags))\nend\n\nlocal keysToDelete = {}\n\nif #tables > 0 then\n local compositeTableNames = redis.call('SUNION', unpack(tables))\n for _, compositeTableName in ipairs(compositeTableNames) do\n keysToDelete[#keysToDelete + 1] = compositeTableName\n end\n for _, table in ipairs(tables) do\n keysToDelete[#keysToDelete + 1] = table\n end\n redis.call('DEL', unpack(keysToDelete))\nend\n`;\n\ntype Script = ReturnType<Redis['createScript']>;\n\ntype ExpireOptions = 'NX' | 'nx' | 'XX' | 'xx' | 'GT' | 'gt' | 'LT' | 'lt';\n\nexport class UpstashCache extends Cache {\n\tstatic override readonly [entityKind]: string = 'UpstashCache';\n\t/**\n\t * Prefix for sets which denote the composite table names for each unique table\n\t *\n\t * Example: In the composite table set of \"table1\", you may find\n\t * `${compositeTablePrefix}table1,table2` and `${compositeTablePrefix}table1,table3`\n\t */\n\tprivate static compositeTableSetPrefix = '__CTS__';\n\t/**\n\t * Prefix for hashes which map hash or tags to cache values\n\t */\n\tprivate static compositeTablePrefix = '__CT__';\n\t/**\n\t * Key which holds the mapping of tags to composite table names\n\t *\n\t * Using this tagsMapKey, you can find the composite table name for a given tag\n\t * and get the cache value for that tag:\n\t *\n\t * ```ts\n\t * const compositeTable = redis.hget(tagsMapKey, 'tag1')\n\t * console.log(compositeTable) // `${compositeTablePrefix}table1,table2`\n\t *\n\t * const cachevalue = redis.hget(compositeTable, 'tag1')\n\t */\n\tprivate static tagsMapKey = '__tagsMap__';\n\t/**\n\t * Queries whose auto invalidation is false aren't stored in their respective\n\t * composite table hashes because those hashes are deleted when a mutation\n\t * occurs on related tables.\n\t *\n\t * Instead, they are stored in a separate hash with the prefix\n\t * `__nonAutoInvalidate__` to prevent them from being deleted when a mutation\n\t */\n\tprivate static nonAutoInvalidateTablePrefix = '__nonAutoInvalidate__';\n\n\tprivate luaScripts: {\n\t\tgetByTagScript: Script;\n\t\tonMutateScript: Script;\n\t};\n\n\tprivate internalConfig: { seconds: number; hexOptions?: ExpireOptions };\n\n\tconstructor(public redis: Redis, config?: CacheConfig, protected useGlobally?: boolean) {\n\t\tsuper();\n\t\tthis.internalConfig = this.toInternalConfig(config);\n\t\tthis.luaScripts = {\n\t\t\tgetByTagScript: this.redis.createScript(getByTagScript, { readonly: true }),\n\t\t\tonMutateScript: this.redis.createScript(onMutateScript),\n\t\t};\n\t}\n\n\tpublic strategy() {\n\t\treturn this.useGlobally ? 'all' : 'explicit';\n\t}\n\n\tprivate toInternalConfig(config?: CacheConfig): { seconds: number; hexOptions?: ExpireOptions } {\n\t\treturn config\n\t\t\t? {\n\t\t\t\tseconds: config.ex!,\n\t\t\t\thexOptions: config.hexOptions,\n\t\t\t}\n\t\t\t: {\n\t\t\t\tseconds: 1,\n\t\t\t};\n\t}\n\n\toverride async get(\n\t\tkey: string,\n\t\ttables: string[],\n\t\tisTag: boolean = false,\n\t\tisAutoInvalidate?: boolean,\n\t): Promise<any[] | undefined> {\n\t\tif (!isAutoInvalidate) {\n\t\t\tconst result = await this.redis.hget(UpstashCache.nonAutoInvalidateTablePrefix, key);\n\t\t\treturn result === null ? undefined : result as any[];\n\t\t}\n\n\t\tif (isTag) {\n\t\t\tconst result = await this.luaScripts.getByTagScript.exec([UpstashCache.tagsMapKey], [key]);\n\t\t\treturn result === null ? undefined : result as any[];\n\t\t}\n\n\t\t// Normal cache lookup for the composite key\n\t\tconst compositeKey = this.getCompositeKey(tables);\n\t\tconst result = await this.redis.hget(compositeKey, key) ?? undefined; // Retrieve result for normal query\n\t\treturn result === null ? undefined : result as any[];\n\t}\n\n\toverride async put(\n\t\tkey: string,\n\t\tresponse: any,\n\t\ttables: string[],\n\t\tisTag: boolean = false,\n\t\tconfig?: CacheConfig,\n\t): Promise<void> {\n\t\tconst isAutoInvalidate = tables.length !== 0;\n\n\t\tconst pipeline = this.redis.pipeline();\n\t\tconst ttlSeconds = config && config.ex ? config.ex : this.internalConfig.seconds;\n\t\tconst hexOptions = config && config.hexOptions ? config.hexOptions : this.internalConfig?.hexOptions;\n\n\t\tif (!isAutoInvalidate) {\n\t\t\tpipeline.hset(UpstashCache.nonAutoInvalidateTablePrefix, { [key]: response });\n\t\t\tpipeline.hexpire(UpstashCache.nonAutoInvalidateTablePrefix, key, ttlSeconds, hexOptions);\n\t\t\tawait pipeline.exec();\n\t\t\treturn;\n\t\t}\n\n\t\tconst compositeKey = this.getCompositeKey(tables);\n\n\t\tpipeline.hset(compositeKey, { [key]: response }); // Store the result with the tag under the composite key\n\t\tpipeline.hexpire(compositeKey, key, ttlSeconds, hexOptions); // Set expiration for the composite key\n\n\t\tif (isTag) {\n\t\t\tpipeline.hset(UpstashCache.tagsMapKey, { [key]: compositeKey }); // Store the tag and its composite key in the map\n\t\t\tpipeline.hexpire(UpstashCache.tagsMapKey, key, ttlSeconds, hexOptions); // Set expiration for the tag\n\t\t}\n\n\t\tfor (const table of tables) {\n\t\t\tpipeline.sadd(this.addTablePrefix(table), compositeKey);\n\t\t}\n\n\t\tawait pipeline.exec();\n\t}\n\n\toverride async onMutate(params: MutationOption) {\n\t\tconst tags = Array.isArray(params.tags) ? params.tags : params.tags ? [params.tags] : [];\n\t\tconst tables = Array.isArray(params.tables) ? params.tables : params.tables ? [params.tables] : [];\n\t\tconst tableNames: string[] = tables.map((table) => is(table, Table) ? table[OriginalName] : table as string);\n\n\t\tconst compositeTableSets = tableNames.map((table) => this.addTablePrefix(table));\n\t\tawait this.luaScripts.onMutateScript.exec([UpstashCache.tagsMapKey, ...compositeTableSets], tags);\n\t}\n\n\tprivate addTablePrefix = (table: string) => `${UpstashCache.compositeTableSetPrefix}${table}`;\n\tprivate getCompositeKey = (tables: string[]) => `${UpstashCache.compositeTablePrefix}${tables.sort().join(',')}`;\n}\n\nexport function upstashCache(\n\t{ url, token, config, global = false }: { url: string; token: string; config?: CacheConfig; global?: boolean },\n): UpstashCache {\n\tconst redis = new Redis({\n\t\turl,\n\t\ttoken,\n\t});\n\n\treturn new UpstashCache(redis, config, global);\n}\n"],"mappings":"AAAA,SAAS,aAAa;AAEtB,SAAS,aAAa;AACtB,SAAS,YAAY,UAAU;AAC/B,SAAS,cAAc,aAAa;AAGpC,MAAM,iBAAiB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAavB,MAAM,iBAAiB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAuChB,MAAM,qBAAqB,MAAM;AAAA,EA2CvC,YAAmB,OAAc,QAAgC,aAAuB;AACvF,UAAM;AADY;AAA8C;AAEhE,SAAK,iBAAiB,KAAK,iBAAiB,MAAM;AAClD,SAAK,aAAa;AAAA,MACjB,gBAAgB,KAAK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,kBAAkB,GAAG,IAAI;AAAA,EACjG;AAAA,EAEQ,iBAAiB,CAAC,UAAkB,GAAG,aAAa,uBAAuB,GAAG,KAAK;AAAA,EACnF,kBAAkB,CAAC,WAAqB,GAAG,aAAa,oBAAoB,GAAG,OAAO,KAAK,EAAE,KAAK,GAAG,CAAC;AAC/G;AAEO,SAAS,aACf,EAAE,KAAK,OAAO,QAAQ,SAAS,MAAM,GACtB;AACf,QAAM,QAAQ,IAAI,MAAM;AAAA,IACvB;AAAA,IACA;AAAA,EACD,CAAC;AAED,SAAO,IAAI,aAAa,OAAO,QAAQ,MAAM;AAC9C;","names":["result"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "drizzle-orm",
3
- "version": "0.43.1-5fb460b",
3
+ "version": "0.43.1-8eb999a",
4
4
  "description": "Drizzle ORM package for SQL databases",
5
5
  "type": "module",
6
6
  "scripts": {
package/version.cjs CHANGED
@@ -26,7 +26,7 @@ __export(version_exports, {
26
26
  module.exports = __toCommonJS(version_exports);
27
27
 
28
28
  // package.json
29
- var version = "0.43.1-5fb460b";
29
+ var version = "0.43.1-8eb999a";
30
30
 
31
31
  // src/version.ts
32
32
  var compatibilityVersion = 10;
package/version.d.cts CHANGED
@@ -1,4 +1,4 @@
1
- var version = "0.43.1-5fb460b";
1
+ var version = "0.43.1-8eb999a";
2
2
 
3
3
  declare const compatibilityVersion = 10;
4
4
 
package/version.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- var version = "0.43.1-5fb460b";
1
+ var version = "0.43.1-8eb999a";
2
2
 
3
3
  declare const compatibilityVersion = 10;
4
4
 
package/version.js CHANGED
@@ -1,5 +1,5 @@
1
1
  // package.json
2
- var version = "0.43.1-5fb460b";
2
+ var version = "0.43.1-8eb999a";
3
3
 
4
4
  // src/version.ts
5
5
  var compatibilityVersion = 10;