Skip to content
Merged
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
31 changes: 28 additions & 3 deletions lib/mongodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,24 @@ MongoDB.prototype.save = function (model, data, callback) {
if (self.debug) {
debug('save.callback', model, err, result);
}
callback && callback(err, result && result.ops);

var info = {};
if (result && result.result) {
// create result formats:
// { ok: 1, n: 1, upserted: [ [Object] ] }
// { ok: 1, nModified: 0, n: 1, upserted: [ [Object] ] }
//
// update result formats:
// { ok: 1, n: 1 }
// { ok: 1, nModified: 1, n: 1 }
if (result.result.ok === 1 && result.result.n === 1) {
info.isNewInstance = !!result.result.upserted;
} else {
debug('save result format not recognized: %j', result.result);
}
}

callback && callback(err, result && result.ops, info);
});
};

Expand Down Expand Up @@ -388,7 +405,15 @@ MongoDB.prototype.updateOrCreate = function updateOrCreate(model, data, callback
self.setIdValue(model, object, id);
object && idName !== '_id' && delete object._id;
}
callback && callback(err, object);

var info;
if (result && result.lastErrorObject) {
info = { isNewInstance: !result.lastErrorObject.updatedExisting };
} else {
debug('updateOrCreate result format not recognized: %j', result);
}

callback && callback(err, object, info);
});
};

Expand Down Expand Up @@ -864,7 +889,7 @@ MongoDB.prototype.automigrate = function (models, cb) {
}
self.db.dropCollection(model, function (err, collection) {
if(err) {
if(!(err.name === 'MongoError' && err.ok === 0
if(!(err.name === 'MongoError' && err.ok === 0
&& err.errmsg === 'ns not found')) {
// For errors other than 'ns not found' (collection doesn't exist)
return modelCallback(err);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"bluebird": "^2.9.12",
"loopback-datasource-juggler": "^2.15.0",
"mocha": "^2.1.0",
"rc": "^0.6.0",
"rc": "^1.0.0",
"semver": "^4.2.0",
"should": "^5.0.0"
},
Expand Down