docs icon indicating copy to clipboard operation
docs copied to clipboard

Update 070-case-sensitivity.mdx

Open lregaladohdez opened this issue 3 years ago • 4 comments

When using NestJS with TypeScript, directly putting mode: 'insensitive' throws an error because 'mode' should be of type Prisma.QueryMode, so one needs to put Prisma.QueryMode.insensitive or Prisma.QueryMode.default instead of putting the string.

Describe this PR

I was getting an error on a NestJS project using the 'mode' option on queries. Reviewing the doc on how to add the mode: insensitive it's not correct if we are using TypeScript you should put mode: Prisma.QueryMode.insensitive

Changes

Only add a small comment in the same line where the example of mode: insensitive is presented

What issue does this fix?

No issues reported, just a clarification for improving the documentation

Any other relevant information

lregaladohdez avatar Sep 01 '22 14:09 lregaladohdez

@lregaladohdez is attempting to deploy a commit to the Prisma Team on Vercel.

A member of the Team first needs to authorize it.

vercel[bot] avatar Sep 01 '22 14:09 vercel[bot]

Thanks for raising this, @lregaladohdez! We'll get a technical review on this and get back to you.

keerlu avatar Sep 02 '22 09:09 keerlu

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
docs ✅ Ready (Inspect) Visit Preview Sep 2, 2022 at 9:28AM (UTC)

vercel[bot] avatar Sep 02 '22 09:09 vercel[bot]

Hmm, I wasn't able to reproduce this either as a compiler error or a runtime error:

  const results = await prisma.test.findFirst({
    where: {
      testId: {
        mode: "insensitive"
      }
    }
  })

Can you share a reproduction with us?

matthewmueller avatar Sep 06 '22 18:09 matthewmueller

@lregaladohdez, in case you didn't see Matt's comment above - are you able to share a reproduction of this with us?

keerlu avatar Oct 21 '22 12:10 keerlu

Prisma.QueryMode.insensitive should be the same thing as 'insensitive' as a literal string. The only way it could be different is if you used it in the context where TypeScript wouldn't have been aware of the expected type and, in addition to that, would be forced to infer a wider type (i.e., string) instead of a specific literal type due to reasons like mutability.

In other words, one way to trigger the type error would be something like this:

  const query = {
    where: {
      email: {
        endsWith: 'prisma.io',
        mode: 'insensitive',
      },
    },
  }

  await prisma.user.findMany(query)

In that case, both

-         mode: 'insensitive',
+         mode: 'insensitive' as const,

and

-         mode: 'insensitive',
+         mode: Prisma.QueryMode.insensitive,

are equally valid ways to fix the type error — however neither of them is the best one. The proper solution, in my opinion, would be to annotate the variable itself with the expected type:

-  const query = {
+  const query: Prisma.UserFindManyArgs = {
     where: {
       email: {
         endsWith: 'prisma.io',
         mode: 'insensitive',
       },
     },
   }

   await prisma.user.findMany(query)

I don't think there's anything that needs to be changed in the docs here, especially in this specific place, as we have quite a lot of enums represented as unions of string literal types. However, some kind of guide explaining this part of TypeScript's type system and how it is used in Prisma might be useful, especially to developers coming to TypeScript from other languages.

aqrln avatar Oct 26 '22 10:10 aqrln

Hey @lregaladohdez 👋

based on the comments from our Engineering team, I'm going to close this PR. Please feel free to reopen if you have another argument for it 🙂

Also, please feel free to open issues and PRs in the docs repo in the future if anything doesn't make sense to you!

Thanks a lot for taking the time to make our documentation better, this is much appreciated 💚

nikolasburk avatar Nov 16 '23 10:11 nikolasburk