sequelize-extension-view

0.0.1 • Public • Published

sequelize-extension-view

Build Status codecov GitHub license

Installation

$ npm install --save sequelize-extension-view

Usage

This library uses sequelize-extension to extend sequelize models. Models with the method createViews will be called to create table views (virtual models). The virtual model/instance inherits all the methods created by Sequelize.

const Sequelize = require('sequelize');
const extendSequelize = require('sequelize-extension');
const enhanceView = require('sequelize-extension-view');
 
const sequelize = new Sequelize(...);
 
const db = {}
db.Task = sequelize.define('task', {
  name: Sequelize.STRING(255),
  status: Sequelize.ENUM('PENDING', 'COMPLETED'),
});
Task.createViews = (create) => {
  db.PendingTask = create({
    where: { status: 'PENDING' },
  });
  db.CompletedTask = create({
    where: { status: 'COMPLETED' },
  });
};
 
extendSequelize([Task], {
  view: enhanceView(),
});
 
// ...
 
// Now you can call .findById(), .find(), .findOne() and 
// .findAll() and they will only return instances that 
// respect the view where statement.
db.PendingTask.find(options).then((pendingTask) => {
  // Do something with pending task
  pendingTask.name = 'new name';
  pendingTask.save().then(...);
}); 

Other Extensions

sequelize-extension-graphql - Create GraphQL schema based on sequelize models.
sequelize-extension-tracking - Automatically track sequelize instance updates.
sequelize-extension-createdby - Automatically set createdBy with options.user.id option.
sequelize-extension-updatedby - Automatically set updatedBy with options.user.id option.
sequelize-extension-deletedby - Automatically set deletedBy with options.user.id option.

Package Sidebar

Install

npm i sequelize-extension-view

Weekly Downloads

0

Version

0.0.1

License

MIT

Unpacked Size

104 kB

Total Files

15

Last publish

Collaborators

  • gcmarques