Skip to content

Commit

Permalink
now can connect with an url
Browse files Browse the repository at this point in the history
  • Loading branch information
ichiriac committed Nov 16, 2014
1 parent 1337f94 commit 398e8bf
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 13 deletions.
12 changes: 12 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var q = require('q');
var extend = require('extend');
var util = require('util');
var url = require('url');
var EventEmitter = require('events').EventEmitter;

/**
Expand Down Expand Up @@ -86,6 +87,17 @@ manager.prototype.connect = function(driver, options) {
options = driver;
driver = 'couchbase';
}
if (typeof options === 'string') {
var meta = url.parse(options, true, true);
options = {
host: meta.host,
database: meta.pathname,
params: meta.query
};
if (meta.protocol) {
driver = meta.protocol.substring(0, meta.protocol.length - 1);
}
}
if (this.cb) this.disconnect();
this.cb = require('./src/drivers/' + driver)(this);
this.cb.connect(options, result);
Expand Down
15 changes: 8 additions & 7 deletions src/drivers/couchbase.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,18 @@ module.exports = function(manager) {
/** Connects **/
connect: function(options, result) {
if (this.cb) this.shutdown();
var cnx = {
host: options.host,
bucket: options.bucket ? options.bucket : options.database
};
var builder = cb.Connection;
var self = this;
this.mock = false;
if (options.hasOwnProperty('mock')) {
if (options.mock) {
builder = cb.Mock.Connection;
this.mock = true;
}
delete options.mock;
if (options.params.mock) {
builder = cb.Mock.Connection;
this.mock = true;
}
this.cb = new builder(options || {}, function(err) {
this.cb = new builder(cnx, function(err) {
if (err) {
result.reject(err);
manager.emit('error', err);
Expand Down
10 changes: 5 additions & 5 deletions test/connector.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ var session = new sofa();
module.exports = function(mock) {
session.mappers = {};
session.removeAllListeners();
return session.connect({
host: 'localhost:8091'
,bucket: 'tests'
,mock: typeof mock === 'undefined' ? true : mock
});
return session.connect(
'couchbase://localhost:8091/tests?mock=' + (
(typeof mock === 'undefined' ? true : mock) ? 'true' : 'false'
)
);
};
4 changes: 3 additions & 1 deletion test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ describe('test main api', function() {
return couchbase.connect({
host: 'demo.123:8091'
, bucket: 'unknown'
, mock: false
, params : {
mock: false
}
});
}).then(function() {
assert(false, 'should not be connected to "unknown" bucket !');
Expand Down

0 comments on commit 398e8bf

Please sign in to comment.