Skip to content

Commit f20f519

Browse files
author
Oliver Rumbelow
committed
Merge pull request #9 from holidayextras/pagination-sorting
Pagination and Sorting
2 parents a99ff5c + 1ff2551 commit f20f519

File tree

4 files changed

+53
-12
lines changed

4 files changed

+53
-12
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
2015-06-29 - Initial release
2+
2015-12-21 - Pagination support
3+
2015-12-21 - Sort support
4+
2015-12-21 - v1.0.0

README.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,4 @@ jsonApi.define({
2828

2929
* Search, Find, Create, Delete, Update
3030
* Efficient lookups via appropriate indexes
31-
32-
### To do
33-
34-
* Filtering happens at the database layer
31+
* Database layer filtering, pagination and sorting

lib/mongoHandler.js

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,15 @@ MongoStore._getRelationshipAttributeNames = function(attributes) {
5151

5252
MongoStore._getSearchCriteria = function(relationships) {
5353
if (!relationships) return {};
54+
5455
var relationshipNames = Object.getOwnPropertyNames(relationships);
5556
var criteria = relationshipNames.reduce(function(partialCriteria, relationshipName) {
5657
var relationshipId = relationships[relationshipName];
5758
partialCriteria[relationshipName + ".id"] = relationshipId;
5859
return partialCriteria;
5960
}, {});
61+
debug("criteria>", JSON.stringify(criteria, null, 2));
62+
6063
return criteria;
6164
};
6265

@@ -80,6 +83,32 @@ MongoStore.prototype._createIndexesForRelationships = function(collection, relat
8083
};
8184

8285

86+
MongoStore.prototype._applySort = function(request, cursor) {
87+
if (!request.params.sort) return cursor;
88+
89+
var attribute = request.params.sort;
90+
var order = 1;
91+
attribute = String(attribute);
92+
if (attribute[0] === "-") {
93+
order = -1;
94+
attribute = attribute.substring(1, attribute.length);
95+
}
96+
var sortParam = { };
97+
sortParam[attribute] = order;
98+
debug("sort>", sortParam);
99+
100+
return cursor.sort(sortParam);
101+
};
102+
103+
104+
MongoStore.prototype._applyPagination = function(request, cursor) {
105+
if (!request.params.page) return cursor;
106+
107+
debug("pagination>", request.params.page.offset, request.params.page.limit);
108+
return cursor.skip(request.params.page.offset).limit(request.params.page.limit);
109+
};
110+
111+
83112
/**
84113
Initialise gets invoked once for each resource that uses this handler.
85114
*/
@@ -110,7 +139,7 @@ MongoStore.prototype.initialise = function(resourceConfig) {
110139
MongoStore.prototype.populate = function(callback) {
111140
var self = this;
112141
self._db.dropDatabase(function(err) {
113-
if (err) return console.error("error dropping database");
142+
if (err) return console.error("error dropping database", err.message);
114143
async.each(self.resourceConfig.examples, function(document, cb) {
115144
self.create({ params: {} }, document, cb);
116145
}, function(error) {
@@ -125,11 +154,23 @@ MongoStore.prototype.populate = function(callback) {
125154
Search for a list of resources, give a resource type.
126155
*/
127156
MongoStore.prototype.search = function(request, callback) {
128-
var collection = this._db.collection(request.params.type);
129-
debug("relationships> " + JSON.stringify(request.params.relationships, null, 2));
157+
var self = this;
158+
var collection = self._db.collection(request.params.type);
130159
var criteria = MongoStore._getSearchCriteria(request.params.relationships);
131-
debug("criteria> " + JSON.stringify(criteria, null, 2));
132-
collection.find(criteria, { _id: 0 }).toArray(callback);
160+
161+
async.parallel({
162+
resultSet: function(asyncCallback) {
163+
var cursor = collection.find(criteria, { _id: 0 });
164+
self._applySort(request, cursor);
165+
self._applyPagination(request, cursor);
166+
return cursor.toArray(asyncCallback);
167+
},
168+
totalRows: function(asyncCallback) {
169+
return collection.find(criteria, { _id: 0 }).count(asyncCallback);
170+
}
171+
}, function(err, results) {
172+
return callback(err, results.resultSet, results.totalRows);
173+
});
133174
};
134175

135176

@@ -185,7 +226,7 @@ MongoStore.prototype.update = function(request, partialResource, callback) {
185226
var collection = this._db.collection(request.params.type);
186227
var documentId = MongoStore._mongoUuid(request.params.id);
187228
var partialDocument = _.omit(partialResource, function(value) { return value === undefined; });
188-
debug("partialDocument> " + JSON.stringify(partialDocument, null, 2));
229+
debug("partialDocument>", JSON.stringify(partialDocument, null, 2));
189230
collection.findOneAndUpdate({
190231
_id: documentId
191232
}, {

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jsonapi-store-mongodb",
3-
"version": "0.0.1-alpha",
3+
"version": "1.0.0",
44
"description": "MongoDB data store for jsonapi-server.",
55
"main": "lib/mongoHandler.js",
66
"repository": {
@@ -40,7 +40,7 @@
4040
"coveralls": "2.11.2",
4141
"plato": "1.5.0",
4242
"mocha-performance": "0.1.0",
43-
"jsonapi-server": "0.16.0"
43+
"jsonapi-server": "1.0.3"
4444
},
4545
"scripts": {
4646
"test": "./node_modules/mocha/bin/mocha --timeout 20000 -R spec ./test/*.js",

0 commit comments

Comments
 (0)