MordsithM
Convex Community11mo ago
1 reply
Mordsith

Add a query filter for empty value

This is a proposal to make querying for empty values easier. Handling all possible edge cases

There should be a query filter to handle possible empty values; in situations where we need to
unset
a property and filter by this property, this filter should consider all possible falsey value, something similar to lodash's
isEmpty


https://lodash.com/docs/4.17.15#isEmpty

This may be opionionated but I think there are a few people who see value in this...

For array types => []
For object => {}
For string "" (not to be confused with " ", an empty space; We can consider both instance as
falsey
but I need to hear people's suggestions if this makes sense)
For numbers => 0
It should also apply to
null
,
undefined
and
NaN


Instead of doing things like this
await ctx.db
  .query('users')
  .filter((q) =>
    q.or(
      q.neq(q.field('shouldDeleteAt'), null),
      q.eq(q.field('shouldDeleteAt'), undefined),
    ),
  )
  .collect();


This new approach is cleaner and would automatically handle edge cases. When the field is an
object
and this object is empty, it returns as part of the results; for an array, when it's empty or the length is 0, this also applies; same with
null
,
undefined
,
false
and
0

await ctx.db
  .query('users')
  .filter((q) =>
    q.isEmpty(q.field('shouldDeleteAt')),
  )
  .collect();


Open to questions and why this may not be a good thing.

More: https://developer.mozilla.org/en-US/docs/Glossary/Falsy
MDN Web Docs
A falsy (sometimes written falsey) value is a value that is considered false when encountered in a Boolean context.
Falsy - MDN Web Docs Glossary: Definitions of Web-related terms | MDN
Was this page helpful?