diff --git a/CHANGELOG.md b/CHANGELOG.md index 5ea335a..be89615 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,11 @@ +1.4.0 +----------------- +- Added connection object with additional connection settings + +1.2.0 +----------------- +- Added findOne method. Thanks @youanswer. + 1.1.0 ----------------- - Bumped version of node-mongodb-native to 2.0.31 diff --git a/README.md b/README.md index 74981ec..ddc3cc7 100644 --- a/README.md +++ b/README.md @@ -247,17 +247,21 @@ db.multiUpdate( ## save() -It saves a document. If _id is provided in document body, then document is updated, - otherwise, it is inserted - It maps to mongo-native driver function, but with a promise. +Saves a document. +If _id is provided in document body, then document is updated, otherwise, it is inserted. +It maps to mongo-native driver function, but with a promise. ### Params - collecction *string* collection name - document *object* the document body. (it can have _id) + ### Returns -- promise of document *object*|*number* if _id was not provided, and then it was an insertion, - it will return the document with "_id" field. - Otherwise it will return 1 if existing document was updated +- promise of nrModified *number* | *object* + +Returns promise of 0 if document was not modified. +Returns promise of 1 if document was modified +Returns promise of document object if document was inserted + ### Basic usage ``` //saving a non existing document @@ -267,9 +271,10 @@ db.save( title: "Javascript 101", authors: ["java ninja dev"] -).then(function(doc){ - //document with _id field +).then(function(obj){ + console.log(obj._id); }); + //saving an existing document db.save( "book", @@ -278,8 +283,8 @@ db.save( title: "jQuery for newbies", author: "some jQuery guru" } -).then(function(doc){ - //doc will be 1 +).then(function(nrModified){ + // number of modified records (0 or 1) }); ``` diff --git a/mongo-driver.js b/mongo-driver.js index 7e9bf14..c31f2a0 100644 --- a/mongo-driver.js +++ b/mongo-driver.js @@ -15,20 +15,11 @@ var mongoclient = require("mongodb").MongoClient, /* */ -driver.connect = function(connection) { - - check.assert.unemptyString(connection, - "Invalid connection param, must be a non empty string"); - if (!(/^(mongodb\:\/\/)([\w\.]+|([0-9]{1,3}\.){3}[0-9]{1,3})(\:\d+\/\w+)(\?.*)?$/i). - test(connection)) { - throw new Error( - "Invalid connection param, must be in form of mongodb://:/" - ); - } +driver.connect = function(connection, connectionObject) { - var connect = node.lift(mongoclient.connect), - db = {}; + var connect = node.lift(mongoclient.connect), + db = {}; /* find @@ -293,7 +284,11 @@ driver.connect = function(connection) { defer.reject(err); } else { - defer.resolve(data.result.nModified ? data.result.nModified : data.ops[0]); + defer.resolve( + data.ops && data.ops[0] ? + data.ops[0] : + data.result.nModified + ); } }); @@ -412,7 +407,7 @@ driver.connect = function(connection) { }; //connect with mongodb via native driver and return db custom object. - return connect(connection).then(function(mongodb){ + return connect(connection, connectionObject).then(function(mongodb){ db._mongodb = mongodb; diff --git a/package.json b/package.json index b7a1fe2..3b98986 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mongo-driver", - "version": "1.1.0", + "version": "1.4.0", "description": "A wrapper around mongo native that uses promises instead of callbacks", "main": "mongo-driver.js", "scripts": { @@ -8,7 +8,7 @@ }, "dependencies": { "check-types": "3.2.0", - "mongodb": "2.0.31", + "mongodb": "2.0.39", "when": "3.7.3" }, "devDependencies": { diff --git a/tests/mongo-driver.test.js b/tests/mongo-driver.test.js index 53cf8f4..4e8ffc6 100644 --- a/tests/mongo-driver.test.js +++ b/tests/mongo-driver.test.js @@ -16,29 +16,27 @@ var driver = require("../mongo-driver"), describe("mongo-driver", function(){ var conn = "mongodb://localhost:27017/test", + connObj = {auto_reconnect: true, slaveOk: true }, db; describe("connection", function(){ - it("throws error if connection is not provided", function(){ - expect( driver.connect.bind(driver) ). - to.throw(Error, /non[\s\w]+empty[\s\w]+string/i ); - }); it("throws error if connection is malformed", function(){ var conns = [ + "", "537f6fd2d11fa3c6054a8068", - "localhost:27017/test", - "mongodb://localhost:27017", - "mongodb://:27017/test", - "mongodb://:ADF/test" + "some nonesense" ]; conns.forEach(function(e){ - expect( driver.connect.bind(driver, e) ). - to.throw(Error, /invalid\sconnection\sparam/i ); + driver.connect(e).should.be.rejected; }); }); + it("does not reject when url includes username and password", function(done){ + driver.connect("mongodb://me_development:unoDosNahTr3s@localhost:27017").should.be.rejectedWith('Authentication failed').notify(done); + }); + // TIMEOUT PROBLEM TEST FAILING SOMETIMES TODO: FIX // it("throws error if cannot reach host", function(done){ @@ -63,6 +61,16 @@ describe("mongo-driver", function(){ notify(done); }); + it("connect succesfuly with: " + conn + " and connectionObject:" + connObj, function(done){ + driver.connect(conn, connObj). + then(function(_db){ + db = _db; //passing to global reference (trick) + return _db; + }). + should.eventually.contain.keys("find","insert"). + notify(done); + }); + }); describe("features", function(){ @@ -663,6 +671,24 @@ describe("mongo-driver", function(){ }); }); + it("works when you save with _id unmodified", function(done) { + var p; + db.find( + "book", + { + title: /dracula/i + } + ).then(function(books){ + p = db.save( + "book", + books[0] + ); + p.should.eventually.be.an("number"). + should.eventually.equals(0).notify(done); + }); + + }); + }); describe("saveComplete", function(){