@@ -51,12 +51,15 @@ MongoStore._getRelationshipAttributeNames = function(attributes) {
5151
5252MongoStore . _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) {
110139MongoStore . 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 */
127156MongoStore . 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 } , {
0 commit comments