Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"tamasfe.even-better-toml",
"oven.bun-vscode",
"oxc.oxc-vscode",
"vitest.explorer"
"vitest.explorer",
"zenstack.zenstack-v3"
]
}
}
Expand Down
3 changes: 2 additions & 1 deletion .vscode/extensions.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"tamasfe.even-better-toml",
"oven.bun-vscode",
"oxc.oxc-vscode",
"vitest.explorer"
"vitest.explorer",
"zenstack.zenstack-v3"
]
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "workspace",
"type": "module",
"version": "1.0.2",
"version": "1.0.3",
"private": "true",
"license": "MIT",
"scripts": {
Expand Down
21 changes: 12 additions & 9 deletions packages/cache/package.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
{
"name": "@visualbravo/zenstack-cache",
"version": "1.0.2",
"version": "1.0.3",
"keywords": [
"accelerate",
"cache",
"caching",
"orm",
"prisma",
"redis",
"self-hosted",
"typescript",
"zenstack"
],
"license": "MIT",
"repository": "github:visualbravo/zenstack-cache",
"type": "module",
"main": "./dist/index.cjs",
"module": "./dist/index.mjs",
"types": "./dist/index.d.cts",
"keywords": [
"zenstack",
"cache",
"caching",
"prisma",
"accelerate",
"orm"
],
"exports": {
".": {
"@zenstack-cache/source": "./src/index.ts",
Expand Down
29 changes: 22 additions & 7 deletions packages/cache/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,28 @@ export function defineCachePlugin(pluginOptions: CachePluginOptions) {
},
},

onQuery: async ({ args, model, operation, proceed }) => {
onQuery: async ({ args, model, operation, proceed, client }) => {
if (args && 'cache' in args) {
const json = stableHash({
args,
model,
operation,
})
let json: string

if (client.$auth) {
const userId = Object.keys(client.$auth)
.filter(key => client.$schema.models[client.$schema.authType!]!.idFields.includes(key))
.join('_')

json = stableHash({
args,
model,
operation,
userId,
})
} else {
json = stableHash({
args,
model,
operation,
})
}

if (!json) {
throw new Error(
Expand Down Expand Up @@ -114,4 +129,4 @@ export function defineCachePlugin(pluginOptions: CachePluginOptions) {
return proceed(args)
},
})
}
}
60 changes: 59 additions & 1 deletion packages/cache/tests/memory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { defineCachePlugin } from '../src'
import { SqliteDialect } from 'kysely'
import SQLite from 'better-sqlite3'
import { MemoryCacheProvider } from '../src/providers/memory'
import { schema } from './schemas/basic'
import { schema } from './schemas/basic/schema'

describe('Cache plugin (memory)', () => {
let db: ClientContract<typeof schema>
Expand Down Expand Up @@ -1095,4 +1095,62 @@ describe('Cache plugin (memory)', () => {
}),
).rejects.toThrow('Invalid findMany')
})

it('caches auth separately', async () => {
let extDb = db.$use(
defineCachePlugin({
provider: new MemoryCacheProvider(),
}),
)

await expect(
extDb.user.exists({
where: {
id: '1',
},

cache: {},
}),
).resolves.toBe(false)

await extDb.user.create({
data: {
createdAt: new Date(),
email: 'test@email.com',
id: '1',
name: 'test',
role: 'USER',
updatedAt: new Date(),
},
})

await expect(
extDb.user.exists({
where: {
id: '1',
},

cache: {},
}),
).resolves.toBe(false)

extDb = extDb.$setAuth({
createdAt: new Date(),
email: 'test@email.com',
id: '1',
name: 'test',
role: 'USER',
updatedAt: new Date(),
})

await expect(
extDb.user.exists({
where: {
id: '1',
},

cache: {},
}),
).resolves.toBe(true)
})
})
62 changes: 61 additions & 1 deletion packages/cache/tests/redis.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { defineCachePlugin } from '../src'
import { SqliteDialect } from 'kysely'
import SQLite from 'better-sqlite3'
import { RedisCacheProvider } from '../src/providers/redis'
import { schema } from './schemas/basic'
import { schema } from './schemas/basic/schema'
import { Redis, Pipeline } from 'ioredis'

const expire = vi.spyOn(Pipeline.prototype, 'expire')
Expand Down Expand Up @@ -1131,4 +1131,64 @@ describe('Cache plugin (redis)', () => {
await expect(redis.ttl('zenstack:cache:tag:test2')).resolves.toBeCloseTo(80, 2)
await expect(redis.ttl('zenstack:cache:tag:test3')).resolves.toBeCloseTo(80, 2)
})

it('caches auth separately', async () => {
let extDb = db.$use(
defineCachePlugin({
provider: new RedisCacheProvider({
url: process.env['REDIS_URL'] as string,
}),
}),
)

await expect(
extDb.user.exists({
where: {
id: '1',
},

cache: {},
}),
).resolves.toBe(false)

await extDb.user.create({
data: {
createdAt: new Date(),
email: 'test@email.com',
id: '1',
name: 'test',
role: 'USER',
updatedAt: new Date(),
},
})

await expect(
extDb.user.exists({
where: {
id: '1',
},

cache: {},
}),
).resolves.toBe(false)

extDb = extDb.$setAuth({
createdAt: new Date(),
email: 'test@email.com',
id: '1',
name: 'test',
role: 'USER',
updatedAt: new Date(),
})

await expect(
extDb.user.exists({
where: {
id: '1',
},

cache: {},
}),
).resolves.toBe(true)
})
})
131 changes: 131 additions & 0 deletions packages/cache/tests/schemas/basic/input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
//////////////////////////////////////////////////////////////////////////////////////////////
// DO NOT MODIFY THIS FILE //
// This file is automatically generated by ZenStack CLI and should not be manually updated. //
//////////////////////////////////////////////////////////////////////////////////////////////

