Merge pull request #110 from imothee/mongodb-replicaset
Mongodb native driver replicaset and authentication
This commit is contained in:
commit
d2d602117f
|
@ -11,19 +11,43 @@ exports.initialize = function initializeSchema(schema, callback) {
|
||||||
|
|
||||||
var s = schema.settings;
|
var s = schema.settings;
|
||||||
|
|
||||||
if (schema.settings.url) {
|
if (schema.settings.rs) {
|
||||||
var url = require('url').parse(schema.settings.url);
|
|
||||||
s.host = url.hostname;
|
s.rs = schema.settings.rs;
|
||||||
s.port = url.port;
|
if (schema.settings.url) {
|
||||||
s.database = url.pathname.replace(/^\//, '');
|
var uris = schema.settings.url.split(',');
|
||||||
s.username = url.auth && url.auth.split(':')[0];
|
s.hosts = []
|
||||||
s.password = url.auth && url.auth.split(':')[1];
|
s.ports = []
|
||||||
|
uris.forEach(function(uri) {
|
||||||
|
var url = require('url').parse(uri);
|
||||||
|
|
||||||
|
s.hosts.push(url.hostname || 'localhost');
|
||||||
|
s.ports.push(parseInt(url.port || '27017', 10));
|
||||||
|
|
||||||
|
if (!s.database) s.database = url.pathname.replace(/^\//, '');
|
||||||
|
if (!s.username) s.username = url.auth && url.auth.split(':')[0];
|
||||||
|
if (!s.password) s.password = url.auth && url.auth.split(':')[1];
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
s.database = s.database || 'test';
|
||||||
|
|
||||||
|
} else {
|
||||||
|
|
||||||
|
if (schema.settings.url) {
|
||||||
|
var url = require('url').parse(schema.settings.url);
|
||||||
|
s.host = url.hostname;
|
||||||
|
s.port = url.port;
|
||||||
|
s.database = url.pathname.replace(/^\//, '');
|
||||||
|
s.username = url.auth && url.auth.split(':')[0];
|
||||||
|
s.password = url.auth && url.auth.split(':')[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
s.host = s.host || 'localhost';
|
||||||
|
s.port = parseInt(s.port || '27017', 10);
|
||||||
|
s.database = s.database || 'test';
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
s.host = s.host || 'localhost';
|
|
||||||
s.port = parseInt(s.port || '27017', 10);
|
|
||||||
s.database = s.database || 'test';
|
|
||||||
|
|
||||||
schema.adapter = new MongoDB(s, schema, callback);
|
schema.adapter = new MongoDB(s, schema, callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -31,12 +55,34 @@ function MongoDB(s, schema, callback) {
|
||||||
this._models = {};
|
this._models = {};
|
||||||
this.collections = {};
|
this.collections = {};
|
||||||
|
|
||||||
var server = new mongodb.Server(s.host, s.port, {});
|
var server;
|
||||||
|
if (s.rs) {
|
||||||
|
set = [];
|
||||||
|
for(i=0, n=s.hosts.length; i<n; i++) {
|
||||||
|
set.push(new mongodb.Server(s.hosts[i], s.ports[i], {auto_reconnect: true}));
|
||||||
|
}
|
||||||
|
server = new mongodb.ReplSetServers(set, {rs_name:s.rs});
|
||||||
|
|
||||||
|
} else {
|
||||||
|
server = new mongodb.Server(s.host, s.port, {});
|
||||||
|
}
|
||||||
|
|
||||||
new mongodb.Db(s.database, server, {}).open(function (err, client) {
|
new mongodb.Db(s.database, server, {}).open(function (err, client) {
|
||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
this.client = client;
|
if (s.username && s.password) {
|
||||||
schema.client = client;
|
t = this;
|
||||||
callback();
|
client.authenticate(s.username, s.password, function(err, result) {
|
||||||
|
if (err) throw err;
|
||||||
|
t.client = client;
|
||||||
|
schema.client = client;
|
||||||
|
callback();
|
||||||
|
});
|
||||||
|
|
||||||
|
} else {
|
||||||
|
this.client = client;
|
||||||
|
schema.client = client;
|
||||||
|
callback();
|
||||||
|
}
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -185,5 +231,4 @@ MongoDB.prototype.updateAttributes = function updateAttrs(model, id, data, cb) {
|
||||||
|
|
||||||
MongoDB.prototype.disconnect = function () {
|
MongoDB.prototype.disconnect = function () {
|
||||||
this.client.close();
|
this.client.close();
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue