HuserB1989
HuserB1989

Reputation: 380

How to concatenate fields in prisma using nestjs?

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?

Upvotes: 1

Views: 4095

Answers (1)

Nurul Sundarani
Nurul Sundarani

Reputation: 7588

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]}`
            },
          },
        ],
      },
    });

Upvotes: 3

Related Questions