/* eslint-disable */

import { type SchemaType as $Schema } from './schema'
import type {
FindManyArgs as $FindManyArgs,
FindUniqueArgs as $FindUniqueArgs,
FindFirstArgs as $FindFirstArgs,
ExistsArgs as $ExistsArgs,
CreateArgs as $CreateArgs,
CreateManyArgs as $CreateManyArgs,
CreateManyAndReturnArgs as $CreateManyAndReturnArgs,
UpdateArgs as $UpdateArgs,
UpdateManyArgs as $UpdateManyArgs,
UpdateManyAndReturnArgs as $UpdateManyAndReturnArgs,
UpsertArgs as $UpsertArgs,
DeleteArgs as $DeleteArgs,
DeleteManyArgs as $DeleteManyArgs,
CountArgs as $CountArgs,
AggregateArgs as $AggregateArgs,
GroupByArgs as $GroupByArgs,
WhereInput as $WhereInput,
SelectInput as $SelectInput,
IncludeInput as $IncludeInput,
OmitInput as $OmitInput,
QueryOptions as $QueryOptions,
} from '@zenstackhq/orm'
import type {
SimplifiedPlainResult as $Result,
SelectIncludeOmit as $SelectIncludeOmit,
} from '@zenstackhq/orm'
export type UserFindManyArgs = $FindManyArgs<$Schema, 'User'>
export type UserFindUniqueArgs = $FindUniqueArgs<$Schema, 'User'>
export type UserFindFirstArgs = $FindFirstArgs<$Schema, 'User'>
export type UserExistsArgs = $ExistsArgs<$Schema, 'User'>
export type UserCreateArgs = $CreateArgs<$Schema, 'User'>
export type UserCreateManyArgs = $CreateManyArgs<$Schema, 'User'>
export type UserCreateManyAndReturnArgs = $CreateManyAndReturnArgs<$Schema, 'User'>
export type UserUpdateArgs = $UpdateArgs<$Schema, 'User'>
export type UserUpdateManyArgs = $UpdateManyArgs<$Schema, 'User'>
export type UserUpdateManyAndReturnArgs = $UpdateManyAndReturnArgs<$Schema, 'User'>
export type UserUpsertArgs = $UpsertArgs<$Schema, 'User'>
export type UserDeleteArgs = $DeleteArgs<$Schema, 'User'>
export type UserDeleteManyArgs = $DeleteManyArgs<$Schema, 'User'>
export type UserCountArgs = $CountArgs<$Schema, 'User'>
export type UserAggregateArgs = $AggregateArgs<$Schema, 'User'>
export type UserGroupByArgs = $GroupByArgs<$Schema, 'User'>
export type UserWhereInput = $WhereInput<$Schema, 'User'>
export type UserSelect = $SelectInput<$Schema, 'User'>
export type UserInclude = $IncludeInput<$Schema, 'User'>
export type UserOmit = $OmitInput<$Schema, 'User'>
export type UserGetPayload<
Args extends $SelectIncludeOmit<$Schema, 'User', true>,
Options extends $QueryOptions<$Schema> = $QueryOptions<$Schema>,
> = $Result<$Schema, 'User', Args, Options>
export type PostFindManyArgs = $FindManyArgs<$Schema, 'Post'>
export type PostFindUniqueArgs = $FindUniqueArgs<$Schema, 'Post'>
export type PostFindFirstArgs = $FindFirstArgs<$Schema, 'Post'>
export type PostExistsArgs = $ExistsArgs<$Schema, 'Post'>
export type PostCreateArgs = $CreateArgs<$Schema, 'Post'>
export type PostCreateManyArgs = $CreateManyArgs<$Schema, 'Post'>
export type PostCreateManyAndReturnArgs = $CreateManyAndReturnArgs<$Schema, 'Post'>
export type PostUpdateArgs = $UpdateArgs<$Schema, 'Post'>
export type PostUpdateManyArgs = $UpdateManyArgs<$Schema, 'Post'>
export type PostUpdateManyAndReturnArgs = $UpdateManyAndReturnArgs<$Schema, 'Post'>
export type PostUpsertArgs = $UpsertArgs<$Schema, 'Post'>
export type PostDeleteArgs = $DeleteArgs<$Schema, 'Post'>
export type PostDeleteManyArgs = $DeleteManyArgs<$Schema, 'Post'>
export type PostCountArgs = $CountArgs<$Schema, 'Post'>
export type PostAggregateArgs = $AggregateArgs<$Schema, 'Post'>
export type PostGroupByArgs = $GroupByArgs<$Schema, 'Post'>
export type PostWhereInput = $WhereInput<$Schema, 'Post'>
export type PostSelect = $SelectInput<$Schema, 'Post'>
export type PostInclude = $IncludeInput<$Schema, 'Post'>
export type PostOmit = $OmitInput<$Schema, 'Post'>
export type PostGetPayload<
Args extends $SelectIncludeOmit<$Schema, 'Post', true>,
Options extends $QueryOptions<$Schema> = $QueryOptions<$Schema>,
> = $Result<$Schema, 'Post', Args, Options>
export type CommentFindManyArgs = $FindManyArgs<$Schema, 'Comment'>
export type CommentFindUniqueArgs = $FindUniqueArgs<$Schema, 'Comment'>
export type CommentFindFirstArgs = $FindFirstArgs<$Schema, 'Comment'>
export type CommentExistsArgs = $ExistsArgs<$Schema, 'Comment'>
export type CommentCreateArgs = $CreateArgs<$Schema, 'Comment'>
export type CommentCreateManyArgs = $CreateManyArgs<$Schema, 'Comment'>
export type CommentCreateManyAndReturnArgs = $CreateManyAndReturnArgs<$Schema, 'Comment'>
export type CommentUpdateArgs = $UpdateArgs<$Schema, 'Comment'>
export type CommentUpdateManyArgs = $UpdateManyArgs<$Schema, 'Comment'>
export type CommentUpdateManyAndReturnArgs = $UpdateManyAndReturnArgs<$Schema, 'Comment'>
export type CommentUpsertArgs = $UpsertArgs<$Schema, 'Comment'>
export type CommentDeleteArgs = $DeleteArgs<$Schema, 'Comment'>
export type CommentDeleteManyArgs = $DeleteManyArgs<$Schema, 'Comment'>
export type CommentCountArgs = $CountArgs<$Schema, 'Comment'>
export type CommentAggregateArgs = $AggregateArgs<$Schema, 'Comment'>
export type CommentGroupByArgs = $GroupByArgs<$Schema, 'Comment'>
export type CommentWhereInput = $WhereInput<$Schema, 'Comment'>
export type CommentSelect = $SelectInput<$Schema, 'Comment'>
export type CommentInclude = $IncludeInput<$Schema, 'Comment'>
export type CommentOmit = $OmitInput<$Schema, 'Comment'>
export type CommentGetPayload<
Args extends $SelectIncludeOmit<$Schema, 'Comment', true>,
Options extends $QueryOptions<$Schema> = $QueryOptions<$Schema>,
> = $Result<$Schema, 'Comment', Args, Options>
export type ProfileFindManyArgs = $FindManyArgs<$Schema, 'Profile'>
export type ProfileFindUniqueArgs = $FindUniqueArgs<$Schema, 'Profile'>
export type ProfileFindFirstArgs = $FindFirstArgs<$Schema, 'Profile'>
export type ProfileExistsArgs = $ExistsArgs<$Schema, 'Profile'>
export type ProfileCreateArgs = $CreateArgs<$Schema, 'Profile'>
export type ProfileCreateManyArgs = $CreateManyArgs<$Schema, 'Profile'>
export type ProfileCreateManyAndReturnArgs = $CreateManyAndReturnArgs<$Schema, 'Profile'>
export type ProfileUpdateArgs = $UpdateArgs<$Schema, 'Profile'>
export type ProfileUpdateManyArgs = $UpdateManyArgs<$Schema, 'Profile'>
export type ProfileUpdateManyAndReturnArgs = $UpdateManyAndReturnArgs<$Schema, 'Profile'>
export type ProfileUpsertArgs = $UpsertArgs<$Schema, 'Profile'>
export type ProfileDeleteArgs = $DeleteArgs<$Schema, 'Profile'>
export type ProfileDeleteManyArgs = $DeleteManyArgs<$Schema, 'Profile'>
export type ProfileCountArgs = $CountArgs<$Schema, 'Profile'>
export type ProfileAggregateArgs = $AggregateArgs<$Schema, 'Profile'>
export type ProfileGroupByArgs = $GroupByArgs<$Schema, 'Profile'>
export type ProfileWhereInput = $WhereInput<$Schema, 'Profile'>
export type ProfileSelect = $SelectInput<$Schema, 'Profile'>
export type ProfileInclude = $IncludeInput<$Schema, 'Profile'>
export type ProfileOmit = $OmitInput<$Schema, 'Profile'>
export type ProfileGetPayload<
Args extends $SelectIncludeOmit<$Schema, 'Profile', true>,
Options extends $QueryOptions<$Schema> = $QueryOptions<$Schema>,
> = $Result<$Schema, 'Profile', Args, Options>
Loading