sequelize-temporal
Advanced tools
Comparing version 2.1.0 to 2.1.1
@@ -75,2 +75,6 @@ var _ = require('lodash'); | ||
function isVirtual(attribute) { | ||
return attributes[attribute].type instanceof Sequelize.VIRTUAL; | ||
} | ||
async function getDataValues() { | ||
@@ -89,3 +93,3 @@ if (!temporalOptions.full) { | ||
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; | ||
@@ -102,3 +106,3 @@ } | ||
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}) | ||
} | ||
@@ -105,0 +109,0 @@ |
{ | ||
"name": "sequelize-temporal", | ||
"version": "2.1.0", | ||
"version": "2.1.1", | ||
"description": "Temporal tables for Sequelize", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -10,4 +10,12 @@ var Temporal = require('../'); | ||
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(){ | ||
@@ -26,8 +34,17 @@ return freshDBWithOptions(); | ||
storage: __dirname + '/.test.sqlite', | ||
logging: process.env.LOGGING === 'true' ? console.log : false | ||
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; | ||
Group = sequelize.define('Group', { | ||
name: Sequelize.TEXT | ||
}); | ||
Group.hasMany(User); | ||
User.belongsTo(Group); | ||
User.addScope('withGroup', { include: [Group] }); | ||
return sequelize.sync({ force: true }); | ||
@@ -46,2 +63,6 @@ } | ||
afterEach(function() { | ||
queries.length = 0; | ||
}); | ||
describe('hooks', function(){ | ||
@@ -334,3 +355,3 @@ beforeEach(freshDB); | ||
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 | ||
@@ -355,4 +376,44 @@ await user.update({ name: 'newName' }); | ||
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`;" | ||
]) | ||
}); | ||
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`;" | ||
]) | ||
}); | ||
}); | ||
@@ -359,0 +420,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
31453
542