Skip to content

Commit 1a04099

Browse files
committed
Add option to use returnDocument
The `mongodb` Node.js driver deprecated use of `returnOriginal` in favour of `returnDocument` in [v3.6][1]. This non-breaking change allows consumers to opt in to using the newer `returnDocument` by setting an option on construction ```js var queue = mongoDbQueue(db, 'queue', { returnDocument : true }) ``` [1]: mongodb/node-mongodb-native#2808
1 parent 4a10a75 commit 1a04099

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,18 @@ msg = {
237237
Notice that the payload from the `deadQueue` is exactly the same as the original message
238238
when it was on the original queue (except with the number of tries set to 5).
239239

240+
### returnDocument ###
241+
242+
The `mongodb` Node.js driver [deprecated](https://github.com/mongodb/node-mongodb-native/pull/2808)
243+
use of `returnOriginal` in favor of `returnDocument` when using `findOneAndUpdate()`.
244+
245+
If you want to opt in to using the newer `returnDocument`, set the `returnDocument` option
246+
to `true`:
247+
248+
```
249+
var queue = mongoDbQueue(db, 'queue', { returnDocument : true })
250+
```
251+
240252
## Operations ##
241253

242254
### .add() ###

mongodb-queue.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ function Queue(db, name, opts) {
4444
this.col = db.collection(name)
4545
this.visibility = opts.visibility || 30
4646
this.delay = opts.delay || 0
47+
this.returnDocument = opts.returnDocument
4748

4849
if ( opts.deadQueue ) {
4950
this.deadQueue = opts.deadQueue
@@ -120,8 +121,11 @@ Queue.prototype.get = function(opts, callback) {
120121
visible : nowPlusSecs(visibility),
121122
}
122123
}
124+
var options = self._optionsWithNewDocument({
125+
sort: sort
126+
})
123127

124-
self.col.findOneAndUpdate(query, update, { sort: sort, returnOriginal : false }, function(err, result) {
128+
self.col.findOneAndUpdate(query, update, options, function(err, result) {
125129
if (err) return callback(err)
126130
var msg = result.value
127131
if (!msg) return callback()
@@ -175,7 +179,9 @@ Queue.prototype.ping = function(ack, opts, callback) {
175179
visible : nowPlusSecs(visibility)
176180
}
177181
}
178-
self.col.findOneAndUpdate(query, update, { returnOriginal : false }, function(err, msg, blah) {
182+
var options = self._optionsWithNewDocument({})
183+
184+
self.col.findOneAndUpdate(query, update, options, function(err, msg, blah) {
179185
if (err) return callback(err)
180186
if ( !msg.value ) {
181187
return callback(new Error("Queue.ping(): Unidentified ack : " + ack))
@@ -197,7 +203,8 @@ Queue.prototype.ack = function(ack, callback) {
197203
deleted : now(),
198204
}
199205
}
200-
self.col.findOneAndUpdate(query, update, { returnOriginal : false }, function(err, msg, blah) {
206+
var options = self._optionsWithNewDocument({})
207+
self.col.findOneAndUpdate(query, update, options, function(err, msg, blah) {
201208
if (err) return callback(err)
202209
if ( !msg.value ) {
203210
return callback(new Error("Queue.ack(): Unidentified ack : " + ack))
@@ -266,3 +273,12 @@ Queue.prototype.done = function(callback) {
266273
callback(null, count)
267274
})
268275
}
276+
277+
Queue.prototype._optionsWithNewDocument = function(query) {
278+
if (this.returnDocument) {
279+
query.returnDocument = 'after'
280+
} else {
281+
query.returnOriginal = false
282+
}
283+
return query
284+
}

0 commit comments

Comments
 (0)