npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sequelize-oro

v1.0.5

Published

Automatically generate bare sequelize Models,Migrations & Seeders from your database.

Downloads

7

Readme

Sequelize-oro

Automatically generate Migrations, Models and Seeder for SequelizeJS via config file

Produced Migrations, Models are made to compatible with sequelize-cli package.

Install

npm install sequelize-oro

Prerequisites

You will need to install the correct dialect binding before using sequelize-oro.

| Dialect | Install | | ------------- | ---------------------------------------- | | Postgres | npm install sequelize pg pg-hstore | | sequelize-cli | npm install --save-dev sequelize-cli |

Usage

Migration

Sequelize migration generate from external config file.

// Used For Generate Migrations from live database
const { SequelizeMigrate } = require('sequelize-oro');
const path = require('path');

require('dotenv-safe').config({
  path: path.join(__dirname, '/.env'),
  sample: path.join(__dirname, '/.env.example'),
});

const env = process.env.NODE_ENV || 'development';
const { [env]: config } = require('./config/config.json');

// Association is only created in lang ts type
// const options = { directory: `${__dirname}/models`, dialect: 'postgres' };
const migration = new SequelizeMigrate(config.database, config.username, config.password, {
  host: config.host,
  dialect: config.dialect,
  caseModel: 'p',
  directory: './db/migrations', // where to write files
  // lang:'ts',
  // tables: ['branch'],
  migrationTimestamp: 20211209091019,
  additional: {
    timestamps: true,
    underscored: true,
    // ...options added to each model
  },
});
migration.run().then((data) => {
  console.log(data.tables);
  // console.log(data.foreignKeys); // table foreign key list
  // console.log(data.indexes);     // table indexes
  // console.log(data.hasTriggerTables); // tables that have triggers
  // console.log(data.relations);   // relationships between models
  // console.log(data.text)         // text of generated models
});

All Basic table data, indexes, functions, triggers, forign keys will be migrated in to specified directory. All migrations files are compatible with sequelize-cli, so after generating migration files, just run

node_modules/.bin/sequelize db:migrate

Migrations are generated in alphabetical table order with given timestamp and after creating all basic tables it will create forign key constraint migrations.

Model

Sequelize Model generate from external config file.

// Used For Generate Models from live database
const { SequelizeModel } = require('sequelize-oro');
const path = require('path');

require('dotenv-safe').config({
  path: path.join(__dirname, '/.env'),
  sample: path.join(__dirname, '/.env.example'),
});

const env = process.env.NODE_ENV || 'development';
const { [env]: config } = require('./config/config.json');

// Association is only created in lang ts type
// const options = { directory: `${__dirname}/models`, dialect: 'postgres' };
const modelGenerate = new SequelizeModel(config.database, config.username, config.password, {
  host: config.host,
  dialect: config.dialect,
  caseModel: 'p',
  directory: './models', // where to write files
  // lang:'ts',
  // tables: ['plan'],
  additional: {
    timestamps: true,
    underscored: true,
    // ...options added to each model
  },
});
modelGenerate.run().then((data) => {
  console.log(data.tables);
  // console.log(data.foreignKeys); // table foreign key list
  // console.log(data.indexes);     // table indexes
  // console.log(data.hasTriggerTables); // tables that have triggers
  // console.log(data.relations);   // relationships between models
  // console.log(data.text)         // text of generated models
});

All Model data with relations and indexes will be generated from live database. Generated Model is compatible sequelize-cli.

Seeders

Sequelize Seeder Generate from live Database

// Used For Generate Models from live database
const { SequelizeSeeder } = require('sequelize-oro');
const path = require('path');

require('dotenv-safe').config({
  path: path.join(__dirname, '/.env'),
  sample: path.join(__dirname, '/.env.example'),
});

const env = process.env.NODE_ENV || 'development';
const { [env]: config } = require('./config/config.json');

// Association is only created in lang ts type
// const options = { directory: `${__dirname}/models`, dialect: 'postgres' };
const seederGenerate = new SequelizeSeeder(config.database, config.username, config.password, {
  logging: false,
  host: config.host,
  dialect: config.dialect,
  caseModel: 'p',
  directory: './db/seeders', // where to write files
  seederTimestamp: 20211209091019,
  // to avoid forign key dependency error it is ncessary to insert in certain order
  tables: ['city', 'role', 'user', 'address'],
  tableOptions: {
    city: {
      conditions: [
        {
          column: 'is_deleted',
          condition: '=',
          value: false,
        },
      ],
    },
    user: {
      conditions: [
        {
          column: 'is_deleted',
          condition: '=',
          value: false,
        },
        {
          column: 'id',
          condition: 'IN',
          value: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
        },
      ],
    },
    role: {
      conditions: [
        {
          column: 'is_deleted',
          condition: '=',
          value: false,
        },
      ],
    },
    address: {
      conditions: [
        {
          column: 'is_deleted',
          condition: '=',
          value: false,
        },
        {
          column: 'user_id',
          condition: 'IS NOT',
          quotes: false,
          value: 'NULL',
        },
      ],
    },
  },
});
seederGenerate.run().then((data) => {
  // console.log(data.tables);
  // console.log(data.foreignKeys); // table foreign key list
  // console.log(data.indexes);     // table indexes
  // console.log(data.hasTriggerTables); // tables that have triggers
  // console.log(data.relations);   // relationships between models
  // console.log(data.text)         // text of generated models
});

Seeders can be generated with conditions and optional columns can be skipped from remote database.

How to Run Config files

Open package.json and add thos two scripts to execute Migration and Model or directly run scripts using node command

"scripts": {
    "generate_model": "node sequelizeModel.js",
    "generate_migration": "node sequelizeMigration.js"
  },

Now Finally Generate sequelize Models and Migration using following commands

npm run generate_model

npm run generate_migration