Skip to content

Commit

Permalink
Add option to use returnDocument
Browse files Browse the repository at this point in the history
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
  • Loading branch information
alecgibson committed Jun 29, 2021
1 parent 4a10a75 commit 1a04099
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,18 @@ msg = {
Notice that the payload from the `deadQueue` is exactly the same as the original message
when it was on the original queue (except with the number of tries set to 5).

### returnDocument ###

The `mongodb` Node.js driver [deprecated](https://github.com/mongodb/node-mongodb-native/pull/2808)
use of `returnOriginal` in favor of `returnDocument` when using `findOneAndUpdate()`.

If you want to opt in to using the newer `returnDocument`, set the `returnDocument` option
to `true`:

```
var queue = mongoDbQueue(db, 'queue', { returnDocument : true })
```

## Operations ##

### .add() ###
Expand Down
22 changes: 19 additions & 3 deletions mongodb-queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ function Queue(db, name, opts) {
this.col = db.collection(name)
this.visibility = opts.visibility || 30
this.delay = opts.delay || 0
this.returnDocument = opts.returnDocument

if ( opts.deadQueue ) {
this.deadQueue = opts.deadQueue
Expand Down Expand Up @@ -120,8 +121,11 @@ Queue.prototype.get = function(opts, callback) {
visible : nowPlusSecs(visibility),
}
}
var options = self._optionsWithNewDocument({
sort: sort
})

self.col.findOneAndUpdate(query, update, { sort: sort, returnOriginal : false }, function(err, result) {
self.col.findOneAndUpdate(query, update, options, function(err, result) {
if (err) return callback(err)
var msg = result.value
if (!msg) return callback()
Expand Down Expand Up @@ -175,7 +179,9 @@ Queue.prototype.ping = function(ack, opts, callback) {
visible : nowPlusSecs(visibility)
}
}
self.col.findOneAndUpdate(query, update, { returnOriginal : false }, function(err, msg, blah) {
var options = self._optionsWithNewDocument({})

self.col.findOneAndUpdate(query, update, options, function(err, msg, blah) {
if (err) return callback(err)
if ( !msg.value ) {
return callback(new Error("Queue.ping(): Unidentified ack : " + ack))
Expand All @@ -197,7 +203,8 @@ Queue.prototype.ack = function(ack, callback) {
deleted : now(),
}
}
self.col.findOneAndUpdate(query, update, { returnOriginal : false }, function(err, msg, blah) {
var options = self._optionsWithNewDocument({})
self.col.findOneAndUpdate(query, update, options, function(err, msg, blah) {
if (err) return callback(err)
if ( !msg.value ) {
return callback(new Error("Queue.ack(): Unidentified ack : " + ack))
Expand Down Expand Up @@ -266,3 +273,12 @@ Queue.prototype.done = function(callback) {
callback(null, count)
})
}

Queue.prototype._optionsWithNewDocument = function(query) {
if (this.returnDocument) {
query.returnDocument = 'after'
} else {
query.returnOriginal = false
}
return query
}

0 comments on commit 1a04099

Please sign in to comment.