Is there a good way to do "exists"
Im using pg. Is there a clean way to do a query analagous to something like
await db.user.exists({where: {x: y} });
9 Replies
What would you expect to get as a return from that function?
A boolean or a list of users?
You have to think of drizzle as writing SQL
But in javascript and typesafe
So, if it exists in sql, it probably exists in drizzle, or it's in the way
i would expect a boolean to return
is the most optimal way to run this function
!!(await db.query.user.findFirst({ where: {x: y} }))
There is no statement in sql that returns a boolean
But there is an
exists
clauseyeah, but that is a bit seaprate
i feel liek this would be a cool feature to add to drizzle
db.query.user.exists({where: {x: y}})
under the hood running a select nothing query
What query should drizzle run under the hood?
!!(await db.query.user.findFirst({ columns: {} }))
That query would return you the first user the database can find, without any columns
yeah, but the !! would give it as a boolean value, thus functioning as an exists function
if the where query is not able to find any row that matches the condition, then the query would return undefined
so if it exists you'd get
!!{} → true
if it doesn't exist you'd get
!!undefined → false
GitHub
[FEATURE]: Exists function when making queries · Issue #1689 · driz...
Describe what you want It would be handy to be able to make a query like: await db.query.users.exists({ where: whereCondition }); that returns a boolean value. Under the hood it would simply run: r...