Merge pull request #308 from strongloop/fix/instance-status-after-save

Set isNewInstance to undefined for after save hook on replaceOrCreate and updateOrCreate
This commit is contained in:
Biniam Admikew 2017-07-26 22:27:01 -04:00 committed by GitHub
commit 28d63b8dca
3 changed files with 61 additions and 24 deletions

View File

@ -280,13 +280,13 @@ MySQL.prototype._modifyOrCreate = function(model, data, options, fields, cb) {
data.id = info.insertId; data.id = info.insertId;
} }
var meta = {}; var meta = {};
if (info) { // When using the INSERT ... ON DUPLICATE KEY UPDATE statement,
// When using the INSERT ... ON DUPLICATE KEY UPDATE statement, // the returned value is as follows:
// the returned value is as follows: // 1 for each successful INSERT.
// 1 for each successful INSERT. // 2 for each successful UPDATE.
// 2 for each successful UPDATE. // 1 also for UPDATE with same values, so we cannot accurately
meta.isNewInstance = (info.affectedRows === 1); // report if we have a new instance.
} meta.isNewInstance = undefined;
cb(err, data, meta); cb(err, data, meta);
}); });
}; };

View File

@ -4,10 +4,11 @@
// License text available at https://opensource.org/licenses/MIT // License text available at https://opensource.org/licenses/MIT
'use strict'; 'use strict';
var async = require('async');
var should = require('./init.js'); var should = require('./init.js');
var sinon = require('sinon'); var sinon = require('sinon');
var Post, PostWithStringId, PostWithUniqueTitle, PostWithNumId, db; var Post, PostWithStringId, PostWithUniqueTitle, PostWithNumId, Student, db;
// Mock up mongodb ObjectID // Mock up mongodb ObjectID
function ObjectID(id) { function ObjectID(id) {
@ -57,18 +58,24 @@ describe('mysql', function() {
content: {type: String}, content: {type: String},
}); });
db.automigrate(['PostWithDefaultId', 'PostWithStringId', 'PostWithUniqueTitle', 'PostWithNumId'], function(err) { Student = db.define('Student', {
should.not.exist(err); name: {type: String, length: 255},
done(err); age: {type: Number},
}, {
forceId: false,
}); });
db.automigrate(
['PostWithDefaultId', 'PostWithStringId',
'PostWithUniqueTitle', 'PostWithNumId', 'Student'],
function(err) {
should.not.exist(err);
done(err);
});
}); });
beforeEach(function(done) { beforeEach(function() {
Post.destroyAll(function() { return deleteAllModelInstances();
PostWithStringId.destroyAll(function() {
PostWithUniqueTitle.destroyAll(done);
});
});
}); });
it('should allow array or object', function(done) { it('should allow array or object', function(done) {
@ -236,6 +243,33 @@ describe('mysql', function() {
}); });
}); });
}); });
it('isNewInstance should be undefined for after save hook', function(done) {
var student = {name: 'Joe', age: 20};
var newStudent = {};
var isNewInstanceBefore = false;
var isNewInstanceAfter = false;
Student.create(student, function(err, createdStudent) {
if (err) return done(err);
newStudent.id = createdStudent.id;
newStudent.name = 'Hannah';
newStudent.age = 25;
Student.observe('before save', function(ctx, next) {
isNewInstanceBefore = ctx.isNewInstance;
next();
});
Student.observe('after save', function(ctx, next) {
isNewInstanceAfter = ctx.isNewInstance;
next();
});
Student.replaceOrCreate(newStudent, function(err, s) {
if (err) return done(err);
should.not.exist(isNewInstanceBefore);
should.not.exist(isNewInstanceAfter);
done();
});
});
});
}); });
it('save should update the instance with the same id', function(done) { it('save should update the instance with the same id', function(done) {
@ -876,11 +910,14 @@ describe('mysql', function() {
}); });
}); });
after(function(done) { function deleteAllModelInstances() {
Post.destroyAll(function() { const models = [
PostWithStringId.destroyAll(function() { Post, PostWithStringId, PostWithUniqueTitle, PostWithNumId, Student,
PostWithUniqueTitle.destroyAll(done); ];
}); return Promise.all(models.map(m => m.destroyAll()));
}); }
after(function() {
return deleteAllModelInstances();
}); });
}); });

View File

@ -8,5 +8,5 @@ var should = require('./init');
var suite = require('loopback-datasource-juggler/test/persistence-hooks.suite.js'); var suite = require('loopback-datasource-juggler/test/persistence-hooks.suite.js');
suite(global.getDataSource(), should, { suite(global.getDataSource(), should, {
replaceOrCreateReportsNewInstance: true, replaceOrCreateReportsNewInstance: false,
}); });