Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
25 changes: 15 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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",
Expand All @@ -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)
});
```

Expand Down
23 changes: 9 additions & 14 deletions mongo-driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -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://<host>:<port>/<dbname>"
);
}
driver.connect = function(connection, connectionObject) {

var connect = node.lift(mongoclient.connect),
db = {};

var connect = node.lift(mongoclient.connect),
db = {};

/*
find
Expand Down Expand Up @@ -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
);
}
});

Expand Down Expand Up @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"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": {
"test": "./node_modules/mocha/bin/mocha --ui bdd --reporter spec --recursive tests/"
},
"dependencies": {
"check-types": "3.2.0",
"mongodb": "2.0.31",
"mongodb": "2.0.39",
"when": "3.7.3"
},
"devDependencies": {
Expand Down
46 changes: 36 additions & 10 deletions tests/mongo-driver.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand All @@ -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(){
Expand Down Expand Up @@ -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(){
Expand Down