-
Notifications
You must be signed in to change notification settings - Fork 64
♻️ Use mongodb
promises
#140
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
[`mongodb@5`][1] removes support for using callbacks, forcing consumers to use promises instead. This non-breaking change does the minimum amount of work to move us to using `mongodb` promises, while keeping all our interfaces intact. [1]: https://github.com/mongodb/node-mongodb-native/releases/tag/v5.0.0
aa03087
to
21a5ce5
Compare
index.js
Outdated
this.pendingConnect = []; | ||
this._connect(mongo, options); | ||
var connection = this._connect(mongo, options); | ||
this.mongo = connection.then(function(result) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems to be changing from a DB
instance to a Promise<DB>
. Since this isn't an underscore-prefixed property, I'd like to avoid this being a breaking change if possible. The published driver defers populating mongo
anyways. The promise versions can go onto new (internal?) variables if needed.
Same for mongoPoll
.
The underscore-prefixed properties are fine to change to promises, though we should consider renaming them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On that note, this.pendingConnect
wasn't underscored; does that make this breaking anyway if I remove that...?
index.js
Outdated
}; | ||
if (typeof mongo === 'function') { | ||
return new Promise(function(resolve, reject) { | ||
mongo(function(error, db) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The mongo
function can return either a MongoClient
or DB
, so let's name it to reflect that.
Note - It also could be possible for a user's mongo
function to return a DB
even if they're on a recent Mongo driver version. It'd be a breaking change if we required it to be a MongoClient
always.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you sure about that? The existing code looks like it assumes it's always a MongoClient
(ie it can call .db()
on the result)
index.js
Outdated
.then(function() { | ||
callback(null); | ||
}) | ||
.catch(callback); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I remembered why I prefer using the 2-arg form of then()
, from discussion last week.
It's specifically when doing interop back to callbacks. If the callback(null)
throws, then the catch
here will invoke the callback a second time.
Using the 2-arg form of then
does not result in that issue:
.then(function() {
callback(null);
}, callback)
We should also switch to 2-arg form of then
in the other places where relevant too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ew. But fine.
For the record, I've also had a quick test with chained promises in this style (eg promise.then(cb1, cb).then(cb2, cb)
), because I was worried that catching an early rejection in a chain would rescue and continue chain execution, but it doesn't work like that so it seems okay.
91fab6b
to
f260786
Compare
- Turn `mongo` and `mongoPoll` back into `Db` instances instead of `Promise<Db>` - Use 2-argument form of `.then()` to avoid callbacks being invoked twice if the callback itself throws
f260786
to
9116e04
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change LGTM, assuming we can get passing tests.
One new assertion from here seems to be failing
https://github.com/share/sharedb/pull/590/files#diff-ecbf5ca870ee70ce75c048bebe7b69654963ac89d372212970b196e323c9b8f1R190
mongodb@5
removes support for using callbacks, forcing consumers to use promises instead.This non-breaking change does the minimum amount of work to move us to using
mongodb
promises, while keeping all our interfaces intact.