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/dist/index.mjs
ADDED
|
@@ -0,0 +1,323 @@
|
|
|
1
|
+
import ms from 'ms';
|
|
2
|
+
import { EJSON } from 'bson';
|
|
3
|
+
import IORedis from 'ioredis';
|
|
4
|
+
import mongoose from 'mongoose';
|
|
5
|
+
import { satisfies } from 'semver';
|
|
6
|
+
import { createHash } from 'node:crypto';
|
|
7
|
+
import sortKeys from 'sort-keys';
|
|
8
|
+
|
|
9
|
+
var __typeError$3 = (msg) => {
|
|
10
|
+
throw TypeError(msg);
|
|
11
|
+
};
|
|
12
|
+
var __accessCheck$3 = (obj, member, msg) => member.has(obj) || __typeError$3("Cannot " + msg);
|
|
13
|
+
var __privateGet$3 = (obj, member, getter) => (__accessCheck$3(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
|
|
14
|
+
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);
|
|
15
|
+
var __privateSet$3 = (obj, member, value, setter) => (__accessCheck$3(obj, member, "write to private field"), member.set(obj, value), value);
|
|
16
|
+
var _cache;
|
|
17
|
+
class MemoryCacheEngine {
|
|
18
|
+
constructor() {
|
|
19
|
+
__privateAdd$3(this, _cache);
|
|
20
|
+
__privateSet$3(this, _cache, /* @__PURE__ */ new Map());
|
|
21
|
+
}
|
|
22
|
+
get(key) {
|
|
23
|
+
const item = __privateGet$3(this, _cache).get(key);
|
|
24
|
+
if (!item || item.expiresAt < Date.now()) {
|
|
25
|
+
this.del(key);
|
|
26
|
+
return void 0;
|
|
27
|
+
}
|
|
28
|
+
return item.value;
|
|
29
|
+
}
|
|
30
|
+
set(key, value, ttl) {
|
|
31
|
+
const givenTTL = typeof ttl === "string" ? ms(ttl) : ttl;
|
|
32
|
+
const actualTTL = givenTTL ?? Number.POSITIVE_INFINITY;
|
|
33
|
+
__privateGet$3(this, _cache).set(key, {
|
|
34
|
+
value,
|
|
35
|
+
expiresAt: Date.now() + actualTTL
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
del(key) {
|
|
39
|
+
__privateGet$3(this, _cache).delete(key);
|
|
40
|
+
}
|
|
41
|
+
clear() {
|
|
42
|
+
__privateGet$3(this, _cache).clear();
|
|
43
|
+
}
|
|
44
|
+
close() {
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
_cache = new WeakMap();
|
|
48
|
+
|
|
49
|
+
const isMongooseLessThan7 = satisfies(mongoose.version, "<7");
|
|
50
|
+
const convertToObject = (value) => {
|
|
51
|
+
if (isMongooseLessThan7) {
|
|
52
|
+
if (value != null && typeof value === "object" && !Array.isArray(value) && value.toObject) {
|
|
53
|
+
return value.toObject();
|
|
54
|
+
}
|
|
55
|
+
if (Array.isArray(value)) {
|
|
56
|
+
return value.map((doc) => convertToObject(doc));
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return value;
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
var __typeError$2 = (msg) => {
|
|
63
|
+
throw TypeError(msg);
|
|
64
|
+
};
|
|
65
|
+
var __accessCheck$2 = (obj, member, msg) => member.has(obj) || __typeError$2("Cannot " + msg);
|
|
66
|
+
var __privateGet$2 = (obj, member, getter) => (__accessCheck$2(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
|
|
67
|
+
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);
|
|
68
|
+
var __privateSet$2 = (obj, member, value, setter) => (__accessCheck$2(obj, member, "write to private field"), member.set(obj, value), value);
|
|
69
|
+
var _client;
|
|
70
|
+
class RedisCacheEngine {
|
|
71
|
+
constructor(options) {
|
|
72
|
+
__privateAdd$2(this, _client);
|
|
73
|
+
if (!options.keyPrefix) {
|
|
74
|
+
options.keyPrefix = "cache-mongoose:";
|
|
75
|
+
}
|
|
76
|
+
__privateSet$2(this, _client, new IORedis(options));
|
|
77
|
+
}
|
|
78
|
+
async get(key) {
|
|
79
|
+
try {
|
|
80
|
+
const value = await __privateGet$2(this, _client).get(key);
|
|
81
|
+
if (value === null) {
|
|
82
|
+
return void 0;
|
|
83
|
+
}
|
|
84
|
+
return EJSON.parse(value);
|
|
85
|
+
} catch (err) {
|
|
86
|
+
console.error(err);
|
|
87
|
+
return void 0;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
async set(key, value, ttl) {
|
|
91
|
+
try {
|
|
92
|
+
const givenTTL = typeof ttl === "string" ? ms(ttl) : ttl;
|
|
93
|
+
const actualTTL = givenTTL ?? Number.POSITIVE_INFINITY;
|
|
94
|
+
const serializedValue = EJSON.stringify(convertToObject(value));
|
|
95
|
+
await __privateGet$2(this, _client).setex(key, Math.ceil(actualTTL / 1e3), serializedValue);
|
|
96
|
+
} catch (err) {
|
|
97
|
+
console.error(err);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
async del(key) {
|
|
101
|
+
await __privateGet$2(this, _client).del(key);
|
|
102
|
+
}
|
|
103
|
+
async clear() {
|
|
104
|
+
await __privateGet$2(this, _client).flushdb();
|
|
105
|
+
}
|
|
106
|
+
async close() {
|
|
107
|
+
await __privateGet$2(this, _client).quit();
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
_client = new WeakMap();
|
|
111
|
+
|
|
112
|
+
var __typeError$1 = (msg) => {
|
|
113
|
+
throw TypeError(msg);
|
|
114
|
+
};
|
|
115
|
+
var __accessCheck$1 = (obj, member, msg) => member.has(obj) || __typeError$1("Cannot " + msg);
|
|
116
|
+
var __privateGet$1 = (obj, member, getter) => (__accessCheck$1(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
|
|
117
|
+
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);
|
|
118
|
+
var __privateSet$1 = (obj, member, value, setter) => (__accessCheck$1(obj, member, "write to private field"), member.set(obj, value), value);
|
|
119
|
+
var _engine, _defaultTTL, _debug, _engines;
|
|
120
|
+
class Cache {
|
|
121
|
+
constructor(cacheOptions) {
|
|
122
|
+
__privateAdd$1(this, _engine);
|
|
123
|
+
__privateAdd$1(this, _defaultTTL);
|
|
124
|
+
__privateAdd$1(this, _debug);
|
|
125
|
+
__privateAdd$1(this, _engines, ["memory", "redis"]);
|
|
126
|
+
if (!__privateGet$1(this, _engines).includes(cacheOptions.engine)) {
|
|
127
|
+
throw new Error(`Invalid engine name: ${cacheOptions.engine}`);
|
|
128
|
+
}
|
|
129
|
+
if (cacheOptions.engine === "redis" && !cacheOptions.engineOptions) {
|
|
130
|
+
throw new Error(`Engine options are required for ${cacheOptions.engine} engine`);
|
|
131
|
+
}
|
|
132
|
+
if (!cacheOptions.defaultTTL) {
|
|
133
|
+
cacheOptions.defaultTTL = "1 minute";
|
|
134
|
+
}
|
|
135
|
+
__privateSet$1(this, _defaultTTL, typeof cacheOptions.defaultTTL === "string" ? ms(cacheOptions.defaultTTL) : cacheOptions.defaultTTL);
|
|
136
|
+
if (cacheOptions.engine === "redis" && cacheOptions.engineOptions) {
|
|
137
|
+
__privateSet$1(this, _engine, new RedisCacheEngine(cacheOptions.engineOptions));
|
|
138
|
+
}
|
|
139
|
+
if (cacheOptions.engine === "memory") {
|
|
140
|
+
__privateSet$1(this, _engine, new MemoryCacheEngine());
|
|
141
|
+
}
|
|
142
|
+
__privateSet$1(this, _debug, cacheOptions.debug === true);
|
|
143
|
+
}
|
|
144
|
+
async get(key) {
|
|
145
|
+
const cacheEntry = await __privateGet$1(this, _engine).get(key);
|
|
146
|
+
if (__privateGet$1(this, _debug)) {
|
|
147
|
+
const cacheHit = cacheEntry != null ? "HIT" : "MISS";
|
|
148
|
+
console.log(`[ts-cache-mongoose] GET '${key}' - ${cacheHit}`);
|
|
149
|
+
}
|
|
150
|
+
return cacheEntry;
|
|
151
|
+
}
|
|
152
|
+
async set(key, value, ttl) {
|
|
153
|
+
const givenTTL = typeof ttl === "string" ? ms(ttl) : ttl;
|
|
154
|
+
const actualTTL = givenTTL ?? __privateGet$1(this, _defaultTTL);
|
|
155
|
+
await __privateGet$1(this, _engine).set(key, value, actualTTL);
|
|
156
|
+
if (__privateGet$1(this, _debug)) {
|
|
157
|
+
console.log(`[ts-cache-mongoose] SET '${key}' - ttl: ${actualTTL.toFixed(0)} ms`);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
async del(key) {
|
|
161
|
+
await __privateGet$1(this, _engine).del(key);
|
|
162
|
+
if (__privateGet$1(this, _debug)) {
|
|
163
|
+
console.log(`[ts-cache-mongoose] DEL '${key}'`);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
async clear() {
|
|
167
|
+
await __privateGet$1(this, _engine).clear();
|
|
168
|
+
if (__privateGet$1(this, _debug)) {
|
|
169
|
+
console.log("[ts-cache-mongoose] CLEAR");
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
async close() {
|
|
173
|
+
return __privateGet$1(this, _engine).close();
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
_engine = new WeakMap();
|
|
177
|
+
_defaultTTL = new WeakMap();
|
|
178
|
+
_debug = new WeakMap();
|
|
179
|
+
_engines = new WeakMap();
|
|
180
|
+
|
|
181
|
+
function getKey(data) {
|
|
182
|
+
const sortedObj = sortKeys(data, { deep: true });
|
|
183
|
+
const sortedStr = JSON.stringify(sortedObj, (_, val) => {
|
|
184
|
+
return val instanceof RegExp ? String(val) : val;
|
|
185
|
+
});
|
|
186
|
+
return createHash("sha1").update(sortedStr).digest("hex");
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function extendAggregate(mongoose, cache) {
|
|
190
|
+
const mongooseExec = mongoose.Aggregate.prototype.exec;
|
|
191
|
+
mongoose.Aggregate.prototype.getCacheKey = function() {
|
|
192
|
+
if (this._key != null) return this._key;
|
|
193
|
+
return getKey({
|
|
194
|
+
pipeline: this.pipeline()
|
|
195
|
+
});
|
|
196
|
+
};
|
|
197
|
+
mongoose.Aggregate.prototype.getCacheTTL = function() {
|
|
198
|
+
return this._ttl;
|
|
199
|
+
};
|
|
200
|
+
mongoose.Aggregate.prototype.cache = function(ttl, customKey) {
|
|
201
|
+
this._ttl = ttl ?? null;
|
|
202
|
+
this._key = customKey ?? null;
|
|
203
|
+
return this;
|
|
204
|
+
};
|
|
205
|
+
mongoose.Aggregate.prototype.exec = async function(...args) {
|
|
206
|
+
if (!Object.prototype.hasOwnProperty.call(this, "_ttl")) {
|
|
207
|
+
return mongooseExec.apply(this, args);
|
|
208
|
+
}
|
|
209
|
+
const key = this.getCacheKey();
|
|
210
|
+
const ttl = this.getCacheTTL();
|
|
211
|
+
const resultCache = await cache.get(key).catch((err) => {
|
|
212
|
+
console.error(err);
|
|
213
|
+
});
|
|
214
|
+
if (resultCache) {
|
|
215
|
+
return resultCache;
|
|
216
|
+
}
|
|
217
|
+
const result = await mongooseExec.call(this);
|
|
218
|
+
await cache.set(key, result, ttl).catch((err) => {
|
|
219
|
+
console.error(err);
|
|
220
|
+
});
|
|
221
|
+
return result;
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
function extendQuery(mongoose, cache) {
|
|
226
|
+
const mongooseExec = mongoose.Query.prototype.exec;
|
|
227
|
+
mongoose.Query.prototype.getCacheKey = function() {
|
|
228
|
+
if (this._key != null) return this._key;
|
|
229
|
+
const filter = this.getFilter();
|
|
230
|
+
const update = this.getUpdate();
|
|
231
|
+
const options = this.getOptions();
|
|
232
|
+
const mongooseOptions = this.mongooseOptions();
|
|
233
|
+
return getKey({
|
|
234
|
+
model: this.model.modelName,
|
|
235
|
+
op: this.op,
|
|
236
|
+
filter,
|
|
237
|
+
update,
|
|
238
|
+
options,
|
|
239
|
+
mongooseOptions,
|
|
240
|
+
_path: this._path,
|
|
241
|
+
_fields: this._fields,
|
|
242
|
+
_distinct: this._distinct,
|
|
243
|
+
_conditions: this._conditions
|
|
244
|
+
});
|
|
245
|
+
};
|
|
246
|
+
mongoose.Query.prototype.getCacheTTL = function() {
|
|
247
|
+
return this._ttl;
|
|
248
|
+
};
|
|
249
|
+
mongoose.Query.prototype.cache = function(ttl, customKey) {
|
|
250
|
+
this._ttl = ttl ?? null;
|
|
251
|
+
this._key = customKey ?? null;
|
|
252
|
+
return this;
|
|
253
|
+
};
|
|
254
|
+
mongoose.Query.prototype.exec = async function(...args) {
|
|
255
|
+
if (!Object.prototype.hasOwnProperty.call(this, "_ttl")) {
|
|
256
|
+
return mongooseExec.apply(this, args);
|
|
257
|
+
}
|
|
258
|
+
const key = this.getCacheKey();
|
|
259
|
+
const ttl = this.getCacheTTL();
|
|
260
|
+
const mongooseOptions = this.mongooseOptions();
|
|
261
|
+
const isCount = this.op?.includes("count") ?? false;
|
|
262
|
+
const isDistinct = this.op === "distinct";
|
|
263
|
+
const model = this.model.modelName;
|
|
264
|
+
const resultCache = await cache.get(key).catch((err) => {
|
|
265
|
+
console.error(err);
|
|
266
|
+
});
|
|
267
|
+
if (resultCache) {
|
|
268
|
+
if (isCount || isDistinct || mongooseOptions.lean) {
|
|
269
|
+
return resultCache;
|
|
270
|
+
}
|
|
271
|
+
const modelConstructor = mongoose.model(model);
|
|
272
|
+
if (Array.isArray(resultCache)) {
|
|
273
|
+
return resultCache.map((item) => {
|
|
274
|
+
return modelConstructor.hydrate(item);
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
return modelConstructor.hydrate(resultCache);
|
|
278
|
+
}
|
|
279
|
+
const result = await mongooseExec.call(this);
|
|
280
|
+
await cache.set(key, result, ttl).catch((err) => {
|
|
281
|
+
console.error(err);
|
|
282
|
+
});
|
|
283
|
+
return result;
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
var __typeError = (msg) => {
|
|
288
|
+
throw TypeError(msg);
|
|
289
|
+
};
|
|
290
|
+
var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
|
|
291
|
+
var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
|
|
292
|
+
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);
|
|
293
|
+
var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), member.set(obj, value), value);
|
|
294
|
+
var _instance;
|
|
295
|
+
const _CacheMongoose = class _CacheMongoose {
|
|
296
|
+
constructor() {
|
|
297
|
+
}
|
|
298
|
+
static init(mongoose, cacheOptions) {
|
|
299
|
+
if (!__privateGet(_CacheMongoose, _instance)) {
|
|
300
|
+
__privateSet(_CacheMongoose, _instance, new _CacheMongoose());
|
|
301
|
+
__privateGet(_CacheMongoose, _instance).cache = new Cache(cacheOptions);
|
|
302
|
+
const cache = __privateGet(_CacheMongoose, _instance).cache;
|
|
303
|
+
extendQuery(mongoose, cache);
|
|
304
|
+
extendAggregate(mongoose, cache);
|
|
305
|
+
}
|
|
306
|
+
return __privateGet(_CacheMongoose, _instance);
|
|
307
|
+
}
|
|
308
|
+
async clear(customKey) {
|
|
309
|
+
if (customKey != null) {
|
|
310
|
+
await this.cache.del(customKey);
|
|
311
|
+
} else {
|
|
312
|
+
await this.cache.clear();
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
async close() {
|
|
316
|
+
await this.cache.close();
|
|
317
|
+
}
|
|
318
|
+
};
|
|
319
|
+
_instance = new WeakMap();
|
|
320
|
+
__privateAdd(_CacheMongoose, _instance);
|
|
321
|
+
let CacheMongoose = _CacheMongoose;
|
|
322
|
+
|
|
323
|
+
export { CacheMongoose as default };
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ts-cache-mongoose",
|
|
3
|
-
"version": "1.7.
|
|
3
|
+
"version": "1.7.2",
|
|
4
4
|
"description": "Cache plugin for mongoose Queries and Aggregate (in-memory, redis)",
|
|
5
|
-
"author": "
|
|
5
|
+
"author": "ilovepixelart",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"repository": {
|
|
8
8
|
"type": "git",
|
|
@@ -26,7 +26,6 @@
|
|
|
26
26
|
"nosql",
|
|
27
27
|
"ts",
|
|
28
28
|
"typescript",
|
|
29
|
-
"swc",
|
|
30
29
|
"cache",
|
|
31
30
|
"redis",
|
|
32
31
|
"store",
|
|
@@ -42,59 +41,65 @@
|
|
|
42
41
|
"dist",
|
|
43
42
|
"src",
|
|
44
43
|
"tests",
|
|
45
|
-
".swcrc",
|
|
46
44
|
"tsconfig.json",
|
|
47
45
|
"vite.config.mts",
|
|
48
46
|
"biome.json"
|
|
49
47
|
],
|
|
48
|
+
"type": "module",
|
|
50
49
|
"exports": {
|
|
51
|
-
"
|
|
52
|
-
"
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
"
|
|
57
|
-
|
|
58
|
-
"default": "./dist/cjs/plugin.js"
|
|
59
|
-
}
|
|
50
|
+
"require": {
|
|
51
|
+
"types": "./dist/index.d.cts",
|
|
52
|
+
"default": "./dist/index.cjs"
|
|
53
|
+
},
|
|
54
|
+
"import": {
|
|
55
|
+
"types": "./dist/index.d.mts",
|
|
56
|
+
"default": "./dist/index.mjs"
|
|
60
57
|
}
|
|
61
58
|
},
|
|
62
|
-
"main": "dist/cjs
|
|
63
|
-
"module": "dist/
|
|
64
|
-
"types": "dist/
|
|
59
|
+
"main": "./dist/index.cjs",
|
|
60
|
+
"module": "./dist/index.mjs",
|
|
61
|
+
"types": "./dist/index.d.cts",
|
|
65
62
|
"scripts": {
|
|
63
|
+
"prepare": "simple-git-hooks",
|
|
66
64
|
"biome": "npx @biomejs/biome check",
|
|
67
65
|
"biome:fix": "npx @biomejs/biome check --write .",
|
|
68
66
|
"test": "vitest run --coverage",
|
|
69
67
|
"test:open": "vitest run --coverage && open-cli coverage/lcov-report/index.html",
|
|
70
68
|
"clean": "rm -rf ./dist",
|
|
71
|
-
"
|
|
72
|
-
"build
|
|
73
|
-
"
|
|
74
|
-
"release": "npm install && npm run biome && npm run build && np"
|
|
69
|
+
"type:check": "tsc --noEmit",
|
|
70
|
+
"build": "pkgroll --clean-dist",
|
|
71
|
+
"release": "npm install && npm run biome && npm run type:check && npm run build && np"
|
|
75
72
|
},
|
|
76
73
|
"dependencies": {
|
|
77
|
-
"
|
|
74
|
+
"@types/ms": "2.1.0",
|
|
75
|
+
"@types/semver": "7.5.8",
|
|
76
|
+
"ioredis": "5.5.0",
|
|
78
77
|
"ms": "2.1.3",
|
|
79
|
-
"semver": "7.
|
|
78
|
+
"semver": "7.7.1",
|
|
80
79
|
"sort-keys": "4.2.0"
|
|
81
80
|
},
|
|
82
81
|
"devDependencies": {
|
|
83
82
|
"@biomejs/biome": "1.9.4",
|
|
84
|
-
"@types/
|
|
85
|
-
"@
|
|
86
|
-
"
|
|
87
|
-
"
|
|
88
|
-
"
|
|
89
|
-
"merge": "2.1.1",
|
|
90
|
-
"mongodb-memory-server": "10.1.3",
|
|
91
|
-
"mongoose": "8.9.5",
|
|
83
|
+
"@types/node": "22.13.8",
|
|
84
|
+
"@vitest/coverage-v8": "3.0.7",
|
|
85
|
+
"bson": "^6.10.3",
|
|
86
|
+
"mongodb-memory-server": "10.1.4",
|
|
87
|
+
"mongoose": "8.11.0",
|
|
92
88
|
"open-cli": "8.0.0",
|
|
93
|
-
"
|
|
94
|
-
"
|
|
89
|
+
"pkgroll": "2.11.2",
|
|
90
|
+
"simple-git-hooks": "^2.11.1",
|
|
91
|
+
"typescript": "5.8.2",
|
|
92
|
+
"vitest": "3.0.7"
|
|
95
93
|
},
|
|
96
94
|
"peerDependencies": {
|
|
97
95
|
"bson": ">=4.7.2 < 7",
|
|
98
96
|
"mongoose": ">=6.6.0 < 9"
|
|
97
|
+
},
|
|
98
|
+
"simple-git-hooks": {
|
|
99
|
+
"pre-commit": "npm run type:check",
|
|
100
|
+
"pre-push": "npm run biome:fix"
|
|
101
|
+
},
|
|
102
|
+
"overrides": {
|
|
103
|
+
"esbuild": "0.25.0"
|
|
99
104
|
}
|
|
100
105
|
}
|
package/src/extend/aggregate.ts
CHANGED
|
@@ -7,7 +7,7 @@ import type { CacheTTL } from '../types'
|
|
|
7
7
|
export function extendAggregate(mongoose: Mongoose, cache: Cache): void {
|
|
8
8
|
const mongooseExec = mongoose.Aggregate.prototype.exec
|
|
9
9
|
|
|
10
|
-
mongoose.Aggregate.prototype.getCacheKey = function () {
|
|
10
|
+
mongoose.Aggregate.prototype.getCacheKey = function (): string {
|
|
11
11
|
if (this._key != null) return this._key
|
|
12
12
|
|
|
13
13
|
return getKey({
|
|
@@ -15,7 +15,7 @@ export function extendAggregate(mongoose: Mongoose, cache: Cache): void {
|
|
|
15
15
|
})
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
-
mongoose.Aggregate.prototype.getCacheTTL = function () {
|
|
18
|
+
mongoose.Aggregate.prototype.getCacheTTL = function (): CacheTTL | null {
|
|
19
19
|
return this._ttl
|
|
20
20
|
}
|
|
21
21
|
|
package/src/extend/query.ts
CHANGED
|
@@ -7,7 +7,7 @@ import type { CacheTTL } from '../types'
|
|
|
7
7
|
export function extendQuery(mongoose: Mongoose, cache: Cache): void {
|
|
8
8
|
const mongooseExec = mongoose.Query.prototype.exec
|
|
9
9
|
|
|
10
|
-
mongoose.Query.prototype.getCacheKey = function () {
|
|
10
|
+
mongoose.Query.prototype.getCacheKey = function (): string {
|
|
11
11
|
if (this._key != null) return this._key
|
|
12
12
|
|
|
13
13
|
const filter = this.getFilter()
|
|
@@ -29,7 +29,7 @@ export function extendQuery(mongoose: Mongoose, cache: Cache): void {
|
|
|
29
29
|
})
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
-
mongoose.Query.prototype.getCacheTTL = function () {
|
|
32
|
+
mongoose.Query.prototype.getCacheTTL = function (): CacheTTL | null {
|
|
33
33
|
return this._ttl
|
|
34
34
|
}
|
|
35
35
|
|
|
@@ -5,6 +5,8 @@ import { extendQuery } from './extend/query'
|
|
|
5
5
|
import type { Mongoose } from 'mongoose'
|
|
6
6
|
import type { CacheOptions, CacheTTL } from './types'
|
|
7
7
|
|
|
8
|
+
export * from './types'
|
|
9
|
+
|
|
8
10
|
declare module 'mongoose' {
|
|
9
11
|
interface Query<ResultType, DocType, THelpers, RawDocType> {
|
|
10
12
|
cache: (this: Query<ResultType, DocType, THelpers, RawDocType>, ttl?: CacheTTL, customKey?: string) => this
|
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
2
2
|
|
|
3
3
|
import mongoose from 'mongoose'
|
|
4
|
-
import CacheMongoose from '../src/
|
|
5
|
-
import { server } from './mongo/server'
|
|
6
|
-
|
|
4
|
+
import CacheMongoose from '../src/index'
|
|
7
5
|
import { UserModel } from './models/User'
|
|
6
|
+
import { server } from './mongo/server'
|
|
8
7
|
|
|
9
8
|
describe('cache-debug', async () => {
|
|
10
9
|
const instance = server('cache-debug')
|
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import { afterAll, beforeAll, beforeEach, describe, expect, it } from 'vitest'
|
|
2
2
|
|
|
3
3
|
import mongoose from 'mongoose'
|
|
4
|
-
import CacheMongoose from '../src/
|
|
5
|
-
import { server } from './mongo/server'
|
|
6
|
-
|
|
4
|
+
import CacheMongoose from '../src/index'
|
|
7
5
|
import { UserModel } from './models/User'
|
|
6
|
+
import { server } from './mongo/server'
|
|
8
7
|
|
|
9
8
|
describe('cache-memory', async () => {
|
|
10
9
|
const instance = server('cache-memory')
|
|
@@ -1,16 +1,14 @@
|
|
|
1
1
|
import { afterAll, beforeAll, beforeEach, describe, expect, it } from 'vitest'
|
|
2
2
|
|
|
3
3
|
import mongoose from 'mongoose'
|
|
4
|
-
import plugin from '../src/
|
|
4
|
+
import plugin from '../src/index'
|
|
5
5
|
import { server } from './mongo/server'
|
|
6
6
|
|
|
7
|
+
import { ObjectId } from 'bson'
|
|
7
8
|
import { StoryModel } from './models/Story'
|
|
8
9
|
import { UserModel } from './models/User'
|
|
9
10
|
|
|
10
|
-
import
|
|
11
|
-
import { Types } from 'mongoose'
|
|
12
|
-
|
|
13
|
-
import type CacheMongoose from '../src/plugin'
|
|
11
|
+
import type CacheMongoose from '../src/index'
|
|
14
12
|
|
|
15
13
|
describe('cache-redis', async () => {
|
|
16
14
|
const instance = server('cache-redis')
|
|
@@ -244,7 +242,7 @@ describe('cache-redis', async () => {
|
|
|
244
242
|
expect(miss).not.toBeNull()
|
|
245
243
|
|
|
246
244
|
expect(typeof miss?._id).toBe('object')
|
|
247
|
-
expect(miss?._id instanceof Types.ObjectId).toBeTruthy()
|
|
245
|
+
expect(miss?._id instanceof mongoose.Types.ObjectId).toBeTruthy()
|
|
248
246
|
|
|
249
247
|
expect(miss).toHaveProperty('name', 'G')
|
|
250
248
|
|
|
@@ -273,7 +271,7 @@ describe('cache-redis', async () => {
|
|
|
273
271
|
expect(miss?.length).toBe(3)
|
|
274
272
|
|
|
275
273
|
expect(typeof miss?.[0]).toBe('object')
|
|
276
|
-
expect(miss?.[0] instanceof Types.ObjectId).toBeTruthy()
|
|
274
|
+
expect(miss?.[0] instanceof mongoose.Types.ObjectId).toBeTruthy()
|
|
277
275
|
|
|
278
276
|
const hit = await UserModel.distinct('_id').cache('30 seconds').exec()
|
|
279
277
|
expect(hit).not.toBeNull()
|
|
@@ -314,7 +312,7 @@ describe('cache-redis', async () => {
|
|
|
314
312
|
expect(miss).not.toBeNull()
|
|
315
313
|
|
|
316
314
|
expect(typeof miss?._id).toBe('object')
|
|
317
|
-
expect(miss?._id instanceof Types.ObjectId).toBeTruthy()
|
|
315
|
+
expect(miss?._id instanceof mongoose.Types.ObjectId).toBeTruthy()
|
|
318
316
|
|
|
319
317
|
expect(miss?.name).toBe('i')
|
|
320
318
|
expect(miss?.stories).not.toBeNull()
|
|
@@ -323,7 +321,7 @@ describe('cache-redis', async () => {
|
|
|
323
321
|
expect(miss?.stories?.[0]._id.toString()).toBe(story1._id.toString())
|
|
324
322
|
|
|
325
323
|
expect(typeof miss?.stories?.[0]._id).toBe('object')
|
|
326
|
-
expect(miss?.stories?.[0]._id instanceof Types.ObjectId).toBeTruthy()
|
|
324
|
+
expect(miss?.stories?.[0]._id instanceof mongoose.Types.ObjectId).toBeTruthy()
|
|
327
325
|
|
|
328
326
|
expect(typeof miss?.stories?.[0].createdAt).toBe('object')
|
|
329
327
|
expect(miss?.stories?.[0].createdAt instanceof Date).toBeTruthy()
|
|
@@ -331,7 +329,7 @@ describe('cache-redis', async () => {
|
|
|
331
329
|
expect(miss?.stories?.[1]._id.toString()).toBe(story2._id.toString())
|
|
332
330
|
|
|
333
331
|
expect(typeof miss?.stories?.[1]._id).toBe('object')
|
|
334
|
-
expect(miss?.stories?.[1]._id instanceof Types.ObjectId).toBeTruthy()
|
|
332
|
+
expect(miss?.stories?.[1]._id instanceof mongoose.Types.ObjectId).toBeTruthy()
|
|
335
333
|
|
|
336
334
|
expect(typeof miss?.stories?.[1].createdAt).toBe('object')
|
|
337
335
|
expect(miss?.stories?.[1].createdAt instanceof Date).toBeTruthy()
|
|
@@ -450,7 +448,7 @@ describe('cache-redis', async () => {
|
|
|
450
448
|
expect(populatedOriginal).not.toBeNull()
|
|
451
449
|
|
|
452
450
|
expect(typeof populatedOriginal?._id).toBe('object')
|
|
453
|
-
expect(populatedOriginal?._id instanceof Types.ObjectId).toBeTruthy()
|
|
451
|
+
expect(populatedOriginal?._id instanceof mongoose.Types.ObjectId).toBeTruthy()
|
|
454
452
|
|
|
455
453
|
expect(populatedOriginal?.name).toBe('Alex')
|
|
456
454
|
expect(populatedOriginal?.stories).not.toBeNull()
|
|
@@ -459,7 +457,7 @@ describe('cache-redis', async () => {
|
|
|
459
457
|
expect(populatedOriginal?.stories?.[0]._id.toString()).toBe(story1._id.toString())
|
|
460
458
|
|
|
461
459
|
expect(typeof populatedOriginal?.stories?.[0]._id).toBe('object')
|
|
462
|
-
expect(populatedOriginal?.stories?.[0]._id instanceof Types.ObjectId).toBeTruthy()
|
|
460
|
+
expect(populatedOriginal?.stories?.[0]._id instanceof mongoose.Types.ObjectId).toBeTruthy()
|
|
463
461
|
|
|
464
462
|
expect(typeof populatedOriginal?.stories?.[0].createdAt).toBe('object')
|
|
465
463
|
expect(populatedOriginal?.stories?.[0].createdAt instanceof Date).toBeTruthy()
|
|
@@ -467,7 +465,7 @@ describe('cache-redis', async () => {
|
|
|
467
465
|
expect(populatedOriginal?.stories?.[1]._id.toString()).toBe(story2._id.toString())
|
|
468
466
|
|
|
469
467
|
expect(typeof populatedOriginal?.stories?.[1]._id).toBe('object')
|
|
470
|
-
expect(populatedOriginal?.stories?.[1]._id instanceof Types.ObjectId).toBeTruthy()
|
|
468
|
+
expect(populatedOriginal?.stories?.[1]._id instanceof mongoose.Types.ObjectId).toBeTruthy()
|
|
471
469
|
|
|
472
470
|
expect(typeof populatedOriginal?.stories?.[1].createdAt).toBe('object')
|
|
473
471
|
expect(populatedOriginal?.stories?.[1].createdAt instanceof Date).toBeTruthy()
|
package/tests/key.test.ts
CHANGED
package/tsconfig.json
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
{
|
|
2
|
+
"include": ["src"],
|
|
2
3
|
"compilerOptions": {
|
|
3
4
|
"target": "ES2021",
|
|
4
5
|
"lib": ["ES2021"],
|
|
5
|
-
"
|
|
6
|
-
"
|
|
6
|
+
"types": ["node"],
|
|
7
|
+
"module": "Preserve",
|
|
8
|
+
"moduleResolution": "bundler",
|
|
7
9
|
"outDir": "dist",
|
|
8
10
|
"strict": true,
|
|
9
11
|
"allowSyntheticDefaultImports": true,
|
|
@@ -27,7 +29,5 @@
|
|
|
27
29
|
"removeComments": true,
|
|
28
30
|
"skipLibCheck": true,
|
|
29
31
|
"sourceMap": true
|
|
30
|
-
}
|
|
31
|
-
"include": ["src"],
|
|
32
|
-
"exclude": ["tests", "dist", "coverage", "node_modules"]
|
|
32
|
+
}
|
|
33
33
|
}
|
package/vite.config.mts
CHANGED
package/.swcrc
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"$schema": "https://json.schemastore.org/swcrc",
|
|
3
|
-
"jsc": {
|
|
4
|
-
"parser": {
|
|
5
|
-
"syntax": "typescript",
|
|
6
|
-
"decorators": true,
|
|
7
|
-
"dynamicImport": true
|
|
8
|
-
},
|
|
9
|
-
"target": "es2021",
|
|
10
|
-
"keepClassNames": true,
|
|
11
|
-
"loose": true
|
|
12
|
-
},
|
|
13
|
-
"module": {
|
|
14
|
-
"type": "commonjs"
|
|
15
|
-
},
|
|
16
|
-
"sourceMaps": true
|
|
17
|
-
}
|