Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drizzle ORM Add where clause to `findMany`

I am attempting to add a where clause to my find many query but I'm not seeing any documentation for how this syntax should look. I just want to search where name is like "whatever"

        const data = await db.query.GamesTable.findMany({
            with: {
                platforms: {
                    columns: {},
                    with: {
                        platform: true,
                    },
                },
            },
            where: {
                name: {
                    contains: query.searchText,
                },
            },
            offset: page * 20,
            limit: 20,
        })

I get the following type error

Type  { name: { contains: string; }; }  is not assignable to type SQL | ((fields: { id: PgColumn<{ name: "id"; tableName: "games"; dataType: "string"; columnType: "PgUUID"; data: string; driverParam: string; notNull: true; hasDefault: true; enumValues: undefined; baseColumn: never; }, {}, {}>;

like image 717
BamBam22 Avatar asked Oct 19 '25 13:10

BamBam22


1 Answers

I can put a where at the top level that takes a function callback passing it the entity and an object of filter types:

        const data = await db.query.GamesTable.findMany({
            with: {
                platforms: {
                    columns: {},
                    with: {
                        platform: true,
                    },
                },
            },
            where: (game, { ilike }) => ilike(game.name, `%${query.searchText}%`),
            offset: page * 20,
            limit: 20,
        })

There seems to be a typescript bug though, I get a warning "Unused property where", but it works fine.

like image 67
BamBam22 Avatar answered Oct 22 '25 03:10

BamBam22



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!