Search code examples
typescriptpostgresqldrizzle

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; }, {}, {}>;


Solution

  • 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.