Skip to content

Commit 8bdefd6

Browse files
author
A-J Roos
authored
Add custom transformer options (#503)
2 parents 5aebbc7 + 506f3e2 commit 8bdefd6

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,27 @@ Options:
200200
});
201201
```
202202

203+
- `transformer`: (optional) the transformer to used to serialize and deserialize the cache entries. It must be an object
204+
with the following methods:
205+
206+
- `serialize`: a function that receives the result of the original function and returns a serializable object.
207+
- `deserialize`: a function that receives the serialized object and returns the original result.
208+
209+
- Default is `undefined`, so the default transformer is used.
210+
211+
Example
212+
213+
```js
214+
import superjson from "superjson";
215+
216+
createPrismaRedisCache({
217+
transformer: {
218+
serialize: (result) => superjson.serialize(result),
219+
deserialize: (serialized) => superjson.deserialize(serialized),
220+
},
221+
});
222+
```
223+
203224
## Debugging
204225

205226
You can pass functions for `onMiss`, `onHit`, `onError` and `onDedupe` to `createPrismaRedisCache` which can then be

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export const createPrismaRedisCache = ({
2424
cacheTime = DEFAULT_CACHE_TIME,
2525
excludeModels = [],
2626
excludeMethods = [],
27+
transformer,
2728
}: CreatePrismaRedisCache) => {
2829
// Default options for "async-cache-dedupe"
2930
const cacheOptions = {
@@ -33,6 +34,7 @@ export const createPrismaRedisCache = ({
3334
onMiss,
3435
storage,
3536
ttl: cacheTime,
37+
transformer,
3638
};
3739

3840
const cache: any = createCache(cacheOptions);

src/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,8 @@ export type CreatePrismaRedisCache = {
8585
onHit?: (key: string) => void;
8686
onMiss?: (key: string) => void;
8787
onDedupe?: (key: string) => void;
88+
transformer?: {
89+
serialize: (data: any) => any;
90+
deserialize: (data: any) => any;
91+
};
8892
};

0 commit comments

Comments
 (0)