Node.js - Sequelize support

Introduction

Sequelize is a promise-based Node.js ORM tool for Postgres, MySQL, SQLite, Microsoft SQL Server, Oracle Database - see https://sequelize.org/docs/ for more information.

Objects

This extension creates the entity, entity operation and query objects:

  • a ‘NodeJS Entity’ object when the APIs ‘define’ or ‘init’ of a Sequelize.Model instance is found.
  • a ‘NodeJS Entity Operation’ object when one of the supported Sequelize APIs is used and linked to one of the entities.
  • a ‘TypeScript SQL Query’ object when the API ‘query’ is found.
Icon Description
NodeJS Entity
NodeJS Entity Operation
TypeScript SQL Query

Supported persistence SQL databases

Supported operations

Entity Operation Supported APIs
Add
  • Model.create
  • Model.bulkCreate
Update
  • Model.update
  • Model.restore
  • Model.increment
  • Model.decrement
Remove
  • Model.destroy
Select
  • Model.findAll
  • Model.findByPk
  • Model.findOne
  • Model.findOrCreate
  • Model.findAndCountAll
  • Model.count
  • Model.max
  • Model.min
  • Model.sum

‘Model’ above can be defined by ‘Sequelize.define’ or using a class extending ‘Sequelize.Model’.

Link Type Caller type Callee type Comment
callLink
  • TypeScript Function
  • TypeScript Method
  • TypeScript Module
  • NodeJS Entity Operation
  • TypeScript SQL Query
relyonLink
  • NodeJS Entity
  • TypeScript Class
When the entity is defined using a class extending ‘Sequelize.Model’.
useInsertLink
  • NodeJS Entity Operation: Add
  • TypeScript SQL Query
  • Table
  • Missing Table
Created by SQL Analyzer when DDL source files are analyzed or by Missing tables and procedures for Node.js when the object is not analyzed.
useUpdateLink
  • NodeJS Entity Operation: Update
  • TypeScript SQL Query
  • Table
  • Missing Table
Created by SQL Analyzer when DDL source files are analyzed or by Missing tables and procedures for Node.js when the object is not analyzed.
useDeleteLink
  • NodeJS Entity Operation: Remove
  • TypeScript SQL Query
  • Table
  • Missing Table
Created by SQL Analyzer when DDL source files are analyzed or by Missing tables and procedures for Node.js when the object is not analyzed.
useSelectLink
  • NodeJS Entity Operation: Select
  • TypeScript SQL Query
  • Table
  • Missing Table
Created by SQL Analyzer when DDL source files are analyzed or by Missing tables and procedures for Node.js when the object is not analyzed .

Example

Take the following codes:

import Sequelize from 'sequelize';
const sequelize = new Sequelize('sqlite::memory:');

class User extends Sequelize.Model { }
User.init(
  {
    // ... (attributes)
  },
  {
    sequelize,
    modelName: 'user',
  }
);

await User.findAll().then(t_user=> { });

const Post = sequelize.define(
  'post',
  {
    // ... (attributes)
  },
  {
    freezeTableName: true,
  },
);

await Post.update(
  { lastName: 'Doe' },
);

function userSearch (req) {
    var query = "SELECT name,id FROM Users WHERE login='" + req.body.login + "'";
    db.sequelize.query(query, {
        model: db.User
    })
      .then(user => {
    })
};

In this example, two ‘Node.js Entity’ objects, two ‘Node.js Entity Operation’ objects, and a ‘SQL Query’ object are created. A ‘relyOn’ link is added when entity ‘user’ is defined using a class extending ‘Sequelize.Model’. The SQL Analyzer or Missing tables and procedures for Node.js links these entity operations and this query with the corresponding tables. In the present case, this extension creates two ‘useSelect’ and a ‘useUpdate’ links to the missing tables ‘Users’ and ‘post’: