Skip to content

Commit

Permalink
Make sure to not reload any relation
Browse files Browse the repository at this point in the history
  • Loading branch information
ddolcimascolo committed Nov 21, 2024
1 parent d7bb06e commit 01af385
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ var Temporal = function(model, sequelize, temporalOptions){
* Model.reload() will do its magic to merge the newly fetched values directly in dataValues. #gg
*/
if (attributesToReload.length > 0) {
await obj.reload({attributes: attributesToReload, transaction: options.transaction, paranoid: false, include: []})
await obj.reload({attributes: attributesToReload, transaction: options.transaction, paranoid: false, include: null})
}

return obj.dataValues;
Expand Down
34 changes: 31 additions & 3 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@ var assert = chai.assert;
var eventually = assert.eventually;

describe('Read-only API', function(){
var sequelize, User, UserHistory;
var sequelize, User, UserHistory, Group, queries = [];

function logging(message) {
if (process.env.LOGGING === 'true') {
console.log(message);
}

queries.push(message.substring(message.indexOf(':') + 2));
}

function freshDB(){
return freshDBWithOptions();
Expand All @@ -22,12 +30,20 @@ describe('Read-only API', function(){
sequelize = new Sequelize('', '', '', {
dialect: 'sqlite',
storage: __dirname + '/.test.sqlite',
logging: process.env.LOGGING === 'true' ? console.log : false
logging
});
User = Temporal(sequelize.define('User', {
name: Sequelize.TEXT
}, modelOptions), sequelize, temporalOptions);
UserHistory = sequelize.models.UserHistory;

Group = sequelize.define('Group', {
name: Sequelize.TEXT
});
Group.hasMany(User);
User.belongsTo(Group);
User.addScope('withGroup', { include: [Group] });

return sequelize.sync({ force: true });
}

Expand All @@ -41,6 +57,10 @@ describe('Read-only API', function(){
}
}

afterEach(function() {
queries.length = 0;
});

describe('hooks', function(){
beforeEach(freshDB);
it('onCreate: should not store the new version in history db' , function(){
Expand Down Expand Up @@ -328,7 +348,7 @@ describe('Read-only API', function(){

it('onUpdate: should store the previous version to the historyDB even if entity was partially loaded' , async function(){
const created = await User.create({ name: 'name' });
const user = await User.findByPk(created.id, { attributes: ['id', 'name'] }); // Don't fetch timestamps
const user = await User.scope('withGroup').findByPk(created.id, { attributes: ['id', 'name'] }); // Don't fetch timestamps

await user.update({ name: 'newName' });
await user.update({ name: 'thirdName' });
Expand All @@ -348,6 +368,14 @@ describe('Read-only API', function(){
assert.equal('name', initial.name);
assert.equal('newName', firstUpdate.name);
assert.equal('thirdName', secondUpdate.name);

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

assert.deepEqual(selects, [
"SELECT `User`.`id`, `User`.`name`, `Group`.`id` AS `Group.id`, `Group`.`name` AS `Group.name`, `Group`.`createdAt` AS `Group.createdAt`, `Group`.`updatedAt` AS `Group.updatedAt` FROM `Users` AS `User` LEFT OUTER JOIN `Groups` AS `Group` ON `User`.`GroupId` = `Group`.`id` WHERE (`User`.`deletedAt` IS NULL AND `User`.`id` = 1);",
"SELECT `createdAt`, `GroupId` FROM `Users` AS `User` WHERE `User`.`id` = 1;", // Reload for first update, no includes and only required fields. Second update does not need a reload
"SELECT `id`, `name`, `createdAt`, `updatedAt`, `deletedAt`, `hid`, `archivedAt` FROM `UserHistories` AS `UserHistory`;"
])
});

});
Expand Down

0 comments on commit 01af385

Please sign in to comment.