Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change normalized callback arguments.
After this commit the callbacks for the write functions are as follows. ``` db.users.insert(document, callback); // => callback(err, [document]) db.users.save(document, callback); // => callback(err, document) db.users.update({ ... }, { ... }, callback); // => callback(err, lastErrorObject) db.users.remove({ ... }, callback); // => callback(err, {n: amountRemoved}) db.users.findAndModify({ query: { ... }, update: { ... }}, callback); // => callback(err, document, lastErrorObject) ``` The reason for these changes is that sometimes you might want to save a document and retrieve the ObjectId it was saved with. The problem of `save` returning '1' when the operation was an update is solved here, and a test case was added for this as well.
- Loading branch information
Showing
4 changed files
with
40 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
var assert = require('assert'); | ||
var mongojs = require('../index'); | ||
var db = mongojs('test', ['a','b']); | ||
|
||
db.a.insert([{name: "Squirtle"}, {name: "Charmander"}, {name: "Bulbasaur"}], function(err, docs) { | ||
assert.ok(docs[0]._id); | ||
assert.ok(docs[1]._id); | ||
assert.ok(docs[2]._id); | ||
db.close(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
var assert = require('assert'); | ||
var mongojs = require('../index'); | ||
var db = mongojs('test', ['a','b']); | ||
|
||
db.a.save({hello: "world"}, function(err, doc) { | ||
assert.equal(doc.hello, "world"); | ||
assert.ok(doc._id); | ||
|
||
doc.hello = "verden"; | ||
db.a.save(doc, function(err, doc) { | ||
assert.ok(doc._id); | ||
assert.equal(doc.hello, "verden") | ||
db.a.remove(function() { | ||
db.close(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,4 +14,4 @@ insert([{ | |
done(); | ||
}); | ||
sync = false; | ||
}); | ||
}); |
1c1dbd5
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.
Do we have an lastErrorObject for .save as well containing information whether or it did an upsert? If so we should added as the third argument to the callback
1c1dbd5
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 .save function doesn't have a
lastErrorObject
as far as I know.1c1dbd5
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.
Now that I tested it I think that it has a
lastErrorObject
when it does an update. I will add it to the arguments as well. In the case of a save I will fake it the same way it is done on.findAndModify
.1c1dbd5
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.
Couldn't insert be made intuitiv? When passing a single document return that document, instead of an array. Else if array is passed, return the array.
And also, why not fake lastErrorObject for insert?
1c1dbd5
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 see
save
can either performe an update or insert a new document,lastErrorObject
makes sense there.1c1dbd5
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.
@kapetan Yes to the insert thingy. I'll fix that.
save
has got alastErrorObject
in master now.