Comparing version 1.0.16 to 1.1.0
1.1.0 / 2011-02-25 | ||
================== | ||
* Added support for replica sets. | ||
1.0.16 / 2011-02-18 | ||
@@ -3,0 +8,0 @@ =================== |
@@ -121,8 +121,11 @@ | ||
// open connection | ||
this.doOpen(function(err){ | ||
this.doOpen(function (err) { | ||
if (err) { | ||
if (typeof callback == 'function') callback(err); | ||
if (typeof callback == 'function') | ||
callback(err); | ||
} else { | ||
self.onOpen(); | ||
if (typeof callback == 'function') callback(null); | ||
if (typeof callback == 'function') | ||
callback(null); | ||
} | ||
@@ -135,2 +138,58 @@ }); | ||
/** | ||
* Connects to a replica set. | ||
* | ||
* Supply a comma-separted list of mongodb:// URIs. You only need to specify | ||
* the database name and/or auth to one of them. | ||
* | ||
* @param {String} comma-separated mongodb:// URIs | ||
* @param {Function} optional callback | ||
*/ | ||
Connection.prototype.openSet = function (uris, database, callback) { | ||
var uris = uris.split(',') | ||
, self = this; | ||
if (uris.length < 2) { | ||
if (callback) callback(new Error('Please provide comma-separated URIs')); | ||
return this; | ||
} | ||
// signal connecting | ||
this.readyState = 2; | ||
this.emit('opening'); | ||
this.host = []; | ||
this.port = []; | ||
uris.forEach(function (uri) { | ||
var uri = url.parse(uri); | ||
self.host.push(uri.hostname); | ||
self.port.push(uri.port || 27017); | ||
if (!self.name && uri.pathname.replace(/\//g, '')) | ||
self.name = uri.pathname.replace(/\//g, ''); | ||
if (!self.user && uri.auth) { | ||
var auth = uri.auth.split(':'); | ||
self.user = auth[0]; | ||
self.pass = auth[1]; | ||
} | ||
}); | ||
// open connection | ||
this.doOpenSet(function (err) { | ||
if (err) { | ||
if (typeof callback == 'function') | ||
callback(err); | ||
} else { | ||
self.onOpen(); | ||
if (typeof callback == 'function') | ||
callback(null); | ||
} | ||
}); | ||
}; | ||
/** | ||
* Called when the connection is opened | ||
@@ -144,17 +203,18 @@ * | ||
var continuation = function(){ | ||
self.readyState = 1; | ||
// avoid having the collection subscribe to our event emitter | ||
// to prevent 0.3 warning | ||
for (var i in self.collections) | ||
self.collections[i].onOpen(); | ||
function open () { | ||
self.readyState = 1; | ||
// avoid having the collection subscribe to our event emitter | ||
// to prevent 0.3 warning | ||
for (var i in self.collections) | ||
self.collections[i].onOpen(); | ||
self.emit('open'); | ||
self.emit('open'); | ||
}; | ||
//do authentication before we continue if a database username and password exist | ||
if(self.user && self.pass) | ||
self.db.authenticate(self.user,self.pass,continuation); | ||
// re-authenticate | ||
if (self.user && self.pass) | ||
self.db.authenticate(self.user, self.pass, open); | ||
else | ||
continuation(); | ||
open(); | ||
}; | ||
@@ -161,0 +221,0 @@ |
@@ -7,3 +7,5 @@ | ||
var Connection = require('../../connection') | ||
, mongo = require('../../../../support/node-mongodb-native/lib/mongodb/'); | ||
, mongo = require('../../../../support/node-mongodb-native/lib/mongodb/') | ||
, Server = mongo.Server | ||
, ReplSetServers = mongo.ReplSetServers; | ||
@@ -36,3 +38,5 @@ /** | ||
this.db = new mongo.Db(this.name, new mongo.Server(this.host, this.port)); | ||
this.db.open(fn); | ||
return this; | ||
@@ -42,2 +46,27 @@ }; | ||
/** | ||
* Opens a set connection | ||
* | ||
* @param {Function} callback | ||
* @api private | ||
*/ | ||
NativeConnection.prototype.doOpenSet = function (fn) { | ||
if (!this.db) { | ||
var servers = [] | ||
, ports = this.port; | ||
this.host.forEach(function (host, i) { | ||
servers.push(new mongo.Server(host, ports[i], {})); | ||
}); | ||
this.db = new mongo.Db(this.name, new ReplSetServers(servers)); | ||
} | ||
this.db.open(fn); | ||
return this; | ||
}; | ||
/** | ||
* Closes the connection | ||
@@ -44,0 +73,0 @@ * |
@@ -80,2 +80,17 @@ | ||
/** | ||
* Creates a replica set connection | ||
* | ||
* @see {Mongoose#createConnection} | ||
* @api public | ||
*/ | ||
Mongoose.prototype.createSetConnection = function () { | ||
var conn = new Connection(this); | ||
this.connections.push(conn); | ||
if (arguments.length) | ||
conn.openSet.apply(conn, arguments); | ||
return conn; | ||
}; | ||
/** | ||
* Connects the default mongoose connection | ||
@@ -93,2 +108,14 @@ * | ||
/** | ||
* Connects the default mongoose connection to a replica set | ||
* | ||
* @see {Mongoose#createConnection} | ||
* @api public | ||
*/ | ||
Mongoose.prototype.connectSet = function (){ | ||
this.connection.openSet.apply(this.connection, arguments); | ||
return this; | ||
}; | ||
/** | ||
* Disconnects from all connections. | ||
@@ -258,3 +285,3 @@ * | ||
exports.version = '1.0.16'; | ||
exports.version = '1.1.0'; | ||
@@ -261,0 +288,0 @@ /** |
@@ -16,3 +16,3 @@ | ||
function SchemaMixed (path, options) { | ||
function Mixed (path, options) { | ||
SchemaType.call(this, path, options); | ||
@@ -24,3 +24,3 @@ }; | ||
*/ | ||
SchemaMixed.prototype.__proto__ = SchemaType.prototype; | ||
Mixed.prototype.__proto__ = SchemaType.prototype; | ||
@@ -33,3 +33,3 @@ /** | ||
SchemaMixed.prototype.checkRequired = function (value) { | ||
Mixed.prototype.checkRequired = function (value) { | ||
return true; | ||
@@ -45,7 +45,7 @@ }; | ||
SchemaMixed.prototype.cast = function (value) { | ||
Mixed.prototype.cast = function (value) { | ||
return value; | ||
}; | ||
SchemaMixed.prototype.castForQuery = SchemaMixed.prototype.cast; | ||
Mixed.prototype.castForQuery = Mixed.prototype.cast; | ||
@@ -56,2 +56,2 @@ /** | ||
module.exports = SchemaMixed; | ||
module.exports = Mixed; |
{ | ||
"name": "mongoose" | ||
, "description": "Mongoose MongoDB ORM" | ||
, "version": "1.0.16" | ||
, "version": "1.1.0" | ||
, "author": "Guillermo Rauch <guillermo@learnboost.com>" | ||
@@ -6,0 +6,0 @@ , "keywords": ["mongodb", "mongoose", "orm", "data", "datastore", "nosql"] |
@@ -151,4 +151,42 @@ | ||
}); | ||
}, | ||
'test connecting to a replica set': function () { | ||
var uri = process.env.MONGOOSE_SET_TEST_URI; | ||
if (!uri) { | ||
console.log('\033[31m', '\n', 'You\'re not testing for replica sets!' | ||
, '\n', 'Please set the MONGOOSE_SET_TEST_URI env variable.', '\n' | ||
, 'e.g: `mongodb://localhost:27017/db,mongodb://localhost…`', '\n' | ||
, '\033[39m'); | ||
return; | ||
} | ||
var mong = new Mongoose(); | ||
mongoose.connectSet(uri, function (err) { | ||
should.strictEqual(err, null); | ||
mong.model('Test', new mongoose.Schema({ | ||
test: String | ||
})); | ||
var Test = mong.model('Test') | ||
, test = new Test(); | ||
test.test = 'aa'; | ||
test.save(function (err) { | ||
should.strictEqual(err, null); | ||
Test.findById(test._id, function (err, doc) { | ||
should.strictEqual(err, null); | ||
doc.test.should.eql('aa'); | ||
mongoose.connection.close(); | ||
}); | ||
}); | ||
}); | ||
} | ||
}; |
@@ -16,2 +16,3 @@ | ||
, DocumentObjectId = mongoose.Types.ObjectId | ||
, Mixed = SchemaTypes.Mixed | ||
, MongooseNumber = mongoose.Types.Number; | ||
@@ -44,2 +45,3 @@ | ||
, alive : Boolean | ||
, extra : Mixed | ||
}); | ||
@@ -56,2 +58,3 @@ | ||
Ferret.path('alive').should.be.an.instanceof(SchemaTypes.Boolean); | ||
Ferret.path('extra').should.be.an.instanceof(SchemaTypes.Mixed); | ||
@@ -332,2 +335,3 @@ should.strictEqual(Ferret.path('unexistent'), undefined); | ||
, nocast : [] | ||
, mixed : [Mixed] | ||
}); | ||
@@ -365,2 +369,11 @@ | ||
nocasts[1].should.eql(123); | ||
var mixed = Loki.path('mixed').cast(['test', 123, '123', {}, new Date, new DocumentObjectId]); | ||
mixed[0].should.be.a('string'); | ||
mixed[1].should.be.a('number'); | ||
mixed[2].should.be.a('string'); | ||
mixed[3].should.be.a('object'); | ||
mixed[4].should.be.an.instanceof(Date); | ||
mixed[5].should.be.an.instanceof(DocumentObjectId); | ||
}, | ||
@@ -367,0 +380,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1412814
192
25023
79