UNPKG

2.75 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 global: options.isGlobal,
28 providers: [{ provide: cache_constants_1.CACHE_MODULE_OPTIONS, useValue: options }],
29 };
30 }
31 /**
32 * Configure the cache manager dynamically.
33 *
34 * @param options method for dynamically supplying cache manager configuration
35 * options
36 *
37 * @see [Async configuration](https://docs.nestjs.com/techniques/caching#async-configuration)
38 */
39 static registerAsync(options) {
40 return {
41 module: CacheModule_1,
42 global: options.isGlobal,
43 imports: options.imports,
44 providers: [
45 ...this.createAsyncProviders(options),
46 ...(options.extraProviders || []),
47 ],
48 };
49 }
50 static createAsyncProviders(options) {
51 if (options.useExisting || options.useFactory) {
52 return [this.createAsyncOptionsProvider(options)];
53 }
54 return [
55 this.createAsyncOptionsProvider(options),
56 {
57 provide: options.useClass,
58 useClass: options.useClass,
59 },
60 ];
61 }
62 static createAsyncOptionsProvider(options) {
63 if (options.useFactory) {
64 return {
65 provide: cache_constants_1.CACHE_MODULE_OPTIONS,
66 useFactory: options.useFactory,
67 inject: options.inject || [],
68 };
69 }
70 return {
71 provide: cache_constants_1.CACHE_MODULE_OPTIONS,
72 useFactory: async (optionsFactory) => optionsFactory.createCacheOptions(),
73 inject: [options.useExisting || options.useClass],
74 };
75 }
76};
77CacheModule = CacheModule_1 = tslib_1.__decorate([
78 (0, decorators_1.Module)({
79 providers: [(0, cache_providers_1.createCacheManager)()],
80 exports: [cache_constants_1.CACHE_MANAGER],
81 })
82], CacheModule);
83exports.CacheModule = CacheModule;