Skip to content
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(client): extensions generic type api #16936

Merged
merged 32 commits into from Jan 16, 2023

Conversation

millsp
Copy link
Member

@millsp millsp commented Dec 21, 2022

This PR enables extensions to deal with input and output types in generic extensions to be able to augment them dynamically on any given model. For this, three new utilities appeared:

  • Exact: Enforces exact type inputs, no more, no less while also narrowing input
  • Args: Dynamically retrieve the input type of a given model and a given operation
  • Result: Takes the input types and gives the result for a given model and operation
  • Payload: Retrieve the payload output types which represent scalars & relations objects

The gist:

const prisma = new PrismaClient()

const xprisma = prisma.$extends({
  model: {
    $allModels: {
      findOrCreate<T, A>(
        this: T,
        x: Prisma.Exact<A, Prisma.Args<T, 'findFirstOrThrow'> & { cache: boolean }>,
      ): Prisma.Result<T, A, 'findFirstOrThrow'> {
        const ctx = Prisma.getExtensionContext(this)

        console.log(ctx.name)

        return {} as any // TODO
      },
    },
  },
})

async function main() {
  const test0 = xprisma.link.findOrCreate({
    cache: true,
    select: {
      id: true,
    },
  })
}

closes #17236
closes #17339
closes #16600
closes #17258
closes https://github.com/prisma/client-planning/issues/214
closes https://github.com/prisma/client-planning/issues/210

@millsp millsp added this to the 4.9.0 milestone Dec 21, 2022
@millsp millsp marked this pull request as ready for review January 14, 2023 04:33
@millsp millsp requested a review from a team January 14, 2023 04:33
@millsp millsp requested review from aqrln and removed request for a team January 14, 2023 04:33
@Jolg42
Copy link
Member

Jolg42 commented Jan 16, 2023

Note: @millsp Looks like there is a conflict to solve

@millsp millsp merged commit 7716c77 into main Jan 16, 2023
@millsp millsp deleted the integration/feat/client-ext-generic-type-api branch January 16, 2023 15:34
@leumasme
Copy link

Is there any documentation (or examples) on how to use this?
I'm having a hard time figuring it out from sifting through the typedefs in the client and just figuring something like
Prisma.Result<typeof db.measurement, {include: {Location: true}}, "findMany">
out took a little while and is still using typeof.
The Advanced Type Safety docs article could probably use some docs on this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants