@knaus94/prisma-extension-cache-manager 1.3.8 → 1.3.9

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
@@ -27,9 +27,7 @@ async function main() {
27
27
  ttl: 10000,
28
28
  max: 200,
29
29
  });
30
- const prisma = new PrismaClient().$extends(
31
- cacheExtension({ cache })
32
- );
30
+ const prisma = new PrismaClient().$extends(cacheExtension({ cache }));
33
31
  await prisma.user.findUniqueOrThrow({
34
32
  where: {
35
33
  email: user.email,
package/dist/index.js CHANGED
@@ -7,7 +7,10 @@ function generateComposedKey(options) {
7
7
  const hash = (0, crypto_1.createHash)("md5")
8
8
  .update(JSON.stringify(options?.queryArgs))
9
9
  .digest("hex");
10
- return `Prisma@${options.model}@${hash}`;
10
+ return `${options.model}@${hash}`;
11
+ }
12
+ function createKey(key, namespace) {
13
+ return namespace ? `${namespace}:${key}` : key;
11
14
  }
12
15
  function serializeData(data) {
13
16
  return JSON.stringify({ data });
@@ -31,6 +34,8 @@ exports.default = ({ cache }) => {
31
34
  return query(args);
32
35
  const isWriteOperation = [
33
36
  "create",
37
+ "createMany",
38
+ "updateMany",
34
39
  "upsert",
35
40
  "update",
36
41
  ].includes(operation);
@@ -46,7 +51,12 @@ exports.default = ({ cache }) => {
46
51
  keysToDelete = [option];
47
52
  }
48
53
  else if (Array.isArray(option)) {
49
- keysToDelete = option;
54
+ if (typeof option[0] === "string") {
55
+ keysToDelete = option;
56
+ }
57
+ else if (typeof option[0] === "object") {
58
+ keysToDelete = option.map((obj) => obj.namespace ? `${obj.namespace}:${obj.key}` : obj.key);
59
+ }
50
60
  }
51
61
  if (!keysToDelete.length)
52
62
  return true;
@@ -56,7 +66,7 @@ exports.default = ({ cache }) => {
56
66
  .catch(() => false);
57
67
  }
58
68
  const useCache = cacheOption !== undefined &&
59
- ["boolean", "object", "number"].includes(typeof cacheOption);
69
+ ["boolean", "object", "number", "string"].includes(typeof cacheOption);
60
70
  const useUncache = uncacheOption !== undefined &&
61
71
  (typeof uncacheOption === "function" ||
62
72
  typeof uncacheOption === "string" ||
@@ -67,11 +77,13 @@ exports.default = ({ cache }) => {
67
77
  processUncache(result);
68
78
  return result;
69
79
  }
70
- if (["boolean", "number"].includes(typeof cacheOption)) {
71
- const cacheKey = generateComposedKey({
72
- model,
73
- queryArgs,
74
- });
80
+ if (["boolean", "number", "string"].includes(typeof cacheOption)) {
81
+ const cacheKey = typeof cacheOption === "string"
82
+ ? cacheOption
83
+ : generateComposedKey({
84
+ model,
85
+ queryArgs,
86
+ });
75
87
  if (!isWriteOperation) {
76
88
  const cached = await cache.get(cacheKey);
77
89
  if (cached) {
@@ -85,16 +97,15 @@ exports.default = ({ cache }) => {
85
97
  await cache.set(cacheKey, serializeData(result), ttl);
86
98
  return result;
87
99
  }
88
- const { key, ttl } = cacheOption;
89
- if (typeof key === "function") {
100
+ if (typeof cacheOption.key === "function") {
90
101
  const result = await query(queryArgs);
91
102
  if (useUncache)
92
103
  processUncache(result);
93
- const customCacheKey = key(result);
94
- await cache.set(customCacheKey, serializeData(result), ttl);
104
+ const customCacheKey = cacheOption.key(result);
105
+ await cache.set(customCacheKey, serializeData(result), cacheOption.ttl);
95
106
  return result;
96
107
  }
97
- const customCacheKey = key ||
108
+ const customCacheKey = createKey(cacheOption.key, cacheOption.namespace) ||
98
109
  generateComposedKey({
99
110
  model,
100
111
  queryArgs,
@@ -108,7 +119,7 @@ exports.default = ({ cache }) => {
108
119
  const result = await query(queryArgs);
109
120
  if (useUncache)
110
121
  processUncache(result);
111
- await cache.set(customCacheKey, serializeData(result), ttl);
122
+ await cache.set(customCacheKey, serializeData(result), cacheOption.ttl);
112
123
  return result;
113
124
  },
114
125
  },
package/dist/types.d.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  import { Prisma } from "@prisma/client/extension";
2
2
  import { Cache } from "cache-manager";
3
- export declare const REQUIRED_ARGS_OPERATIONS: readonly ["delete", "findUnique", "findUniqueOrThrow", "groupBy", "update", "upsert", "create"];
3
+ export declare const REQUIRED_ARGS_OPERATIONS: readonly ["delete", "findUnique", "findUniqueOrThrow", "groupBy", "update", "upsert", "create", "createMany", "updateMany"];
4
4
  export declare const OPTIONAL_ARGS_OPERATIONS: readonly ["findMany", "findFirst", "findFirstOrThrow", "count"];
5
- export declare const CACHE_OPERATIONS: readonly ["delete", "findUnique", "findUniqueOrThrow", "groupBy", "update", "upsert", "create", "findMany", "findFirst", "findFirstOrThrow", "count"];
5
+ export declare const CACHE_OPERATIONS: readonly ["delete", "findUnique", "findUniqueOrThrow", "groupBy", "update", "upsert", "create", "createMany", "updateMany", "findMany", "findFirst", "findFirstOrThrow", "count"];
6
6
  type RequiredArgsOperation = (typeof REQUIRED_ARGS_OPERATIONS)[number];
7
7
  type OptionalArgsOperation = (typeof OPTIONAL_ARGS_OPERATIONS)[number];
8
8
  type RequiredArgsFunction<O extends RequiredArgsOperation> = <T, A>(this: T, args: Prisma.Exact<A, Prisma.Args<T, O> & PrismaCacheArgs<T, A, O>>) => Promise<Prisma.Result<T, A, O>>;
@@ -17,14 +17,21 @@ export interface CacheOptions<T, A, O extends RequiredArgsOperation | OptionalAr
17
17
  * Cache key
18
18
  */
19
19
  key: ((result: Prisma.Result<T, A, O>) => string) | string;
20
+ /**
21
+ * Cache namespace
22
+ */
23
+ namespace?: string;
20
24
  /**
21
25
  * Time to live
22
26
  */
23
27
  ttl?: number;
24
28
  }
25
29
  export interface PrismaCacheArgs<T, A, O extends RequiredArgsOperation | OptionalArgsOperation> {
26
- cache?: boolean | number | CacheOptions<T, A, O>;
27
- uncache?: ((result: Prisma.Result<T, A, O>) => string[] | string) | string | string[];
30
+ cache?: boolean | number | string | CacheOptions<T, A, O>;
31
+ uncache?: ((result: Prisma.Result<T, A, O>) => string[] | string) | string | string[] | {
32
+ key: string;
33
+ namespace?: string;
34
+ }[];
28
35
  }
29
36
  export interface PrismaRedisCacheConfig {
30
37
  cache: Cache;
package/dist/types.js CHANGED
@@ -9,6 +9,8 @@ exports.REQUIRED_ARGS_OPERATIONS = [
9
9
  "update",
10
10
  "upsert",
11
11
  "create",
12
+ "createMany",
13
+ "updateMany",
12
14
  ];
13
15
  exports.OPTIONAL_ARGS_OPERATIONS = [
14
16
  "findMany",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@knaus94/prisma-extension-cache-manager",
3
- "version": "1.3.8",
3
+ "version": "1.3.9",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/knaus94/prisma-extension-cache-manager.git"
@@ -59,4 +59,4 @@
59
59
  "@prisma/client": "^5.7.1",
60
60
  "cache-manager": "^5.2.1"
61
61
  }
62
- }
62
+ }