Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Some minor write concern fixes, Added mongoclient and mongoclient sin…
…gle server
- Loading branch information
Showing
6 changed files
with
195 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// var inherits = require('util').inherits | ||
var Db = require('./db').Db; | ||
|
||
var MongoClient = function MongoClient(serverConfig, options) { | ||
options = options == null ? {} : options; | ||
// If no write concern is set set the default to w:1 | ||
if(options != null && !options.safe && !options.journal && !options.w && !options.fsync) { | ||
options.w = 1; | ||
} | ||
|
||
// The internal db instance we are wrapping | ||
var _db = new Db('test', serverConfig, options); | ||
// Self reference | ||
var self = this; | ||
|
||
// Open command for the MongoClient | ||
MongoClient.prototype.open = function(callback) { | ||
_db.open(function(err, db) { | ||
if(err) return callback(err, null); | ||
callback(null, self); | ||
}) | ||
} | ||
|
||
// Close command for the MongoClient | ||
MongoClient.prototype.close = function(callback) { | ||
_db.close(callback); | ||
} | ||
|
||
// Get a db object | ||
MongoClient.prototype.db = function(dbName) { | ||
return _db.db(dbName); | ||
} | ||
} | ||
|
||
MongoClient.connect = function(url, options, callback) { | ||
Db.connect(url, options, function(err, db) { | ||
if(err) return callback(err, null); | ||
// If no write concern is set set the default to w:1 | ||
if(db.options != null && !db.options.safe && !db.options.journal && !db.options.w && !db.options.fsync) { | ||
db.options.w = 1; | ||
} | ||
// Return the db | ||
callback(null, db); | ||
}); | ||
} | ||
|
||
exports.MongoClient = MongoClient; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
var mongodb = process.env['TEST_NATIVE'] != null ? require('../lib/mongodb').native() : require('../lib/mongodb').pure(); | ||
var useSSL = process.env['USE_SSL'] != null ? true : false; | ||
var native_parser = (process.env['TEST_NATIVE'] != null); | ||
|
||
var testCase = require('nodeunit').testCase, | ||
debug = require('util').debug, | ||
inspect = require('util').inspect, | ||
nodeunit = require('nodeunit'), | ||
gleak = require('../dev/tools/gleak'), | ||
Db = mongodb.Db, | ||
Cursor = mongodb.Cursor, | ||
Collection = mongodb.Collection, | ||
Server = mongodb.Server, | ||
MongoClient = mongodb.MongoClient; | ||
|
||
var MONGODB = 'integration_tests'; | ||
var client = null; | ||
|
||
/** | ||
* Retrieve the server information for the current | ||
* instance of the db client | ||
* | ||
* @ignore | ||
*/ | ||
exports.setUp = function(callback) { | ||
var self = exports; | ||
client = new Db(MONGODB, new Server("127.0.0.1", 27017, {auto_reconnect: true, poolSize: 4, ssl:useSSL}), {safe:false, native_parser: (process.env['TEST_NATIVE'] != null)}); | ||
client.open(function(err, db_p) { | ||
if(numberOfTestsRun == (Object.keys(self).length)) { | ||
// If first test drop the db | ||
client.dropDatabase(function(err, done) { | ||
callback(); | ||
}); | ||
} else { | ||
return callback(); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Retrieve the server information for the current | ||
* instance of the db client | ||
* | ||
* @ignore | ||
*/ | ||
exports.tearDown = function(callback) { | ||
var self = this; | ||
numberOfTestsRun = numberOfTestsRun - 1; | ||
// Close connection | ||
client.close(); | ||
callback(); | ||
} | ||
|
||
// MongoClient interface | ||
// .open | ||
// .close | ||
// .db | ||
// .connect | ||
exports['Should correctly connect using MongoClient to a single server'] = function(test) { | ||
var mongoclient = new MongoClient(new Server("localhost", 27017, {ssl:useSSL}), {native_parser: (process.env['TEST_NATIVE'] != null)}); | ||
mongoclient.open(function(err, mongoclient) { | ||
|
||
// Get the first db and do an update document on it | ||
var db = mongoclient.db("integration_tests"); | ||
db.collection('mongoclient_test').update({a:1}, {b:1}, {upsert:true}, function(err, result) { | ||
test.equal(null, err); | ||
test.equal(1, result); | ||
|
||
// Get another db and do an update document on it | ||
var db2 = mongoclient.db("integration_tests2"); | ||
db2.collection('mongoclient_test').update({a:1}, {b:1}, {upsert:true}, function(err, result) { | ||
test.equal(null, err); | ||
test.equal(1, result); | ||
|
||
mongoclient.close(); | ||
test.done(); | ||
}); | ||
}); | ||
}); | ||
} | ||
|
||
exports['Should correctly connect using MongoClient to a single server using connect'] = function(test) { | ||
var options = {native_parser: (process.env['TEST_NATIVE'] != null)}; | ||
// Connect using the connection string | ||
MongoClient.connect("mongodb://localhost:27017/integration_tests", options, function(err, db) { | ||
test.equal(null, err); | ||
|
||
db.collection('mongoclient_test').update({a:1}, {b:1}, {upsert:true}, function(err, result) { | ||
test.equal(null, err); | ||
test.equal(1, result); | ||
|
||
db.close(); | ||
test.done(); | ||
}); | ||
}); | ||
} | ||
|
||
/** | ||
* Retrieve the server information for the current | ||
* instance of the db client | ||
* | ||
* @ignore | ||
*/ | ||
exports.noGlobalsLeaked = function(test) { | ||
var leaks = gleak.detectNew(); | ||
test.equal(0, leaks.length, "global var leak detected: " + leaks.join(', ')); | ||
test.done(); | ||
} | ||
|
||
/** | ||
* Retrieve the server information for the current | ||
* instance of the db client | ||
* | ||
* @ignore | ||
*/ | ||
var numberOfTestsRun = Object.keys(this).length - 2; |