-
-
Notifications
You must be signed in to change notification settings - Fork 271
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
[prisma] Using extensions to remove the need for subject
helper with prisma
#783
Comments
Makes sense. Im glad to accept PR. |
Why not go further and write an extension that will add additional query functions that will accept the ability, Prisma.defineExtension({
model: {
$allModels: {
async tryFindMany<T, A extends Prisma.Args<T, 'findMany'>>(
this: T,
{ ability, ...args }: A & { ability: AppAbility },
): Promise<Prisma.Result<T, A, 'findMany'>> {
const context = Prisma.getExtensionContext(this) as { name: Prisma.ModelName };
const where = {
AND: [accessibleBy(args.ability, 'read')[context.name], args.where],
};
return (context as any).findMany({ ...args, where });
},
// ... other methods
},
},
}); |
Adding methods is a bit more opinionated, also I don’t know how to adjust prisma types in this case. I have experience adding methods to mongoose models, it made initial setup complicated and eventually I deprecated that and implemented so, there is no much difference what to use
However boilerplate reduction can be reduced with methods but it’s very little reduction. If you see this differently, please share your thoughts |
@stalniy what do you mean by |
Maybe this could be handy: According to their docs: This means you could get a user /ability scoped prisma client that works on all models and operations and adds to nessecary checks as defined in your ability.
|
Is your feature request related to a problem? Please describe.
Currently, you need the
subject
helper to use CASL with Prisma. With client extensions being stable now, we can do better.Describe the solution you'd like
Provide a pre-packaged extension in CASL, that will add the model name to a field in prisma client (maybe use a symbol for the field?). Make the default
detectSubjectType
know how to read that field.Describe alternatives you've considered
This is fairly easily solvable in user-land, just thought it might be a good idea if the prisma package made the process streamlined.
The text was updated successfully, but these errors were encountered: