Permalink
Browse files

support db options, remove mongoskin.bind, just use db.bind

  • Loading branch information...
1 parent c17c9ca commit 9a429ce4cb5993a4568a2c35eb357b8cc5fb6044 @guileen committed Jan 7, 2012
Showing with 21 additions and 21 deletions.
  1. +2 −2 integration/integration_tests.js
  2. +10 −17 lib/mongoskin/index.js
  3. +9 −2 lib/mongoskin/server.js
@@ -60,9 +60,9 @@ assert.equal(router.user, testdb1.user, 'user property should router to testdb1'
assert.equal(router.others, testdb2.others, 'user property should router to testdb1');
console.log('======== test MongoSkin.bind ========');
-mongo.bind('blog', bindToBlog);
-mongo.bind('users');
var db = mongo.db('localhost/test_mongoskin');
+db.bind('blog', bindToBlog);
+db.bind('users');
assert.equal(db.blog.first, bindToBlog.first);
assert.ok(db.users);
@@ -47,17 +47,8 @@ var parseServer = function(serverUrl) {
};
function MongoSkin() {
- this._preBindArgs = [];
}
-/**
- * affect to all SkinDb.bind
- */
-MongoSkin.prototype.bind = function() {
- var args = 1 <= arguments.length ? Array.prototype.slice.call(arguments, 0) : [];
- return this._preBindArgs.push(args);
-};
-
MongoSkin.prototype.pair = function(left, right) {
return new SkinServer(new ServerPair(parseServer(left), parseServer(right)));
};
@@ -73,20 +64,22 @@ MongoSkin.prototype.cluster = function() {
/**
* constructor SkinDb from serverUrl
*/
-MongoSkin.prototype.db = function(serverUrl) {
+MongoSkin.prototype.db = function(serverUrl, options) {
var config = parseUrl(serverUrl);
if (!config.database) {
throw new Error('Please provide a database to connect to.');
}
var server = new Server(config.host, config.port, config.options);
- var db = new Db(config.database, server, {
- native_parser: BSONNative !== undefined
- });
- var skindb = new SkinDb(db, config.username, config.password);
- for (var i = 0, len = this._preBindArgs.length; i < len; i++) {
- skindb.bind.apply(skindb, this._preBindArgs[i]);
+ var skinServer = new SkinServer(server);
+
+ if(!options) {
+ options = {};
+ }
+ if(options.username === undefined) {
+ options.username = config.username;
+ options.password = config.password;
}
- return skindb;
+ return skinServer.db(config.database, options);
};
/**
@@ -23,11 +23,18 @@ var SkinServer = exports.SkinServer = function(server) {
*
* TODO add options
*/
-SkinServer.prototype.db = function(name, username, password) {
+SkinServer.prototype.db = function(name, options) {
var key = (username || '') + '@' + name;
var skinDb = this._cache_[key];
if (!skinDb || skinDb.fail) {
- var db = new Db(name, this.server, {native_parser: !!mongodb.BSONNative});
+ var username = options.username,
+ password = options.password;
+ delete options.username;
+ delete options.password;
+ if(options.native_parser === undefined) {
+ options.native_parser = !! mongodb.BSONNative;
+ }
+ var db = new Db(name, this.server, options);
skinDb = new SkinDb(db, username, password);
this._cache_[key] = skinDb;
}

0 comments on commit 9a429ce

Please sign in to comment.