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) {
      opts.ctx[prismaModelKey] = prisma[prismaModelKey]; // this should actually be `model.findUniqueOrThrow(rawInputValue)`
  return{ 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 🙂 ?