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
Angelelz
Angelelz12mo ago
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
jakeleventhal
jakeleventhalOP12mo ago
i would expect a boolean to return is the most optimal way to run this function !!(await db.query.user.findFirst({ where: {x: y} }))
Angelelz
Angelelz12mo ago
There is no statement in sql that returns a boolean But there is an exists clause
jakeleventhal
jakeleventhalOP12mo ago
yeah, 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
Angelelz
Angelelz12mo ago
What query should drizzle run under the hood?
jakeleventhal
jakeleventhalOP12mo ago
!!(await db.query.user.findFirst({ columns: {} }))
Angelelz
Angelelz12mo ago
That query would return you the first user the database can find, without any columns
jakeleventhal
jakeleventhalOP12mo ago
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
jakeleventhal
jakeleventhalOP12mo ago
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...
Want results from more Discord servers?
Add your server