Skip to content

Commit 377e1ff

Browse files
committed
Make sure to not reload virtual fields
1 parent 054871e commit 377e1ff

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ var Temporal = function(model, sequelize, temporalOptions){
7272
return;
7373
}
7474

75+
function isVirtual(attribute) {
76+
return attributes[attribute].type instanceof Sequelize.VIRTUAL;
77+
}
78+
7579
async function getDataValues() {
7680
if (!temporalOptions.full) {
7781
return obj._previousDataValues || obj.dataValues;
@@ -85,7 +89,7 @@ var Temporal = function(model, sequelize, temporalOptions){
8589
}
8690

8791
const attributesToReload = Object.keys(attributes).filter(attribute => {
88-
if (!temporalOptions.reloadIgnoredAttributes.includes(attribute) && !(attribute in obj.dataValues)) {
92+
if (!temporalOptions.reloadIgnoredAttributes.includes(attribute) && !isVirtual(attribute) && !(attribute in obj.dataValues)) {
8993
return true;
9094
}
9195
});

test/test.js

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ describe('Read-only API', function(){
3333
logging
3434
});
3535
User = Temporal(sequelize.define('User', {
36-
name: Sequelize.TEXT
36+
name: Sequelize.TEXT,
37+
virtual: Sequelize.VIRTUAL(Sequelize.STRING, ['name']),
3738
}, modelOptions), sequelize, temporalOptions);
3839
UserHistory = sequelize.models.UserHistory;
3940

@@ -378,6 +379,38 @@ describe('Read-only API', function(){
378379
])
379380
});
380381

382+
it('onUpdate: should not reload virtual fields' , async function(){
383+
const created = await User.create({ name: 'name' });
384+
const user = await User.findByPk(created.id, { attributes: ['id', 'name', 'createdAt', 'GroupId'] }); // Don't fetch timestamps
385+
386+
await user.update({ name: 'newName' });
387+
await user.update({ name: 'thirdName' });
388+
389+
const history = await UserHistory.findAll();
390+
391+
assert.equal(history.length, 3, 'initial revision and to updates saved');
392+
393+
const [initial, firstUpdate, secondUpdate] = history;
394+
395+
assert.equal(+initial.createdAt, +firstUpdate.createdAt, 'createdAt was saved during first update, despite not being eagerly loaded');
396+
assert.equal(+initial.createdAt, +secondUpdate.createdAt, 'createdAt was saved during second update, despite not being eagerly loaded');
397+
398+
assert.isAtLeast(firstUpdate.updatedAt, initial.createdAt, 'updatedAt was saved during first update');
399+
assert.isAtLeast(secondUpdate.updatedAt, firstUpdate.updatedAt, 'updatedAt was saved during second update');
400+
401+
assert.equal('name', initial.name);
402+
assert.equal('newName', firstUpdate.name);
403+
assert.equal('thirdName', secondUpdate.name);
404+
405+
const selects = queries.filter(query => query.startsWith('SELECT'));
406+
407+
// No reload
408+
assert.deepEqual(selects, [
409+
"SELECT `id`, `name`, `createdAt`, `GroupId` FROM `Users` AS `User` WHERE (`User`.`deletedAt` IS NULL AND `User`.`id` = 1);",
410+
"SELECT `id`, `name`, `createdAt`, `updatedAt`, `deletedAt`, `hid`, `archivedAt` FROM `UserHistories` AS `UserHistory`;"
411+
])
412+
});
413+
381414
});
382415

383416
describe('silent mode', function(){

0 commit comments

Comments
 (0)