Skip to content

Commit

Permalink
update deprecated mongo driver commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Yaty authored and virkt25 committed Sep 12, 2018
1 parent 2615c4d commit b87e617
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
34 changes: 20 additions & 14 deletions lib/mongodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ MongoDB.prototype.create = function(model, data, options, callback) {

data = self.toDatabase(model, data);

this.execute(model, 'insert', data, {safe: true}, function(err, result) {
this.execute(model, 'insertOne', data, {safe: true}, function(err, result) {
if (self.debug) {
debug('create.callback', model, err, result);
}
Expand Down Expand Up @@ -550,7 +550,7 @@ MongoDB.prototype.save = function(model, data, options, callback) {

data = self.toDatabase(model, data);

this.execute(model, 'save', data, {}, function(err, result) {
this.execute(model, 'updateOne', {_id: oid}, {$set: data}, {upsert: true}, function(err, result) {
if (!err) {
self.setIdValue(model, data, idValue);
if (idName !== '_id') {
Expand Down Expand Up @@ -736,13 +736,16 @@ MongoDB.prototype.updateOrCreate = function updateOrCreate(

this.execute(
model,
'findAndModify',
'findOneAndUpdate',
{
_id: oid,
},
[['_id', 'asc']],
data,
{upsert: true, new: true},
{
upsert: true,
returnOriginal: false,
sort: [['_id', 'asc']],
},
function(err, result) {
if (self.debug) {
debug('updateOrCreate.callback', model, id, err, result);
Expand Down Expand Up @@ -804,7 +807,7 @@ MongoDB.prototype.destroy = function destroy(model, id, options, callback) {
debug('delete', model, id);
}
id = self.coerceId(model, id);
this.execute(model, 'remove', {_id: id}, function(err, result) {
this.execute(model, 'deleteOne', {_id: id}, function(err, result) {
if (self.debug) {
debug('delete.callback', model, id, err, result);
}
Expand Down Expand Up @@ -1319,7 +1322,7 @@ MongoDB.prototype.destroyAll = function destroyAll(
}
where = self.buildWhere(model, where, options);

this.execute(model, 'remove', where || {}, function(err, info) {
this.execute(model, 'deleteMany', where || {}, function(err, info) {
if (err) return callback && callback(err);

if (self.debug) debug('destroyAll.callback', model, where, err, info);
Expand Down Expand Up @@ -1394,7 +1397,7 @@ MongoDB.prototype.replaceWithOptions = function(model, id, data, options, cb) {
var self = this;
var idName = self.idName(model);
delete data[idName];
this.execute(model, 'update', {_id: id}, data, options, function(
this.execute(model, 'replaceOne', {_id: id}, data, options, function(
err,
info
) {
Expand Down Expand Up @@ -1464,11 +1467,14 @@ MongoDB.prototype.updateAttributes = function updateAttrs(

this.execute(
model,
'findAndModify',
{_id: oid},
[['_id', 'asc']],
'findOneAndUpdate',
{
_id: oid,
},
data,
{},
{
sort: [['_id', 'asc']],
},
function(err, result) {
if (self.debug) {
debug('updateAttributes.callback', model, id, err, result);
Expand Down Expand Up @@ -1526,10 +1532,10 @@ MongoDB.prototype.update = MongoDB.prototype.updateAll = function updateAll(

this.execute(
model,
'update',
'updateMany',
where,
data,
{multi: true, upsert: false},
{upsert: false},
function(err, info) {
if (err) return cb && cb(err);

Expand Down
10 changes: 5 additions & 5 deletions test/mongodb.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ describe('lazyConnect', function() {

ds.connector.execute(
'TestLazy',
'insert',
'insertOne',
{value: 'test value'},
function(err, success) {
if (err) {
Expand All @@ -100,11 +100,11 @@ describe('lazyConnect', function() {

ds.connector.execute(
'TestLazy',
'insert',
'insertOne',
{value: 'test value'},
function(err, success) {
if (err) done(err);
var id = success.insertedIds[0];
var id = success.insertedId;
ds.connector.should.have.property('db');
ds.connector.db.should.have.property('topology');
ds.connector.db.topology.should.have.property('isDestroyed');
Expand Down Expand Up @@ -805,8 +805,8 @@ describe('mongodb connector', function() {
) {
Post.find(function(err, results) {
events.should.eql([
'before execute insert',
'after execute insert',
'before execute insertOne',
'after execute insertOne',
'before execute find',
'after execute find',
]);
Expand Down

0 comments on commit b87e617

Please # to comment.