How to concatenate fields in prisma using nestjs?

Viewed 791

I have a firstname and a lastname field. I'm writing a query to filter by a string. My query is something like this:

await this.prisma.person.findMany({
      where: {
        OR: [
          {
            firstname: {
              contains: filterString
            },
            lastname: {
              contains: filterString
            },
          },
        ],
      },
    });

If I try to search for "John" then it works, but doesn't work for "john doe" even if I have a firstname as John and lastname as Doe. Is there any way to concatenate these two fields within the query?

1 Answers

You would need to use the Full Text Search feature which is currently in preview.

You can split your filterString by space, and pass all the elements in firstName and lastName clause. So considering element array, element[0] would have firstName and element[1] would have lastName.

await this.prisma.person.findMany({
      where: {
        OR: [
          {
            firstname: {
              search: `${element[0]} | ${element[1]}`
            },
            lastname: {
              search: `${element[0]} | ${element[1]}`
            },
          },
        ],
      },
    });
Related