jawj / zapatos

Zero-abstraction Postgres for TypeScript: a non-ORM database library
https://jawj.github.io/zapatos/
Other
1.3k stars 46 forks source link

is it possible to do do OR where clauses with the select API? #178

Closed jcoveney-anchorzero closed 3 months ago

jcoveney-anchorzero commented 3 months ago

let's say I have a table with columns a, b, c that are all strings.

I have arrays arrA, arrB, arrC. I want to essentially do the following

SELECT *
FROM table
WHERE a in arrA AND (b in arrB OR c in arrC);

we like using the select API a lot, but I am struggling to get it working with this. doing the following is of course trivial

zapatosDb.select('table',
  {
    a: dc.isIn(arrA),
    b: dc.isIn(arrB),
    c: dc.isIn(arrC),
  });

but what I want is essentially

zapatosDb.select('table',
  {
    a: dc.isIn(arrA),
    ??? dc.or(b + dc.isIn(arrB), c + dc.isIn(arrC))
  });

I tried using dc.or with the whereables, but it didn't seem to work? that is to say

zapatosDb.select('table',
  dc.and({a: dc.isIn(arrA) }, dc.or({b: dc.isIn(arrB) }, {c: dc.isIn(arrC) }))
);

gave me an error. is it expected that that would work?

jawj commented 3 months ago

Try this:

const result = await db.select('table', 
  dc.and(
    { a: dc.isIn(arrA) },
    dc.or(
      { b: dc.isIn(arrB) },
      { c: dc.isIn(arrC) }
    )
  )
).run(pool);

EDIT: Huh, this looks the same as your suggestion, but it ought to work, and it does seem to work for me. Can you produce a test case where it fails? arrA, arrB and arrC should be string[] typed.

jcoveney-anchorzero commented 3 months ago

I will investigate! we have slightly modified zapatos, and I think that that may be what's creating the issue here. thank you for the response! I will see if I can reproduce the issue in vanilla zapatos!

jcoveney-anchorzero commented 3 months ago

going to close this for now, we are going to work on this soon, I'll reopen if we can come up with a specific issue. thank you!