Skip to content
This repository has been archived by the owner on Mar 15, 2023. It is now read-only.

Commit

Permalink
Merge pull request #9 from marshallswain/init-with-db
Browse files Browse the repository at this point in the history
Allow services to share a common db connection.
  • Loading branch information
marshallswain committed Apr 6, 2015
2 parents 79c4037 + 20390eb commit 6d02cc6
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 33 deletions.
23 changes: 21 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@ The following options can be passed when creating a new MongoDB service:

General options:

- `collection` - The name of the collection
- `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 property
- `username` - MongoDB username
- `password` - MongoDB password

Connection options (when `connectionString` is not set):

- `db` (default: `"feathers"`) - The name of the database
- `host` (default: `"localhost"`) - The MongoDB host
- `port` (default: `27017`) - The MongoDB port
- `db` (default: `"feathers"`) - The name of the database

MongoDB options:

Expand All @@ -54,6 +54,25 @@ MongoDB options:
- `fsync` (default: `false`) - Don't wait for syncing to disk before acknowledgment
- `safe` (default: `false`) - Safe mode

## Sharing a MongoDB connection between services
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:

```js
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);
```

## Extending MongoDB services

To extend the basic MongoDB service there are two options. Either through using [Uberproto's](https://github.com/daffl/uberproto) inheritance mechanism or by using [feathers-hooks](https://github.com/feathersjs/feathers-hooks).
Expand Down
59 changes: 29 additions & 30 deletions lib/mongodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,41 +20,40 @@ var MongoService = Proto.extend({
this._connect(this.options);
},

// NOTE (EK): We create a new database connection for every MongoService.
// This may not be good but... in the mean time the rational for this
// design is because each user of a MongoService instance could be a separate
// app residing on a totally different server.

// TODO (EK): We need to handle replica sets.
_connect: function(options) {
var connectionString = options.connectionString;
var ackOptions = {
w: options.w || 1, // write acknowledgment
journal: options.journal || false, // doesn't wait for journal before acknowledgment
fsync: options.fsync || false, // doesn't wait for syncing to disk before acknowledgment
safe: options.safe || false
};

if(!connectionString) {
var config = _.extend({
host: 'localhost',
port: 27017,
db: 'feathers'
}, options);

connectionString = config.host + ':' + config.port + '/' + config.db;
}
if (typeof options.collection === 'string') {
var connectionString = options.connectionString;
var ackOptions = {
w: options.w || 1, // write acknowledgment
journal: options.journal || false, // doesn't wait for journal before acknowledgment
fsync: options.fsync || false, // doesn't wait for syncing to disk before acknowledgment
safe: options.safe || false
};

if(!connectionString) {
var config = _.extend({
host: 'localhost',
port: 27017,
db: 'feathers'
}, options);

connectionString = config.host + ':' + config.port + '/' + config.db;
}

if(options.username && options.password) {
connectionString += options.username + ':' + options.password + '@';
}
if(options.username && options.password) {
connectionString += options.username + ':' + options.password + '@';
}

if(options.reconnect) {
connectionString += '?auto_reconnect=true';
}
if(options.reconnect) {
connectionString += '?auto_reconnect=true';
}

this.store = mongo.db(connectionString, ackOptions);
this.collection = this.store.collection(options.collection);
this.store = mongo.db(connectionString, ackOptions);
this.collection = this.store.collection(options.collection);
} else {
this.collection = options.collection;
}
},

find: function(params, cb) {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"author": "Feathers <[email protected]> (http://feathersjs.com)",
"contributors": [
"Eric Kryski <[email protected]> (http://erickryski.com)",
"David Luecke <[email protected]> (http://neyeon.com)"
"David Luecke <[email protected]> (http://neyeon.com)",
"Marshall Thompson <[email protected]>"
],
"license": "MIT",
"bugs": {
Expand Down

0 comments on commit 6d02cc6

Please sign in to comment.