-
-
Notifications
You must be signed in to change notification settings - Fork 113
Support for auth() in @default attribute #958
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ymc9
merged 14 commits into
zenstackhq:v2
from
Azzerty23:feature/default-auth-attributes
Jan 26, 2024
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
4198963
WIP default auth
Azzerty23 a31a422
feat: handle default auth for literal fields
Azzerty23 0cc43bb
fix: avoid create operation called twice
Azzerty23 a551f30
remove unused code
Azzerty23 c8f049c
clean up
Azzerty23 08a7282
fix: do not cache proxy detection flags in test environment
ymc9 92de62e
fix: use create callback to add default auth args
Azzerty23 258b56b
fix: default args should not override passed args
Azzerty23 3311530
considering auth() without selector
Azzerty23 37c5cd1
A few refactors
ymc9 6bc36a5
remove useless newArgs in writer visitor
Azzerty23 a601e9f
Revert "remove useless newArgs in writer visitor"
ymc9 7b98938
Merge pull request #1 from Azzerty23/feat/default-auth-ymc9
ymc9 8558e93
Merge remote-tracking branch 'origin/v2' into feature/default-auth-at…
ymc9 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
|
||
import deepcopy from 'deepcopy'; | ||
import { FieldInfo, NestedWriteVisitor, PrismaWriteActionType, enumerate, getFields } from '../cross'; | ||
import { DbClientContract } from '../types'; | ||
import { EnhancementContext, EnhancementOptions } from './create-enhancement'; | ||
import { DefaultPrismaProxyHandler, PrismaProxyActions, makeProxy } from './proxy'; | ||
|
||
/** | ||
* Gets an enhanced Prisma client that supports `@default(auth())` attribute. | ||
* | ||
* @private | ||
*/ | ||
export function withDefaultAuth<DbClient extends object>( | ||
ymc9 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
prisma: DbClient, | ||
options: EnhancementOptions, | ||
context?: EnhancementContext | ||
): DbClient { | ||
return makeProxy( | ||
prisma, | ||
options.modelMeta, | ||
(_prisma, model) => new DefaultAuthHandler(_prisma as DbClientContract, model, options, context), | ||
'defaultAuth' | ||
); | ||
} | ||
|
||
class DefaultAuthHandler extends DefaultPrismaProxyHandler { | ||
private readonly db: DbClientContract; | ||
private readonly userContext: any; | ||
|
||
constructor( | ||
prisma: DbClientContract, | ||
model: string, | ||
private readonly options: EnhancementOptions, | ||
private readonly context?: EnhancementContext | ||
) { | ||
super(prisma, model); | ||
this.db = prisma; | ||
|
||
if (!this.context?.user) { | ||
throw new Error(`Using \`auth()\` in \`@default\` requires a user context`); | ||
} | ||
|
||
this.userContext = this.context.user; | ||
} | ||
|
||
// base override | ||
protected async preprocessArgs(action: PrismaProxyActions, args: any) { | ||
const actionsOfInterest: PrismaProxyActions[] = ['create', 'createMany', 'update', 'updateMany', 'upsert']; | ||
if (actionsOfInterest.includes(action)) { | ||
const newArgs = await this.preprocessWritePayload(this.model, action as PrismaWriteActionType, args); | ||
return newArgs; | ||
} | ||
return args; | ||
} | ||
|
||
private async preprocessWritePayload(model: string, action: PrismaWriteActionType, args: any) { | ||
const newArgs = deepcopy(args); | ||
|
||
const processCreatePayload = (model: string, data: any) => { | ||
const fields = getFields(this.options.modelMeta, model); | ||
for (const fieldInfo of Object.values(fields)) { | ||
if (fieldInfo.name in data) { | ||
// create payload already sets field value | ||
continue; | ||
} | ||
|
||
if (!fieldInfo.defaultValueProvider) { | ||
// field doesn't have a runtime default value provider | ||
continue; | ||
} | ||
|
||
const authDefaultValue = this.getDefaultValueFromAuth(fieldInfo); | ||
if (authDefaultValue !== undefined) { | ||
// set field value extracted from `auth()` | ||
data[fieldInfo.name] = authDefaultValue; | ||
} | ||
} | ||
}; | ||
|
||
// visit create payload and set default value to fields using `auth()` in `@default()` | ||
const visitor = new NestedWriteVisitor(this.options.modelMeta, { | ||
create: (model, data) => { | ||
processCreatePayload(model, data); | ||
}, | ||
|
||
createMany: (model, args) => { | ||
for (const item of enumerate(args.data)) { | ||
processCreatePayload(model, item); | ||
} | ||
}, | ||
}); | ||
|
||
await visitor.visit(model, action, newArgs); | ||
return newArgs; | ||
} | ||
|
||
private getDefaultValueFromAuth(fieldInfo: FieldInfo) { | ||
return fieldInfo.defaultValueProvider?.(this.userContext); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.