Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ whereas the options are listed here (with default value).
(i.e. this includes the latest state.)
This allows to only query the hostory table to get the full history of an entity.
*/
full: false
full: false,
/* If provided, this function will be used to determine if a given record should be logged into the history table
*/
shouldRecordHistory: function(obj){return true}
```

Details
Expand Down
7 changes: 6 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ var temporalDefaultOptions = {
// runs the insert within the sequelize hook chain, disable
// for increased performance
blocking: true,
full: false
full: false,
shouldRecordHistory: function(obj){return true}
};

var excludeAttributes = function(obj, attrsToExclude){
Expand Down Expand Up @@ -65,6 +66,8 @@ var Temporal = function(model, sequelize, temporalOptions){

// we already get the updatedAt timestamp from our models
var insertHook = function(obj, options){
// if shouldRecordHistory returns false then skip record creration
if (!temporalOptions.shouldRecordHistory(obj)){return;}
var dataValues = (!temporalOptions.full && obj._previousDataValues) || obj.dataValues;
var historyRecord = modelHistory.create(dataValues, {transaction: options.transaction});
if(temporalOptions.blocking){
Expand All @@ -76,6 +79,8 @@ var Temporal = function(model, sequelize, temporalOptions){
var queryAll = model.findAll({where: options.where, transaction: options.transaction}).then(function(hits){
if(hits){
hits = _.map(hits, 'dataValues');
// if shouldRecordHistory returns false then skip record creration
hits = _.filter(hits, function(o){return temporalOptions.shouldRecordHistory(o)});
return modelHistory.bulkCreate(hits, {transaction: options.transaction});
}
});
Expand Down
Loading