Search code examples
mysqlormprisma

How to use MySQL's LIKE in Prisma ORM on non string fields?


Having some experience in writing raw SQL queries for some time, I want to use Prisma in Node.js to ask MySQL for something like that:

SELECT ..... WHERE dateField LIKE '2020-05%'

or

SELECT ..... WHERE numberField LIKE '%99'

I know, that the database will return what I want.

I just can't make Prisma to do that. Is it possible?

For string/varchar fields i use contains keyword in where object and it works fine.

const orders = await prisma.order.findMany({
  where: {
    textField: {
        contains: 'test'
    }
  }
});

Is there any workaround to get such functionality for date/number type fields?


Solution

  • I managed to run raw SQL in Prisma as a workaround:

    const orders = await prisma.$queryRaw`SELECT * FROM Order WHERE dataField LIKE '%-05-%'`;
    

    It can even be type casted:

    import { Order } from '@prisma/client'
    const orders = await prisma.$queryRaw<Order[]>`SELECT * FROM Order WHERE dataField LIKE '%-05-%'`;
    

    Not pretty, but works as intended.