@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 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 hashKey = (m, a) => `${m}@${(0, object_code_1.hash)(a)}`;
7
+ const composedKey = (m, a) => `${m}@${(0, object_code_1.hash)(a)}`;
8
8
  const ns = (k, n) => (n ? `${n}:${k}` : k);
9
- const WRITE_OPS = new Set([
10
- "create",
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
- ? Array(opt(res))
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
- const finish = async (res) => {
35
- const dels = toUncache(uncache, res);
36
- if (dels.length)
37
- cache.mdel(dels).catch(() => { });
38
- return res;
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 ttl;
40
+ let ttlMs;
45
41
  if (typeof cOpt !== "object" || Array.isArray(cOpt)) {
46
- key = typeof cOpt === "string" ? cOpt : hashKey(model, queryArgs);
42
+ key =
43
+ typeof cOpt === "string" ? cOpt : composedKey(model, queryArgs);
47
44
  if (typeof cOpt === "number")
48
- ttl = cOpt;
45
+ ttlMs = cOpt;
49
46
  }
50
47
  else if (typeof cOpt.key === "function") {
51
- const res = await query(queryArgs);
52
- await finish(res);
53
- key = cOpt.key(res);
54
- ttl = cOpt.ttl; // как есть
55
- try {
56
- await cache.set(key, res, ttl);
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 ?? hashKey(model, queryArgs), cOpt.namespace);
63
- ttl = cOpt.ttl;
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
- /* DB round-trip */
75
- const res = await query(queryArgs);
76
- await finish(res);
77
- try {
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
- catch { }
81
- return res;
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
@@ -35,7 +35,5 @@ export interface PrismaCacheArgs<T, A, O extends RequiredArgsOperation | Optiona
35
35
  }
36
36
  export interface PrismaRedisCacheConfig {
37
37
  cache: Cache;
38
- debug?: boolean;
39
- ttl?: number;
40
38
  }
41
39
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@knaus94/prisma-extension-cache-manager",
3
- "version": "2.0.1",
3
+ "version": "2.0.2",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/knaus94/prisma-extension-cache-manager.git"