drizzle-team / drizzle-orm

Headless TypeScript ORM with a head. Runs on Node, Bun and Deno. Lives on the Edge and yes, it's a JavaScript ORM too 😅
https://orm.drizzle.team
Apache License 2.0
21.44k stars 484 forks source link

[BUG]: Issues with nested conditions & placeholders in SQLite query #2529

Open ridhwaans opened 1 week ago

ridhwaans commented 1 week ago

What version of drizzle-orm are you using?

^0.30.10

What version of drizzle-kit are you using?

^0.21.4

Describe the Bug

import { db, sql, desc, asc, eq } from 'drizzle-orm';

...
const prepared = db.query.booksTable.findMany({
      limit: sql.placeholder('limit'),
      orderBy: (books, { desc }) => 
                [desc(eq(books.publishDate, sql.placeholder('sort_by_newest'), 
                      eq(books.sales, sql.placeholder('sort_by_sales'))], // doesnt work
      with: {
        booksToGenres: {
          with: {
            genre: {
              where: (genre, { eq }) => eq(genre.name, sql.placeholder('genre_name')), // doesnt work
            }
          },
        },
        booksToProductionCompanies: {
          with: {
            productionCompany: true,
          },
        },
        },
    });
  }).prepare()

  await prepared.execute({...})

hi, I have some questions around prepared queries in sqlite drizzle

  1. for the orderBy , sometimes I need to enable ordering by a specific column name while other times it needs to be on another column name. how do i achieve that by passing a boolean placeholder?
  2. Where filter clause does not work on many to many relation genre. Object literal may only specify known properties, and 'where' does not exist in type books. Nested filter is a basic need. It probably works one level higher on the joined relation booksToGenres Discussion on V2 queries https://github.com/drizzle-team/drizzle-orm/discussions/2316 My other option is to filter later using ES6 array methods but I want to achieve this using within findMany query

Expected behavior

It should be able to manage multiple orderBy and where conditions by passing placeholders. It should allow performing where conditions on joined tables

Environment & setup

Mac, linux