Skip to content

Commit 6d02cc6

Browse files
committed
Merge pull request #9 from marshallswain/init-with-db
Allow services to share a common db connection.
2 parents 79c4037 + 20390eb commit 6d02cc6

File tree

3 files changed

+52
-33
lines changed

3 files changed

+52
-33
lines changed

README.md

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,17 @@ The following options can be passed when creating a new MongoDB service:
3535

3636
General options:
3737

38-
- `collection` - The name of the collection
38+
- `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.
3939
- `connectionString` - A MongoDB connection string
4040
- `[_id]` (default: `"_id"`) - The id property
4141
- `username` - MongoDB username
4242
- `password` - MongoDB password
4343

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

46+
- `db` (default: `"feathers"`) - The name of the database
4647
- `host` (default: `"localhost"`) - The MongoDB host
4748
- `port` (default: `27017`) - The MongoDB port
48-
- `db` (default: `"feathers"`) - The name of the database
4949

5050
MongoDB options:
5151

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

57+
## Sharing a MongoDB connection between services
58+
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:
59+
60+
```js
61+
var feathers = require('feathers')
62+
, mongo = require('mongoskin')
63+
, mongoService = require('feathers-mongodb')
64+
, app = feathers();
65+
66+
// First, make the connection.
67+
var db = mongo.db('mongodb://localhost:27017/my-project');
68+
69+
// Use the same db connection in both of these services.
70+
app.use('/api/users', mongoService({collection:db.collection('users')}));
71+
app.use('/api/todos', mongoService({collection:db.collection('todos')}));
72+
73+
app.listen(8080);
74+
```
75+
5776
## Extending MongoDB services
5877

5978
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).

lib/mongodb.js

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,41 +20,40 @@ var MongoService = Proto.extend({
2020
this._connect(this.options);
2121
},
2222

23-
// NOTE (EK): We create a new database connection for every MongoService.
24-
// This may not be good but... in the mean time the rational for this
25-
// design is because each user of a MongoService instance could be a separate
26-
// app residing on a totally different server.
27-
2823
// TODO (EK): We need to handle replica sets.
2924
_connect: function(options) {
30-
var connectionString = options.connectionString;
31-
var ackOptions = {
32-
w: options.w || 1, // write acknowledgment
33-
journal: options.journal || false, // doesn't wait for journal before acknowledgment
34-
fsync: options.fsync || false, // doesn't wait for syncing to disk before acknowledgment
35-
safe: options.safe || false
36-
};
37-
38-
if(!connectionString) {
39-
var config = _.extend({
40-
host: 'localhost',
41-
port: 27017,
42-
db: 'feathers'
43-
}, options);
44-
45-
connectionString = config.host + ':' + config.port + '/' + config.db;
46-
}
25+
if (typeof options.collection === 'string') {
26+
var connectionString = options.connectionString;
27+
var ackOptions = {
28+
w: options.w || 1, // write acknowledgment
29+
journal: options.journal || false, // doesn't wait for journal before acknowledgment
30+
fsync: options.fsync || false, // doesn't wait for syncing to disk before acknowledgment
31+
safe: options.safe || false
32+
};
33+
34+
if(!connectionString) {
35+
var config = _.extend({
36+
host: 'localhost',
37+
port: 27017,
38+
db: 'feathers'
39+
}, options);
40+
41+
connectionString = config.host + ':' + config.port + '/' + config.db;
42+
}
4743

48-
if(options.username && options.password) {
49-
connectionString += options.username + ':' + options.password + '@';
50-
}
44+
if(options.username && options.password) {
45+
connectionString += options.username + ':' + options.password + '@';
46+
}
5147

52-
if(options.reconnect) {
53-
connectionString += '?auto_reconnect=true';
54-
}
48+
if(options.reconnect) {
49+
connectionString += '?auto_reconnect=true';
50+
}
5551

56-
this.store = mongo.db(connectionString, ackOptions);
57-
this.collection = this.store.collection(options.collection);
52+
this.store = mongo.db(connectionString, ackOptions);
53+
this.collection = this.store.collection(options.collection);
54+
} else {
55+
this.collection = options.collection;
56+
}
5857
},
5958

6059
find: function(params, cb) {

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
"author": "Feathers <[email protected]> (http://feathersjs.com)",
1616
"contributors": [
1717
"Eric Kryski <[email protected]> (http://erickryski.com)",
18-
"David Luecke <[email protected]> (http://neyeon.com)"
18+
"David Luecke <[email protected]> (http://neyeon.com)",
19+
"Marshall Thompson <[email protected]>"
1920
],
2021
"license": "MIT",
2122
"bugs": {

0 commit comments

Comments
 (0)