@knaus94/prisma-extension-cache-manager 1.3.1 → 1.3.3

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.
Files changed (2) hide show
  1. package/dist/index.js +13 -20
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -10,6 +10,14 @@ function generateComposedKey(options) {
10
10
  .digest("hex");
11
11
  return `Prisma@${options.model}@${hash}`;
12
12
  }
13
+ function toSerialize(value) {
14
+ const serializedResult = (0, bson_1.serialize)({ value });
15
+ return Buffer.from(serializedResult).toString("base64");
16
+ }
17
+ function toDeserialize(value) {
18
+ const binaryData = Buffer.from(value, "base64");
19
+ return (0, bson_1.deserialize)(binaryData).value;
20
+ }
13
21
  exports.default = ({ cache, serialize }) => {
14
22
  return extension_1.Prisma.defineExtension({
15
23
  name: "prisma-extension-cache-manager",
@@ -72,7 +80,7 @@ exports.default = ({ cache, serialize }) => {
72
80
  const cached = await cache.get(cacheKey);
73
81
  if (cached) {
74
82
  if (isBinarySerialize) {
75
- return (0, bson_1.serialize)(Buffer.from(cached, 'base64'));
83
+ return toDeserialize(cached);
76
84
  }
77
85
  return typeof cached === "string" ? JSON.parse(cached) : cached;
78
86
  }
@@ -81,12 +89,7 @@ exports.default = ({ cache, serialize }) => {
81
89
  if (useUncache)
82
90
  processUncache(result);
83
91
  const ttl = typeof cacheOption === "number" ? cacheOption : undefined;
84
- if (isBinarySerialize) {
85
- await cache.set(cacheKey, Buffer.from((0, bson_1.serialize)(result)).toString('base64'), ttl);
86
- }
87
- else {
88
- await cache.set(cacheKey, JSON.stringify(result), ttl);
89
- }
92
+ await cache.set(cacheKey, isBinarySerialize ? toSerialize(result) : JSON.stringify(result), ttl);
90
93
  return result;
91
94
  }
92
95
  const { key, ttl } = cacheOption;
@@ -95,12 +98,7 @@ exports.default = ({ cache, serialize }) => {
95
98
  if (useUncache)
96
99
  processUncache(result);
97
100
  const customCacheKey = key(result);
98
- if (isBinarySerialize) {
99
- await cache.set(customCacheKey, Buffer.from((0, bson_1.serialize)(result)).toString('base64'), ttl);
100
- }
101
- else {
102
- await cache.set(customCacheKey, JSON.stringify(result), ttl);
103
- }
101
+ await cache.set(customCacheKey, isBinarySerialize ? toSerialize(result) : JSON.stringify(result), ttl);
104
102
  return result;
105
103
  }
106
104
  const customCacheKey = key ||
@@ -112,7 +110,7 @@ exports.default = ({ cache, serialize }) => {
112
110
  const cached = await cache.get(customCacheKey);
113
111
  if (cached) {
114
112
  if (isBinarySerialize) {
115
- return (0, bson_1.deserialize)(Buffer.from(cached, 'base64'));
113
+ return toDeserialize(cached);
116
114
  }
117
115
  return typeof cached === "string" ? JSON.parse(cached) : cached;
118
116
  }
@@ -120,12 +118,7 @@ exports.default = ({ cache, serialize }) => {
120
118
  const result = await query(queryArgs);
121
119
  if (useUncache)
122
120
  processUncache(result);
123
- if (isBinarySerialize) {
124
- await cache.set(customCacheKey, Buffer.from((0, bson_1.serialize)(result)).toString('base64'), ttl);
125
- }
126
- else {
127
- await cache.set(customCacheKey, JSON.stringify(result), ttl);
128
- }
121
+ await cache.set(customCacheKey, isBinarySerialize ? toDeserialize(result) : JSON.stringify(result), ttl);
129
122
  return result;
130
123
  },
131
124
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@knaus94/prisma-extension-cache-manager",
3
- "version": "1.3.1",
3
+ "version": "1.3.3",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/knaus94/prisma-extension-cache-manager.git"