sequelize-temporal
Advanced tools
Comparing version 1.0.2 to 1.0.3
62
index.js
var _ = require('lodash'); | ||
var Temporal = function(model, sequelize, temporalOptions){ | ||
var temporalDefaultOptions = { | ||
// runs the insert within the sequelize hook chain, disable | ||
// for increased performance | ||
blocking: true | ||
}; | ||
var temporalDefaultOptions = { | ||
// runs the insert within the sequelize hook chain, disable | ||
// for increased performance | ||
blocking: true | ||
}; | ||
var excludeAttributes = function(obj, attrsToExclude){ | ||
// fancy way to exclude attributes | ||
return _.omit(obj, _.partial(_.rearg(_.contains,0,2,1), attrsToExclude)); | ||
} | ||
var Temporal = function(model, sequelize, temporalOptions){ | ||
temporalOptions = _.extend({},temporalDefaultOptions, temporalOptions); | ||
@@ -18,15 +23,2 @@ | ||
var historyOwnOptions = { | ||
timestamps: false | ||
}; | ||
var historyOptions = _.assign({}, model.options, historyOwnOptions); | ||
delete historyOptions.name; | ||
delete historyOptions.sequelize; | ||
delete historyOptions.uniqueKeys; | ||
delete historyOptions.hasPrimaryKey; | ||
delete historyOptions.hooks; | ||
delete historyOptions.scopes; | ||
delete historyOptions.instanceMethods; | ||
delete historyOptions.defaultScope; | ||
var historyOwnAttrs = { | ||
@@ -46,16 +38,11 @@ hid: { | ||
var historyAttributes = _(model.rawAttributes).omit(function(v){ | ||
//if(_.has(v,'_autoGenerated')){ | ||
}).mapValues(function(v){ | ||
var v = _.extend({}, v); | ||
delete v.Model; | ||
// remove all uniqueness, primary and | ||
delete v.unique; | ||
delete v.primaryKey; | ||
delete v.autoIncrement; | ||
if(v.fieldName == "createdAt" || | ||
v.fieldName == "updatedAt"){ | ||
v.type = Sequelize.DATE; | ||
} | ||
return v | ||
var excludedAttributes = ["Model","unique","primaryKey","autoIncrement", "set", "get", "_modelAttribute"]; | ||
var historyAttributes = _(model.rawAttributes).mapValues(function(v){ | ||
v = excludeAttributes(v, excludedAttributes); | ||
// remove the "NOW" defaultValue for the default timestamps | ||
// we want to save them, but just a copy from our master record | ||
if(v.fieldName == "createdAt" || v.fieldName == "updatedAt"){ | ||
v.type = Sequelize.DATE; | ||
} | ||
return v; | ||
}).assign(historyOwnAttrs).value(); | ||
@@ -65,2 +52,9 @@ // If the order matters, use this: | ||
var historyOwnOptions = { | ||
timestamps: false | ||
}; | ||
var excludedNames = ["name", "sequelize", "uniqueKeys", "hasPrimaryKey", "hooks", "scopes", "instanceMethods", "defaultScope"]; | ||
var modelOptions = excludeAttributes(model.options, excludedNames); | ||
var historyOptions = _.assign({}, modelOptions, historyOwnOptions); | ||
var modelHistory = sequelize.define(historyName, historyAttributes, historyOptions); | ||
@@ -98,4 +92,2 @@ | ||
// TODO: handle bulk operations | ||
var readOnlyHook = function(){ | ||
@@ -102,0 +94,0 @@ throw new Error("This is a read-only history database. You aren't allowed to modify it."); |
{ | ||
"name": "sequelize-temporal", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "Temporal tables for Sequelize", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -56,3 +56,3 @@ var Temporal = require('../'); | ||
.then(function(){ | ||
return UserHistory.findAll(); | ||
return UserHistory.findAll(); | ||
}).then(function(users){ | ||
@@ -62,3 +62,3 @@ assert.equal(users.length,1, "only one entry in DB"); | ||
}).then(function(user){ | ||
return User.findOne(); | ||
return User.findOne(); | ||
}).then(function(user){ | ||
@@ -75,3 +75,3 @@ return user.destroy(); | ||
.then(function(){ | ||
return UserHistory.findAll(); | ||
return UserHistory.findAll(); | ||
}).then(function(users){ | ||
@@ -182,2 +182,57 @@ assert.equal(users.length,1, "only one entry in DB"); | ||
}); | ||
describe('interference with the original model', function(){ | ||
beforeEach(freshDB); | ||
it('shouldn\'t delete instance methods' , function(){ | ||
Fruit = Temporal(sequelize.define('Fruit', { | ||
name: Sequelize.TEXT | ||
}, { | ||
instanceMethods:{ | ||
sayHi: function(){ return 2;} | ||
} | ||
}), sequelize); | ||
return sequelize.sync().then(function(){ | ||
return Fruit.create(); | ||
}).then(function(f){ | ||
assert.isFunction(f.sayHi); | ||
assert.equal(f.sayHi(), 2); | ||
}); | ||
}); | ||
it('shouldn\'t interfere with hooks of the model' , function(){ | ||
var triggered = 0; | ||
Fruit = Temporal(sequelize.define('Fruit', { | ||
name: Sequelize.TEXT | ||
}, { | ||
hooks:{ | ||
beforeCreate: function(){ triggered++;} | ||
} | ||
}), sequelize); | ||
return sequelize.sync().then(function(){ | ||
return Fruit.create(); | ||
}).then(function(f){ | ||
assert.equal(triggered, 1,"hook trigger count"); | ||
}); | ||
}); | ||
it('shouldn\'t interfere with setters' , function(){ | ||
var triggered = 0; | ||
Fruit = Temporal(sequelize.define('Fruit', { | ||
name: { | ||
type: Sequelize.TEXT, | ||
set: function(){ | ||
triggered++; | ||
} | ||
} | ||
}), sequelize); | ||
return sequelize.sync().then(function(){ | ||
return Fruit.create({name: "apple"}); | ||
}).then(function(f){ | ||
assert.equal(triggered, 1,"hook trigger count"); | ||
}); | ||
}); | ||
}); | ||
}) |
16646
300