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

smodg

v1.3.0

Published

Generate basic Sequelize models from TypeScript declaration files

Downloads

25

Readme

smodg - Sequelize Model Generator

Generate Sequelize models from TypeScript type definitions

npm version tests build

Installation

npm install -g smodg@latest

or

npx smodg@latest 

Usage

Usage:

    smodg [options] <filepath> 

Options:

    --help, -h                   show help
    --migration                  create an Umzug migration. default: false
    --outputDir=PATH, -o PATH    model output directory, relative to current path. default: "src/models"         
    --schema=NAME, -s NAME       specify a schema. default is no schema 
    --version, -v                print installed version

Examples:

Create a model in src/models:

smodg ./my-type-file.ts

Create a model with a specific schema name:

smodg --schema=my_schema ./my-type-file.ts

Create a model and include a CREATE TABLE migration in ./src/migrations:

smodg --migration ./my-type-file.ts

Create a model with a custom output path:

# do not prefix output dir with ./
smodg --outputDir=src/db-schema ./my-type-file.ts

Features

  • Generates boilerplate Sequelize model in ./src/models/ that includes:
    • imports from sequelize-typescript and sequelize
    • Attributes and ColumnOptions interfaces
    • table definition object with table name
    • column definition object with column names and datatypes
    • model definition with @Table and @Column decorators

Limitations

  • Currently only works with one type per file. Multiple type aliases in one file will not work.
  • Defaults properties of type number to Sequelize.FLOAT. All generated numeric column definitons need to be reviewed for accuracy.
  • Foreign keys and constraints must be added manually
  • interface is not supported yet

Example Output

// my-custom-type.ts
export type UserAccount = {
    name: string,
    userName: string,
    birthday: Date,
    age: number,
    isActive: boolean
}
# terminal command
smodg --schema=applicationAccess --migration ./my-custom-type.ts

The above command will generate two files:

// src/models/user-account.model.ts
import {
    Column,
    Table,
    Model,
    DataType,
    CreatedAt,
    UpdatedAt,
    ForeignKey,
    HasMany,
} from 'sequelize-typescript';

import { UserAccount as UserAccountType } from '@_YOUR_TYPES'
import { ModelAttributeColumnOptions } from 'sequelize';

interface UserAccountCreationAttributes extends UserAccountType {}

interface UserAccountAttributes extends UserAccountCreationAttributes {
    id: number;
    createdBy: string;
    createdDate: Date;
    updatedBy: string;
    updatedDate: Date;
}

type UserAccountKeys = keyof UserAccountAttributes

interface ColumnOptions extends ModelAttributeColumnOptions {
    field: string
}

export const tableDefinition = {
    tableName: 'user_account',
    schema: 'application_access',
}

export const columnDefinition: Record<UserAccountKeys, ColumnOptions> = {
    id: {
        field: "id",
        type: DataType.INTEGER,
        primaryKey: true,
        autoIncrement: true,
    },
    name: {
        field: 'name',
        type: DataType.STRING
    },
    userName: {
        field: 'user_name',
        type: DataType.STRING
    },
    birthday: {
        field: 'birthday',
        type: DataType.DATE
    },
    age: {
        field: 'age',
        type: DataType.FLOAT
    },
    isActive: {
        field: 'is_active',
        type: DataType.BOOLEAN
    },
    createdBy: {
        field: "created_by",
        type: DataType.STRING,
    },
    createdDate: {
        field: "created_date",
        type: DataType.DATE,
    },
    updatedBy: {
        field: "updated_by",
        type: DataType.STRING,
    },
    updatedDate: {
        field: "updated_date",
        type: DataType.DATE,
    },
}

@Table(tableDefinition)
export class UserAccount
extends Model<UserAccountAttributes, UserAccountCreationAttributes>
implements UserAccountAttributes {
    @Column(columnDefinition.id)
    id!: number;

    @Column(columnDefinition.name)
    name!: string

    @Column(columnDefinition.userName)
    userName!: string

    @Column(columnDefinition.birthday)
    birthday!: Date

    @Column(columnDefinition.age)
    age!: number

    @Column(columnDefinition.isActive)
    isActive!: boolean

    @Column(columnDefinition.createdBy)
    createdBy!: string;

    @CreatedAt
    @Column(columnDefinition.createdDate)
    createdDate!: Date;

    @Column(columnDefinition.updatedBy)
    updatedBy!: string;

    @UpdatedAt
    @Column(columnDefinition.updatedDate)
    updatedDate!: Date;
}
// src/migrations/2023.05.18T18.13.57-Create-Table-UserAccount.ts
import { DataType } from 'sequelize-typescript';
import type { Migration } from '../db';

const tableDefinition = {
    tableName: 'user_account',
    schema: 'application_access',
}

const columnDefinition = {
    id: {
        field: "id",
        type: DataType.INTEGER,
        primaryKey: true,
        autoIncrement: true,
    },
    name: {
        field: 'name',
        type: DataType.STRING
    },
    userName: {
        field: 'user_name',
        type: DataType.STRING
    },
    birthday: {
        field: 'birthday',
        type: DataType.DATE
    },
    age: {
        field: 'age',
        type: DataType.FLOAT
    },
    isActive: {
        field: 'is_active',
        type: DataType.BOOLEAN
    },
    createdBy: {
        field: "created_by",
        type: DataType.STRING,
    },
    createdDate: {
        field: "created_date",
        type: DataType.DATE,
    },
    updatedBy: {
        field: "updated_by",
        type: DataType.STRING,
    },
    updatedDate: {
        field: "updated_date",
        type: DataType.DATE,
    },
};

export const up: Migration = async ({ context: queryInterface }) => {
    await queryInterface.createTable(tableDefinition, columnDefinition);
};