@credo-ts/core
Version:
<p align="center"> <br /> <img alt="Credo Logo" src="https://github.com/openwallet-foundation/credo-ts/blob/c7886cb8377ceb8ee4efe8d264211e561a75072d/images/credo-logo.png" height="250px" /> </p> <h1 align="center"><b>Credo - Core</b></h1
1 lines • 5.17 kB
Source Map (JSON)
{"version":3,"file":"CachedStorageService.mjs","names":[],"sources":["../../../src/modules/cache/CachedStorageService.ts"],"sourcesContent":["import { AgentContext } from '../../agent'\nimport { InjectionSymbols } from '../../constants'\nimport { inject, injectable } from '../../plugins'\nimport { BaseRecord } from '../../storage/BaseRecord'\nimport type { BaseRecordConstructor, Query, QueryOptions, StorageService } from '../../storage/StorageService'\nimport { JsonTransformer } from '../../utils'\nimport { CacheModuleConfig } from './CacheModuleConfig'\n\n@injectable()\n// biome-ignore lint/suspicious/noExplicitAny: no explanation\nexport class CachedStorageService<T extends BaseRecord<any, any, any>> implements StorageService<T> {\n public readonly supportsCursorPagination = false\n\n public constructor(@inject(InjectionSymbols.StorageService) private storageService: StorageService<T>) {}\n\n private cache(agentContext: AgentContext) {\n return agentContext.resolve(CacheModuleConfig).cache\n }\n\n private getCacheKey(options: { type: string; id: string }) {\n return `${options.type}:${options.id}`\n }\n\n public async save(agentContext: AgentContext, record: T): Promise<void> {\n if (record.allowCache) {\n await this.cache(agentContext).set(agentContext, this.getCacheKey(record), record.toJSON())\n }\n\n return await this.storageService.save(agentContext, record)\n }\n\n public async update(agentContext: AgentContext, record: T): Promise<void> {\n if (record.allowCache) {\n await this.cache(agentContext).set(agentContext, this.getCacheKey(record), record.toJSON())\n }\n\n return await this.storageService.update(agentContext, record)\n }\n\n public async delete(agentContext: AgentContext, record: T): Promise<void> {\n if (record.allowCache) {\n await this.cache(agentContext).remove(agentContext, this.getCacheKey(record))\n }\n return await this.storageService.delete(agentContext, record)\n }\n\n public async deleteById(\n agentContext: AgentContext,\n recordClass: BaseRecordConstructor<T>,\n id: string\n ): Promise<void> {\n if (recordClass.allowCache) {\n await this.cache(agentContext).remove(agentContext, this.getCacheKey({ ...recordClass, id }))\n }\n return await this.storageService.deleteById(agentContext, recordClass, id)\n }\n\n public async getById(agentContext: AgentContext, recordClass: BaseRecordConstructor<T>, id: string): Promise<T> {\n if (recordClass.allowCache) {\n const cachedValue = await this.cache(agentContext).get<T>(\n agentContext,\n this.getCacheKey({ type: recordClass.type, id })\n )\n\n if (cachedValue) return JsonTransformer.fromJSON<T>(cachedValue, recordClass)\n }\n\n const record = await this.storageService.getById(agentContext, recordClass, id)\n\n if (recordClass.allowCache) {\n await this.cache(agentContext).set(\n agentContext,\n this.getCacheKey({ type: recordClass.type, id }),\n record.toJSON()\n )\n }\n\n return record\n }\n\n // TODO: not in caching interface, yet\n public async getAll(agentContext: AgentContext, recordClass: BaseRecordConstructor<T>): Promise<T[]> {\n return await this.storageService.getAll(agentContext, recordClass)\n }\n\n // TODO: not in caching interface, yet\n public async findByQuery(\n agentContext: AgentContext,\n recordClass: BaseRecordConstructor<T>,\n query: Query<T>,\n queryOptions?: QueryOptions\n ): Promise<T[]> {\n return await this.storageService.findByQuery(agentContext, recordClass, query, queryOptions)\n }\n}\n"],"mappings":";;;;;;;;;;;AAUO,IAAM,uBAAN,MAAM,qBAAuF;CAGlG,YAAmB,gBAAoF;EAAnC,KAAA,iBAAA;EAFpE,KAAgB,2BAA2B;CAE6D;CAExG,MAAc,cAA4B;EACxC,OAAO,aAAa,QAAQ,iBAAiB,CAAC,CAAC;CACjD;CAEA,YAAoB,SAAuC;EACzD,OAAO,GAAG,QAAQ,KAAK,GAAG,QAAQ;CACpC;CAEA,MAAa,KAAK,cAA4B,QAA0B;EACtE,IAAI,OAAO,YACT,MAAM,KAAK,MAAM,YAAY,CAAC,CAAC,IAAI,cAAc,KAAK,YAAY,MAAM,GAAG,OAAO,OAAO,CAAC;EAG5F,OAAO,MAAM,KAAK,eAAe,KAAK,cAAc,MAAM;CAC5D;CAEA,MAAa,OAAO,cAA4B,QAA0B;EACxE,IAAI,OAAO,YACT,MAAM,KAA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