Skip to content

Commit

Permalink
Make sure to not reload virtual fields
Browse files Browse the repository at this point in the history
  • Loading branch information
ddolcimascolo committed Nov 21, 2024
1 parent 01af385 commit d42bcd4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ var Temporal = function(model, sequelize, temporalOptions){
return;
}

function isVirtual(attribute) {
return attributes[attribute].type instanceof Sequelize.VIRTUAL;
}

async function getDataValues() {
if (!temporalOptions.full) {
return obj._previousDataValues || obj.dataValues;
Expand All @@ -85,7 +89,7 @@ var Temporal = function(model, sequelize, temporalOptions){
}

const attributesToReload = Object.keys(attributes).filter(attribute => {
if (!temporalOptions.reloadIgnoredAttributes.includes(attribute) && !(attribute in obj.dataValues)) {
if (!temporalOptions.reloadIgnoredAttributes.includes(attribute) && !isVirtual(attribute) && !(attribute in obj.dataValues)) {
return true;
}
});
Expand Down
35 changes: 34 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ describe('Read-only API', function(){
logging
});
User = Temporal(sequelize.define('User', {
name: Sequelize.TEXT
name: Sequelize.TEXT,
virtual: Sequelize.VIRTUAL(Sequelize.STRING, ['name']),
}, modelOptions), sequelize, temporalOptions);
UserHistory = sequelize.models.UserHistory;

Expand Down Expand Up @@ -378,6 +379,38 @@ describe('Read-only API', function(){
])
});

it('onUpdate: should not reload virtual fields' , async function(){
const created = await User.create({ name: 'name' });
const user = await User.findByPk(created.id, { attributes: ['id', 'name', 'createdAt', 'GroupId'] }); // Don't fetch timestamps

await user.update({ name: 'newName' });
await user.update({ name: 'thirdName' });

const history = await UserHistory.findAll();

assert.equal(history.length, 3, 'initial revision and to updates saved');

const [initial, firstUpdate, secondUpdate] = history;

assert.equal(+initial.createdAt, +firstUpdate.createdAt, 'createdAt was saved during first update, despite not being eagerly loaded');
assert.equal(+initial.createdAt, +secondUpdate.createdAt, 'createdAt was saved during second update, despite not being eagerly loaded');

assert.isAtLeast(firstUpdate.updatedAt, initial.createdAt, 'updatedAt was saved during first update');
assert.isAtLeast(secondUpdate.updatedAt, firstUpdate.updatedAt, 'updatedAt was saved during second update');

assert.equal('name', initial.name);
assert.equal('newName', firstUpdate.name);
assert.equal('thirdName', secondUpdate.name);

const selects = queries.filter(query => query.startsWith('SELECT'));

// No reload
assert.deepEqual(selects, [
"SELECT `id`, `name`, `createdAt`, `GroupId` FROM `Users` AS `User` WHERE (`User`.`deletedAt` IS NULL AND `User`.`id` = 1);",
"SELECT `id`, `name`, `createdAt`, `updatedAt`, `deletedAt`, `hid`, `archivedAt` FROM `UserHistories` AS `UserHistory`;"
])
});

});

describe('silent mode', function(){
Expand Down

0 comments on commit d42bcd4

Please sign in to comment.