balderdashy / waterline-sequel

A SQL generator for use in Waterline Adapters
MIT License
16 stars 61 forks source link

count queries limited #69

Closed jean1880 closed 8 years ago

jean1880 commented 8 years ago

When creating queries for count, they do not allow the same complex, nested queries that find allows, such as relational database queries. Count queries should be able to perform the same queries as a find statement, simply returning the count instead of the data. Currently when passing in a nested table, the query tries to query the column of the nested table from the parent table, without joining the tables first.

devinivy commented 8 years ago

We haven't settled on a syntax within waterline to count associated records, though there is a conversation about this at https://github.com/balderdashy/waterline/issues/811. But yes, something like what you're describing is desirable once waterline officially supports counting associated records. There's also the problem of keeping the count query optimized when possible. Related to https://github.com/balderdashy/waterline-sequel/pull/65

sailsbot commented 8 years ago

Thanks for posting, @jean1880. I'm a repo bot-- nice to meet you!

It has been 30 days since there have been any updates or new comments on this page. If this issue has been resolved, feel free to disregard the rest of this message. On the other hand, if you are still waiting on a patch, please:

Thanks so much for your help!

jaroslav-muller commented 8 years ago

I'd like to reiterate on this. Let's consider a (very artificial) example - two entities User and Address (User must have one Address). I can do this:

User.find({
  name: {contains: "a"},
  address: {
    street: {contains: "b"}
  }
});

however this throws:

User.count({
  name: {contains: "a"},
  address: {
    street: {contains: "b"}
  }
});

It's because count (in contrast to find) will not add needed joins.

Note that I'm not interested in counting users nor his addresses. I merely want to how e.g. many users live in certain street.

Currently I know no way to do it in waterline, except for doing find and getting length of the resulting array (which is unacceptable).