A MongoDB CRUD service for feathers
To install feathers-hooks from npm, run:
$ npm install feathers-mongodb --saveFinally, to use the plugin in your Feathers app:
var feathers = require('feathers');
var mongodb = require('feathers-mongodb');
// Initialize a MongoDB service with the users collection on a local MongoDB instance
var app = feathers()
.use('/users', mongodb({
collection: 'users'
}));
app.listen(8080);The following options can be passed when creating a new MongoDB service:
General options:
collection- The name of the collection or an already-connected collection object. When using an object, no other options are needed. See the example below.connectionString- A MongoDB connection string[_id](default:"_id") - The id propertyusername- MongoDB usernamepassword- MongoDB password
Connection options (when connectionString is not set):
db(default:"feathers") - The name of the databasehost(default:"localhost") - The MongoDB hostport(default:27017) - The MongoDB port
MongoDB options:
w(default:1) - Write acknowledgementsjournal(default:false) - Don't wait for journal before acknowledgementfsync(default:false) - Don't wait for syncing to disk before acknowledgmentsafe(default:false) - Safe mode
When creating a new service, the default behavior is to create a new connection to the specified database. If you would rather share a database connection between multiple services, connect to the database then pass an already-connected collection object in on options.collection. For example:
var feathers = require('feathers')
, mongo = require('mongoskin')
, mongoService = require('feathers-mongodb')
, app = feathers();
// First, make the connection.
var db = mongo.db('mongodb://localhost:27017/my-project');
// Use the same db connection in both of these services.
app.use('/api/users', mongoService({collection:db.collection('users')}));
app.use('/api/todos', mongoService({collection:db.collection('todos')}));
app.listen(8080);To extend the basic MongoDB service there are two options. Either through using Uberproto's inheritance mechanism or by using feathers-hooks.
The basic MongoDB Feathers service is implemented using Uberproto, a small EcmaScript 5 inheritance library so you can use the Uberproto syntax to add your custom functionality.
For example, you might want update and patch to behave the same (the basic implementation of update replaces the entire object instead of merging it) and add an updatedAt and createdAt flag to your data:
// myservice.js
var mongodb = require('feathers-mongodb');
var Proto = require('uberproto');
var TimestampPatchService = mongodb.Service.extend({
create: function(data, params, callback) {
data.createdAt = new Date();
// Call the original `create`
return this._super(data, params, callback);
},
update: function() {
// Call `patch` instead so that PUT calls merge
// instead of replace data, too
this.patch(id, data, params, callback);
},
patch: function(id, data, params, callback) {
data.updatedAt = new Date();
// Call the original `patch`
this._super(id, data, params, callback);
}
});
// Export a simple function that instantiates this new service like
// var myservice = require('myservice');
// app.use('/users', myservice(options));
module.exports = function(options) {
// We need to call `Proto.create` explicitly here since we are overriding
// the original `create` method
return Proto.create.call(TimestampPatchService, options);
}
module.exports.Service = TimestampPatchService;Another option is to weave functionality into your existing services using feathers-hooks, for example the above createdAt and updatedAt functionality:
var feathers = require('feathers');
var hooks = require('feathers-hooks');
var mongodb = require('feathers-mongodb');
// Initialize a MongoDB service with the users collection on a local MongoDB instance
var app = feathers()
.configure(hooks())
.use('/users', mongodb({
collection: 'users'
});
app.lookup('users').before({
create: function(hook, next) {
hook.data.createdAt = new Date();
next();
},
update: function(hook, next) {
hook.data.updatedAt = new Date();
next();
}
});
app.listen(8080);Internally the MongoDB service uses MongoSkin.
The query options (e.g. for a .find) call are taken from params.query.
To set the query options, set params.options in your service calls. Using feathers-hooks, for example, a sort mechanism based on query parameters
(e.g. /users?__sort=name) can be implemented as a hook like this:
var app = feathers()
.configure(hooks())
.use('/users', mongodb({
collection: 'users'
});
app.lookup('users').before({
find: function(hook, next) {
// Grab the fieldname from `params.query`
var field = hook.params.query.__sort;
if(field) {
hook.params.options = { sort: [[field, -1]] };
}
// Delete it from the query so that it's not passed as
// the MongoSkin query object
delete hook.params.query.__sort;
next();
}
});0.3.0
- Implement
.patchsupport (#5) - Better documentation
- Refactoring that removes pre-implemented MongoSkin options
0.2.x
- Pre-releases
Copyright (c) 2014 Eric Kryski, David Luecke
Licensed under the MIT license.


