ts-cache-mongoose 2.0.0 → 2.1.0

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/README.md CHANGED
@@ -17,7 +17,7 @@ Cache query and aggregate in mongoose using in-memory or redis
17
17
 
18
18
  ts-cache-mongoose is a plugin for mongoose
19
19
  \
20
- Caching queries is a good way to improve performance of your application
20
+ I need a way to cache mongoose queries and aggregations to improve application performance. It should support both in-memory and Redis cache engines, work with all major Node.js frameworks, and be easy to use with a simple `.cache()` method on queries and aggregations.
21
21
 
22
22
  ## Supports and tested with
23
23
 
@@ -42,22 +42,13 @@ Caching queries is a good way to improve performance of your application
42
42
 
43
43
  ## Installation
44
44
 
45
- - Locally inside your project
45
+ `mongoose` is a peer dependency — install it alongside `ts-cache-mongoose`.
46
46
 
47
47
  ```bash
48
- npm install ts-cache-mongoose
49
- pnpm add ts-cache-mongoose
50
- yarn add ts-cache-mongoose
51
- bun add ts-cache-mongoose
52
- ```
53
-
54
- - This plugin requires `mongoose` to be installed as a peer dependency
55
-
56
- ```bash
57
- npm install mongoose
58
- pnpm add mongoose
59
- yarn add mongoose
60
- bun add mongoose
48
+ npm install ts-cache-mongoose mongoose
49
+ pnpm add ts-cache-mongoose mongoose
50
+ yarn add ts-cache-mongoose mongoose
51
+ bun add ts-cache-mongoose mongoose
61
52
  ```
62
53
 
63
54
  ## Example
@@ -164,6 +155,14 @@ export class SomeService {
164
155
  }
