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-modelgen

v0.1.0

Published

Generate sequelize models from sql

Downloads

4

Readme

sequelize-modelgen

Generate mode ts files from sql file

Get Started

  1. install tool
npm i -g sequelize-modelgen
  1. write sql file
 CREATE TABLE `Table1` (
    `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    `name` VARCHAR(99) NOT NULL,
    `pass` VARCHAR(99) NOT NULL,
    `hobby` VARCHAR(99),
    `isForbid` TINYINT DEFAULT 0 COMMENT "Is forbidden user",
    `createdAt` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    UNIQUE KEY `uniqName` (`name`)
) COMMENT "table 1";

 CREATE TABLE `Table2` (
    `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    `name` VARCHAR(99) NOT NULL,
    `createdAt` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
) COMMENT "table 2";
  1. run modelgen
modelgen db.sql models

genrate models/index.ts

import { Sequelize } from "sequelize";

// AutoGenImportBegin {
import Table1, { Table1Attributes } from "./Table1";
import Table2, { Table2Attributes } from "./Table2";
// } AutoGenImportEnd

export function load(sequelize: Sequelize) {
  // AutoGenBootstrapBegin {
  Table1.bootstrap(sequelize);
  Table2.bootstrap(sequelize);
  // } AutoGenBootstrapEnd
}

export {
  // AutoGenExportBegin {
  Table1,
  Table1Attributes,
  Table2,
  Table2Attributes,
  // } AutoGenExportEnd
};

genrate models/Table1.ts

import { Sequelize, Model, DataTypes, NOW } from "sequelize";

export interface Table1Attributes {
  // AutoGenIntefaceAttrBegin {
  id?: number;
  name: string;
  pass: string;
  hobby?: string;
  isForbid?: number;
  createdAt?: Date;
  // } AutoGenIntefaceAttrEnd
}

export default class Table1 extends Model<Table1Attributes, Partial<Table1Attributes>> {
  // AutoGenModelAttrsBegin {
  public id: number;
  public name!: string;
  public pass!: string;
  public hobby: string;
  public isForbid: number;
  public createdAt: Date;
  // } AutoGenModelAttrsEnd

  public static bootstrap(sequelize: Sequelize) {
    Table1.init(
      {
        // AutoGenColumnDefsBegin {
        id: {
          type: DataTypes.BIGINT().UNSIGNED,
          autoIncrement: true,
          primaryKey: true,
        },
        name: {
          type: DataTypes.STRING(99),
          allowNull: false,
        },
        pass: {
          type: DataTypes.STRING(99),
          allowNull: false,
        },
        hobby: {
          type: DataTypes.STRING(99),
        },
        isForbid: {
          type: DataTypes.TINYINT(),
          defaultValue: 0,
        },
        createdAt: {
          type: DataTypes.DATE(),
          allowNull: false,
          defaultValue: NOW,
        },
        // } AutoGenColumnDefsEnd
      },
      {
        sequelize,
        tableName: "Table1",
        timestamps: false,
      },
    );
  }
}

AutoGen wrapped code will be replaced by modelgen, other parts will keep unchaning.