javascriptnode.jsprisma

Node js search on prisma for string with case insensitivity


I want to find the user with a specific email regardless case sensitivity of the email in the DB or in the request body. Using any of the following methods is rendering an undefined function error

    const user = await prisma.User.findUnique({
        where: {
            email: {
                insensitive: {
                    equals: req.body.email,
                },
            }
        }
    })

    const user = await prisma.User.findUnique({
        where: {
            email: {
                insensitiveEquals: req.body.email
            }
        }
    })

    const user = await prisma.User.findUnique({
        where: {
            email: req.body.email,
            mode: 'insensitive'
        }
    })

Solution

  • Undefined function error would mean that there is no function findUnique available on the user model. This would happen if PrismaClient is not up to date with the schema file. You would need to execute npx prisma generate to generate the latest PrismaClient.

    After generating PrismaClient following query can be used to find user with email in case insensitive mode.

      const user = await prisma.user.findMany({
        where: {
          email: {
            equals: 'test@test.com',
            mode: 'insensitive',
          },
        },
      });
    

    This reference of case insensitive filtering will be helpful as well.