nehalist
nehalist15mo ago

Implementing a "Prisma model converter middleware"

Bit of a fancy name, but if you've ever worked with Symfonys param converters (especially the DoctrineParamConverter) you get the idea. It's basically just automatically converting input to models. Currently trying to implement this, but can't find a way for proper types. Implementation so far:
export const modelConverter = middleware(async (opts) => {
const ctx: Record<string, any> = {};
if (typeof opts.rawInput === "object") {
for await (const key of Object.keys(opts.rawInput)) {
const prismaModelKey = Object.keys(prisma).find(
(m) => m.toLowerCase() === key
);
if (!prismaModelKey) {
continue;
}
opts.ctx[prismaModelKey] = prisma[prismaModelKey]; // this should actually be `model.findUniqueOrThrow(rawInputValue)`
}
}
return opts.next({ ctx } as { ctx: { [key: keyof typeof ctx]: any } });
});
export const modelConverter = middleware(async (opts) => {
const ctx: Record<string, any> = {};
if (typeof opts.rawInput === "object") {
for await (const key of Object.keys(opts.rawInput)) {
const prismaModelKey = Object.keys(prisma).find(
(m) => m.toLowerCase() === key
);
if (!prismaModelKey) {
continue;
}
opts.ctx[prismaModelKey] = prisma[prismaModelKey]; // this should actually be `model.findUniqueOrThrow(rawInputValue)`
}
}
return opts.next({ ctx } as { ctx: { [key: keyof typeof ctx]: any } });
});
Unfortunately I can't come up with a solution to knowing the types here. Anyone got some ideas 🙂 ?
0 Replies
No replies yetBe the first to reply to this messageJoin