fordth / jinqJs

jinqJs provides a simple way to perform SQL like queries on javaScript arrays, collections and web services using LINQ expressions.
Other
93 stars 29 forks source link

Creating new jinqJs instance for different queries on same base #29

Open bekatd opened 6 years ago

bekatd commented 6 years ago

Hi,

First of all thank you for such great lib, I really appreciate it.

The only drawback i found during usage is: Let's say i want to apply different "filters" on initially filtered data. jinqJs needs to be created and filtered on initial state for all of such types of cases.

ex: Let's say we have an array with 100 elements (rows), with date field inside.

First of all, I want to filter by month and assign it to some variable:

var year = 2017;
var month = 12;
var filtered_by_month = new jinqJs().from(DATA).where(function(row) {
    var date = row.date;
    return date.getFullYear() === year && date.getMonth() === month;
});

Now if i want to get for example: SUM and COUNT, at first glance it's easy, because:

var sum = filtered_by_month.sum('amount').select();
var count = filtered_by_month.sum('amount').select();

But, as it appears, after the first usage (when we did get sum) of jinqJs instance - filtered_by_month, jinqJs instance appears to be empty and second expression did not evaluate anything, it became null.

This happens not only in case of SUM or COUNT, If i apply some GROUPING to it, I can't apply other GROUPING on filtered_by_month instance.

For that, unusual reason, I am creating new jinqJs instances every time, with same initial filter, to get desired results at the end.

I wish i could query my data like this:

var year = 2017;
var month = 12;
var filtered_by_month = new jinqJs().from(DATA).where(function(row) {
    var date = row.date;
    return date.getFullYear() === year && date.getMonth() === month;
});

var sum = filtered_by_month.groupBy('user').sum('amount').select();
var count = filtered_by_month.groupBy('branch').count('amount').select();
...

Thanks for creating such a great library again :)

fordth commented 6 years ago

Thanks bekatd! I stopped working on the project for a little over a year now, but I am thinking about adding some enhancements to it. Thank you for the long explanation, its enough I believe for me to have to replicate your issue. I will update this issue if I have any further updates. -Tom