@knaus94/prisma-extension-cache-manager 2.0.1 → 2.0.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/dist/index.js +35 -49
- package/dist/types.d.ts +0 -2
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,25 +1,25 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const types_1 = require("./types");
|
|
4
3
|
const object_code_1 = require("object-code");
|
|
4
|
+
const types_1 = require("./types");
|
|
5
5
|
const extension_1 = require("@prisma/client/extension");
|
|
6
6
|
/* helpers -------------------------------------------------------------- */
|
|
7
|
-
const
|
|
7
|
+
const composedKey = (m, a) => `${m}@${(0, object_code_1.hash)(a)}`;
|
|
8
8
|
const ns = (k, n) => (n ? `${n}:${k}` : k);
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
"createMany",
|
|
12
|
-
"updateMany",
|
|
13
|
-
"upsert",
|
|
14
|
-
"update",
|
|
15
|
-
]);
|
|
9
|
+
const isWrite = (op) => ["create", "createMany", "updateMany", "upsert", "update"].includes(op);
|
|
10
|
+
/* uncache → массив строк */
|
|
16
11
|
const toUncache = (opt, res) => !opt
|
|
17
12
|
? []
|
|
18
13
|
: typeof opt === "function"
|
|
19
|
-
?
|
|
14
|
+
? [].concat(opt(res))
|
|
20
15
|
: typeof opt === "string"
|
|
21
16
|
? [opt]
|
|
22
17
|
: opt.map((o) => typeof o === "string" ? o : ns(o.key, o.namespace));
|
|
18
|
+
const processUncache = async (cache, opt, res) => {
|
|
19
|
+
const keys = toUncache(opt, res);
|
|
20
|
+
if (keys.length)
|
|
21
|
+
await cache.mdel(keys).catch(() => { });
|
|
22
|
+
};
|
|
23
23
|
/* extension ------------------------------------------------------------ */
|
|
24
24
|
exports.default = ({ cache }) => extension_1.Prisma.defineExtension({
|
|
25
25
|
name: "prisma-extension-cache-manager",
|
|
@@ -31,54 +31,40 @@ exports.default = ({ cache }) => extension_1.Prisma.defineExtension({
|
|
|
31
31
|
if (!types_1.CACHE_OPERATIONS.includes(operation))
|
|
32
32
|
return query(args);
|
|
33
33
|
const { cache: cOpt, uncache, ...queryArgs } = args;
|
|
34
|
-
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
};
|
|
40
|
-
if (cOpt === undefined)
|
|
41
|
-
return finish(await query(queryArgs));
|
|
42
|
-
/* key + ttl */
|
|
34
|
+
if (cOpt === undefined) {
|
|
35
|
+
const db = await query(queryArgs);
|
|
36
|
+
await processUncache(cache, uncache, db);
|
|
37
|
+
return db;
|
|
38
|
+
}
|
|
43
39
|
let key;
|
|
44
|
-
let
|
|
40
|
+
let ttlMs;
|
|
45
41
|
if (typeof cOpt !== "object" || Array.isArray(cOpt)) {
|
|
46
|
-
key =
|
|
42
|
+
key =
|
|
43
|
+
typeof cOpt === "string" ? cOpt : composedKey(model, queryArgs);
|
|
47
44
|
if (typeof cOpt === "number")
|
|
48
|
-
|
|
45
|
+
ttlMs = cOpt;
|
|
49
46
|
}
|
|
50
47
|
else if (typeof cOpt.key === "function") {
|
|
51
|
-
const
|
|
52
|
-
await
|
|
53
|
-
key = cOpt.key(
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
}
|
|
58
|
-
catch { }
|
|
59
|
-
return res;
|
|
48
|
+
const db = await query(queryArgs);
|
|
49
|
+
await processUncache(cache, uncache, db);
|
|
50
|
+
key = cOpt.key(db);
|
|
51
|
+
ttlMs = cOpt.ttl ?? undefined;
|
|
52
|
+
await cache.set(key, db, Math.ceil((ttlMs ?? 0) / 1000));
|
|
53
|
+
return db;
|
|
60
54
|
}
|
|
61
55
|
else {
|
|
62
|
-
key = ns(cOpt.key ??
|
|
63
|
-
|
|
64
|
-
}
|
|
65
|
-
/* try cache for reads */
|
|
66
|
-
if (!WRITE_OPS.has(operation)) {
|
|
67
|
-
try {
|
|
68
|
-
const hit = await cache.get(key);
|
|
69
|
-
if (hit != null)
|
|
70
|
-
return hit;
|
|
71
|
-
}
|
|
72
|
-
catch { }
|
|
56
|
+
key = ns(cOpt.key ?? composedKey(model, queryArgs), cOpt.namespace);
|
|
57
|
+
ttlMs = cOpt.ttl ?? undefined;
|
|
73
58
|
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
await cache.set(key, res, ttl);
|
|
59
|
+
if (!isWrite(operation)) {
|
|
60
|
+
const hit = await cache.get(key).catch(() => undefined);
|
|
61
|
+
if (hit !== undefined && hit !== null)
|
|
62
|
+
return hit;
|
|
79
63
|
}
|
|
80
|
-
|
|
81
|
-
|
|
64
|
+
const db = await query(queryArgs);
|
|
65
|
+
await processUncache(cache, uncache, db);
|
|
66
|
+
await cache.set(key, db, ttlMs !== undefined ? Math.ceil(ttlMs / 1000) : undefined);
|
|
67
|
+
return db;
|
|
82
68
|
},
|
|
83
69
|
},
|
|
84
70
|
},
|
package/dist/types.d.ts
CHANGED