Counts in Breeze.js
Asked Answered
P

3

5

I have a complex query that returns item counts. If I run a query on the client, will it always return the objects, or is there a way to just return the item counts without sending the object array in the payload? I tried doing something like

var query = breeze.EntityQuery.from('Items').inlineCount(true);

but that still pulls all the records down. Any solutions?

Privation answered 6/5, 2013 at 1:33 Comment(1)
Are you trying to load up all of the objects and then count then or simply get a count of the objects in the database?Attitudinize
A
8

I don't know if this exactly answers your question, but you would need to query the records in order to know how many there are (to my knowledge, there may be a more efficient way to tie Breeze directly into a SQL command that is way over my head) so you could do something like -

var query = breeze.EntityQuery.from('Items')
    .take(0)
    .inlineCount(true);

Edited the answer - this would return no objects and simply get the count.

Attitudinize answered 6/5, 2013 at 4:11 Comment(3)
Interesting approach, and while it feels a little 'hackish', at least it's much better than grabbing 100 items. I wonder what @Ward has to say about this.Privation
Just to clarify the problem: I'm trying to grab counts for an item. An item has 3 types of subitems (A,B,C). I'm showing a list of items (could be in the hundreds), and each one of them needs to display the total count for each type of subitem: eg Item1 (A: 4, B:10, C:2) Item2 (A:23, B:20, C:33), etc. I could make the above call on each sub-type to get its count, but with hundreds of items, that's 3 times as many calls to the server as the number of items, which might become prohibitive and not scale very well.Privation
It is both correct and hackish ;-) It also makes 2 queries to the database. Both occur on the server so there is no network penalty. But it's not nice. Of course I'd probably do it myself until I had a chance to create a custom service method on the server as Jay suggests. I might also keep the inlineCount query if, for some unlikely reason, I needed to compose ad hoc client-side queries (e.g. .where('Price', 'ge', 100) and only cared about the number of such items.Malachy
A
6

The inlineCount answer already provided is absolutely correct.

Another alternative is to calculate the counts on the server and just send down the "summary". For example this server side controller method will return an array of two element objects to the client:

  [HttpGet]
  public Object CustomerCountsByCountry() {
    return ContextProvider.Context.Customers.GroupBy(c => c.Country).Select(g => new {g.Key, Count = g.Count()});
  }

This would be called via

  EntityQuery.from("CustomerCountsByCountry")
     .using(myEntityManager).execute()
     .then(function(data) {

     var results = data.results;
     results.forEach(function(r) {
       var country = r.Key;
       var count = r.Count
     });

  });
Apathy answered 6/5, 2013 at 22:49 Comment(1)
Excellent. For some reason I was doing the same thing but returning JSON, and Breeze was complaining that the type was not mapped, but this definitely works. Thanks.Privation
M
-2

var query = breeze.EntityQuery.from('Items') .take(0) .inlineCount(true);

Meliamelic answered 21/2, 2014 at 10:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.