Closed iamandrewluca closed 2 months ago
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 e842f7d36c3fcc63a9e6aea1732629a9bb919b07:
Sandbox | Source |
---|---|
@keystone-6/sandbox | Configuration |
I was looking to integrate @prisma/extension-read-replicas
with this yesterday, right on time!
hope this gets into the release.
We are already using this with a patch on top of KS
LGTM, doesn't resolve the issues raised by https://github.com/keystonejs/keystone/pull/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 getPrismaHooks
or is this exported by prisma?
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