@knaus94/prisma-extension-cache-manager 1.3.8 → 1.4.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 +1 -3
- package/dist/index.d.ts +1 -1
- package/dist/index.js +29 -16
- package/dist/types.d.ts +12 -4
- package/dist/types.js +2 -0
- package/package.json +2 -4
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.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ModelExtension, PrismaRedisCacheConfig } from "./types";
|
|
2
|
-
declare const _default: ({ cache }: PrismaRedisCacheConfig) => (client: any) => import("@prisma/client/extension").PrismaClientExtends<import("@prisma/client/runtime/library").InternalArgs<{}, {
|
|
2
|
+
declare const _default: ({ cache, defaultTTL }: PrismaRedisCacheConfig) => (client: any) => import("@prisma/client/extension").PrismaClientExtends<import("@prisma/client/runtime/library").InternalArgs<{}, {
|
|
3
3
|
$allModels: ModelExtension;
|
|
4
4
|
}, {}, {
|
|
5
5
|
$cache: import("cache-manager").Cache;
|
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
|
|
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 });
|
|
@@ -15,7 +18,7 @@ function serializeData(data) {
|
|
|
15
18
|
function deserializeData(serializedData) {
|
|
16
19
|
return JSON.parse(serializedData).data;
|
|
17
20
|
}
|
|
18
|
-
exports.default = ({ cache }) => {
|
|
21
|
+
exports.default = ({ cache, defaultTTL }) => {
|
|
19
22
|
return extension_1.Prisma.defineExtension({
|
|
20
23
|
name: "prisma-extension-cache-manager",
|
|
21
24
|
client: {
|
|
@@ -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
|
-
|
|
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 =
|
|
72
|
-
|
|
73
|
-
|
|
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) {
|
|
@@ -81,20 +93,21 @@ exports.default = ({ cache }) => {
|
|
|
81
93
|
const result = await query(queryArgs);
|
|
82
94
|
if (useUncache)
|
|
83
95
|
processUncache(result);
|
|
84
|
-
const ttl = typeof cacheOption === "number"
|
|
96
|
+
const ttl = typeof cacheOption === "number"
|
|
97
|
+
? cacheOption
|
|
98
|
+
: defaultTTL ?? undefined;
|
|
85
99
|
await cache.set(cacheKey, serializeData(result), ttl);
|
|
86
100
|
return result;
|
|
87
101
|
}
|
|
88
|
-
|
|
89
|
-
if (typeof key === "function") {
|
|
102
|
+
if (typeof cacheOption.key === "function") {
|
|
90
103
|
const result = await query(queryArgs);
|
|
91
104
|
if (useUncache)
|
|
92
105
|
processUncache(result);
|
|
93
|
-
const customCacheKey = key(result);
|
|
94
|
-
await cache.set(customCacheKey, serializeData(result), ttl);
|
|
106
|
+
const customCacheKey = cacheOption.key(result);
|
|
107
|
+
await cache.set(customCacheKey, serializeData(result), cacheOption.ttl ?? defaultTTL);
|
|
95
108
|
return result;
|
|
96
109
|
}
|
|
97
|
-
const customCacheKey = key ||
|
|
110
|
+
const customCacheKey = createKey(cacheOption.key, cacheOption.namespace) ||
|
|
98
111
|
generateComposedKey({
|
|
99
112
|
model,
|
|
100
113
|
queryArgs,
|
|
@@ -108,7 +121,7 @@ exports.default = ({ cache }) => {
|
|
|
108
121
|
const result = await query(queryArgs);
|
|
109
122
|
if (useUncache)
|
|
110
123
|
processUncache(result);
|
|
111
|
-
await cache.set(customCacheKey, serializeData(result), ttl);
|
|
124
|
+
await cache.set(customCacheKey, serializeData(result), cacheOption.ttl ?? defaultTTL);
|
|
112
125
|
return result;
|
|
113
126
|
},
|
|
114
127
|
},
|
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,16 +17,24 @@ 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;
|
|
38
|
+
defaultTTL?: number;
|
|
31
39
|
}
|
|
32
40
|
export {};
|
package/dist/types.js
CHANGED
package/package.json
CHANGED
|
@@ -1,12 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@knaus94/prisma-extension-cache-manager",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.0",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "https://github.com/knaus94/prisma-extension-cache-manager.git"
|
|
7
7
|
},
|
|
8
|
-
"homepage": "https://github.com/knaus94/prisma-extension-cache-manager",
|
|
9
|
-
"bugs": "https://github.com/knaus94/prisma-extension-cache-manager/issues",
|
|
10
8
|
"author": {
|
|
11
9
|
"name": "knaus94",
|
|
12
10
|
"url": "https://github.com/knaus94"
|
|
@@ -59,4 +57,4 @@
|
|
|
59
57
|
"@prisma/client": "^5.7.1",
|
|
60
58
|
"cache-manager": "^5.2.1"
|
|
61
59
|
}
|
|
62
|
-
}
|
|
60
|
+
}
|