Skip to content

Commit 34f620b

Browse files
committed
NODE-504 Collection - Default options when using promiseLibrary
1 parent c570099 commit 34f620b

File tree

3 files changed

+113
-1
lines changed

3 files changed

+113
-1
lines changed

HISTORY.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
2.0.37
2+
-----------------
3+
- NODE-504 Collection - Default options when using promiseLibrary.
4+
- Updated mongodb-core to 1.2.5 to fix NODE-492.
5+
16
2.0.36 07-07-2015
27
-----------------
38
- Fully promisified allowing the use of ES6 generators and libraries like co. Also allows for BYOP (Bring your own promises).

lib/collection.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1768,6 +1768,8 @@ Collection.prototype.findOneAndDelete = function(filter, options, callback) {
17681768

17691769
// Return a Promise
17701770
return new this.s.promiseLibrary(function(resolve, reject) {
1771+
options = options || {};
1772+
17711773
findOneAndDelete(self, filter, options, function(err, r) {
17721774
if(err) return reject(err);
17731775
resolve(r);
@@ -1812,6 +1814,8 @@ Collection.prototype.findOneAndReplace = function(filter, replacement, options,
18121814

18131815
// Return a Promise
18141816
return new this.s.promiseLibrary(function(resolve, reject) {
1817+
options = options || {};
1818+
18151819
findOneAndReplace(self, filter, replacement, options, function(err, r) {
18161820
if(err) return reject(err);
18171821
resolve(r);
@@ -1858,6 +1862,8 @@ Collection.prototype.findOneAndUpdate = function(filter, update, options, callba
18581862

18591863
// Return a Promise
18601864
return new this.s.promiseLibrary(function(resolve, reject) {
1865+
options = options || {};
1866+
18611867
findOneAndUpdate(self, filter, update, options, function(err, r) {
18621868
if(err) return reject(err);
18631869
resolve(r);
@@ -1917,6 +1923,8 @@ Collection.prototype.findAndModify = function(query, sort, doc, options, callbac
19171923

19181924
// Return a Promise
19191925
return new this.s.promiseLibrary(function(resolve, reject) {
1926+
options = options || {};
1927+
19201928
findAndModify(self, query, sort, doc, options, function(err, r) {
19211929
if(err) return reject(err);
19221930
resolve(r);

test/functional/promises_collection_tests.js

Lines changed: 100 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ exports['Should correctly execute Collection.prototype.insertOne'] = {
1515
test: function(configuration, test) {
1616
var MongoClient = configuration.require.MongoClient;
1717
var url = configuration.url();
18-
url = url.indexOf('?') != -1
18+
url = url.indexOf('?') != -1
1919
? f('%s&%s', url, 'maxPoolSize=100')
2020
: f('%s?%s', url, 'maxPoolSize=100');
2121

@@ -30,3 +30,102 @@ exports['Should correctly execute Collection.prototype.insertOne'] = {
3030
});
3131
}
3232
}
33+
34+
exports['Should correctly execute findOneAndDelete operation With Promises and no options passed in'] = {
35+
metadata: { requires: { promises:true, topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'] } },
36+
37+
// The actual test we wish to run
38+
test: function(configuration, test) {
39+
var db = configuration.newDbInstance(configuration.writeConcernMax(), {poolSize:1, auto_reconnect:false});
40+
db.open().then(function(db) {
41+
// LINE var MongoClient = require('mongodb').MongoClient,
42+
// LINE test = require('assert');
43+
// LINE MongoClient.connect('mongodb://localhost:27017/test', function(err, db) {
44+
// REPLACE configuration.writeConcernMax() WITH {w:1}
45+
// REMOVE-LINE test.done();
46+
// BEGIN
47+
// Get the collection
48+
var col = db.collection('find_one_and_delete_with_promise_no_option');
49+
col.insertMany([{a:1, b:1}], {w:1}).then(function(r) {
50+
test.equal(1, r.result.n);
51+
52+
col.findOneAndDelete({a:1}).then(function(r) {
53+
test.equal(1, r.lastErrorObject.n);
54+
test.equal(1, r.value.b);
55+
56+
db.close();
57+
test.done();
58+
}).catch(function(err) {
59+
console.log(err.stack)
60+
});
61+
});
62+
});
63+
// END
64+
}
65+
}
66+
67+
exports['Should correctly execute findOneAndUpate operation With Promises and no options passed in'] = {
68+
metadata: { requires: { promises:true, topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'] } },
69+
70+
// The actual test we wish to run
71+
test: function(configuration, test) {
72+
var db = configuration.newDbInstance(configuration.writeConcernMax(), {poolSize:1, auto_reconnect:false});
73+
db.open().then(function(db) {
74+
// LINE var MongoClient = require('mongodb').MongoClient,
75+
// LINE test = require('assert');
76+
// LINE MongoClient.connect('mongodb://localhost:27017/test', function(err, db) {
77+
// REPLACE configuration.writeConcernMax() WITH {w:1}
78+
// REMOVE-LINE test.done();
79+
// BEGIN
80+
// Get the collection
81+
var col = db.collection('find_one_and_update_with_promise_no_option');
82+
col.insertMany([{a:1, b:1}], {w:1}).then(function(r) {
83+
test.equal(1, r.result.n);
84+
85+
col.findOneAndUpdate({a:1}, {$set:{a:1}}).then(function(r) {
86+
test.equal(1, r.lastErrorObject.n);
87+
test.equal(1, r.value.b);
88+
89+
db.close();
90+
test.done();
91+
}).catch(function(err) {
92+
console.log(err.stack)
93+
});
94+
});
95+
});
96+
// END
97+
}
98+
}
99+
100+
exports['Should correctly execute findOneAndReplace operation With Promises and no options passed in'] = {
101+
metadata: { requires: { promises:true, topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'] } },
102+
103+
// The actual test we wish to run
104+
test: function(configuration, test) {
105+
var db = configuration.newDbInstance(configuration.writeConcernMax(), {poolSize:1, auto_reconnect:false});
106+
db.open().then(function(db) {
107+
// LINE var MongoClient = require('mongodb').MongoClient,
108+
// LINE test = require('assert');
109+
// LINE MongoClient.connect('mongodb://localhost:27017/test', function(err, db) {
110+
// REPLACE configuration.writeConcernMax() WITH {w:1}
111+
// REMOVE-LINE test.done();
112+
// BEGIN
113+
// Get the collection
114+
var col = db.collection('find_one_and_replace_with_promise_no_option');
115+
col.insertMany([{a:1, b:1}], {w:1}).then(function(r) {
116+
test.equal(1, r.result.n);
117+
118+
col.findOneAndReplace({a:1}, {a:1}).then(function(r) {
119+
test.equal(1, r.lastErrorObject.n);
120+
test.equal(1, r.value.b);
121+
122+
db.close();
123+
test.done();
124+
}).catch(function(err) {
125+
console.log(err.stack)
126+
});
127+
});
128+
});
129+
// END
130+
}
131+
}

0 commit comments

Comments
 (0)