Skip to content

Commit

Permalink
Adds AggregateFunctions.limit() and changes Readme to avoid confusion (
Browse files Browse the repository at this point in the history
  • Loading branch information
dresende committed May 24, 2013
1 parent ea3e93d commit a5b0b2c
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 7 deletions.
19 changes: 12 additions & 7 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -437,27 +437,32 @@ Person.aggregate({ surname: "Doe" }).min("age").max("age").get(function (err, mi
console.log("The youngest Doe guy has %d years, while the oldest is %d", min, max);
});
```
Here's an example to illustrate how to use groupby:

An `Array` of properties can be passed to select only a few properties. An `Object` is also accepted to define conditions.

Here's an example to illustrate how to use `.groupBy()`:

```js
//The same as "select avg(weight), age from person where country='someCountry' group by age;"
Person.aggregate(["age"], { country: "someCountry" }).avg("weight").groupBy("age").get(function (err, stats) {
// stats is an Array, each item should have 'age' and 'avg_weight'
});
```

Possible aggregating functions:
### Base `.aggregate()` methods

- `.limit()`: you can pass a number as a limit, or two numbers as offset and limit respectively
- `.order()`: same as `Model.find().order()`

### Additional `.aggregate()` methods

- `min`
- `max`
- `avg`
- `sum`
- `count` (there's a shortcut to this - `Model.count`)

### Available options

- `offset`: discards the first `N` elements
- `limit`: although it can be passed as a direct argument, you can use it here if you prefer
- `only`: if you don't want all properties, you can give an array with the list of properties you want
There are more aggregate functions depending on the driver (Math functions for example).

#### Chaining

Expand Down
11 changes: 11 additions & 0 deletions lib/AggregateFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ function AggregateFunctions(opts) {
group_by = Array.prototype.slice.apply(arguments);
return this;
},
limit: function (offset, limit) {
if (typeof limit == "number") {
opts.limit = [ offset, limit ];
} else {
opts.limit = [ 0, offset ]; // offset = limit
}
return this;
},
order: function (property, order) {
opts.order = [ property, order ];
return this;
Expand Down Expand Up @@ -87,6 +95,9 @@ function AggregateFunctions(opts) {
if (opts.order) {
query.order.apply(query, opts.order);
}
if (opts.limit) {
query.offset(opts.limit[0]).limit(opts.limit[1]);
}

opts.driver.execQuery(query.build(), function (err, data) {
if (err) {
Expand Down
27 changes: 27 additions & 0 deletions test/integration/test-aggregate-limit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
var common = require('../common');
var assert = require('assert');

common.createConnection(function (err, db) {
common.createModelTable('test_aggregate_limit', db.driver.db, function () {
common.insertModelData('test_aggregate_limit', db.driver.db, [
{ id : 1, name : 'test1' },
{ id : 2, name : 'test1' },
{ id : 3, name : 'test2' },
{ id : 4, name : 'test2' },
{ id : 5, name : 'test2' },
{ id : 6, name : 'test3' },
{ id : 7, name : 'test3' }
], function (err) {
if (err) throw err;

var TestModel = db.define('test_aggregate_limit', common.getModelProperties());

TestModel.aggregate().distinct('name').limit(1).get(function (err, names) {
assert.equal(err, null);
assert.equal(Array.isArray(names), true);
assert.equal(names.length, 1);
db.close();
});
});
});
});

0 comments on commit a5b0b2c

Please sign in to comment.