165
156
  ```
166
157
 
158
+ ## Contributing
159
+
160
+ Check [CONTRIBUTING.md](CONTRIBUTING.md)
161
+
162
+ ## License
163
+
164
+ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details
165
+
167
166
  ## Check my other projects
168
167
 
169
168
  - [ts-migrate-mongoose](https://github.com/ilovepixelart/ts-migrate-mongoose) - Migration framework for mongoose
package/dist/index.cjs CHANGED
@@ -11,65 +11,62 @@ const h = m * 60;
11
11
  const d = h * 24;
12
12
  const w = d * 7;
13
13
  const y = d * 365.25;
14
- const RE = /^(-?(?:\d+)?\.?\d+)\s*(milliseconds?|msecs?|ms|seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|weeks?|w|years?|yrs?|y)?$/i;
14
+ const mo = y / 12;
15
15
  const UNITS = {
16
- years: y,
17
- year: y,
18
- yrs: y,
19
- yr: y,
20
- y,
21
- weeks: w,
22
- week: w,
23
- w,
24
- days: d,
25
- day: d,
26
- d,
27
- hours: h,
28
- hour: h,
29
- hrs: h,
30
- hr: h,
31
- h,
32
- minutes: m,
33
- minute: m,
34
- mins: m,
35
- min: m,
36
- m,
16
+ milliseconds: 1,
17
+ millisecond: 1,
18
+ msecs: 1,
19
+ msec: 1,
20
+ ms: 1,
37
21
  seconds: s,
38
22
  second: s,
39
23
  secs: s,
40
24
  sec: s,
41
25
  s,
42
- milliseconds: 1,
43
- millisecond: 1,
44
- msecs: 1,
45
- msec: 1,
46
- ms: 1
26
+ minutes: m,
27
+ minute: m,
28
+ mins: m,
29
+ min: m,
30
+ m,
31
+ hours: h,
32
+ hour: h,
33
+ hrs: h,
34
+ hr: h,
35
+ h,
36
+ days: d,
37
+ day: d,
38
+ d,
39
+ weeks: w,
40
+ week: w,
41
+ w,
42
+ months: mo,
43
+ month: mo,
44
+ mo,
45
+ years: y,
46
+ year: y,
47
+ yrs: y,
48
+ yr: y,
49
+ y
47
50
  };
51
+ const unitPattern = Object.keys(UNITS).sort((a, b) => b.length - a.length).join("|");
52
+ const RE = new RegExp(String.raw`^(-?(?:\d+)?\.?\d+)\s*(${unitPattern})?$`, "i");
48
53
  const ms = (val) => {
49
54
  const str = String(val);
50
- if (str.length > 100) return 0;
55
+ if (str.length > 100) return Number.NaN;
51
56
  const match = RE.exec(str);
52
- if (!match) return 0;
57
+ if (!match) return Number.NaN;
53
58
  const n = Number.parseFloat(match[1] ?? "");
54
59
  const type = (match[2] ?? "ms").toLowerCase();
55
60
  return n * (UNITS[type] ?? 0);
56
61
  };
57
62
 
58
- var __typeError$3 = (msg) => {
59
- throw TypeError(msg);
60
- };
61
- var __accessCheck$3 = (obj, member, msg) => member.has(obj) || __typeError$3("Cannot " + msg);
62
- var __privateGet$3 = (obj, member, getter) => (__accessCheck$3(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
63
- 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);
64
- var __privateSet$3 = (obj, member, value, setter) => (__accessCheck$3(obj, member, "write to private field"), member.set(obj, value), value);
65
- var _cache;
66
63
  class MemoryCacheEngine {
64
+ #cache;
67
65
  constructor() {
68
- __privateAdd$3(this, _cache);
69
- __privateSet$3(this, _cache, /* @__PURE__ */ new Map());
66
+ this.#cache = /* @__PURE__ */ new Map();
70
67
  }
71
68
  get(key) {
72
- const item = __privateGet$3(this, _cache).get(key);
69
+ const item = this.#cache.get(key);
73
70
  if (!item || item.expiresAt < Date.now()) {
74
71
  this.del(key);
75
72
  return void 0;
@@ -77,23 +74,22 @@ class MemoryCacheEngine {
77
74
  return item.value;
78
75
  }
79
76
  set(key, value, ttl) {
80
- const givenTTL = typeof ttl === "string" ? ms(ttl) : ttl;
77
+ const givenTTL = ttl == null ? void 0 : ms(ttl);
81
78
  const actualTTL = givenTTL ?? Number.POSITIVE_INFINITY;
82
- __privateGet$3(this, _cache).set(key, {
79
+ this.#cache.set(key, {
83
80
  value,
84
81
  expiresAt: Date.now() + actualTTL
85
82
  });
86
83
  }
87
84
  del(key) {
88
- __privateGet$3(this, _cache).delete(key);
85
+ this.#cache.delete(key);
89
86
  }
90
87
  clear() {
91
- __privateGet$3(this, _cache).clear();
88
+ this.#cache.clear();
92
89
  }
93
90
  close() {
94
91
  }
95
92
  }
96
- _cache = new WeakMap();
97
93
 
98
94
  const isMongooseLessThan7 = Number.parseInt(mongoose.version, 10) < 7;
99
95
  const convertToObject = (value) => {
@@ -108,23 +104,15 @@ const convertToObject = (value) => {
108
104
  return value;
109
105
  };
110
106
 
111
- var __typeError$2 = (msg) => {
112
- throw TypeError(msg);
113
- };
114
- var __accessCheck$2 = (obj, member, msg) => member.has(obj) || __typeError$2("Cannot " + msg);
115
- var __privateGet$2 = (obj, member, getter) => (__accessCheck$2(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
116
- 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);
117
- var __privateSet$2 = (obj, member, value, setter) => (__accessCheck$2(obj, member, "write to private field"), member.set(obj, value), value);
118
- var _client;
119
107
  class RedisCacheEngine {
108
+ #client;
120
109
  constructor(options) {
121
- __privateAdd$2(this, _client);
122
110
  options.keyPrefix ??= "cache-mongoose:";
123
- __privateSet$2(this, _client, new IORedis(options));
111
+ this.#client = new IORedis(options);
124
112
  }
125
113
  async get(key) {
126
114
  try {
127
- const value = await __privateGet$2(this, _client).get(key);
115
+ const value = await this.#client.get(key);
128
116
  if (value === null) {
129
117
  return void 0;
130
118
  }
@@ -136,92 +124,79 @@ class RedisCacheEngine {
136
124
  }
137
125
  async set(key, value, ttl) {
138
126
  try {
139
- const givenTTL = typeof ttl === "string" ? ms(ttl) : ttl;
127
+ const givenTTL = ttl == null ? void 0 : ms(ttl);
140
128
  const actualTTL = givenTTL ?? Number.POSITIVE_INFINITY;
141
129
  const serializedValue = bson.EJSON.stringify(convertToObject(value));
142
- await __privateGet$2(this, _client).setex(key, Math.ceil(actualTTL / 1e3), serializedValue);
130
+ await this.#client.setex(key, Math.ceil(actualTTL / 1e3), serializedValue);
143
131
  } catch (err) {
144
132
  console.error(err);
145
133
  }
146
134
  }
147
135
  async del(key) {
148
- await __privateGet$2(this, _client).del(key);
136
+ await this.#client.del(key);
149
137
  }
150
138
  async clear() {
151
- await __privateGet$2(this, _client).flushdb();
139
+ await this.#client.flushdb();
152
140
  }
153
141
  async close() {
154
- await __privateGet$2(this, _client).quit();
142
+ await this.#client.quit();
155
143
  }
156
144
  }
157
- _client = new WeakMap();
158
145
 
159
- var __typeError$1 = (msg) => {
160
- throw TypeError(msg);
161
- };
162
- var __accessCheck$1 = (obj, member, msg) => member.has(obj) || __typeError$1("Cannot " + msg);
163
- var __privateGet$1 = (obj, member, getter) => (__accessCheck$1(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
164
- 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);
165
- var __privateSet$1 = (obj, member, value, setter) => (__accessCheck$1(obj, member, "write to private field"), member.set(obj, value), value);
166
- var _engine, _defaultTTL, _debug, _engines;
167
146
  class Cache {
147
+ #engine;
148
+ #defaultTTL;
149
+ #debug;
150
+ #engines = ["memory", "redis"];
168
151
  constructor(cacheOptions) {
169
- __privateAdd$1(this, _engine);
170
- __privateAdd$1(this, _defaultTTL);
171
- __privateAdd$1(this, _debug);
172
- __privateAdd$1(this, _engines, ["memory", "redis"]);
173
- if (!__privateGet$1(this, _engines).includes(cacheOptions.engine)) {
152
+ if (!this.#engines.includes(cacheOptions.engine)) {
174
153
  throw new Error(`Invalid engine name: ${cacheOptions.engine}`);
175
154
  }
176
155
  if (cacheOptions.engine === "redis" && !cacheOptions.engineOptions) {
177
156
  throw new Error(`Engine options are required for ${cacheOptions.engine} engine`);
178
157
  }
179
158
  cacheOptions.defaultTTL ??= "1 minute";
180
- __privateSet$1(this, _defaultTTL, typeof cacheOptions.defaultTTL === "string" ? ms(cacheOptions.defaultTTL) : cacheOptions.defaultTTL);
159
+ this.#defaultTTL = ms(cacheOptions.defaultTTL);
181
160
  if (cacheOptions.engine === "redis" && cacheOptions.engineOptions) {
182
- __privateSet$1(this, _engine, new RedisCacheEngine(cacheOptions.engineOptions));
161
+ this.#engine = new RedisCacheEngine(cacheOptions.engineOptions);
183
162
  }
184
163
  if (cacheOptions.engine === "memory") {
185
- __privateSet$1(this, _engine, new MemoryCacheEngine());
164
+ this.#engine = new MemoryCacheEngine();
186
165
  }
187
- __privateSet$1(this, _debug, cacheOptions.debug === true);
166
+ this.#debug = cacheOptions.debug === true;
188
167
  }
189
168
  async get(key) {
190
- const cacheEntry = await __privateGet$1(this, _engine).get(key);
191
- if (__privateGet$1(this, _debug)) {
169
+ const cacheEntry = await this.#engine.get(key);
170
+ if (this.#debug) {
192
171
  const cacheHit = cacheEntry == null ? "MISS" : "HIT";
193
172
  console.log(`[ts-cache-mongoose] GET '${key}' - ${cacheHit}`);
194
173
  }
195
174
  return cacheEntry;
196
175
  }
197
176
  async set(key, value, ttl) {
198
- const givenTTL = typeof ttl === "string" ? ms(ttl) : ttl;
199
- const actualTTL = givenTTL ?? __privateGet$1(this, _defaultTTL);
200
- await __privateGet$1(this, _engine).set(key, value, actualTTL);
201
- if (__privateGet$1(this, _debug)) {
177
+ const givenTTL = ttl == null ? null : ms(ttl);
178
+ const actualTTL = givenTTL ?? this.#defaultTTL;
179
+ await this.#engine.set(key, value, actualTTL);
180
+ if (this.#debug) {
202
181
  console.log(`[ts-cache-mongoose] SET '${key}' - ttl: ${actualTTL.toFixed(0)} ms`);
203
182
  }
204
183
  }
205
184
  async del(key) {
206
- await __privateGet$1(this, _engine).del(key);
207
- if (__privateGet$1(this, _debug)) {
185
+ await this.#engine.del(key);
186
+ if (this.#debug) {
208
187
  console.log(`[ts-cache-mongoose] DEL '${key}'`);
209
188
  }
210
189
  }
211
190
  async clear() {
212
- await __privateGet$1(this, _engine).clear();
213
- if (__privateGet$1(this, _debug)) {
191
+ await this.#engine.clear();
192
+ if (this.#debug) {
214
193
  console.log("[ts-cache-mongoose] CLEAR");
215
194
  }
216
195
  }
217
196
  async close() {
218
- return __privateGet$1(this, _engine).close();
197
+ return this.#engine.close();
219
198
  }
220
199
  }
221
- _engine = new WeakMap();
222
- _defaultTTL = new WeakMap();
223
- _debug = new WeakMap();
224
- _engines = new WeakMap();
225
200
 
226
201
  const isPlainObject = (value) => {
227
202
  if (typeof value !== "object" || value === null) return false;
@@ -273,7 +248,7 @@ function extendAggregate(mongoose, cache) {
273
248
  pipeline: this.pipeline()
274
249
  });
275
250
  };
276
- mongoose.Aggregate.prototype.getCacheTTL = function() {
251
+ mongoose.Aggregate.prototype.getDuration = function() {
277
252
  return this._ttl;
278
253
  };
279
254
  mongoose.Aggregate.prototype.cache = function(ttl, customKey) {
@@ -282,11 +257,11 @@ function extendAggregate(mongoose, cache) {
282
257
  return this;
283
258
  };
284
259
  mongoose.Aggregate.prototype.exec = async function(...args) {
285
- if (!Object.prototype.hasOwnProperty.call(this, "_ttl")) {
260
+ if (!Object.hasOwn(this, "_ttl")) {
286
261
  return mongooseExec.apply(this, args);
287
262
  }
288
263
  const key = this.getCacheKey();
289
- const ttl = this.getCacheTTL();
264
+ const ttl = this.getDuration();
290
265
  const resultCache = await cache.get(key).catch((err) => {
291
266
  console.error(err);
292
267
  });
@@ -322,7 +297,7 @@ function extendQuery(mongoose, cache) {
322
297
  _conditions: this._conditions
323
298
  });
324
299
  };
325
- mongoose.Query.prototype.getCacheTTL = function() {
300
+ mongoose.Query.prototype.getDuration = function() {
326
301
  return this._ttl;
327
302
  };
328
303
  mongoose.Query.prototype.cache = function(ttl, customKey) {
@@ -331,11 +306,11 @@ function extendQuery(mongoose, cache) {
331
306
  return this;
332
307
  };
333
308
  mongoose.Query.prototype.exec = async function(...args) {
334
- if (!Object.prototype.hasOwnProperty.call(this, "_ttl")) {
309
+ if (!Object.hasOwn(this, "_ttl")) {
335
310
  return mongooseExec.apply(this, args);
336
311
  }
337
312
  const key = this.getCacheKey();
338
- const ttl = this.getCacheTTL();
313
+ const ttl = this.getDuration();
339
314
  const mongooseOptions = this.mongooseOptions();
340
315
  const isCount = this.op?.includes("count") ?? false;
341
316
  const isDistinct = this.op === "distinct";
@@ -363,26 +338,20 @@ function extendQuery(mongoose, cache) {
363
338
  };
364
339
  }
365
340
 
366
- var __typeError = (msg) => {
367
- throw TypeError(msg);
368
- };
369
- var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
370
- var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
371
- 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);
372
- var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), member.set(obj, value), value);
373
- var _instance;
374
- const _CacheMongoose = class _CacheMongoose {
341
+ class CacheMongoose {
342
+ static #instance;
343
+ cache;
375
344
  constructor() {
376
345
  }
377
346
  static init(mongoose, cacheOptions) {
378
- if (!__privateGet(_CacheMongoose, _instance)) {
379
- __privateSet(_CacheMongoose, _instance, new _CacheMongoose());
380
- __privateGet(_CacheMongoose, _instance).cache = new Cache(cacheOptions);
381
- const cache = __privateGet(_CacheMongoose, _instance).cache;
347
+ if (!CacheMongoose.#instance) {
348
+ CacheMongoose.#instance = new CacheMongoose();
349
+ CacheMongoose.#instance.cache = new Cache(cacheOptions);
350
+ const cache = CacheMongoose.#instance.cache;
382
351
  extendQuery(mongoose, cache);
383
352
  extendAggregate(mongoose, cache);
384
353
  }
385
- return __privateGet(_CacheMongoose, _instance);
354
+ return CacheMongoose.#instance;
386
355
  }
387
356
  async clear(customKey) {
388
357
  if (customKey == null) {
@@ -394,9 +363,6 @@ const _CacheMongoose = class _CacheMongoose {
394
363
  async close() {
395
364
  await this.cache.close();
396
365
  }
397
- };
398
- _instance = new WeakMap();
399
- __privateAdd(_CacheMongoose, _instance);
400
- let CacheMongoose = _CacheMongoose;
366
+ }
401
367
 
402
368
  module.exports = CacheMongoose;
package/dist/index.d.cts CHANGED
@@ -1,17 +1,55 @@
1
1
  import { Mongoose } from 'mongoose';
2
2
  import { RedisOptions } from 'ioredis';
3
3
 
4
- type CacheTTL = number | string;
4
+ declare const UNITS: {
5
+ readonly milliseconds: 1;
6
+ readonly millisecond: 1;
7
+ readonly msecs: 1;
8
+ readonly msec: 1;
9
+ readonly ms: 1;
10
+ readonly seconds: 1000;
11
+ readonly second: 1000;
12
+ readonly secs: 1000;
13
+ readonly sec: 1000;
14
+ readonly s: 1000;
15
+ readonly minutes: number;
16
+ readonly minute: number;
17
+ readonly mins: number;
18
+ readonly min: number;
19
+ readonly m: number;
20
+ readonly hours: number;
21
+ readonly hour: number;
22
+ readonly hrs: number;
23
+ readonly hr: number;
24
+ readonly h: number;
25
+ readonly days: number;
26
+ readonly day: number;
27
+ readonly d: number;
28
+ readonly weeks: number;
29
+ readonly week: number;
30
+ readonly w: number;
31
+ readonly months: number;
32
+ readonly month: number;
33
+ readonly mo: number;
34
+ readonly years: number;
35
+ readonly year: number;
36
+ readonly yrs: number;
37
+ readonly yr: number;
38
+ readonly y: number;
39
+ };
40
+ type Unit = keyof typeof UNITS;
41
+ type Duration = number | `${number}` | `${number}${Unit}` | `${number} ${Unit}`;
42
+
5
43
  type CacheData = Record<string, unknown> | Record<string, unknown>[] | unknown[] | number | undefined;
6
44
  type CacheOptions = {
7
45
  engine: 'memory' | 'redis';
8
46
  engineOptions?: RedisOptions;
9
- defaultTTL?: CacheTTL;
47
+ defaultTTL?: Duration;
10
48
  debug?: boolean;
11
49
  };
12
50
  interface CacheEngine {
13
51
  get: (key: string) => Promise<CacheData> | CacheData;
14
- set: (key: string, value: CacheData, ttl?: CacheTTL) => Promise<void> | void;
52
+ set: (key: string, value: CacheData, ttl?: Duration) => Promise<void> | void;
15
53
  del: (key: string) => Promise<void> | void;
16
54
  clear: () => Promise<void> | void;
17
55
  close: () => Promise<void> | void;
@@ -19,11 +57,11 @@ interface CacheEngine {
19
57
 
20
58
  declare module 'mongoose' {
21
59
  interface Query<ResultType, DocType, THelpers, RawDocType> {
22
- cache: (this: Query<ResultType, DocType, THelpers, RawDocType>, ttl?: CacheTTL, customKey?: string) => this;
60
+ cache: (this: Query<ResultType, DocType, THelpers, RawDocType>, ttl?: Duration, customKey?: string) => this;
23
61
  _key: string | null;
24
62
  getCacheKey: (this: Query<ResultType, DocType, THelpers, RawDocType>) => string;
25
- _ttl: CacheTTL | null;
26
- getCacheTTL: (this: Query<ResultType, DocType, THelpers, RawDocType>) => CacheTTL | null;
63
+ _ttl: Duration | null;
64
+ getDuration: (this: Query<ResultType, DocType, THelpers, RawDocType>) => Duration | null;
27
65
  op?: string;
28
66
  _path?: unknown;
29
67
  _fields?: unknown;
@@ -31,11 +69,11 @@ declare module 'mongoose' {
31
69
  _conditions?: unknown;
32
70
  }
33
71
  interface Aggregate<ResultType> {
34
- cache: (this: Aggregate<ResultType>, ttl?: CacheTTL, customKey?: string) => this;
72
+ cache: (this: Aggregate<ResultType>, ttl?: Duration, customKey?: string) => this;
35
73
  _key: string | null;
36
74
  getCacheKey: (this: Aggregate<ResultType>) => string;
37
- _ttl: CacheTTL | null;
38
- getCacheTTL: (this: Aggregate<ResultType>) => CacheTTL | null;
75
+ _ttl: Duration | null;
76
+ getDuration: (this: Aggregate<ResultType>) => Duration | null;
39
77
  }
40
78
  }
41
79
  declare class CacheMongoose {
@@ -48,5 +86,5 @@ declare class CacheMongoose {
48
86
  }
49
87
 
50
88
  export { CacheMongoose as default };
51
- export type { CacheData, CacheEngine, CacheOptions, CacheTTL };
89
+ export type { CacheData, CacheEngine, CacheOptions, Duration };
52
90
  //# sourceMappingURL=index.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.cts","sources":["../src/types.ts","../src/index.ts"],"mappings":";;;AAEM,KAAM,QAAQ;AAEd,KAAM,SAAS,GAAG,MAAM,oBAAoB,MAAM;AAElD,KAAM,YAAY;;oBAEN,YAAY;iBACf,QAAQ;;;AAIjB,UAAW,WAAW;0BACJ,OAAO,CAAC,SAAS,IAAI,SAAS;8BAC1B,SAAS,QAAQ,QAAQ,KAAK,OAAO;0BACzC,OAAO;iBAChB,OAAO;iBACP,OAAO;;;ACTtB;;8EAE0E,QAAQ;;;cAGxE,QAAQ;iFAC2D,QAAQ;;;;;;;;mDAStC,QAAQ;;;cAG7C,QAAQ;sDACgC,QAAQ;;;AAI1D,cAAM,aAAa;;;;0BAQY,QAAQ,gBAAgB,YAAY,GAAG,aAAa;+BAczC,OAAO;aAQzB,OAAO","names":[]}
1
+ {"version":3,"file":"index.d.cts","sources":["../src/ms.ts","../src/types.ts","../src/index.ts"],"mappings":";;;AAQA,cAAa,KAAK;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAqCZ,KAAM,IAAI,gBAAgB,KAAK;AAE/B,KAAM,QAAQ,sCAAsC,IAAI,kBAAkB,IAAI;;AC1C9E,KAAM,SAAS,GAAG,MAAM,oBAAoB,MAAM;AAElD,KAAM,YAAY;;oBAEN,YAAY;iBACf,QAAQ;;;AAIjB,UAAW,WAAW;0BACJ,OAAO,CAAC,SAAS,IAAI,SAAS;8BAC1B,SAAS,QAAQ,QAAQ,KAAK,OAAO;0BACzC,OAAO;iBAChB,OAAO;iBACP,OAAO;;;ACVtB;;8EAE0E,QAAQ;;;cAGxE,QAAQ;iFAC2D,QAAQ;;;;;;;;mDAStC,QAAQ;;;cAG7C,QAAQ;sDACgC,QAAQ;;;AAI1D,cAAM,aAAa;;;;0BAQY,QAAQ,gBAAgB,YAAY,GAAG,aAAa;+BAczC,OAAO;aAQzB,OAAO","names":[]}
package/dist/index.d.mts CHANGED
@@ -1,17 +1,55 @@
1
1
  import { Mongoose } from 'mongoose';
2
2
  import { RedisOptions } from 'ioredis';
3
3
 
4
- type CacheTTL = number | string;
4
+ declare const UNITS: {
5
+ readonly milliseconds: 1;
6
+ readonly millisecond: 1;
7
+ readonly msecs: 1;
8
+ readonly msec: 1;
9
+ readonly ms: 1;
10
+ readonly seconds: 1000;
11
+ readonly second: 1000;
12
+ readonly secs: 1000;
13
+ readonly sec: 1000;
14
+ readonly s: 1000;
15
+ readonly minutes: number;
16
+ readonly minute: number;
17
+ readonly mins: number;
18
+ readonly min: number;
19
+ readonly m: number;
20
+ readonly hours: number;
21
+ readonly hour: number;
22
+ readonly hrs: number;
23
+ readonly hr: number;
24
+ readonly h: number;
25
+ readonly days: number;
26
+ readonly day: number;
27
+ readonly d: number;
28
+ readonly weeks: number;
29
+ readonly week: number;
30
+ readonly w: number;
31
+ readonly months: number;
32
+ readonly month: number;
33
+ readonly mo: number;
34
+ readonly years: number;
35
+ readonly year: number;
36
+ readonly yrs: number;
37
+ readonly yr: number;
38
+ readonly y: number;
39
+ };
40
+ type Unit = keyof typeof UNITS;
41
+ type Duration = number | `${number}` | `${number}${Unit}` | `${number} ${Unit}`;
42
+
5
43
  type CacheData = Record<string, unknown> | Record<string, unknown>[] | unknown[] | number | undefined;
6
44
  type CacheOptions = {
7
45
  engine: 'memory' | 'redis';
8
46
  engineOptions?: RedisOptions;
9
- defaultTTL?: CacheTTL;
47
+ defaultTTL?: Duration;
10
48
  debug?: boolean;
11
49
  };
12
50
  interface CacheEngine {
13
51
  get: (key: string) => Promise<CacheData> | CacheData;
14
- set: (key: string, value: CacheData, ttl?: CacheTTL) => Promise<void> | void;
52
+ set: (key: string, value: CacheData, ttl?: Duration) => Promise<void> | void;
15
53
  del: (key: string) => Promise<void> | void;
16
54
  clear: () => Promise<void> | void;
17
55
  close: () => Promise<void> | void;
@@ -19,11 +57,11 @@ interface CacheEngine {
19
57
 
20
58
  declare module 'mongoose' {
21
59
  interface Query<ResultType, DocType, THelpers, RawDocType> {
22
- cache: (this: Query<ResultType, DocType, THelpers, RawDocType>, ttl?: CacheTTL, customKey?: string) => this;
60
+ cache: (this: Query<ResultType, DocType, THelpers, RawDocType>, ttl?: Duration, customKey?: string) => this;
23
61
  _key: string | null;
24
62
  getCacheKey: (this: Query<ResultType, DocType, THelpers, RawDocType>) => string;
25
- _ttl: CacheTTL | null;
26
- getCacheTTL: (this: Query<ResultType, DocType, THelpers, RawDocType>) => CacheTTL | null;
63
+ _ttl: Duration | null;
64
+ getDuration: (this: Query<ResultType, DocType, THelpers, RawDocType>) => Duration | null;
27
65
  op?: string;
28
66
  _path?: unknown;
29
67
  _fields?: unknown;
@@ -31,11 +69,11 @@ declare module 'mongoose' {
31
69
  _conditions?: unknown;
32
70
  }
33
71
  interface Aggregate<ResultType> {
34
- cache: (this: Aggregate<ResultType>, ttl?: CacheTTL, customKey?: string) => this;
72
+ cache: (this: Aggregate<ResultType>, ttl?: Duration, customKey?: string) => this;
35
73
  _key: string | null;
36
74
  getCacheKey: (this: Aggregate<ResultType>) => string;
37
- _ttl: CacheTTL | null;
38
- getCacheTTL: (this: Aggregate<ResultType>) => CacheTTL | null;
75
+ _ttl: Duration | null;
76
+ getDuration: (this: Aggregate<ResultType>) => Duration | null;
39
77
  }
40
78
  }
41
79
  declare class CacheMongoose {
@@ -48,5 +86,5 @@ declare class CacheMongoose {
48
86
  }
49
87
 
50
88
  export { CacheMongoose as default };
51
- export type { CacheData, CacheEngine, CacheOptions, CacheTTL };
89
+ export type { CacheData, CacheEngine, CacheOptions, Duration };
52
90
  //# sourceMappingURL=index.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.mts","sources":["../src/types.ts","../src/index.ts"],"mappings":";;;AAEM,KAAM,QAAQ;AAEd,KAAM,SAAS,GAAG,MAAM,oBAAoB,MAAM;AAElD,KAAM,YAAY;;oBAEN,YAAY;iBACf,QAAQ;;;AAIjB,UAAW,WAAW;0BACJ,OAAO,CAAC,SAAS,IAAI,SAAS;8BAC1B,SAAS,QAAQ,QAAQ,KAAK,OAAO;0BACzC,OAAO;iBAChB,OAAO;iBACP,OAAO;;;ACTtB;;8EAE0E,QAAQ;;;cAGxE,QAAQ;iFAC2D,QAAQ;;;;;;;;mDAStC,QAAQ;;;cAG7C,QAAQ;sDACgC,QAAQ;;;AAI1D,cAAM,aAAa;;;;0BAQY,QAAQ,gBAAgB,YAAY,GAAG,aAAa;+BAczC,OAAO;aAQzB,OAAO","names":[]}
1
+ {"version":3,"file":"index.d.mts","sources":["../src/ms.ts","../src/types.ts","../src/index.ts"],"mappings":";;;AAQA,cAAa,KAAK;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAqCZ,KAAM,IAAI,gBAAgB,KAAK;AAE/B,KAAM,QAAQ,sCAAsC,IAAI,kBAAkB,IAAI;;AC1C9E,KAAM,SAAS,GAAG,MAAM,oBAAoB,MAAM;AAElD,KAAM,YAAY;;oBAEN,YAAY;iBACf,QAAQ;;;AAIjB,UAAW,WAAW;0BACJ,OAAO,CAAC,SAAS,IAAI,SAAS;8BAC1B,SAAS,QAAQ,QAAQ,KAAK,OAAO;0BACzC,OAAO;iBAChB,OAAO;iBACP,OAAO;;;ACVtB;;8EAE0E,QAAQ;;;cAGxE,QAAQ;iFAC2D,QAAQ;;;;;;;;mDAStC,QAAQ;;;cAG7C,QAAQ;sDACgC,QAAQ;;;AAI1D,cAAM,aAAa;;;;0BAQY,QAAQ,gBAAgB,YAAY,GAAG,aAAa;+BAczC,OAAO;aAQzB,OAAO","names":[]}