Skip to content
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

Add option to use returnDocument #41

Closed
wants to merge 1 commit into from
Closed
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
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._returnNewDocument({
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._returnNewDocument({})

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._returnNewDocument({})
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._returnNewDocument = function(options) {
if (this.returnDocument) {
options.returnDocument = 'after'
} else {
options.returnOriginal = false
}
return options
}