Hi all. I was searching for ways to determine whether or not an Ecto query results in (a) result(s) without requiring the database to select columns as I don’t need the data and found this discussion in the Slack archive.
@michalmuskala shared this function which one can put in an app’s Repo module:
def exists?(query, opts \\ []) do
query = from q in query, select: true, limit: 1
case one(query, opts) do
nil -> false
true -> true
end
end
An alternative to the case
, which Paul Smith shared, which also gives true
or false
is:
!!one(query, opts)
I haven’t been able to find info on where the !!
comes from and exactly what it does, so I thought I’d ask here. Is it a type coercion type of thing, and does anyone know where it’s documented? Apologies if this is just me utterly failing at using docs.
Thanks.