Skip to content

Commit 8c4f991

Browse files
committed
Getting special query filters working and fixing a few bugs
1 parent b488561 commit 8c4f991

File tree

1 file changed

+50
-49
lines changed

1 file changed

+50
-49
lines changed

lib/feathers-mongoose.js

Lines changed: 50 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
var _ = require('lodash');
22
var Proto = require('uberproto');
33
var mongoose = require('mongoose');
4-
var errors = require('feathers').errors.types;
4+
var errors = require('feathers-errors').types;
5+
var filter = require('feathers-query-filters');
56
var Schema = mongoose.Schema;
67
var ObjectId = mongoose.Types.ObjectId;
78

@@ -10,7 +11,7 @@ var MongooseService = Proto.extend({
1011
init: function(modelName, entity, options) {
1112
options = options || {};
1213

13-
if(typeof modelName !== 'string') {
14+
if (!_.isString(modelName)) {
1415
throw new errors.GeneralError('Must provide a valid model name');
1516
}
1617

@@ -86,22 +87,25 @@ var MongooseService = Proto.extend({
8687
return;
8788
}
8889

89-
if(!connectionString) {
90+
if (!connectionString) {
9091
var config = _.extend({
9192
host: 'localhost',
9293
port: 27017,
93-
db: 'feathers'
94+
db: 'feathers',
95+
reconnect: true
9496
}, options);
9597

96-
connectionString = config.host + ':' + config.port + '/' + config.db;
97-
}
98+
connectionString = 'mongodb://';
9899

99-
if(options.username && options.password) {
100-
connectionString += options.username + ':' + options.password + '@';
101-
}
100+
if (config.username && config.password) {
101+
connectionString += config.username + ':' + config.password + '@';
102+
}
103+
104+
connectionString += config.host + ':' + config.port + '/' + config.db;
102105

103-
if(options.reconnect) {
104-
connectionString += '?auto_reconnect=true';
106+
if (config.reconnect) {
107+
connectionString += '?auto_reconnect=true';
108+
}
105109
}
106110

107111
// TODO (EK): Support mongoose connection options
@@ -115,53 +119,44 @@ var MongooseService = Proto.extend({
115119
params = {};
116120
}
117121

118-
var sort = {};
119-
var populate = '';
120-
var limit = 100;
121-
var skip = 0;
122-
var select = {};
123-
124122
params.query = params.query || {};
123+
var filters = filter(params.query);
125124

126-
if (params.query.sort) {
127-
sort = params.query.sort;
128-
delete params.query.sort;
125+
var query = this.model.find(params.query);
126+
127+
if (filters.$select && filters.$select.length) {
128+
var fields = {};
129+
130+
_.each(filters.$select, function(key){
131+
fields[key] = 1;
132+
});
133+
134+
query.select(fields);
129135
}
130136

131-
if (params.query.populate) {
132-
populate = params.query.populate;
133-
delete params.query.populate;
137+
if (filters.$sort){
138+
query.sort(filters.$sort);
134139
}
135140

136-
if (params.query.limit) {
137-
limit = params.query.limit;
138-
delete params.query.limit;
141+
if (filters.$limit){
142+
query.limit(filters.$limit);
139143
}
140144

141-
if (params.query.skip) {
142-
skip = params.query.skip;
143-
delete params.query.skip;
145+
if (filters.$skip){
146+
query.skip(filters.$skip);
144147
}
145148

146-
if (params.query.select) {
147-
select = params.query.select;
148-
delete params.query.select;
149+
if (filters.$populate){
150+
query.populate(filters.$populate);
149151
}
150-
151-
this.model
152-
.find(params.query)
153-
.sort(sort)
154-
.limit(limit)
155-
.skip(skip)
156-
.select(select)
157-
.populate(populate)
158-
.exec(function (error, data) {
159-
if (error) {
160-
return cb(new errors.BadRequest(error));
161-
}
162152

163-
cb(null, data);
164-
});
153+
query.exec(function(error, data) {
154+
if (error) {
155+
return cb(new errors.BadRequest(error));
156+
}
157+
158+
cb(null, data);
159+
});
165160
},
166161

167162
get: function(id, params, cb) {
@@ -215,16 +210,22 @@ var MongooseService = Proto.extend({
215210
},
216211

217212
patch: function(id, data, params, cb) {
218-
this.update.apply(this, arguments);
213+
this.update.call(this, id, data, params, cb);
219214
},
220215

221216
update: function(id, data, params, cb) {
222-
if(_.isFunction(params)) {
217+
if (_.isFunction(data)) {
218+
cb = data;
219+
return cb(new errors.BadRequest('You need to provide data to be updated'));
220+
}
221+
222+
if (_.isFunction(params)) {
223223
cb = params;
224224
params = {};
225225
}
226226

227-
this.model.findByIdAndUpdate(new ObjectId(id), data, { upsert: true }, function(error, data) {
227+
// TODO (EK): Support updating multiple docs. Maybe we just use feathers-batch
228+
this.model.findByIdAndUpdate(new ObjectId(id), data, {new: true}, function(error, data) {
228229
if (error) {
229230
return cb(new errors.BadRequest(error));
230231
}

0 commit comments

Comments
 (0)