Skip to content

Commit 4ba6ad6

Browse files
authored
fix: handle auth separately (#11)
1 parent 18e1693 commit 4ba6ad6

File tree

10 files changed

+375
-92
lines changed

10 files changed

+375
-92
lines changed

.devcontainer/devcontainer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"tamasfe.even-better-toml",
1717
"oven.bun-vscode",
1818
"oxc.oxc-vscode",
19-
"vitest.explorer"
19+
"vitest.explorer",
20+
"zenstack.zenstack-v3"
2021
]
2122
}
2223
}

.vscode/extensions.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"tamasfe.even-better-toml",
66
"oven.bun-vscode",
77
"oxc.oxc-vscode",
8-
"vitest.explorer"
8+
"vitest.explorer",
9+
"zenstack.zenstack-v3"
910
]
1011
}

packages/cache/package.json

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
{
22
"name": "@visualbravo/zenstack-cache",
33
"version": "1.0.2",
4+
"keywords": [
5+
"accelerate",
6+
"cache",
7+
"caching",
8+
"orm",
9+
"prisma",
10+
"redis",
11+
"self-hosted",
12+
"typescript",
13+
"zenstack"
14+
],
415
"license": "MIT",
516
"repository": "github:visualbravo/zenstack-cache",
617
"type": "module",
718
"main": "./dist/index.cjs",
819
"module": "./dist/index.mjs",
920
"types": "./dist/index.d.cts",
10-
"keywords": [
11-
"zenstack",
12-
"cache",
13-
"caching",
14-
"prisma",
15-
"accelerate",
16-
"orm"
17-
],
1821
"exports": {
1922
".": {
2023
"@zenstack-cache/source": "./src/index.ts",

packages/cache/src/plugin.ts

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,28 @@ export function defineCachePlugin(pluginOptions: CachePluginOptions) {
5656
},
5757
},
5858

59-
onQuery: async ({ args, model, operation, proceed }) => {
59+
onQuery: async ({ args, model, operation, proceed, client }) => {
6060
if (args && 'cache' in args) {
61-
const json = stableHash({
62-
args,
63-
model,
64-
operation,
65-
})
61+
let json: string
62+
63+
if (client.$auth) {
64+
const userId = Object.keys(client.$auth)
65+
.filter(key => client.$schema.models[client.$schema.authType!]!.idFields.includes(key))
66+
.join('_')
67+
68+
json = stableHash({
69+
args,
70+
model,
71+
operation,
72+
userId,
73+
})
74+
} else {
75+
json = stableHash({
76+
args,
77+
model,
78+
operation,
79+
})
80+
}
6681

6782
if (!json) {
6883
throw new Error(
@@ -114,4 +129,4 @@ export function defineCachePlugin(pluginOptions: CachePluginOptions) {
114129
return proceed(args)
115130
},
116131
})
117-
}
132+
}

packages/cache/tests/memory.test.ts

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { defineCachePlugin } from '../src'
44
import { SqliteDialect } from 'kysely'
55
import SQLite from 'better-sqlite3'
66
import { MemoryCacheProvider } from '../src/providers/memory'
7-
import { schema } from './schemas/basic'
7+
import { schema } from './schemas/basic/schema'
88

99
describe('Cache plugin (memory)', () => {
1010
let db: ClientContract<typeof schema>
@@ -1095,4 +1095,62 @@ describe('Cache plugin (memory)', () => {
10951095
}),
10961096
).rejects.toThrow('Invalid findMany')
10971097
})
1098+
1099+
it('caches auth separately', async () => {
1100+
let extDb = db.$use(
1101+
defineCachePlugin({
1102+
provider: new MemoryCacheProvider(),
1103+
}),
1104+
)
1105+
1106+
await expect(
1107+
extDb.user.exists({
1108+
where: {
1109+
id: '1',
1110+
},
1111+
1112+
cache: {},
1113+
}),
1114+
).resolves.toBe(false)
1115+
1116+
await extDb.user.create({
1117+
data: {
1118+
createdAt: new Date(),
1119+
email: 'test@email.com',
1120+
id: '1',
1121+
name: 'test',
1122+
role: 'USER',
1123+
updatedAt: new Date(),
1124+
},
1125+
})
1126+
1127+
await expect(
1128+
extDb.user.exists({
1129+
where: {
1130+
id: '1',
1131+
},
1132+
1133+
cache: {},
1134+
}),
1135+
).resolves.toBe(false)
1136+
1137+
extDb = extDb.$setAuth({
1138+
createdAt: new Date(),
1139+
email: 'test@email.com',
1140+
id: '1',
1141+
name: 'test',
1142+
role: 'USER',
1143+
updatedAt: new Date(),
1144+
})
1145+
1146+
await expect(
1147+
extDb.user.exists({
1148+
where: {
1149+
id: '1',
1150+
},
1151+
1152+
cache: {},
1153+
}),
1154+
).resolves.toBe(true)
1155+
})
10981156
})

