sequelize-extension-createdby
v0.0.3
Published
This extension will automatically set createdBy to options.user.id when an instance is created.
Downloads
12
Maintainers
Readme
sequelize-extension-createdBy
Installation
$ npm install --save sequelize-extension-createdBy
Usage
This library uses sequelize-extension to extend sequelize models. If a model has a createdBy
field, this extension will automatically set createdBy
to options.user.id
when an instance is created.
const Sequelize = require('sequelize');
const extendSequelize = require('sequelize-extension');
const enhanceCreatedBy = require('sequelize-extension-createdBy');
const sequelize = new Sequelize(...);
const Task = sequelize.define('task', {
name: Sequelize.STRING(255),
});
extendSequelize([Task], {
createdBy: enhanceCreatedBy(),
});
const task1 = await Task.create({...}, { user: { id: 2 } });
console.log(task1.createdBy);
// 2
const task2 = await Task.create({...});
console.log(task2.createdBy);
// 1 <- default userId
await Task.bulkCreate([
{...},
{...},
], { user: { id: 3 } });
// All created tasks will have createdBy === 3
Other Extensions
sequelize-extension-tracking - Automatically track sequelize instance updates.
sequelize-extension-updatedby - Automatically set updatedBy
with options.user.id
option.
sequelize-extension-deletedby - Automatically set deletedBy
with options.user.id
option.
sequelize-extension-graphql - Create GraphQL schema based on sequelize models.
sequelize-extension-view - Models with the method createViews
will be called to create table views (virtual models).