Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support sequelize options through context.sequelizeOptions #210

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ArnaudD
Copy link

@ArnaudD ArnaudD commented Apr 24, 2017

Hi,

We've been running with this fork for a year and though that this might interest someone.

We've added an attribute (sequelizeOptions) to epilogue context to specify options to sequelize find, create, update or destroy methods.

This is useful for enforcing attribute/model access control with sequelize hooks (with something that looks like ssacl-attribute-roles), or to easily dispatch "changes" somewhere else :

In epilogue resources declaration:

const passRequestToSequelize = {
  write: {
    before: function(request, response, context) {
      context.sequelizeOptions = {
        _request: request,
      };
      return context.continue;
    }
  }
};

export const changelog = {
  create: passRequestToSequelize,
  update: passRequestToSequelize,
  delete: passRequestToSequelize,
};

// ...

epilogueResource.use(changelog);

In sequelize initialization:

function customLog(type, instance, options) {
  const changes = instance.changed();
  logEvent({
    type: type,
    model: instance.Model,
    changes: changes ? changes.map(k => instance.previous(k)) : false,
    userId: options._request.currentUser.id
  })
}

const sequelize = new Sequelize('postgres://user:[email protected]:5432/dbname', config);

sequelize.addHook('afterCreate', customLog.bind(null, 'create'));
sequelize.addHook('afterUpdate', customLog.bind(null, 'update'));
sequelize.addHook('afterDestroy', customLog.bind(null, 'delete'));

What do you think ?

Thanks for epilogue !

Example : Logging data changes along with request info
------------------------------------------------------

In epilogue resources declaration:

```javascript
const passRequestToSequelize = {
  write: {
    before: function(request, response, context) {
      context.sequelizeOptions = {
        _request: request,
      };
      return context.continue;
    }
  }
};

export const changelog = {
  create: passRequestToSequelize,
  update: passRequestToSequelize,
  delete: passRequestToSequelize,
};
```

In sequelize initialization:

```javascript
function customLog(type, instance, options) {
  const changes = instance.changed();
  logEvent({
    type: type,
    model: instance.Model,
    changes: changes ? changes.map(k => instance.previous(k)) : false,
    userId: options._request.currentUser.id
  })
}

const config = {
  define: {
    hooks: {
      afterCreate: customLog.bind(null, 'create'),
      afterUpdate: customLog.bind(null, 'update'),
      afterDestroy: customLog.bind(null, 'delete'),
    }
  }
}

const sequelize = new Sequelize('postgres://user:[email protected]:5432/dbname', config);
```
@Tmassery
Copy link

So I was about to open the exact same PR, this would be a wonderful enhancement. Any chance this could get traction?

We need to pass in an auth token from the initial call handled by epilogue, down through to the sequelize hooks to make some external calls and notify other systems of the changes (using that same auth token)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants