https://github.com/nealwp/smodg
Generate Sequelize models from TypeScript type definitions
https://github.com/nealwp/smodg
cli code-generation orm-models sequelize typescript
Last synced: 4 months ago
JSON representation
Generate Sequelize models from TypeScript type definitions
- Host: GitHub
- URL: https://github.com/nealwp/smodg
- Owner: nealwp
- License: isc
- Created: 2023-03-16T04:14:12.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2025-03-16T17:23:30.000Z (about 1 year ago)
- Last Synced: 2025-10-10T08:14:56.524Z (8 months ago)
- Topics: cli, code-generation, orm-models, sequelize, typescript
- Language: TypeScript
- Homepage:
- Size: 152 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# smodg - Sequelize Model Generator
> Generate Sequelize models from TypeScript type definitions
[](https://www.npmjs.com/package/smodg)


## Installation
```bash
npm install -g smodg@latest
```
or
```bash
npx smodg@latest
```
## Usage
```
Usage:
smodg [options]
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`:
```bash
smodg ./my-type-file.ts
```
Create a model with a specific schema name:
```bash
smodg --schema=my_schema ./my-type-file.ts
```
Create a model and include a CREATE TABLE migration in `./src/migrations`:
```bash
smodg --migration ./my-type-file.ts
```
Create a model with a custom output path:
```bash
# 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
```typescript
// my-custom-type.ts
export type UserAccount = {
name: string,
userName: string,
birthday: Date,
age: number,
isActive: boolean
}
```
```bash
# terminal command
smodg --schema=applicationAccess --migration ./my-custom-type.ts
```
The above command will generate two files:
```typescript
// 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 = {
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
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;
}
```
```typescript
// 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);
};
```