ts-cache-mongoose 0.0.3 → 0.0.4
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/dist/cjs/crypto.js +7 -20
- package/dist/cjs/crypto.js.map +1 -1
- package/dist/cjs/extend/aggregate.js +41 -0
- package/dist/cjs/extend/aggregate.js.map +1 -0
- package/dist/cjs/extend/query.js +64 -0
- package/dist/cjs/extend/query.js.map +1 -0
- package/dist/cjs/plugin.js +10 -61
- package/dist/cjs/plugin.js.map +1 -1
- package/dist/cjs/types/crypto.d.ts +0 -1
- package/dist/cjs/types/crypto.d.ts.map +1 -1
- package/dist/cjs/types/extend/aggregate.d.ts +4 -0
- package/dist/cjs/types/extend/aggregate.d.ts.map +1 -0
- package/dist/cjs/types/extend/query.d.ts +4 -0
- package/dist/cjs/types/extend/query.d.ts.map +1 -0
- package/dist/cjs/types/plugin.d.ts +9 -2
- package/dist/cjs/types/plugin.d.ts.map +1 -1
- package/dist/esm/crypto.js +6 -18
- package/dist/esm/crypto.js.map +1 -1
- package/dist/esm/extend/aggregate.js +37 -0
- package/dist/esm/extend/aggregate.js.map +1 -0
- package/dist/esm/extend/query.js +60 -0
- package/dist/esm/extend/query.js.map +1 -0
- package/dist/esm/plugin.js.map +1 -1
- package/dist/esm/plugin.mjs +10 -61
- package/dist/esm/types/crypto.d.ts +0 -1
- package/dist/esm/types/crypto.d.ts.map +1 -1
- package/dist/esm/types/extend/aggregate.d.ts +4 -0
- package/dist/esm/types/extend/aggregate.d.ts.map +1 -0
- package/dist/esm/types/extend/query.d.ts +4 -0
- package/dist/esm/types/extend/query.d.ts.map +1 -0
- package/dist/esm/types/plugin.d.ts +9 -2
- package/dist/esm/types/plugin.d.ts.map +1 -1
- package/package.json +5 -4
- package/src/crypto.ts +6 -23
- package/src/extend/aggregate.ts +55 -0
- package/src/extend/query.ts +80 -0
- package/src/plugin.ts +20 -79
- package/tests/cache-memory.test.ts +118 -0
- package/tests/{cache.test.ts → cache-redis.test.ts} +41 -28
- package/tests/crypto.test.ts +33 -8
package/dist/cjs/crypto.js
CHANGED
|
@@ -1,28 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getKey =
|
|
3
|
+
exports.getKey = void 0;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
|
-
const lodash_1 = tslib_1.__importDefault(require("lodash"));
|
|
6
5
|
const crypto_1 = require("crypto");
|
|
7
|
-
|
|
8
|
-
if (lodash_1.default.isObjectLike(data) && lodash_1.default.isArray(data)) {
|
|
9
|
-
return data.map(sortDeep);
|
|
10
|
-
}
|
|
11
|
-
if (lodash_1.default.isObjectLike(data) && !lodash_1.default.isArray(data)) {
|
|
12
|
-
const sortedKeys = lodash_1.default.keys(data).sort((a, b) => a.localeCompare(b));
|
|
13
|
-
const sortedObj = {};
|
|
14
|
-
sortedKeys.forEach((key) => {
|
|
15
|
-
sortedObj[key] = sortDeep(data[key]);
|
|
16
|
-
});
|
|
17
|
-
return sortedObj;
|
|
18
|
-
}
|
|
19
|
-
return data;
|
|
20
|
-
}
|
|
21
|
-
exports.sortDeep = sortDeep;
|
|
6
|
+
const sort_keys_1 = tslib_1.__importDefault(require("sort-keys"));
|
|
22
7
|
function getKey(data) {
|
|
23
|
-
const sortedObj =
|
|
24
|
-
const sortedStr = JSON.stringify(sortedObj)
|
|
25
|
-
|
|
8
|
+
const sortedObj = (0, sort_keys_1.default)(data, { deep: true });
|
|
9
|
+
const sortedStr = JSON.stringify(sortedObj, (_key, val) => {
|
|
10
|
+
return val instanceof RegExp ? String(val) : val;
|
|
11
|
+
});
|
|
12
|
+
return (0, crypto_1.createHash)('sha1').update(sortedStr).digest('hex');
|
|
26
13
|
}
|
|
27
14
|
exports.getKey = getKey;
|
|
28
15
|
//# sourceMappingURL=crypto.js.map
|
package/dist/cjs/crypto.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"crypto.js","sourceRoot":"","sources":["../../src/crypto.ts"],"names":[],"mappings":";;;;AAAA,
|
|
1
|
+
{"version":3,"file":"crypto.js","sourceRoot":"","sources":["../../src/crypto.ts"],"names":[],"mappings":";;;;AAAA,mCAAmC;AACnC,kEAAgC;AAEhC,SAAgB,MAAM,CAAE,IAAyD;IAC/E,MAAM,SAAS,GAAG,IAAA,mBAAQ,EAAC,IAAI,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,CAAA;IAChD,MAAM,SAAS,GAAG,IAAI,CAAC,SAAS,CAAC,SAAS,EAAE,CAAC,IAAI,EAAE,GAAY,EAAE,EAAE;QACjE,OAAO,GAAG,YAAY,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAA;IAClD,CAAC,CAAC,CAAA;IACF,OAAO,IAAA,mBAAU,EAAC,MAAM,CAAC,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAA;AAC3D,CAAC;AAND,wBAMC"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
const lodash_1 = tslib_1.__importDefault(require("lodash"));
|
|
5
|
+
const crypto_1 = require("../crypto");
|
|
6
|
+
function extendQuery(mongoose, cache) {
|
|
7
|
+
const mongooseExec = mongoose.Aggregate.prototype.exec;
|
|
8
|
+
mongoose.Aggregate.prototype.getCacheKey = function () {
|
|
9
|
+
return (0, crypto_1.getKey)({
|
|
10
|
+
pipeline: this.pipeline()
|
|
11
|
+
});
|
|
12
|
+
};
|
|
13
|
+
mongoose.Aggregate.prototype.getCacheTTL = function () {
|
|
14
|
+
return this._ttl;
|
|
15
|
+
};
|
|
16
|
+
mongoose.Aggregate.prototype.cache = function (ttl, customKey) {
|
|
17
|
+
this._ttl = ttl;
|
|
18
|
+
this._key = customKey;
|
|
19
|
+
return this;
|
|
20
|
+
};
|
|
21
|
+
mongoose.Aggregate.prototype.exec = async function () {
|
|
22
|
+
if (!lodash_1.default.has(this, '_ttl')) {
|
|
23
|
+
return mongooseExec.apply(this);
|
|
24
|
+
}
|
|
25
|
+
const key = this.getCacheKey();
|
|
26
|
+
const ttl = this.getCacheTTL();
|
|
27
|
+
const resultCache = await cache.get(key).catch((err) => {
|
|
28
|
+
console.error(err);
|
|
29
|
+
});
|
|
30
|
+
if (resultCache) {
|
|
31
|
+
return resultCache;
|
|
32
|
+
}
|
|
33
|
+
const result = await mongooseExec.call(this);
|
|
34
|
+
cache.set(key, result, ttl).catch((err) => {
|
|
35
|
+
console.error(err);
|
|
36
|
+
});
|
|
37
|
+
return result;
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
exports.default = extendQuery;
|
|
41
|
+
//# sourceMappingURL=aggregate.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"aggregate.js","sourceRoot":"","sources":["../../../src/extend/aggregate.ts"],"names":[],"mappings":";;;AAAA,4DAAsB;AAKtB,sCAAkC;AAElC,SAAwB,WAAW,CAAE,QAAkB,EAAE,KAAY;IAEnE,MAAM,YAAY,GAAG,QAAQ,CAAC,SAAS,CAAC,SAAS,CAAC,IAAI,CAAA;IAGtD,QAAQ,CAAC,SAAS,CAAC,SAAS,CAAC,WAAW,GAAG;QACzC,OAAO,IAAA,eAAM,EAAC;YACZ,QAAQ,EAAE,IAAI,CAAC,QAAQ,EAAE;SAC1B,CAAC,CAAA;IACJ,CAAC,CAAA;IAGD,QAAQ,CAAC,SAAS,CAAC,SAAS,CAAC,WAAW,GAAG;QACzC,OAAO,IAAI,CAAC,IAAI,CAAA;IAClB,CAAC,CAAA;IAGD,QAAQ,CAAC,SAAS,CAAC,SAAS,CAAC,KAAK,GAAG,UAAU,GAAY,EAAE,SAAkB;QAC7E,IAAI,CAAC,IAAI,GAAG,GAAG,CAAA;QACf,IAAI,CAAC,IAAI,GAAG,SAAS,CAAA;QACrB,OAAO,IAAI,CAAA;IACb,CAAC,CAAA;IAGD,QAAQ,CAAC,SAAS,CAAC,SAAS,CAAC,IAAI,GAAG,KAAK;QACvC,IAAI,CAAC,gBAAC,CAAC,GAAG,CAAC,IAAI,EAAE,MAAM,CAAC,EAAE;YACxB,OAAO,YAAY,CAAC,KAAK,CAAC,IAAI,CAAC,CAAA;SAChC;QAED,MAAM,GAAG,GAAG,IAAI,CAAC,WAAW,EAAE,CAAA;QAC9B,MAAM,GAAG,GAAG,IAAI,CAAC,WAAW,EAAE,CAAA;QAE9B,MAAM,WAAW,GAAG,MAAM,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,GAAG,EAAE,EAAE;YACrD,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;QACpB,CAAC,CAAC,CAAA;QAEF,IAAI,WAAW,EAAE;YACf,OAAO,WAAW,CAAA;SACnB;QAED,MAAM,MAAM,GAAG,MAAM,YAAY,CAAC,IAAI,CAAC,IAAI,CAAwD,CAAA;QACnG,KAAK,CAAC,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,GAAG,EAAE,EAAE;YACxC,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;QACpB,CAAC,CAAC,CAAA;QAEF,OAAO,MAAM,CAAA;IACf,CAAC,CAAA;AACH,CAAC;AA/CD,8BA+CC"}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
const lodash_1 = tslib_1.__importDefault(require("lodash"));
|
|
5
|
+
const crypto_1 = require("../crypto");
|
|
6
|
+
function extendQuery(mongoose, cache) {
|
|
7
|
+
const mongooseExec = mongoose.Query.prototype.exec;
|
|
8
|
+
mongoose.Query.prototype.getCacheKey = function () {
|
|
9
|
+
if (this._key)
|
|
10
|
+
return this._key;
|
|
11
|
+
const filter = this.getFilter();
|
|
12
|
+
const update = this.getUpdate();
|
|
13
|
+
const options = this.getOptions();
|
|
14
|
+
const mongooseOptions = this.mongooseOptions();
|
|
15
|
+
const data = {
|
|
16
|
+
model: this.model.modelName,
|
|
17
|
+
op: this.op,
|
|
18
|
+
filter,
|
|
19
|
+
update,
|
|
20
|
+
options,
|
|
21
|
+
mongooseOptions,
|
|
22
|
+
_path: this._path,
|
|
23
|
+
_fields: this._fields,
|
|
24
|
+
_distinct: this._distinct,
|
|
25
|
+
_conditions: this._conditions
|
|
26
|
+
};
|
|
27
|
+
return (0, crypto_1.getKey)(data);
|
|
28
|
+
};
|
|
29
|
+
mongoose.Query.prototype.getCacheTTL = function () {
|
|
30
|
+
return this._ttl;
|
|
31
|
+
};
|
|
32
|
+
mongoose.Query.prototype.cache = function (ttl, customKey) {
|
|
33
|
+
this._ttl = ttl;
|
|
34
|
+
this._key = customKey;
|
|
35
|
+
return this;
|
|
36
|
+
};
|
|
37
|
+
mongoose.Query.prototype.exec = async function () {
|
|
38
|
+
if (!lodash_1.default.has(this, '_ttl')) {
|
|
39
|
+
return mongooseExec.apply(this);
|
|
40
|
+
}
|
|
41
|
+
const key = this.getCacheKey();
|
|
42
|
+
const ttl = this.getCacheTTL();
|
|
43
|
+
const model = this.model.modelName;
|
|
44
|
+
const resultCache = await cache.get(key).catch((err) => {
|
|
45
|
+
console.error(err);
|
|
46
|
+
});
|
|
47
|
+
if (resultCache) {
|
|
48
|
+
const constructor = mongoose.model(model);
|
|
49
|
+
if (lodash_1.default.isArray(resultCache)) {
|
|
50
|
+
return resultCache.map((item) => constructor.hydrate(item));
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
return constructor.hydrate(resultCache);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
const result = await mongooseExec.call(this);
|
|
57
|
+
cache.set(key, result, ttl).catch((err) => {
|
|
58
|
+
console.error(err);
|
|
59
|
+
});
|
|
60
|
+
return result;
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
exports.default = extendQuery;
|
|
64
|
+
//# sourceMappingURL=query.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"query.js","sourceRoot":"","sources":["../../../src/extend/query.ts"],"names":[],"mappings":";;;AAAA,4DAAsB;AAKtB,sCAAkC;AAElC,SAAwB,WAAW,CAAE,QAAkB,EAAE,KAAY;IAEnE,MAAM,YAAY,GAAG,QAAQ,CAAC,KAAK,CAAC,SAAS,CAAC,IAAI,CAAA;IAGlD,QAAQ,CAAC,KAAK,CAAC,SAAS,CAAC,WAAW,GAAG;QACrC,IAAI,IAAI,CAAC,IAAI;YAAE,OAAO,IAAI,CAAC,IAAI,CAAA;QAE/B,MAAM,MAAM,GAAG,IAAI,CAAC,SAAS,EAAE,CAAA;QAC/B,MAAM,MAAM,GAAG,IAAI,CAAC,SAAS,EAAE,CAAA;QAC/B,MAAM,OAAO,GAAG,IAAI,CAAC,UAAU,EAAE,CAAA;QACjC,MAAM,eAAe,GAAG,IAAI,CAAC,eAAe,EAAE,CAAA;QAE9C,MAAM,IAAI,GAA4B;YACpC,KAAK,EAAE,IAAI,CAAC,KAAK,CAAC,SAAS;YAC3B,EAAE,EAAE,IAAI,CAAC,EAAE;YACX,MAAM;YACN,MAAM;YACN,OAAO;YACP,eAAe;YACf,KAAK,EAAE,IAAI,CAAC,KAAK;YACjB,OAAO,EAAE,IAAI,CAAC,OAAO;YACrB,SAAS,EAAE,IAAI,CAAC,SAAS;YACzB,WAAW,EAAE,IAAI,CAAC,WAAW;SAC9B,CAAA;QAED,OAAO,IAAA,eAAM,EAAC,IAAI,CAAC,CAAA;IACrB,CAAC,CAAA;IAGD,QAAQ,CAAC,KAAK,CAAC,SAAS,CAAC,WAAW,GAAG;QACrC,OAAO,IAAI,CAAC,IAAI,CAAA;IAClB,CAAC,CAAA;IAGD,QAAQ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AAC,CAAC,GAAG,EAAE,EAAE;YACxC,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;QACpB,CAAC,CAAC,CAAA;QAEF,OAAO,MAAM,CAAA;IACf,CAAC,CAAA;AACH,CAAC;AAxED,8BAwEC"}
|
package/dist/cjs/plugin.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const tslib_1 = require("tslib");
|
|
4
|
-
const lodash_1 = tslib_1.__importDefault(require("lodash"));
|
|
5
4
|
const Cache_1 = tslib_1.__importDefault(require("./cache/Cache"));
|
|
6
|
-
const
|
|
5
|
+
const query_1 = tslib_1.__importDefault(require("./extend/query"));
|
|
6
|
+
const aggregate_1 = tslib_1.__importDefault(require("./extend/aggregate"));
|
|
7
7
|
class CacheMongoose {
|
|
8
8
|
constructor() {
|
|
9
9
|
}
|
|
@@ -13,73 +13,22 @@ class CacheMongoose {
|
|
|
13
13
|
if (!this.instance) {
|
|
14
14
|
this.instance = new CacheMongoose();
|
|
15
15
|
this.instance.cache = new Cache_1.default(cacheOptions);
|
|
16
|
-
this.instance.cacheOptions = cacheOptions;
|
|
17
16
|
const cache = this.instance.cache;
|
|
18
|
-
|
|
19
|
-
mongoose
|
|
20
|
-
if (this._key)
|
|
21
|
-
return this._key;
|
|
22
|
-
const filter = this.getFilter();
|
|
23
|
-
const update = this.getUpdate();
|
|
24
|
-
const options = this.getOptions();
|
|
25
|
-
const data = {
|
|
26
|
-
model: this.model.modelName,
|
|
27
|
-
op: this.op,
|
|
28
|
-
filter,
|
|
29
|
-
update,
|
|
30
|
-
skip: options.skip,
|
|
31
|
-
limit: options.limit,
|
|
32
|
-
sort: options.sort,
|
|
33
|
-
_fields: this._fields,
|
|
34
|
-
_path: this._path,
|
|
35
|
-
_distinct: this._distinct
|
|
36
|
-
};
|
|
37
|
-
return (0, crypto_1.getKey)(data);
|
|
38
|
-
};
|
|
39
|
-
mongoose.Query.prototype.getCacheTTL = function () {
|
|
40
|
-
return this._ttl;
|
|
41
|
-
};
|
|
42
|
-
mongoose.Query.prototype.cache = function (ttl, customKey) {
|
|
43
|
-
this._ttl = ttl;
|
|
44
|
-
this._key = customKey;
|
|
45
|
-
return this;
|
|
46
|
-
};
|
|
47
|
-
mongoose.Query.prototype.exec = async function () {
|
|
48
|
-
if (!this._ttl) {
|
|
49
|
-
return mongooseExec.apply(this);
|
|
50
|
-
}
|
|
51
|
-
const key = this.getCacheKey();
|
|
52
|
-
const ttl = this.getCacheTTL();
|
|
53
|
-
const model = this.model.modelName;
|
|
54
|
-
const resultCache = await cache.get(key);
|
|
55
|
-
if (resultCache) {
|
|
56
|
-
const constructor = mongoose.model(model);
|
|
57
|
-
if (lodash_1.default.isArray(resultCache)) {
|
|
58
|
-
return resultCache.map((item) => constructor.hydrate(item));
|
|
59
|
-
}
|
|
60
|
-
else {
|
|
61
|
-
return constructor.hydrate(resultCache);
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
const result = await mongooseExec.call(this);
|
|
65
|
-
cache.set(key, result, ttl).catch((err) => {
|
|
66
|
-
console.error(err);
|
|
67
|
-
});
|
|
68
|
-
return result;
|
|
69
|
-
};
|
|
17
|
+
(0, query_1.default)(mongoose, cache);
|
|
18
|
+
(0, aggregate_1.default)(mongoose, cache);
|
|
70
19
|
}
|
|
71
20
|
return this.instance;
|
|
72
21
|
}
|
|
73
22
|
async clear(customKey) {
|
|
74
|
-
if (
|
|
75
|
-
|
|
23
|
+
if (customKey) {
|
|
24
|
+
await this.cache.del(customKey);
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
await this.cache.clear();
|
|
76
28
|
}
|
|
77
|
-
return this.cache.del(customKey);
|
|
78
29
|
}
|
|
79
30
|
async close() {
|
|
80
|
-
|
|
81
|
-
await this.cache.close();
|
|
82
|
-
}
|
|
31
|
+
await this.cache.close();
|
|
83
32
|
}
|
|
84
33
|
}
|
|
85
34
|
exports.default = CacheMongoose;
|
package/dist/cjs/plugin.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plugin.js","sourceRoot":"","sources":["../../src/plugin.ts"],"names":[],"mappings":";;;AAAA,
|
|
1
|
+
{"version":3,"file":"plugin.js","sourceRoot":"","sources":["../../src/plugin.ts"],"names":[],"mappings":";;;AAAA,kEAAiC;AAKjC,mEAAwC;AACxC,2EAAgD;AAyBhD,MAAM,aAAa;IAKjB;IAEA,CAAC;IAEM,MAAM,CAAC,IAAI,CAAE,QAAkB,EAAE,YAA2B;QACjE,IAAI,OAAO,QAAQ,CAAC,KAAK,CAAC,OAAO,KAAK,UAAU;YAAE,MAAM,IAAI,KAAK,CAAC,8FAA8F,CAAC,CAAA;QACjK,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE;YAClB,IAAI,CAAC,QAAQ,GAAG,IAAI,aAAa,EAAE,CAAA;YACnC,IAAI,CAAC,QAAQ,CAAC,KAAK,GAAG,IAAI,eAAK,CAAC,YAAY,CAAC,CAAA;YAE7C,MAAM,KAAK,GAAG,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAA;YAEjC,IAAA,eAAW,EAAC,QAAQ,EAAE,KAAK,CAAC,CAAA;YAC5B,IAAA,mBAAe,EAAC,QAAQ,EAAE,KAAK,CAAC,CAAA;SACjC;QAED,OAAO,IAAI,CAAC,QAAQ,CAAA;IACtB,CAAC;IAEM,KAAK,CAAC,KAAK,CAAE,SAAkB;QACpC,IAAI,SAAS,EAAE;YACb,MAAM,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,SAAS,CAAC,CAAA;SAChC;aAAM;YACL,MAAM,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,CAAA;SACzB;IACH,CAAC;IAEM,KAAK,CAAC,KAAK;QAChB,MAAM,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,CAAA;IAC1B,CAAC;CACF;AAED,kBAAe,aAAa,CAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"crypto.d.ts","sourceRoot":"","sources":["../../../src/crypto.ts"],"names":[],"mappings":"AAGA,wBAAgB,
|
|
1
|
+
{"version":3,"file":"crypto.d.ts","sourceRoot":"","sources":["../../../src/crypto.ts"],"names":[],"mappings":"AAGA,wBAAgB,MAAM,CAAE,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,CAMzF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"aggregate.d.ts","sourceRoot":"","sources":["../../../../src/extend/aggregate.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAA;AACxC,OAAO,KAAK,KAAK,MAAM,gBAAgB,CAAA;AAIvC,MAAM,CAAC,OAAO,UAAU,WAAW,CAAE,QAAQ,EAAE,QAAQ,EAAE,KAAK,EAAE,KAAK,GAAG,IAAI,CA+C3E"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"query.d.ts","sourceRoot":"","sources":["../../../../src/extend/query.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAA;AACxC,OAAO,KAAK,KAAK,MAAM,gBAAgB,CAAA;AAIvC,MAAM,CAAC,OAAO,UAAU,WAAW,CAAE,QAAQ,EAAE,QAAQ,EAAE,KAAK,EAAE,KAAK,GAAG,IAAI,CAwE3E"}
|
|
@@ -8,15 +8,22 @@ declare module 'mongoose' {
|
|
|
8
8
|
_ttl?: string;
|
|
9
9
|
getCacheTTL: (this: Query<ResultType, DocType, THelpers, RawDocType>) => string | undefined;
|
|
10
10
|
op?: string;
|
|
11
|
-
_fields?: unknown;
|
|
12
11
|
_path?: unknown;
|
|
12
|
+
_fields?: unknown;
|
|
13
13
|
_distinct?: unknown;
|
|
14
|
+
_conditions?: unknown;
|
|
15
|
+
}
|
|
16
|
+
interface Aggregate<ResultType> {
|
|
17
|
+
cache: (this: Aggregate<ResultType>, ttl?: string, customKey?: string) => this;
|
|
18
|
+
_key?: string;
|
|
19
|
+
getCacheKey: (this: Aggregate<ResultType>) => string;
|
|
20
|
+
_ttl?: string;
|
|
21
|
+
getCacheTTL: (this: Aggregate<ResultType>) => string | undefined;
|
|
14
22
|
}
|
|
15
23
|
}
|
|
16
24
|
declare class CacheMongoose {
|
|
17
25
|
private static instance;
|
|
18
26
|
private cache;
|
|
19
|
-
private cacheOptions;
|
|
20
27
|
private constructor();
|
|
21
28
|
static init(mongoose: Mongoose, cacheOptions: ICacheOptions): CacheMongoose;
|
|
22
29
|
clear(customKey?: string): Promise<void>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["../../../src/plugin.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["../../../src/plugin.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAA;AACxC,OAAO,KAAK,aAAa,MAAM,4BAA4B,CAAA;AAK3D,OAAO,QAAQ,UAAU,CAAC;IACxB,UAAU,KAAK,CAAC,UAAU,EAAE,OAAO,EAAE,QAAQ,EAAE,UAAU;QACvD,KAAK,EAAE,CAAC,IAAI,EAAE,KAAK,CAAC,UAAU,EAAE,OAAO,EAAE,QAAQ,EAAE,UAAU,CAAC,EAAE,GAAG,CAAC,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,MAAM,KAAK,IAAI,CAAA;QACzG,IAAI,CAAC,EAAE,MAAM,CAAA;QACb,WAAW,EAAE,CAAC,IAAI,EAAE,KAAK,CAAC,UAAU,EAAE,OAAO,EAAE,QAAQ,EAAE,UAAU,CAAC,KAAK,MAAM,CAAA;QAC/E,IAAI,CAAC,EAAE,MAAM,CAAA;QACb,WAAW,EAAE,CAAC,IAAI,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|
7
|
-
if (_.isObjectLike(data) && !_.isArray(data)) {
|
|
8
|
-
const sortedKeys = _.keys(data).sort((a, b) => a.localeCompare(b));
|
|
9
|
-
const sortedObj = {};
|
|
10
|
-
sortedKeys.forEach((key) => {
|
|
11
|
-
sortedObj[key] = sortDeep(data[key]);
|
|
12
|
-
});
|
|
13
|
-
return sortedObj;
|
|
14
|
-
}
|
|
15
|
-
return data;
|
|
16
|
-
}
|
|
2
|
+
import sortKeys from 'sort-keys';
|
|
17
3
|
export function getKey(data) {
|
|
18
|
-
const sortedObj =
|
|
19
|
-
const sortedStr = JSON.stringify(sortedObj)
|
|
20
|
-
|
|
4
|
+
const sortedObj = sortKeys(data, { deep: true });
|
|
5
|
+
const sortedStr = JSON.stringify(sortedObj, (_key, val) => {
|
|
6
|
+
return val instanceof RegExp ? String(val) : val;
|
|
7
|
+
});
|
|
8
|
+
return createHash('sha1').update(sortedStr).digest('hex');
|
|
21
9
|
}
|
|
22
10
|
//# sourceMappingURL=crypto.js.map
|
package/dist/esm/crypto.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"crypto.js","sourceRoot":"","sources":["../../src/crypto.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"crypto.js","sourceRoot":"","sources":["../../src/crypto.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,QAAQ,CAAA;AACnC,OAAO,QAAQ,MAAM,WAAW,CAAA;AAEhC,MAAM,UAAU,MAAM,CAAE,IAAyD;IAC/E,MAAM,SAAS,GAAG,QAAQ,CAAC,IAAI,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,CAAA;IAChD,MAAM,SAAS,GAAG,IAAI,CAAC,SAAS,CAAC,SAAS,EAAE,CAAC,IAAI,EAAE,GAAY,EAAE,EAAE;QACjE,OAAO,GAAG,YAAY,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAA;IAClD,CAAC,CAAC,CAAA;IACF,OAAO,UAAU,CAAC,MAAM,CAAC,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAA;AAC3D,CAAC"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import _ from 'lodash';
|
|
2
|
+
import { getKey } from '../crypto';
|
|
3
|
+
export default function extendQuery(mongoose, cache) {
|
|
4
|
+
const mongooseExec = mongoose.Aggregate.prototype.exec;
|
|
5
|
+
mongoose.Aggregate.prototype.getCacheKey = function () {
|
|
6
|
+
return getKey({
|
|
7
|
+
pipeline: this.pipeline()
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
mongoose.Aggregate.prototype.getCacheTTL = function () {
|
|
11
|
+
return this._ttl;
|
|
12
|
+
};
|
|
13
|
+
mongoose.Aggregate.prototype.cache = function (ttl, customKey) {
|
|
14
|
+
this._ttl = ttl;
|
|
15
|
+
this._key = customKey;
|
|
16
|
+
return this;
|
|
17
|
+
};
|
|
18
|
+
mongoose.Aggregate.prototype.exec = async function () {
|
|
19
|
+
if (!_.has(this, '_ttl')) {
|
|
20
|
+
return mongooseExec.apply(this);
|
|
21
|
+
}
|
|
22
|
+
const key = this.getCacheKey();
|
|
23
|
+
const ttl = this.getCacheTTL();
|
|
24
|
+
const resultCache = await cache.get(key).catch((err) => {
|
|
25
|
+
console.error(err);
|
|
26
|
+
});
|
|
27
|
+
if (resultCache) {
|
|
28
|
+
return resultCache;
|
|
29
|
+
}
|
|
30
|
+
const result = await mongooseExec.call(this);
|
|
31
|
+
cache.set(key, result, ttl).catch((err) => {
|
|
32
|
+
console.error(err);
|
|
33
|
+
});
|
|
34
|
+
return result;
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
//# sourceMappingURL=aggregate.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"aggregate.js","sourceRoot":"","sources":["../../../src/extend/aggregate.ts"],"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AAW,CAAA;SACnB;QAED,MAAM,MAAM,GAAG,MAAM,YAAY,CAAC,IAAI,CAAC,IAAI,CAAwD,CAAA;QACnG,KAAK,CAAC,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,GAAG,EAAE,EAAE;YACxC,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;QACpB,CAAC,CAAC,CAAA;QAEF,OAAO,MAAM,CAAA;IACf,CAAC,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import _ from 'lodash';
|
|
2
|
+
import { getKey } from '../crypto';
|
|
3
|
+
export default function extendQuery(mongoose, cache) {
|
|
4
|
+
const mongooseExec = mongoose.Query.prototype.exec;
|
|
5
|
+
mongoose.Query.prototype.getCacheKey = function () {
|
|
6
|
+
if (this._key)
|
|
7
|
+
return this._key;
|
|
8
|
+
const filter = this.getFilter();
|
|
9
|
+
const update = this.getUpdate();
|
|
10
|
+
const options = this.getOptions();
|
|
11
|
+
const mongooseOptions = this.mongooseOptions();
|
|
12
|
+
const data = {
|
|
13
|
+
model: this.model.modelName,
|
|
14
|
+
op: this.op,
|
|
15
|
+
filter,
|
|
16
|
+
update,
|
|
17
|
+
options,
|
|
18
|
+
mongooseOptions,
|
|
19
|
+
_path: this._path,
|
|
20
|
+
_fields: this._fields,
|
|
21
|
+
_distinct: this._distinct,
|
|
22
|
+
_conditions: this._conditions
|
|
23
|
+
};
|
|
24
|
+
return getKey(data);
|
|
25
|
+
};
|
|
26
|
+
mongoose.Query.prototype.getCacheTTL = function () {
|
|
27
|
+
return this._ttl;
|
|
28
|
+
};
|
|
29
|
+
mongoose.Query.prototype.cache = function (ttl, customKey) {
|
|
30
|
+
this._ttl = ttl;
|
|
31
|
+
this._key = customKey;
|
|
32
|
+
return this;
|
|
33
|
+
};
|
|
34
|
+
mongoose.Query.prototype.exec = async function () {
|
|
35
|
+
if (!_.has(this, '_ttl')) {
|
|
36
|
+
return mongooseExec.apply(this);
|
|
37
|
+
}
|
|
38
|
+
const key = this.getCacheKey();
|
|
39
|
+
const ttl = this.getCacheTTL();
|
|
40
|
+
const model = this.model.modelName;
|
|
41
|
+
const resultCache = await cache.get(key).catch((err) => {
|
|
42
|
+
console.error(err);
|
|
43
|
+
});
|
|
44
|
+
if (resultCache) {
|
|
45
|
+
const constructor = mongoose.model(model);
|
|
46
|
+
if (_.isArray(resultCache)) {
|
|
47
|
+
return resultCache.map((item) => constructor.hydrate(item));
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
return constructor.hydrate(resultCache);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
const result = await mongooseExec.call(this);
|
|
54
|
+
cache.set(key, result, ttl).catch((err) => {
|
|
55
|
+
console.error(err);
|
|
56
|
+
});
|
|
57
|
+
return result;
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
//# sourceMappingURL=query.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"query.js","sourceRoot":"","sources":["../../../src/extend/query.ts"],"names":[],"mappings":"AAAA,OAAO,CAAC,MAAM,QAAQ,CAAA;AAKtB,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,CAAA;AAElC,MAAM,CAAC,OAAO,UAAU,WAAW,CAAE,QAAkB,EAAE,KAAY;IAEnE,MAAM,YAAY,GAAG,QAAQ,CAAC,KAAK,CAAC,SAAS,CAAC,IAAI,CAAA;IAGlD,QAAQ,CAAC,KAAK,CAAC,SAAS,CAAC,WAAW,GAAG;QACrC,IAAI,IAAI,CAAC,IAAI;YAAE,OAAO,IAAI,CAAC,IAAI,CAAA;QAE/B,MAAM,MAAM,GAAG,IAAI,CAAC,SAAS,EAAE,CAAA;QAC/B,MAAM,MAAM,GAAG,IAAI,CAAC,SAAS,EAAE,CAAA;QAC/B,MAAM,OAAO,GAAG,IAAI,CAAC,UAAU,EAAE,CAAA;QACjC,MAAM,eAAe,GAAG,IAAI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pB,CAAC,CAAC,CAAA;QAEF,IAAI,WAAW,EAAE;YACf,MAAM,WAAW,GAAG,QAAQ,CAAC,KAAK,CAAC,KAAK,CAAC,CAAA;YACzC,IAAI,CAAC,CAAC,OAAO,CAAC,WAAW,CAAC,EAAE;gBAC1B,OAAO,WAAW,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,WAAW,CAAC,OAAO,CAAC,IAAI,CAA4B,CAAC,CAAA;aACvF;iBAAM;gBACL,OAAO,WAAW,CAAC,OAAO,CAAC,WAAW,CAA4B,CAAA;aACnE;SACF;QAED,MAAM,MAAM,GAAG,MAAM,YAAY,CAAC,IAAI,CAAC,IAAI,CAAwD,CAAA;QACnG,KAAK,CAAC,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,GAAG,EAAE,EAAE;YACxC,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;QACpB,CAAC,CAAC,CAAA;QAEF,OAAO,MAAM,CAAA;IACf,CAAC,CAAA;AACH,CAAC"}
|
package/dist/esm/plugin.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plugin.js","sourceRoot":"","sources":["../../src/plugin.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"plugin.js","sourceRoot":"","sources":["../../src/plugin.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,eAAe,CAAA;AAKjC,OAAO,WAAW,MAAM,gBAAgB,CAAA;AACxC,OAAO,eAAe,MAAM,oBAAoB,CAAA;AAyBhD,MAAM,aAAa;IAKjB;IAEA,CAAC;IAEM,MAAM,CAAC,IAAI,CAAE,QAAkB,EAAE,YAA2B;QACjE,IAAI,OAAO,QAAQ,CAAC,KAAK,CAAC,OAAO,KAAK,UAAU;YAAE,MAAM,IAAI,KAAK,CAAC,8FAA8F,CAAC,CAAA;QACjK,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE;YAClB,IAAI,CAAC,QAAQ,GAAG,IAAI,aAAa,EAAE,CAAA;YACnC,IAAI,CAAC,QAAQ,CAAC,KAAK,GAAG,IAAI,KAAK,CAAC,YAAY,CAAC,CAAA;YAE7C,MAAM,KAAK,GAAG,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAA;YAEjC,WAAW,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAA;YAC5B,eAAe,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAA;SACjC;QAED,OAAO,IAAI,CAAC,QAAQ,CAAA;IACtB,CAAC;IAEM,KAAK,CAAC,KAAK,CAAE,SAAkB;QACpC,IAAI,SAAS,EAAE;YACb,MAAM,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,SAAS,CAAC,CAAA;SAChC;aAAM;YACL,MAAM,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,CAAA;SACzB;IACH,CAAC;IAEM,KAAK,CAAC,KAAK;QAChB,MAAM,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,CAAA;IAC1B,CAAC;CACF;AAED,eAAe,aAAa,CAAA"}
|
package/dist/esm/plugin.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import _ from 'lodash';
|
|
2
1
|
import Cache from './cache/Cache';
|
|
3
|
-
import
|
|
2
|
+
import extendQuery from './extend/query';
|
|
3
|
+
import extendAggregate from './extend/aggregate';
|
|
4
4
|
class CacheMongoose {
|
|
5
5
|
constructor() {
|
|
6
6
|
}
|
|
@@ -10,73 +10,22 @@ class CacheMongoose {
|
|
|
10
10
|
if (!this.instance) {
|
|
11
11
|
this.instance = new CacheMongoose();
|
|
12
12
|
this.instance.cache = new Cache(cacheOptions);
|
|
13
|
-
this.instance.cacheOptions = cacheOptions;
|
|
14
13
|
const cache = this.instance.cache;
|
|
15
|
-
|
|
16
|
-
mongoose
|
|
17
|
-
if (this._key)
|
|
18
|
-
return this._key;
|
|
19
|
-
const filter = this.getFilter();
|
|
20
|
-
const update = this.getUpdate();
|
|
21
|
-
const options = this.getOptions();
|
|
22
|
-
const data = {
|
|
23
|
-
model: this.model.modelName,
|
|
24
|
-
op: this.op,
|
|
25
|
-
filter,
|
|
26
|
-
update,
|
|
27
|
-
skip: options.skip,
|
|
28
|
-
limit: options.limit,
|
|
29
|
-
sort: options.sort,
|
|
30
|
-
_fields: this._fields,
|
|
31
|
-
_path: this._path,
|
|
32
|
-
_distinct: this._distinct
|
|
33
|
-
};
|
|
34
|
-
return getKey(data);
|
|
35
|
-
};
|
|
36
|
-
mongoose.Query.prototype.getCacheTTL = function () {
|
|
37
|
-
return this._ttl;
|
|
38
|
-
};
|
|
39
|
-
mongoose.Query.prototype.cache = function (ttl, customKey) {
|
|
40
|
-
this._ttl = ttl;
|
|
41
|
-
this._key = customKey;
|
|
42
|
-
return this;
|
|
43
|
-
};
|
|
44
|
-
mongoose.Query.prototype.exec = async function () {
|
|
45
|
-
if (!this._ttl) {
|
|
46
|
-
return mongooseExec.apply(this);
|
|
47
|
-
}
|
|
48
|
-
const key = this.getCacheKey();
|
|
49
|
-
const ttl = this.getCacheTTL();
|
|
50
|
-
const model = this.model.modelName;
|
|
51
|
-
const resultCache = await cache.get(key);
|
|
52
|
-
if (resultCache) {
|
|
53
|
-
const constructor = mongoose.model(model);
|
|
54
|
-
if (_.isArray(resultCache)) {
|
|
55
|
-
return resultCache.map((item) => constructor.hydrate(item));
|
|
56
|
-
}
|
|
57
|
-
else {
|
|
58
|
-
return constructor.hydrate(resultCache);
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
const result = await mongooseExec.call(this);
|
|
62
|
-
cache.set(key, result, ttl).catch((err) => {
|
|
63
|
-
console.error(err);
|
|
64
|
-
});
|
|
65
|
-
return result;
|
|
66
|
-
};
|
|
14
|
+
extendQuery(mongoose, cache);
|
|
15
|
+
extendAggregate(mongoose, cache);
|
|
67
16
|
}
|
|
68
17
|
return this.instance;
|
|
69
18
|
}
|
|
70
19
|
async clear(customKey) {
|
|
71
|
-
if (
|
|
72
|
-
|
|
20
|
+
if (customKey) {
|
|
21
|
+
await this.cache.del(customKey);
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
await this.cache.clear();
|
|
73
25
|
}
|
|
74
|
-
return this.cache.del(customKey);
|
|
75
26
|
}
|
|
76
27
|
async close() {
|
|
77
|
-
|
|
78
|
-
await this.cache.close();
|
|
79
|
-
}
|
|
28
|
+
await this.cache.close();
|
|
80
29
|
}
|
|
81
30
|
}
|
|
82
31
|
export default CacheMongoose;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"crypto.d.ts","sourceRoot":"","sources":["../../../src/crypto.ts"],"names":[],"mappings":"AAGA,wBAAgB,
|
|
1
|
+
{"version":3,"file":"crypto.d.ts","sourceRoot":"","sources":["../../../src/crypto.ts"],"names":[],"mappings":"AAGA,wBAAgB,MAAM,CAAE,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,CAMzF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"aggregate.d.ts","sourceRoot":"","sources":["../../../../src/extend/aggregate.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAA;AACxC,OAAO,KAAK,KAAK,MAAM,gBAAgB,CAAA;AAIvC,MAAM,CAAC,OAAO,UAAU,WAAW,CAAE,QAAQ,EAAE,QAAQ,EAAE,KAAK,EAAE,KAAK,GAAG,IAAI,CA+C3E"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"query.d.ts","sourceRoot":"","sources":["../../../../src/extend/query.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAA;AACxC,OAAO,KAAK,KAAK,MAAM,gBAAgB,CAAA;AAIvC,MAAM,CAAC,OAAO,UAAU,WAAW,CAAE,QAAQ,EAAE,QAAQ,EAAE,KAAK,EAAE,KAAK,GAAG,IAAI,CAwE3E"}
|