ts-cache-mongoose 1.7.0 → 1.7.2
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.
- package/LICENSE +1 -1
- package/README.md +9 -9
- package/biome.json +1 -1
- package/dist/index.cjs +325 -0
- package/dist/{cjs/types/plugin.d.ts → index.d.cts} +22 -4
- package/dist/{esm/types/plugin.d.ts → index.d.mts} +22 -4
- package/dist/index.mjs +323 -0
- package/package.json +37 -32
- package/src/extend/aggregate.ts +2 -2
- package/src/extend/query.ts +2 -2
- package/src/{plugin.ts → index.ts} +2 -0
- package/tests/cache-debug.test.ts +2 -3
- package/tests/cache-memory.test.ts +2 -3
- package/tests/cache-options.test.ts +0 -1
- package/tests/cache-redis.test.ts +11 -13
- package/tests/key.test.ts +2 -1
- package/tsconfig.json +5 -5
- package/vite.config.mts +1 -0
- package/.swcrc +0 -17
- package/dist/cjs/cache/Cache.js +0 -67
- package/dist/cjs/cache/Cache.js.map +0 -1
- package/dist/cjs/cache/engine/MemoryCacheEngine.js +0 -39
- package/dist/cjs/cache/engine/MemoryCacheEngine.js.map +0 -1
- package/dist/cjs/cache/engine/RedisCacheEngine.js +0 -54
- package/dist/cjs/cache/engine/RedisCacheEngine.js.map +0 -1
- package/dist/cjs/extend/aggregate.js +0 -41
- package/dist/cjs/extend/aggregate.js.map +0 -1
- package/dist/cjs/extend/query.js +0 -67
- package/dist/cjs/extend/query.js.map +0 -1
- package/dist/cjs/key.js +0 -14
- package/dist/cjs/key.js.map +0 -1
- package/dist/cjs/plugin.js +0 -36
- package/dist/cjs/plugin.js.map +0 -1
- package/dist/cjs/types/cache/Cache.d.ts +0 -11
- package/dist/cjs/types/cache/Cache.d.ts.map +0 -1
- package/dist/cjs/types/cache/engine/MemoryCacheEngine.d.ts +0 -11
- package/dist/cjs/types/cache/engine/MemoryCacheEngine.d.ts.map +0 -1
- package/dist/cjs/types/cache/engine/RedisCacheEngine.d.ts +0 -12
- package/dist/cjs/types/cache/engine/RedisCacheEngine.d.ts.map +0 -1
- package/dist/cjs/types/extend/aggregate.d.ts +0 -4
- package/dist/cjs/types/extend/aggregate.d.ts.map +0 -1
- package/dist/cjs/types/extend/query.d.ts +0 -4
- package/dist/cjs/types/extend/query.d.ts.map +0 -1
- package/dist/cjs/types/key.d.ts +0 -2
- package/dist/cjs/types/key.d.ts.map +0 -1
- package/dist/cjs/types/plugin.d.ts.map +0 -1
- package/dist/cjs/types/types.d.ts +0 -18
- package/dist/cjs/types/types.d.ts.map +0 -1
- package/dist/cjs/types/version.d.ts +0 -6
- package/dist/cjs/types/version.d.ts.map +0 -1
- package/dist/cjs/types.js +0 -3
- package/dist/cjs/types.js.map +0 -1
- package/dist/cjs/version.js +0 -20
- package/dist/cjs/version.js.map +0 -1
- package/dist/esm/cache/Cache.js +0 -63
- package/dist/esm/cache/Cache.js.map +0 -1
- package/dist/esm/cache/engine/MemoryCacheEngine.js +0 -35
- package/dist/esm/cache/engine/MemoryCacheEngine.js.map +0 -1
- package/dist/esm/cache/engine/RedisCacheEngine.js +0 -50
- package/dist/esm/cache/engine/RedisCacheEngine.js.map +0 -1
- package/dist/esm/extend/aggregate.js +0 -38
- package/dist/esm/extend/aggregate.js.map +0 -1
- package/dist/esm/extend/query.js +0 -64
- package/dist/esm/extend/query.js.map +0 -1
- package/dist/esm/key.js +0 -10
- package/dist/esm/key.js.map +0 -1
- package/dist/esm/package.json +0 -1
- package/dist/esm/plugin.js +0 -34
- package/dist/esm/plugin.js.map +0 -1
- package/dist/esm/types/cache/Cache.d.ts +0 -11
- package/dist/esm/types/cache/Cache.d.ts.map +0 -1
- package/dist/esm/types/cache/engine/MemoryCacheEngine.d.ts +0 -11
- package/dist/esm/types/cache/engine/MemoryCacheEngine.d.ts.map +0 -1
- package/dist/esm/types/cache/engine/RedisCacheEngine.d.ts +0 -12
- package/dist/esm/types/cache/engine/RedisCacheEngine.d.ts.map +0 -1
- package/dist/esm/types/extend/aggregate.d.ts +0 -4
- package/dist/esm/types/extend/aggregate.d.ts.map +0 -1
- package/dist/esm/types/extend/query.d.ts +0 -4
- package/dist/esm/types/extend/query.d.ts.map +0 -1
- package/dist/esm/types/key.d.ts +0 -2
- package/dist/esm/types/key.d.ts.map +0 -1
- package/dist/esm/types/plugin.d.ts.map +0 -1
- package/dist/esm/types/types.d.ts +0 -18
- package/dist/esm/types/types.d.ts.map +0 -1
- package/dist/esm/types/version.d.ts +0 -6
- package/dist/esm/types/version.d.ts.map +0 -1
- package/dist/esm/types.js +0 -2
- package/dist/esm/types.js.map +0 -1
- package/dist/esm/version.js +0 -15
- package/dist/esm/version.js.map +0 -1
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -30,15 +30,15 @@ Caching queries is a good way to improve performance of your application
|
|
|
30
30
|
|
|
31
31
|
## Features
|
|
32
32
|
|
|
33
|
-
-
|
|
34
|
-
-
|
|
35
|
-
-
|
|
36
|
-
-
|
|
37
|
-
-
|
|
38
|
-
-
|
|
39
|
-
-
|
|
40
|
-
-
|
|
41
|
-
-
|
|
33
|
+
- In-memory caching
|
|
34
|
+
- Redis caching
|
|
35
|
+
- Cache expiration
|
|
36
|
+
- Cache invalidation
|
|
37
|
+
- Cache key generation
|
|
38
|
+
- Cache key prefix
|
|
39
|
+
- Query caching
|
|
40
|
+
- Aggregate caching
|
|
41
|
+
- Supports ESM and CommonJS
|
|
42
42
|
|
|
43
43
|
## Installation
|
|
44
44
|
|
package/biome.json
CHANGED
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,325 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var ms = require('ms');
|
|
4
|
+
var bson = require('bson');
|
|
5
|
+
var IORedis = require('ioredis');
|
|
6
|
+
var mongoose = require('mongoose');
|
|
7
|
+
var semver = require('semver');
|
|
8
|
+
var node_crypto = require('node:crypto');
|
|
9
|
+
var sortKeys = require('sort-keys');
|
|
10
|
+
|
|
11
|
+
var __typeError$3 = (msg) => {
|
|
12
|
+
throw TypeError(msg);
|
|
13
|
+
};
|
|
14
|
+
var __accessCheck$3 = (obj, member, msg) => member.has(obj) || __typeError$3("Cannot " + msg);
|
|
15
|
+
var __privateGet$3 = (obj, member, getter) => (__accessCheck$3(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
|
|
16
|
+
var __privateAdd$3 = (obj, member, value) => member.has(obj) ? __typeError$3("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
17
|
+
var __privateSet$3 = (obj, member, value, setter) => (__accessCheck$3(obj, member, "write to private field"), member.set(obj, value), value);
|
|
18
|
+
var _cache;
|
|
19
|
+
class MemoryCacheEngine {
|
|
20
|
+
constructor() {
|
|
21
|
+
__privateAdd$3(this, _cache);
|
|
22
|
+
__privateSet$3(this, _cache, /* @__PURE__ */ new Map());
|
|
23
|
+
}
|
|
24
|
+
get(key) {
|
|
25
|
+
const item = __privateGet$3(this, _cache).get(key);
|
|
26
|
+
if (!item || item.expiresAt < Date.now()) {
|
|
27
|
+
this.del(key);
|
|
28
|
+
return void 0;
|
|
29
|
+
}
|
|
30
|
+
return item.value;
|
|
31
|
+
}
|
|
32
|
+
set(key, value, ttl) {
|
|
33
|
+
const givenTTL = typeof ttl === "string" ? ms(ttl) : ttl;
|
|
34
|
+
const actualTTL = givenTTL ?? Number.POSITIVE_INFINITY;
|
|
35
|
+
__privateGet$3(this, _cache).set(key, {
|
|
36
|
+
value,
|
|
37
|
+
expiresAt: Date.now() + actualTTL
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
del(key) {
|
|
41
|
+
__privateGet$3(this, _cache).delete(key);
|
|
42
|
+
}
|
|
43
|
+
clear() {
|
|
44
|
+
__privateGet$3(this, _cache).clear();
|
|
45
|
+
}
|
|
46
|
+
close() {
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
_cache = new WeakMap();
|
|
50
|
+
|
|
51
|
+
const isMongooseLessThan7 = semver.satisfies(mongoose.version, "<7");
|
|
52
|
+
const convertToObject = (value) => {
|
|
53
|
+
if (isMongooseLessThan7) {
|
|
54
|
+
if (value != null && typeof value === "object" && !Array.isArray(value) && value.toObject) {
|
|
55
|
+
return value.toObject();
|
|
56
|
+
}
|
|
57
|
+
if (Array.isArray(value)) {
|
|
58
|
+
return value.map((doc) => convertToObject(doc));
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return value;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
var __typeError$2 = (msg) => {
|
|
65
|
+
throw TypeError(msg);
|
|
66
|
+
};
|
|
67
|
+
var __accessCheck$2 = (obj, member, msg) => member.has(obj) || __typeError$2("Cannot " + msg);
|
|
68
|
+
var __privateGet$2 = (obj, member, getter) => (__accessCheck$2(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
|
|
69
|
+
var __privateAdd$2 = (obj, member, value) => member.has(obj) ? __typeError$2("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
70
|
+
var __privateSet$2 = (obj, member, value, setter) => (__accessCheck$2(obj, member, "write to private field"), member.set(obj, value), value);
|
|
71
|
+
var _client;
|
|
72
|
+
class RedisCacheEngine {
|
|
73
|
+
constructor(options) {
|
|
74
|
+
__privateAdd$2(this, _client);
|
|
75
|
+
if (!options.keyPrefix) {
|
|
76
|
+
options.keyPrefix = "cache-mongoose:";
|
|
77
|
+
}
|
|
78
|
+
__privateSet$2(this, _client, new IORedis(options));
|
|
79
|
+
}
|
|
80
|
+
async get(key) {
|
|
81
|
+
try {
|
|
82
|
+
const value = await __privateGet$2(this, _client).get(key);
|
|
83
|
+
if (value === null) {
|
|
84
|
+
return void 0;
|
|
85
|
+
}
|
|
86
|
+
return bson.EJSON.parse(value);
|
|
87
|
+
} catch (err) {
|
|
88
|
+
console.error(err);
|
|
89
|
+
return void 0;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
async set(key, value, ttl) {
|
|
93
|
+
try {
|
|
94
|
+
const givenTTL = typeof ttl === "string" ? ms(ttl) : ttl;
|
|
95
|
+
const actualTTL = givenTTL ?? Number.POSITIVE_INFINITY;
|
|
96
|
+
const serializedValue = bson.EJSON.stringify(convertToObject(value));
|
|
97
|
+
await __privateGet$2(this, _client).setex(key, Math.ceil(actualTTL / 1e3), serializedValue);
|
|
98
|
+
} catch (err) {
|
|
99
|
+
console.error(err);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
async del(key) {
|
|
103
|
+
await __privateGet$2(this, _client).del(key);
|
|
104
|
+
}
|
|
105
|
+
async clear() {
|
|
106
|
+
await __privateGet$2(this, _client).flushdb();
|
|
107
|
+
}
|
|
108
|
+
async close() {
|
|
109
|
+
await __privateGet$2(this, _client).quit();
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
_client = new WeakMap();
|
|
113
|
+
|
|
114
|
+
var __typeError$1 = (msg) => {
|
|
115
|
+
throw TypeError(msg);
|
|
116
|
+
};
|
|
117
|
+
var __accessCheck$1 = (obj, member, msg) => member.has(obj) || __typeError$1("Cannot " + msg);
|
|
118
|
+
var __privateGet$1 = (obj, member, getter) => (__accessCheck$1(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
|
|
119
|
+
var __privateAdd$1 = (obj, member, value) => member.has(obj) ? __typeError$1("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
120
|
+
var __privateSet$1 = (obj, member, value, setter) => (__accessCheck$1(obj, member, "write to private field"), member.set(obj, value), value);
|
|
121
|
+
var _engine, _defaultTTL, _debug, _engines;
|
|
122
|
+
class Cache {
|
|
123
|
+
constructor(cacheOptions) {
|
|
124
|
+
__privateAdd$1(this, _engine);
|
|
125
|
+
__privateAdd$1(this, _defaultTTL);
|
|
126
|
+
__privateAdd$1(this, _debug);
|
|
127
|
+
__privateAdd$1(this, _engines, ["memory", "redis"]);
|
|
128
|
+
if (!__privateGet$1(this, _engines).includes(cacheOptions.engine)) {
|
|
129
|
+
throw new Error(`Invalid engine name: ${cacheOptions.engine}`);
|
|
130
|
+
}
|
|
131
|
+
if (cacheOptions.engine === "redis" && !cacheOptions.engineOptions) {
|
|
132
|
+
throw new Error(`Engine options are required for ${cacheOptions.engine} engine`);
|
|
133
|
+
}
|
|
134
|
+
if (!cacheOptions.defaultTTL) {
|
|
135
|
+
cacheOptions.defaultTTL = "1 minute";
|
|
136
|
+
}
|
|
137
|
+
__privateSet$1(this, _defaultTTL, typeof cacheOptions.defaultTTL === "string" ? ms(cacheOptions.defaultTTL) : cacheOptions.defaultTTL);
|
|
138
|
+
if (cacheOptions.engine === "redis" && cacheOptions.engineOptions) {
|
|
139
|
+
__privateSet$1(this, _engine, new RedisCacheEngine(cacheOptions.engineOptions));
|
|
140
|
+
}
|
|
141
|
+
if (cacheOptions.engine === "memory") {
|
|
142
|
+
__privateSet$1(this, _engine, new MemoryCacheEngine());
|
|
143
|
+
}
|
|
144
|
+
__privateSet$1(this, _debug, cacheOptions.debug === true);
|
|
145
|
+
}
|
|
146
|
+
async get(key) {
|
|
147
|
+
const cacheEntry = await __privateGet$1(this, _engine).get(key);
|
|
148
|
+
if (__privateGet$1(this, _debug)) {
|
|
149
|
+
const cacheHit = cacheEntry != null ? "HIT" : "MISS";
|
|
150
|
+
console.log(`[ts-cache-mongoose] GET '${key}' - ${cacheHit}`);
|
|
151
|
+
}
|
|
152
|
+
return cacheEntry;
|
|
153
|
+
}
|
|
154
|
+
async set(key, value, ttl) {
|
|
155
|
+
const givenTTL = typeof ttl === "string" ? ms(ttl) : ttl;
|
|
156
|
+
const actualTTL = givenTTL ?? __privateGet$1(this, _defaultTTL);
|
|
157
|
+
await __privateGet$1(this, _engine).set(key, value, actualTTL);
|
|
158
|
+
if (__privateGet$1(this, _debug)) {
|
|
159
|
+
console.log(`[ts-cache-mongoose] SET '${key}' - ttl: ${actualTTL.toFixed(0)} ms`);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
async del(key) {
|
|
163
|
+
await __privateGet$1(this, _engine).del(key);
|
|
164
|
+
if (__privateGet$1(this, _debug)) {
|
|
165
|
+
console.log(`[ts-cache-mongoose] DEL '${key}'`);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
async clear() {
|
|
169
|
+
await __privateGet$1(this, _engine).clear();
|
|
170
|
+
if (__privateGet$1(this, _debug)) {
|
|
171
|
+
console.log("[ts-cache-mongoose] CLEAR");
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
async close() {
|
|
175
|
+
return __privateGet$1(this, _engine).close();
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
_engine = new WeakMap();
|
|
179
|
+
_defaultTTL = new WeakMap();
|
|
180
|
+
_debug = new WeakMap();
|
|
181
|
+
_engines = new WeakMap();
|
|
182
|
+
|
|
183
|
+
function getKey(data) {
|
|
184
|
+
const sortedObj = sortKeys(data, { deep: true });
|
|
185
|
+
const sortedStr = JSON.stringify(sortedObj, (_, val) => {
|
|
186
|
+
return val instanceof RegExp ? String(val) : val;
|
|
187
|
+
});
|
|
188
|
+
return node_crypto.createHash("sha1").update(sortedStr).digest("hex");
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function extendAggregate(mongoose, cache) {
|
|
192
|
+
const mongooseExec = mongoose.Aggregate.prototype.exec;
|
|
193
|
+
mongoose.Aggregate.prototype.getCacheKey = function() {
|
|
194
|
+
if (this._key != null) return this._key;
|
|
195
|
+
return getKey({
|
|
196
|
+
pipeline: this.pipeline()
|
|
197
|
+
});
|
|
198
|
+
};
|
|
199
|
+
mongoose.Aggregate.prototype.getCacheTTL = function() {
|
|
200
|
+
return this._ttl;
|
|
201
|
+
};
|
|
202
|
+
mongoose.Aggregate.prototype.cache = function(ttl, customKey) {
|
|
203
|
+
this._ttl = ttl ?? null;
|
|
204
|
+
this._key = customKey ?? null;
|
|
205
|
+
return this;
|
|
206
|
+
};
|
|
207
|
+
mongoose.Aggregate.prototype.exec = async function(...args) {
|
|
208
|
+
if (!Object.prototype.hasOwnProperty.call(this, "_ttl")) {
|
|
209
|
+
return mongooseExec.apply(this, args);
|
|
210
|
+
}
|
|
211
|
+
const key = this.getCacheKey();
|
|
212
|
+
const ttl = this.getCacheTTL();
|
|
213
|
+
const resultCache = await cache.get(key).catch((err) => {
|
|
214
|
+
console.error(err);
|
|
215
|
+
});
|
|
216
|
+
if (resultCache) {
|
|
217
|
+
return resultCache;
|
|
218
|
+
}
|
|
219
|
+
const result = await mongooseExec.call(this);
|
|
220
|
+
await cache.set(key, result, ttl).catch((err) => {
|
|
221
|
+
console.error(err);
|
|
222
|
+
});
|
|
223
|
+
return result;
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
function extendQuery(mongoose, cache) {
|
|
228
|
+
const mongooseExec = mongoose.Query.prototype.exec;
|
|
229
|
+
mongoose.Query.prototype.getCacheKey = function() {
|
|
230
|
+
if (this._key != null) return this._key;
|
|
231
|
+
const filter = this.getFilter();
|
|
232
|
+
const update = this.getUpdate();
|
|
233
|
+
const options = this.getOptions();
|
|
234
|
+
const mongooseOptions = this.mongooseOptions();
|
|
235
|
+
return getKey({
|
|
236
|
+
model: this.model.modelName,
|
|
237
|
+
op: this.op,
|
|
238
|
+
filter,
|
|
239
|
+
update,
|
|
240
|
+
options,
|
|
241
|
+
mongooseOptions,
|
|
242
|
+
_path: this._path,
|
|
243
|
+
_fields: this._fields,
|
|
244
|
+
_distinct: this._distinct,
|
|
245
|
+
_conditions: this._conditions
|
|
246
|
+
});
|
|
247
|
+
};
|
|
248
|
+
mongoose.Query.prototype.getCacheTTL = function() {
|
|
249
|
+
return this._ttl;
|
|
250
|
+
};
|
|
251
|
+
mongoose.Query.prototype.cache = function(ttl, customKey) {
|
|
252
|
+
this._ttl = ttl ?? null;
|
|
253
|
+
this._key = customKey ?? null;
|
|
254
|
+
return this;
|
|
255
|
+
};
|
|
256
|
+
mongoose.Query.prototype.exec = async function(...args) {
|
|
257
|
+
if (!Object.prototype.hasOwnProperty.call(this, "_ttl")) {
|
|
258
|
+
return mongooseExec.apply(this, args);
|
|
259
|
+
}
|
|
260
|
+
const key = this.getCacheKey();
|
|
261
|
+
const ttl = this.getCacheTTL();
|
|
262
|
+
const mongooseOptions = this.mongooseOptions();
|
|
263
|
+
const isCount = this.op?.includes("count") ?? false;
|
|
264
|
+
const isDistinct = this.op === "distinct";
|
|
265
|
+
const model = this.model.modelName;
|
|
266
|
+
const resultCache = await cache.get(key).catch((err) => {
|
|
267
|
+
console.error(err);
|
|
268
|
+
});
|
|
269
|
+
if (resultCache) {
|
|
270
|
+
if (isCount || isDistinct || mongooseOptions.lean) {
|
|
271
|
+
return resultCache;
|
|
272
|
+
}
|
|
273
|
+
const modelConstructor = mongoose.model(model);
|
|
274
|
+
if (Array.isArray(resultCache)) {
|
|
275
|
+
return resultCache.map((item) => {
|
|
276
|
+
return modelConstructor.hydrate(item);
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
return modelConstructor.hydrate(resultCache);
|
|
280
|
+
}
|
|
281
|
+
const result = await mongooseExec.call(this);
|
|
282
|
+
await cache.set(key, result, ttl).catch((err) => {
|
|
283
|
+
console.error(err);
|
|
284
|
+
});
|
|
285
|
+
return result;
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
var __typeError = (msg) => {
|
|
290
|
+
throw TypeError(msg);
|
|
291
|
+
};
|
|
292
|
+
var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
|
|
293
|
+
var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
|
|
294
|
+
var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
295
|
+
var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), member.set(obj, value), value);
|
|
296
|
+
var _instance;
|
|
297
|
+
const _CacheMongoose = class _CacheMongoose {
|
|
298
|
+
constructor() {
|
|
299
|
+
}
|
|
300
|
+
static init(mongoose, cacheOptions) {
|
|
301
|
+
if (!__privateGet(_CacheMongoose, _instance)) {
|
|
302
|
+
__privateSet(_CacheMongoose, _instance, new _CacheMongoose());
|
|
303
|
+
__privateGet(_CacheMongoose, _instance).cache = new Cache(cacheOptions);
|
|
304
|
+
const cache = __privateGet(_CacheMongoose, _instance).cache;
|
|
305
|
+
extendQuery(mongoose, cache);
|
|
306
|
+
extendAggregate(mongoose, cache);
|
|
307
|
+
}
|
|
308
|
+
return __privateGet(_CacheMongoose, _instance);
|
|
309
|
+
}
|
|
310
|
+
async clear(customKey) {
|
|
311
|
+
if (customKey != null) {
|
|
312
|
+
await this.cache.del(customKey);
|
|
313
|
+
} else {
|
|
314
|
+
await this.cache.clear();
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
async close() {
|
|
318
|
+
await this.cache.close();
|
|
319
|
+
}
|
|
320
|
+
};
|
|
321
|
+
_instance = new WeakMap();
|
|
322
|
+
__privateAdd(_CacheMongoose, _instance);
|
|
323
|
+
let CacheMongoose = _CacheMongoose;
|
|
324
|
+
|
|
325
|
+
module.exports = CacheMongoose;
|
|
@@ -1,5 +1,23 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
1
|
+
import { Mongoose } from 'mongoose';
|
|
2
|
+
import { RedisOptions } from 'ioredis';
|
|
3
|
+
import { StringValue } from 'ms';
|
|
4
|
+
|
|
5
|
+
type CacheTTL = number | StringValue;
|
|
6
|
+
type CacheData = Record<string, unknown> | Record<string, unknown>[] | unknown[] | number | undefined;
|
|
7
|
+
type CacheOptions = {
|
|
8
|
+
engine: 'memory' | 'redis';
|
|
9
|
+
engineOptions?: RedisOptions;
|
|
10
|
+
defaultTTL?: CacheTTL;
|
|
11
|
+
debug?: boolean;
|
|
12
|
+
};
|
|
13
|
+
interface CacheEngine {
|
|
14
|
+
get: (key: string) => Promise<CacheData> | CacheData;
|
|
15
|
+
set: (key: string, value: CacheData, ttl?: CacheTTL) => Promise<void> | void;
|
|
16
|
+
del: (key: string) => Promise<void> | void;
|
|
17
|
+
clear: () => Promise<void> | void;
|
|
18
|
+
close: () => Promise<void> | void;
|
|
19
|
+
}
|
|
20
|
+
|
|
3
21
|
declare module 'mongoose' {
|
|
4
22
|
interface Query<ResultType, DocType, THelpers, RawDocType> {
|
|
5
23
|
cache: (this: Query<ResultType, DocType, THelpers, RawDocType>, ttl?: CacheTTL, customKey?: string) => this;
|
|
@@ -29,5 +47,5 @@ declare class CacheMongoose {
|
|
|
29
47
|
clear(customKey?: string): Promise<void>;
|
|
30
48
|
close(): Promise<void>;
|
|
31
49
|
}
|
|
32
|
-
|
|
33
|
-
|
|
50
|
+
|
|
51
|
+
export { type CacheData, type CacheEngine, type CacheOptions, type CacheTTL, CacheMongoose as default };
|
|
@@ -1,5 +1,23 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
1
|
+
import { Mongoose } from 'mongoose';
|
|
2
|
+
import { RedisOptions } from 'ioredis';
|
|
3
|
+
import { StringValue } from 'ms';
|
|
4
|
+
|
|
5
|
+
type CacheTTL = number | StringValue;
|
|
6
|
+
type CacheData = Record<string, unknown> | Record<string, unknown>[] | unknown[] | number | undefined;
|
|
7
|
+
type CacheOptions = {
|
|
8
|
+
engine: 'memory' | 'redis';
|
|
9
|
+
engineOptions?: RedisOptions;
|
|
10
|
+
defaultTTL?: CacheTTL;
|
|
11
|
+
debug?: boolean;
|
|
12
|
+
};
|
|
13
|
+
interface CacheEngine {
|
|
14
|
+
get: (key: string) => Promise<CacheData> | CacheData;
|
|
15
|
+
set: (key: string, value: CacheData, ttl?: CacheTTL) => Promise<void> | void;
|
|
16
|
+
del: (key: string) => Promise<void> | void;
|
|
17
|
+
clear: () => Promise<void> | void;
|
|
18
|
+
close: () => Promise<void> | void;
|
|
19
|
+
}
|
|
20
|
+
|
|
3
21
|
declare module 'mongoose' {
|
|
4
22
|
interface Query<ResultType, DocType, THelpers, RawDocType> {
|
|
5
23
|
cache: (this: Query<ResultType, DocType, THelpers, RawDocType>, ttl?: CacheTTL, customKey?: string) => this;
|
|
@@ -29,5 +47,5 @@ declare class CacheMongoose {
|
|
|
29
47
|
clear(customKey?: string): Promise<void>;
|
|
30
48
|
close(): Promise<void>;
|
|
31
49
|
}
|
|
32
|
-
|
|
33
|
-
|
|
50
|
+
|
|
51
|
+
export { type CacheData, type CacheEngine, type CacheOptions, type CacheTTL, CacheMongoose as default };
|