nielsgl / sequelize-paper-trail

Sequelize plugin for tracking revision history of model instances.
MIT License
95 stars 69 forks source link

Documentation for enabling User Tracking #98

Closed danieldilly closed 4 years ago

danieldilly commented 4 years ago

Hello. I am trying to implement sequelize-paper-trail into my application but I don't understand how to enable user tracking. I don't understand the 2nd step in your documentation. Can you help?

I have a Users table and a User model. Users have an id column.

I've completed the first step, which is to set userModel: 'User' in the init options. I do not understand what I'm supposed to do next.

Where does this code go?

Model.update({
  /* ... */
}, {
  userId: user.id
}).then(() {
  /* ... */
});

and what should Model be replaced with?

I appreciate any help you can give me. Thanks.

ekeuus commented 4 years ago

Model is your edited database model. The userid you provide in the second parameter(which is the options) will be used in the paper trail entry.

danieldilly commented 4 years ago

I still don't understand. I have several models like Customer, User, Project, etc that I have enabled paper trails for. I used the .sync command to create the Revision and RevisionChange tables. My entire sequelize-paper-trail code is as follows:

` if (process.env.PAPER_TRAIL === 'true') {

// Initiate paper trail

const ptOptions = {

  enableMigration: true,

  enableRevisionChangeModel: true,

  mysql: true,

  userModel: 'User',

  defaultAttributes: {

    documentId: 'DocumentId',

    revisionId: 'RevisionId'

  }

}

const PaperTrail = require('sequelize-paper-trail').init(models.sequelize, ptOptions);

let revisionModel = PaperTrail.defineModels(models)

async function setPaperTrails() {

  await models.Customer.hasPaperTrail()

  await models.Project.hasPaperTrail()

  await models.Bid.hasPaperTrail()

  await models.File.hasPaperTrail()

  await models.Contact.hasPaperTrail()

}

setPaperTrails()

}

if (process.env.PAPER_TRAIL === 'true') {

// Create the tables for sequelize-paper-trail

await models.sequelize.models.Revision.sync()

await models.sequelize.models.RevisionChange.sync()

}`

Now the Revisions table has a UserId field but it never gets populated. How do I get the user ID into that field? You said "Model is your edited database model" but I still don't understand which model that is. And where does it go? Somehow I need to hook into the function where the Revision table is being written to and add the userID there, right?

ekeuus commented 4 years ago

It will be updated if: A. You update your item and provide the current user ID (I am assuming you have your user in the request) await models.File.create({ name: 'a', size: 1000, ...more stuff }, { userId: req.user.id }); B. Use CLS and set it up so that the library takes the ID from context instead of providing it every time.

danieldilly commented 4 years ago

Okay, I think I understand. Since I'm using Finale-REST (an updated version of Epilogue) to create my REST endpoints automatically, I don't ever explicitly call the create() method of my models. I suspect I will need to hook into the milestones that Finale provides and pass the userId in there. I will experiment with this sometime soon and see if I can figure it out. Thanks for your help thus far.

danieldilly commented 4 years ago

Thanks for your hekp ekeuus. I had to fork & update the Finale-REST library to enable the passing of the options object to the Sequelize methods and then hook into the milestones to add the userId to the options object and it works now.