Skip to content

Commit 7dbefee

Browse files
committed
Add superjson.
1 parent 5bdfb58 commit 7dbefee

4 files changed

Lines changed: 35 additions & 2 deletions

File tree

bun.lock

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/cache/package.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@
3131
"require": "./dist/schemas.cjs",
3232
"import": "./dist/schemas.mjs"
3333
},
34+
"./superjson": {
35+
"@zenstack-cache/source": "./src/superjson.ts",
36+
"require": "./dist/superjson.cjs",
37+
"import": "./dist/superjson.mjs"
38+
},
3439
"./types": {
3540
"@zenstack-cache/source": "./src/types.ts",
3641
"require": "./dist/types.cjs",
@@ -65,6 +70,10 @@
6570
"require": "./dist/schemas.cjs",
6671
"import": "./dist/schemas.mjs"
6772
},
73+
"./superjson": {
74+
"require": "./dist/superjson.cjs",
75+
"import": "./dist/superjson.mjs"
76+
},
6877
"./types": {
6978
"require": "./dist/types.cjs",
7079
"import": "./dist/types.mjs"
@@ -86,9 +95,11 @@
8695
"test:watch": "vitest --watch"
8796
},
8897
"dependencies": {
98+
"decimal.js": "^10.6.0",
8999
"ioredis": "catalog:",
90100
"murmurhash": "^2.0.1",
91101
"stable-hash": "^0.0.6",
102+
"superjson": "^2.2.2",
92103
"zod": "catalog:"
93104
},
94105
"devDependencies": {

packages/cache/src/providers/redis.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { CacheInvalidationOptions, CacheProvider, CacheEntry } from '../types'
22
import { Redis } from 'ioredis'
33
import { getTotalTtl } from '../utils'
4+
import { superjson } from '../superjson'
45

56
export class RedisCacheProvider implements CacheProvider {
67
private readonly redis: Redis
@@ -16,14 +17,14 @@ export class RedisCacheProvider implements CacheProvider {
1617
return undefined
1718
}
1819

19-
return JSON.parse(entryJson) as CacheEntry
20+
return superjson.parse(entryJson) as CacheEntry
2021
}
2122

2223
async set(key: string, entry: CacheEntry) {
2324
const multi = this.redis.multi()
2425
const formattedKey = formatQueryKey(key)
2526

26-
multi.set(formattedKey, JSON.stringify(entry))
27+
multi.set(formattedKey, superjson.stringify(entry))
2728

2829
const totalTtl = getTotalTtl(entry)
2930

packages/cache/src/superjson.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Decimal } from 'decimal.js'
2+
import superjson from 'superjson'
3+
4+
superjson.registerCustom<Decimal, string>(
5+
{
6+
isApplicable: (v): v is Decimal => Decimal.isDecimal(v),
7+
serialize: v => v.toJSON(),
8+
deserialize: v => new Decimal(v),
9+
},
10+
'decimal.js',
11+
)
12+
13+
export { superjson }

0 commit comments

Comments
 (0)