UNPKG

2.67 kBJavaScriptView Raw
1"use strict";
2var CacheModule_1;
3Object.defineProperty(exports, "__esModule", { value: true });
4exports.CacheModule = void 0;
5const tslib_1 = require("tslib");
6const decorators_1 = require("../decorators");
7const cache_constants_1 = require("./cache.constants");
8const cache_providers_1 = require("./cache.providers");
9/**
10 * Module that provides Nest cache-manager.
11 *
12 * @see [Caching](https://docs.nestjs.com/techniques/caching)
13 *
14 * @publicApi
15 */
16let CacheModule = CacheModule_1 = class CacheModule {
17 /**
18 * Configure the cache manager statically.
19 *
20 * @param options options to configure the cache manager
21 *
22 * @see [Customize caching](https://docs.nestjs.com/techniques/caching#customize-caching)
23 */
24 static register(options = {}) {
25 return {
26 module: CacheModule_1,
27 providers: [{ provide: cache_constants_1.CACHE_MODULE_OPTIONS, useValue: options }],
28 };
29 }
30 /**
31 * Configure the cache manager dynamically.
32 *
33 * @param options method for dynamically supplying cache manager configuration
34 * options
35 *
36 * @see [Async configuration](https://docs.nestjs.com/techniques/caching#async-configuration)
37 */
38 static registerAsync(options) {
39 return {
40 module: CacheModule_1,
41 imports: options.imports,
42 providers: [
43 ...this.createAsyncProviders(options),
44 ...(options.extraProviders || []),
45 ],
46 };
47 }
48 static createAsyncProviders(options) {
49 if (options.useExisting || options.useFactory) {
50 return [this.createAsyncOptionsProvider(options)];
51 }
52 return [
53 this.createAsyncOptionsProvider(options),
54 {
55 provide: options.useClass,
56 useClass: options.useClass,
57 },
58 ];
59 }
60 static createAsyncOptionsProvider(options) {
61 if (options.useFactory) {
62 return {
63 provide: cache_constants_1.CACHE_MODULE_OPTIONS,
64 useFactory: options.useFactory,
65 inject: options.inject || [],
66 };
67 }
68 return {
69 provide: cache_constants_1.CACHE_MODULE_OPTIONS,
70 useFactory: async (optionsFactory) => optionsFactory.createCacheOptions(),
71 inject: [options.useExisting || options.useClass],
72 };
73 }
74};
75CacheModule = CacheModule_1 = tslib_1.__decorate([
76 decorators_1.Module({
77 providers: [cache_providers_1.createCacheManager()],
78 exports: [cache_constants_1.CACHE_MANAGER],
79 })
80], CacheModule);
81exports.CacheModule = CacheModule;