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,IAAI,MAElF,EAAO,IAAI,uBAAuB,oBAAwB,KAC1D,EAAO,MAAM,cAAe,SACtB,EAAG,IAAI,CAAE,QAAO,YAAW,QAAQ,EAC7C,CACA,OAAO,CACX,EAEJ,OAAO,EAAO,SAAS,CACnB,KAAM,eACN,MAAO,CACH,WAAY,CACR,gBAAM,CAAW,GACb,OAAO,EAAe,cAAc,KAAK,KAAM,EACnD,EACA,uBAAM,CAAkB,GACpB,OAAO,EAAe,qBAAqB,KAAK,KAAM,EAC1D,EACA,eAAM,CAAU,GACZ,OAAO,EAAe,aAAa,KAAK,KAAM,EAClD,EACA,sBAAM,CAAiB,GACnB,OAAO,EAAe,oBAAoB,KAAK,KAAM,EACzD,EACA,cAAM,CAAS,GACX,OAAO,EAAe,YAAY,KAAK,KAAM,EACjD,EACA,WAAM,CAAM,GACR,OAAO,EAAe,SAAS,KAAK,KAAM,EAC9C,EACA,eAAM,CAAU,GACZ,OAAO,EAAe,aAAa,KAAK,KAAM,EAClD,EACA,aAAM,CAAQ,GACV,OAAO,EAAe,WAAW,KAAK,KAAM,EAChD,EACA,gBAAM,GACF,MACM,EADU,OAAO,oBAAoB,MACrB,MACtB,IAAK,EAED,MADA,EAAO,MAAM,8BACP,IAAI,MAAM,8BAEpB,OAAO,EAAG,MAAM,EACpB,IAGR,OAAQ,CACJ,WAAgB,SACL,EAAG,aAGpB,GAEV,QAES,mBAAoB","file":"/npm/prisma-cache-nosql@1.0.9/dist/index.js","sourceRoot":"","sourcesContent":["/**\n * Minified by jsDelivr using Terser v5.39.0.\n * Original file: /npm/prisma-cache-nosql@1.0.9/dist/index.js\n *\n * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files\n */\n","import { Logger } from './utils/logger.js';\nimport { Prisma } from '@prisma/client';\nimport { DefaultCacheConfig } from './types.js';\nexport { SupportedOperations } from './types.js';\nexport { adapterAceBase } from './storage/acebase.js';\nexport { adapterMemory } from './storage/memory.js';\n\nfunction cache(opts) {\n    const logger = new Logger(opts?.logLevel);\n    const db = opts.adapter({ logger });\n    return Prisma.defineExtension((client) => {\n        /**\n         * Call the base prisma client method.\n         * @param model The model name\n         * @param operation The operation to perform\n         * @returns The base result.\n         */\n        const callBaseMethod = async (model, operation, args) => {\n            // eslint-disable-next-line @typescript-eslint/no-explicit-any\n            return client[model][operation](args);\n        };\n        /**\n         * Overwrite a prisma client method with caching.\n         * @param operation The operation to perform\n         * @returns The overwritten method.\n         */\n        const overrideMethod = (operation) => {\n            return async function (args) {\n                const context = Prisma.getExtensionContext(this);\n                const model = context.$name;\n                if (!model) {\n                    logger.error(`context.$name is undefined`);\n                    throw new Error('context.$name is undefined');\n                }\n                let queryCacheArgs = undefined;\n                if (typeof args === 'object' && args !== null && !Array.isArray(args)) {\n                    // If we don't do this TypeScript complains about wrong types\n                    // Maybe there is a better way.\n                    const _args = args;\n                    queryCacheArgs = _args.cache ?? undefined;\n                    delete _args.cache;\n                }\n                const cacheConfig = queryCacheArgs ?? opts.models?.[model] ?? opts.default ?? DefaultCacheConfig;\n                logger.debug(`Cache config for model ${model} with operation ${operation}: `, cacheConfig);\n                if (cacheConfig.get) {\n                    const cached = await db.get({ model, operation, args });\n                    const useCache = cached &&\n                        (cached._cache.expires_at === false || cached._cache.expires_at > Date.now()) &&\n                        (cacheConfig.get === true ||\n                            cached._cache.cached_at + cacheConfig.get.max > Date.now());\n                    if (useCache) {\n                        logger.log(`Found cached value for model ${model} with operation ${operation}`);\n                        logger.debug(`Found cached value with age ${(new Date().getTime() - cached._cache.cached_at).toFixed(0)}ms: `, cached);\n                        return cached.result;\n                    }\n                }\n                const base = await callBaseMethod(model, operation, args);\n                if (cacheConfig.set) {\n                    const cached_at = Date.now();\n                    const expires_at = cacheConfig.set === true ? false : cached_at + cacheConfig.set.ttl;\n                    const pack = { result: base, _cache: { cached_at, expires_at } };\n                    logger.log(`Set cache for model ${model} with operation ${operation}`);\n                    logger.debug(`Set cache: `, pack);\n                    await db.set({ model, operation, args }, pack);\n                }\n                return base;\n            };\n        };\n        return client.$extends({\n            name: 'prisma-cache',\n            model: {\n                $allModels: {\n                    async findUnique(args) {\n                        return overrideMethod('findUnique').call(this, args);\n                    },\n                    async findUniqueOrThrow(args) {\n                        return overrideMethod('findUniqueOrThrow').call(this, args);\n                    },\n                    async findFirst(args) {\n                        return overrideMethod('findFirst').call(this, args);\n                    },\n                    async findFirstOrThrow(args) {\n                        return overrideMethod('findFirstOrThrow').call(this, args);\n                    },\n                    async findMany(args) {\n                        return overrideMethod('findMany').call(this, args);\n                    },\n                    async count(args) {\n                        return overrideMethod('count').call(this, args);\n                    },\n                    async aggregate(args) {\n                        return overrideMethod('aggregate').call(this, args);\n                    },\n                    async groupBy(args) {\n                        return overrideMethod('groupBy').call(this, args);\n                    },\n                    async clearCache() {\n                        const context = Prisma.getExtensionContext(this);\n                        const model = context.$name;\n                        if (!model) {\n                            logger.error(`context.$name is undefined`);\n                            throw new Error('context.$name is undefined');\n                        }\n                        return db.clear(model);\n                    }\n                }\n            },\n            client: {\n                async clearCache() {\n                    return db.clearAll();\n                }\n            }\n        });\n    });\n}\n\nexport { DefaultCacheConfig, cache };\n\n"]}