-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
feat(core/db): allow to extend PrismaClient #9114
feat(core/db): allow to extend PrismaClient #9114
Conversation
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit e842f7d:
|
d953b2f
to
e5062c7
Compare
I was looking to integrate |
We are already using this with a patch on top of KS |
LGTM, doesn't resolve the issues raised by #8847, but I think we'll look at that another day - and there exists a workaround for that |
Thanks @iamandrewluca! I'll try and release this asap |
Thanks! That is great! |
@iamandrewluca can you share the code of |
I have a big object mapper for all models/operations/lifecycles, and getPrismaHooks returns me the functions based on model/operation import { type PrismaClient, type Prisma } from '@prisma/client';
import type { TypeInfo } from '.keystone/types';
type PrismaHook = (
prismaClient: PrismaClient,
args: unknown,
) => Promise<void> | Promise<unknown>;
type PrismaExtendedOperation = {
[ListType in keyof TypeInfo['lists']]?: {
[OperationType in Prisma.DMMF.ModelAction]?: Partial<
Record<'before' | 'after', PrismaHook>
>;
};
};
const PrismaHooks: PrismaExtendedOperation = {
Order: {
create: {
before: async (prismaClient: PrismaClient, args: unknown) => {
// ...
},
},
update: {
before: async (prismaClient: PrismaClient, args: unknown) => {
// ...
},
},
},
};
export function getPrismaHooks(
model: keyof TypeInfo['lists'],
operation: string,
): Partial<Record<'before' | 'after', PrismaHook>> | undefined {
const modelHooks = PrismaHooks[model];
return modelHooks?.[operation as Prisma.DMMF.ModelAction];
} |
@iamandrewluca out of interest, why use these by comparison to the Keystone hooks? |
@dcousens, we had to create custom GQL mutations that use Prisma transactions, and doing that KS hooks did not work. |
Using this feature, one can extend PrismaClient, and add for example hooks at prisma level