@@ -51,12 +51,15 @@ MongoStore._getRelationshipAttributeNames = function(attributes) {
51
51
52
52
MongoStore . _getSearchCriteria = function ( relationships ) {
53
53
if ( ! relationships ) return { } ;
54
+
54
55
var relationshipNames = Object . getOwnPropertyNames ( relationships ) ;
55
56
var criteria = relationshipNames . reduce ( function ( partialCriteria , relationshipName ) {
56
57
var relationshipId = relationships [ relationshipName ] ;
57
58
partialCriteria [ relationshipName + ".id" ] = relationshipId ;
58
59
return partialCriteria ;
59
60
} , { } ) ;
61
+ debug ( "criteria>" , JSON . stringify ( criteria , null , 2 ) ) ;
62
+
60
63
return criteria ;
61
64
} ;
62
65
@@ -80,6 +83,32 @@ MongoStore.prototype._createIndexesForRelationships = function(collection, relat
80
83
} ;
81
84
82
85
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
+
83
112
/**
84
113
Initialise gets invoked once for each resource that uses this handler.
85
114
*/
@@ -110,7 +139,7 @@ MongoStore.prototype.initialise = function(resourceConfig) {
110
139
MongoStore . prototype . populate = function ( callback ) {
111
140
var self = this ;
112
141
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 ) ;
114
143
async . each ( self . resourceConfig . examples , function ( document , cb ) {
115
144
self . create ( { params : { } } , document , cb ) ;
116
145
} , function ( error ) {
@@ -125,11 +154,23 @@ MongoStore.prototype.populate = function(callback) {
125
154
Search for a list of resources, give a resource type.
126
155
*/
127
156
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 ) ;
130
159
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
+ } ) ;
133
174
} ;
134
175
135
176
@@ -185,7 +226,7 @@ MongoStore.prototype.update = function(request, partialResource, callback) {
185
226
var collection = this . _db . collection ( request . params . type ) ;
186
227
var documentId = MongoStore . _mongoUuid ( request . params . id ) ;
187
228
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 ) ) ;
189
230
collection . findOneAndUpdate ( {
190
231
_id : documentId
191
232
} , {
0 commit comments