Skip to content

Commit

Permalink
Merge branch 'master' of github.com:nielsgl/sequelize-paper-trail int…
Browse files Browse the repository at this point in the history
…o develop
  • Loading branch information
nielsgl committed May 2, 2016
2 parents c5b7d71 + 742edf2 commit e9e114d
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 38 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ var sequelize = new Sequelize('database', 'username', 'password');
var PaperTrail = require('sequelize-paper-trail')(sequelize, options || {});
PaperTrail.defineModels();

var User = sequelize.define('user', {
var User = sequelize.define('User', {
username: Sequelize.STRING,
birthday: Sequelize.DATE
});
Expand All @@ -97,8 +97,8 @@ var options = {
'deleted_at'
],
revisionAttribute: 'revision',
revisionModel: 'Revisions',
revisionChangeModel: 'RevisionChanges',
revisionModel: 'Revision',
revisionChangeModel: 'RevisionChange',
UUID: false,
underscored: false,
underscoredAttributes: false,
Expand All @@ -119,8 +119,8 @@ var options = {
| [debug] | Boolean | false | Enables logging to the console. |
| [exclude] | Array | ['id', 'createdAt', 'updatedAt', 'deletedAt', 'created_at', 'updated_at', 'deleted_at'] | Array of global attributes to exclude from the paper trail. |
| [revisionAttribute] | String | 'revision' | Name of the attribute in the table that corresponds to the current revision. |
| [revisionModel] | String | 'Revisions' | Name of the model that keeps the revision models. |
| [revisionChangeModel] | String | 'RevisionChanges' | Name of the model that tracks all the attributes that have changed during each create and update call. |
| [revisionModel] | String | 'Revision' | Name of the model that keeps the revision models. |
| [revisionChangeModel] | String | 'RevisionChange' | Name of the model that tracks all the attributes that have changed during each create and update call. |
| [underscored] | Boolean | false | The [revisionModel] and [revisionChangeModel] have 'createdAt' and 'updatedAt' attributes, by default, setting this option to true changes it to 'created_at' and 'updated_at'. |
| [underscoredAttributes] | Boolean | false | The [revisionModel] has a [defaultAttribute] 'documentId', and the [revisionChangeModel] has a [defaultAttribute] 'revisionId, by default, setting this option to true changes it to 'document_id' and 'revision_id'. |
| [defaultAttributes] | Object | { documentId: 'documentId', revisionId: 'revisionId' } | |
Expand Down
46 changes: 30 additions & 16 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ exports.default = function (sequelize, options) {

// model name for revision table
if (!options.revisionModel) {
options.revisionModel = "Revisions";
options.revisionModel = "Revision";
}

// model name for revision changes tables
if (!options.revisionChangeModel) {
options.revisionChangeModel = "RevisionChanges";
options.revisionChangeModel = "RevisionChange";
}

// support UUID for postgresql
Expand Down Expand Up @@ -139,7 +139,7 @@ exports.default = function (sequelize, options) {
type: Sequelize.INTEGER,
defaultValue: 0
};
// this.revisionable = true;
this.revisionable = true;
this.refreshAttributes();

if (options.enableMigration) {
Expand Down Expand Up @@ -168,6 +168,16 @@ exports.default = function (sequelize, options) {
this.addHook("beforeUpdate", beforeHook);
this.addHook("afterCreate", afterHook);
this.addHook("afterUpdate", afterHook);

// create association
this.hasMany(sequelize.models.Revision, {
foreignKey: "document_id",
constraints: false,
scope: {
model: this.name
}
});

return this;
}
});
Expand Down Expand Up @@ -224,8 +234,8 @@ exports.default = function (sequelize, options) {
}

if (instance.context && instance.context.delta && instance.context.delta.length > 0) {
var Revisions = sequelize.model(options.revisionModel);
var RevisionChanges = sequelize.model(options.revisionChangeModel);
var Revision = sequelize.model(options.revisionModel);
var RevisionChange = sequelize.model(options.revisionChangeModel);
var delta = instance.context.delta;

if (options.enableCompression) {
Expand All @@ -250,7 +260,7 @@ exports.default = function (sequelize, options) {
}

// Build revision
var revision = Revisions.build({
var revision = Revision.build({
model: opt.model.name,
document_id: instance.get("id"),
revision: instance.get(options.revisionAttribute),
Expand All @@ -267,7 +277,7 @@ exports.default = function (sequelize, options) {
var o = helpers.diffToString(difference.item ? difference.item.lhs : difference.lhs);
var n = helpers.diffToString(difference.item ? difference.item.rhs : difference.rhs);

var d = RevisionChanges.build({
var d = RevisionChange.build({
path: difference.path[0],
document: difference,
//revisionId: data.id,
Expand All @@ -276,7 +286,7 @@ exports.default = function (sequelize, options) {

d.save().then(function (d) {
// Add diff to revision
revision.addChange(d);
revision.addRevisionChange(d);
return null;
}).catch(function (err) {
log('RevisionChange save error');
Expand All @@ -300,7 +310,7 @@ exports.default = function (sequelize, options) {

return {
// Return defineModels()
defineModels: function defineModels() {
defineModels: function defineModels(db) {
var attributes = {
model: {
type: Sequelize.TEXT,
Expand Down Expand Up @@ -332,7 +342,7 @@ exports.default = function (sequelize, options) {
attributes.documentId.type = Sequelize.UUID;
}
// Revision model
var Revisions = sequelize.define(options.revisionModel, attributes, {
var Revision = sequelize.define(options.revisionModel, attributes, {
underscored: options.underscored
});

Expand All @@ -359,25 +369,29 @@ exports.default = function (sequelize, options) {
}

// RevisionChange model
var RevisionChanges = sequelize.define(options.revisionChangeModel, attributes, {
var RevisionChange = sequelize.define(options.revisionChangeModel, attributes, {
underscored: options.underscored
});
// Set associations
Revisions.hasMany(RevisionChanges, {
Revision.hasMany(RevisionChange, {
foreignKey: options.defaultAttributes.revisionId,
constraints: true,
as: "changes"
constraints: false
});

RevisionChange.belongsTo(Revision);

db[Revision.name] = Revision;
db[RevisionChange.name] = RevisionChange;

// TODO: Option to track the user that triggered the revision
if (false && options.userModel) {
Revisions.belongsTo(sequelize.model(options.userModel), {
Revision.belongsTo(sequelize.model(options.userModel), {
foreignKey: "user_id",
constraints: true,
as: "user"
});
}
return Revisions;
return Revision;
}
};
};
Expand Down
48 changes: 31 additions & 17 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ export default (sequelize: sequelize, options: object): object => {

// model name for revision table
if(!options.revisionModel){
options.revisionModel = "Revisions";
options.revisionModel = "Revision";
}

// model name for revision changes tables
if(!options.revisionChangeModel){
options.revisionChangeModel = "RevisionChanges";
options.revisionChangeModel = "RevisionChange";
}

// support UUID for postgresql
Expand Down Expand Up @@ -140,7 +140,7 @@ export default (sequelize: sequelize, options: object): object => {
type: Sequelize.INTEGER,
defaultValue: 0
}
// this.revisionable = true;
this.revisionable = true;
this.refreshAttributes();

if(options.enableMigration) {
Expand Down Expand Up @@ -172,6 +172,16 @@ export default (sequelize: sequelize, options: object): object => {
this.addHook("beforeUpdate", beforeHook);
this.addHook("afterCreate", afterHook);
this.addHook("afterUpdate", afterHook);

// create association
this.hasMany(sequelize.models.Revision, {
foreignKey: "document_id",
constraints: false,
scope: {
model: this.name
}
});

return this;
}
});
Expand Down Expand Up @@ -227,8 +237,8 @@ export default (sequelize: sequelize, options: object): object => {
}

if(instance.context && instance.context.delta && instance.context.delta.length > 0) {
var Revisions = sequelize.model(options.revisionModel);
var RevisionChanges = sequelize.model(options.revisionChangeModel);
var Revision = sequelize.model(options.revisionModel);
var RevisionChange = sequelize.model(options.revisionChangeModel);
var delta = instance.context.delta;

if(options.enableCompression) {
Expand All @@ -253,7 +263,7 @@ export default (sequelize: sequelize, options: object): object => {
}

// Build revision
var revision = Revisions.build({
var revision = Revision.build({
model: opt.model.name,
document_id: instance.get("id"),
revision: instance.get(options.revisionAttribute),
Expand All @@ -271,7 +281,7 @@ export default (sequelize: sequelize, options: object): object => {
var o = helpers.diffToString(difference.item ? difference.item.lhs : difference.lhs);
var n = helpers.diffToString(difference.item ? difference.item.rhs : difference.rhs);

var d = RevisionChanges.build({
var d = RevisionChange.build({
path: difference.path[0],
document: difference,
//revisionId: data.id,
Expand All @@ -281,7 +291,7 @@ export default (sequelize: sequelize, options: object): object => {
d.save()
.then(function(d: any){
// Add diff to revision
revision.addChange(d);
revision.addRevisionChange(d);
return null;
})
.catch((err: any) => {
Expand All @@ -305,7 +315,7 @@ export default (sequelize: sequelize, options: object): object => {

return {
// Return defineModels()
defineModels: function(){
defineModels: function(db: object) {
var attributes = {
model: {
type: Sequelize.TEXT,
Expand Down Expand Up @@ -337,10 +347,10 @@ export default (sequelize: sequelize, options: object): object => {
attributes.documentId.type = Sequelize.UUID;
}
// Revision model
var Revisions = sequelize.define(options.revisionModel, attributes, {
var Revision = sequelize.define(options.revisionModel, attributes, {
underscored: options.underscored
});

attributes = {
path: {
type: Sequelize.TEXT,
Expand All @@ -364,25 +374,29 @@ export default (sequelize: sequelize, options: object): object => {
}

// RevisionChange model
var RevisionChanges = sequelize.define(options.revisionChangeModel, attributes, {
var RevisionChange = sequelize.define(options.revisionChangeModel, attributes, {
underscored: options.underscored
});
// Set associations
Revisions.hasMany(RevisionChanges, {
Revision.hasMany(RevisionChange, {
foreignKey: options.defaultAttributes.revisionId,
constraints: true,
as: "changes"
constraints: false
});

RevisionChange.belongsTo(Revision);

db[Revision.name] = Revision;
db[RevisionChange.name] = RevisionChange;

// TODO: Option to track the user that triggered the revision
if (false && options.userModel) {
Revisions.belongsTo(sequelize.model(options.userModel), {
Revision.belongsTo(sequelize.model(options.userModel), {
foreignKey: "user_id",
constraints: true,
as: "user"
});
}
return Revisions;
return Revision;
}
}
};

0 comments on commit e9e114d

Please sign in to comment.