packages/cache/tests/redis.test.ts

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { defineCachePlugin } from '../src'
44
import { SqliteDialect } from 'kysely'
55
import SQLite from 'better-sqlite3'
66
import { RedisCacheProvider } from '../src/providers/redis'
7-
import { schema } from './schemas/basic'
7+
import { schema } from './schemas/basic/schema'
88
import { Redis, Pipeline } from 'ioredis'
99

1010
const expire = vi.spyOn(Pipeline.prototype, 'expire')
@@ -1131,4 +1131,64 @@ describe('Cache plugin (redis)', () => {
11311131
await expect(redis.ttl('zenstack:cache:tag:test2')).resolves.toBeCloseTo(80, 2)
11321132
await expect(redis.ttl('zenstack:cache:tag:test3')).resolves.toBeCloseTo(80, 2)
11331133
})
1134+
1135+
it('caches auth separately', async () => {
1136+
let extDb = db.$use(
1137+
defineCachePlugin({
1138+
provider: new RedisCacheProvider({
1139+
url: process.env['REDIS_URL'] as string,
1140+
}),
1141+
}),
1142+
)
1143+
1144+
await expect(
1145+
extDb.user.exists({
1146+
where: {
1147+
id: '1',
1148+
},
1149+
1150+
cache: {},
1151+
}),
1152+
).resolves.toBe(false)
1153+
1154+
await extDb.user.create({
1155+
data: {
1156+
createdAt: new Date(),
1157+
email: 'test@email.com',
1158+
id: '1',
1159+
name: 'test',
1160+
role: 'USER',
1161+
updatedAt: new Date(),
1162+
},
1163+
})
1164+
1165+
await expect(
1166+
extDb.user.exists({
1167+
where: {
1168+
id: '1',
1169+
},
1170+
1171+
cache: {},
1172+
}),
1173+
).resolves.toBe(false)
1174+
1175+
extDb = extDb.$setAuth({
1176+
createdAt: new Date(),
1177+
email: 'test@email.com',
1178+
id: '1',
1179+
name: 'test',
1180+
role: 'USER',
1181+
updatedAt: new Date(),
1182+
})
1183+
1184+
await expect(
1185+
extDb.user.exists({
1186+
where: {
1187+
id: '1',
1188+
},
1189+
1190+
cache: {},
1191+
}),
1192+
).resolves.toBe(true)
1193+
})
11341194
})
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
//////////////////////////////////////////////////////////////////////////////////////////////
2+
// DO NOT MODIFY THIS FILE //
3+
// This file is automatically generated by ZenStack CLI and should not be manually updated. //
4+
//////////////////////////////////////////////////////////////////////////////////////////////
5+
6+
/* eslint-disable */
7+
8+
import { type SchemaType as $Schema } from './schema'
9+
import type {
10+
FindManyArgs as $FindManyArgs,
11+
FindUniqueArgs as $FindUniqueArgs,
12+
FindFirstArgs as $FindFirstArgs,
13+
ExistsArgs as $ExistsArgs,
14+
CreateArgs as $CreateArgs,
15+
CreateManyArgs as $CreateManyArgs,
16+
CreateManyAndReturnArgs as $CreateManyAndReturnArgs,
17+
UpdateArgs as $UpdateArgs,
18+
UpdateManyArgs as $UpdateManyArgs,
19+
UpdateManyAndReturnArgs as $UpdateManyAndReturnArgs,
20+
UpsertArgs as $UpsertArgs,
21+
DeleteArgs as $DeleteArgs,
22+
DeleteManyArgs as $DeleteManyArgs,
23+
CountArgs as $CountArgs,
24+
AggregateArgs as $AggregateArgs,
25+
GroupByArgs as $GroupByArgs,
26+
WhereInput as $WhereInput,
27+
SelectInput as $SelectInput,
28+
IncludeInput as $IncludeInput,
29+
OmitInput as $OmitInput,
30+
QueryOptions as $QueryOptions,
31+
} from '@zenstackhq/orm'
32+
import type {
33+
SimplifiedPlainResult as $Result,
34+
SelectIncludeOmit as $SelectIncludeOmit,
35+
} from '@zenstackhq/orm'
36+
export type UserFindManyArgs = $FindManyArgs<$Schema, 'User'>
37+
export type UserFindUniqueArgs = $FindUniqueArgs<$Schema, 'User'>
38+
export type UserFindFirstArgs = $FindFirstArgs<$Schema, 'User'>
39+
export type UserExistsArgs = $ExistsArgs<$Schema, 'User'>
40+
export type UserCreateArgs = $CreateArgs<$Schema, 'User'>
41+
export type UserCreateManyArgs = $CreateManyArgs<$Schema, 'User'>
42+
export type UserCreateManyAndReturnArgs = $CreateManyAndReturnArgs<$Schema, 'User'>
43+
export type UserUpdateArgs = $UpdateArgs<$Schema, 'User'>
44+
export type UserUpdateManyArgs = $UpdateManyArgs<$Schema, 'User'>
45+
export type UserUpdateManyAndReturnArgs = $UpdateManyAndReturnArgs<$Schema, 'User'>
46+
export type UserUpsertArgs = $UpsertArgs<$Schema, 'User'>
47+
export type UserDeleteArgs = $DeleteArgs<$Schema, 'User'>
48+
export type UserDeleteManyArgs = $DeleteManyArgs<$Schema, 'User'>
49+
export type UserCountArgs = $CountArgs<$Schema, 'User'>
50+
export type UserAggregateArgs = $AggregateArgs<$Schema, 'User'>
51+
export type UserGroupByArgs = $GroupByArgs<$Schema, 'User'>
52+
export type UserWhereInput = $WhereInput<$Schema, 'User'>
53+
export type UserSelect = $SelectInput<$Schema, 'User'>
54+
export type UserInclude = $IncludeInput<$Schema, 'User'>
55+
export type UserOmit = $OmitInput<$Schema, 'User'>
56+
export type UserGetPayload<
57+
Args extends $SelectIncludeOmit<$Schema, 'User', true>,
58+
Options extends $QueryOptions<$Schema> = $QueryOptions<$Schema>,
59+
> = $Result<$Schema, 'User', Args, Options>
60+
export type PostFindManyArgs = $FindManyArgs<$Schema, 'Post'>
61+
export type PostFindUniqueArgs = $FindUniqueArgs<$Schema, 'Post'>
62+
export type PostFindFirstArgs = $FindFirstArgs<$Schema, 'Post'>
63+
export type PostExistsArgs = $ExistsArgs<$Schema, 'Post'>
64+
export type PostCreateArgs = $CreateArgs<$Schema, 'Post'>
65+
export type PostCreateManyArgs = $CreateManyArgs<$Schema, 'Post'>
66+
export type PostCreateManyAndReturnArgs = $CreateManyAndReturnArgs<$Schema, 'Post'>
67+
export type PostUpdateArgs = $UpdateArgs<$Schema, 'Post'>
68+
export type PostUpdateManyArgs = $UpdateManyArgs<$Schema, 'Post'>
69+
export type PostUpdateManyAndReturnArgs = $UpdateManyAndReturnArgs<$Schema, 'Post'>
70+
export type PostUpsertArgs = $UpsertArgs<$Schema, 'Post'>
71+
export type PostDeleteArgs = $DeleteArgs<$Schema, 'Post'>
72+
export type PostDeleteManyArgs = $DeleteManyArgs<$Schema, 'Post'>
73+
export type PostCountArgs = $CountArgs<$Schema, 'Post'>
74+
export type PostAggregateArgs = $AggregateArgs<$Schema, 'Post'>
75+
export type PostGroupByArgs = $GroupByArgs<$Schema, 'Post'>
76+
export type PostWhereInput = $WhereInput<$Schema, 'Post'>
77+
export type PostSelect = $SelectInput<$Schema, 'Post'>
78+
export type PostInclude = $IncludeInput<$Schema, 'Post'>
79+
export type PostOmit = $OmitInput<$Schema, 'Post'>
80+
export type PostGetPayload<
81+
Args extends $SelectIncludeOmit<$Schema, 'Post', true>,
82+
Options extends $QueryOptions<$Schema> = $QueryOptions<$Schema>,
83+
> = $Result<$Schema, 'Post', Args, Options>
84+
export type CommentFindManyArgs = $FindManyArgs<$Schema, 'Comment'>
85+
export type CommentFindUniqueArgs = $FindUniqueArgs<$Schema, 'Comment'>
86+
export type CommentFindFirstArgs = $FindFirstArgs<$Schema, 'Comment'>
87+
export type CommentExistsArgs = $ExistsArgs<$Schema, 'Comment'>
88+
export type CommentCreateArgs = $CreateArgs<$Schema, 'Comment'>
89+
export type CommentCreateManyArgs = $CreateManyArgs<$Schema, 'Comment'>
90+
export type CommentCreateManyAndReturnArgs = $CreateManyAndReturnArgs<$Schema, 'Comment'>
91+
export type CommentUpdateArgs = $UpdateArgs<$Schema, 'Comment'>
92+
export type CommentUpdateManyArgs = $UpdateManyArgs<$Schema, 'Comment'>
93+
export type CommentUpdateManyAndReturnArgs = $UpdateManyAndReturnArgs<$Schema, 'Comment'>
94+
export type CommentUpsertArgs = $UpsertArgs<$Schema, 'Comment'>
95+
export type CommentDeleteArgs = $DeleteArgs<$Schema, 'Comment'>
96+
export type CommentDeleteManyArgs = $DeleteManyArgs<$Schema, 'Comment'>
97+
export type CommentCountArgs = $CountArgs<$Schema, 'Comment'>
98+
export type CommentAggregateArgs = $AggregateArgs<$Schema, 'Comment'>
99+
export type CommentGroupByArgs = $GroupByArgs<$Schema, 'Comment'>
100+
export type CommentWhereInput = $WhereInput<$Schema, 'Comment'>
101+
export type CommentSelect = $SelectInput<$Schema, 'Comment'>
102+
export type CommentInclude = $IncludeInput<$Schema, 'Comment'>
103+
export type CommentOmit = $OmitInput<$Schema, 'Comment'>
104+
export type CommentGetPayload<
105+
Args extends $SelectIncludeOmit<$Schema, 'Comment', true>,
106+
Options extends $QueryOptions<$Schema> = $QueryOptions<$Schema>,
107+
> = $Result<$Schema, 'Comment', Args, Options>
108+
export type ProfileFindManyArgs = $FindManyArgs<$Schema, 'Profile'>
109+
export type ProfileFindUniqueArgs = $FindUniqueArgs<$Schema, 'Profile'>
110+
export type ProfileFindFirstArgs = $FindFirstArgs<$Schema, 'Profile'>
111+
export type ProfileExistsArgs = $ExistsArgs<$Schema, 'Profile'>
112+
export type ProfileCreateArgs = $CreateArgs<$Schema, 'Profile'>
113+
export type ProfileCreateManyArgs = $CreateManyArgs<$Schema, 'Profile'>
114+
export type ProfileCreateManyAndReturnArgs = $CreateManyAndReturnArgs<$Schema, 'Profile'>
115+
export type ProfileUpdateArgs = $UpdateArgs<$Schema, 'Profile'>
116+
export type ProfileUpdateManyArgs = $UpdateManyArgs<$Schema, 'Profile'>
117+
export type ProfileUpdateManyAndReturnArgs = $UpdateManyAndReturnArgs<$Schema, 'Profile'>
118+
export type ProfileUpsertArgs = $UpsertArgs<$Schema, 'Profile'>
119+
export type ProfileDeleteArgs = $DeleteArgs<$Schema, 'Profile'>
120+
export type ProfileDeleteManyArgs = $DeleteManyArgs<$Schema, 'Profile'>
121+
export type ProfileCountArgs = $CountArgs<$Schema, 'Profile'>
122+
export type ProfileAggregateArgs = $AggregateArgs<$Schema, 'Profile'>
123+
export type ProfileGroupByArgs = $GroupByArgs<$Schema, 'Profile'>
124+
export type ProfileWhereInput = $WhereInput<$Schema, 'Profile'>
125+
export type ProfileSelect = $SelectInput<$Schema, 'Profile'>
126+
export type ProfileInclude = $IncludeInput<$Schema, 'Profile'>
127+
export type ProfileOmit = $OmitInput<$Schema, 'Profile'>
128+
export type ProfileGetPayload<
129+
Args extends $SelectIncludeOmit<$Schema, 'Profile', true>,
130+
Options extends $QueryOptions<$Schema> = $QueryOptions<$Schema>,
131+
> = $Result<$Schema, 'Profile', Args, Options>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//////////////////////////////////////////////////////////////////////////////////////////////
2+
// DO NOT MODIFY THIS FILE //
3+
// This file is automatically generated by ZenStack CLI and should not be manually updated. //
4+
//////////////////////////////////////////////////////////////////////////////////////////////
5+
6+
/* eslint-disable */
7+
8+
import { schema as $schema, type SchemaType as $Schema } from './schema'
9+
import {
10+
type ModelResult as $ModelResult,
11+
type TypeDefResult as $TypeDefResult,
12+
} from '@zenstackhq/orm'
13+
export type User = $ModelResult<$Schema, 'User'>
14+
export type Post = $ModelResult<$Schema, 'Post'>
15+
export type Comment = $ModelResult<$Schema, 'Comment'>
16+
export type Profile = $ModelResult<$Schema, 'Profile'>
17+
export type CommonFields = $TypeDefResult<$Schema, 'CommonFields'>
18+
export const Role = $schema.enums.Role.values
19+
export type Role = (typeof Role)[keyof typeof Role]

0 commit comments

Comments
 (0)