@hero-dynamic-form/sequelize-inspector
v1.0.43
Published
Sequelize entity inspector adapter for @hero-dynamic-form/core
Maintainers
Readme
@hero-dynamic-form/sequelize-inspector
Sequelize entity inspector adapter for @hero-dynamic-form/core.
Installation
npm install @hero-dynamic-form/sequelize-inspector @hero-dynamic-form/core sequelizeCLI Usage
Generate Sequelize model from database
# List all tables
npx sequelize-inspector generate
# Generate model from table
npx sequelize-inspector generate users --output ./src/models
# Overwrite existing file
npx sequelize-inspector generate users --forceOptions:
--output, -o <path>- Output directory (default:./src/models)--force, -f- Overwrite existing files--env <path>- Path to .env file (default:.env)
Environment Variables
DB_HOST=localhost
DB_PORT=5432
DB_USER=postgres
DB_PASS=postgres
DB_NAME=your_databaseExample Output
import { Model, DataTypes, Optional } from "sequelize";
import { Table, Column } from "sequelize-typescript";
export interface UserAttributes {
id?: number;
email: string;
name?: string;
role: string;
createdAt?: Date;
updatedAt?: Date;
}
export interface UserCreationAttributes
extends Optional<UserAttributes, "id" | "createdAt" | "updatedAt"> {}
@Table({ tableName: "users", timestamps: true, underscored: true })
export class User extends Model<UserAttributes, UserCreationAttributes> {
@Column({
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
})
id!: number;
@Column({
type: DataTypes.STRING,
allowNull: false,
})
email!: string;
@Column({
type: DataTypes.STRING,
allowNull: true,
})
name?: string;
@Column({
type: DataTypes.STRING,
allowNull: false,
validate: { isIn: [["USER", "ADMIN", "MODERATOR"]] },
})
role!: string;
}Library Usage
Basic Usage
import { DynamicFormCore } from "@hero-dynamic-form/core";
import { SequelizeEntityInspector } from "@hero-dynamic-form/sequelize-inspector";
import { Sequelize, DataTypes, Model } from "sequelize";
// Define Sequelize models
class User extends Model {}
User.init(
{
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
},
email: {
type: DataTypes.STRING,
allowNull: false,
unique: true,
validate: {
isEmail: true,
},
},
name: {
type: DataTypes.STRING,
allowNull: true,
},
role: {
type: DataTypes.ENUM("USER", "ADMIN", "MODERATOR"),
defaultValue: "USER",
},
age: {
type: DataTypes.INTEGER,
validate: {
min: 0,
max: 150,
},
},
},
{
sequelize: new Sequelize("sqlite::memory:"),
modelName: "User",
}
);
// Create inspector
const inspector = new SequelizeEntityInspector();
const config = {
entityInspector: inspector,
resources: [
{
name: "users",
entity: User, // Pass Sequelize model
},
],
// ... rest of config
};
const core = new DynamicFormCore(config);
await core.init();
await core.start();Features
Extracts Sequelize model metadata from
rawAttributes/tableAttributesGenerates validation rules based on field types and constraints
Supports all Sequelize DataTypes:
- STRING/TEXT/CHAR → text
- INTEGER → int
- BIGINT → bigint
- FLOAT/REAL → float
- DOUBLE → double
- DECIMAL → decimal
- BOOLEAN → boolean
- DATE → timestamp
- DATEONLY → date
- TIME → time
- JSON → json
- JSONB → jsonb
- BLOB → bytea
- UUID → uuid
- ENUM → enum
- ARRAY → array
- GEOMETRY → geometry
- GEOGRAPHY → geography
Supports field options:
primaryKey- Primary key detectionallowNull- Nullable fieldsdefaultValue- Default valuesunique- Unique constraintsautoIncrement- Auto-increment fieldsvalues- Enum values
Automatic validation from Sequelize
validateoption:isEmail- Email formatisUrl- URL formatisUUID- UUID formatlen: [min, max]- String lengthmin- Numeric minimummax- Numeric maximum
Convention-based validation:
- Fields named
email: email pattern - Fields named
url: URL pattern - Fields named
phone/tel: phone pattern
- Fields named
Sequelize Model Example
import { Sequelize, DataTypes, Model } from "sequelize";
const sequelize = new Sequelize("sqlite::memory:");
class User extends Model {}
User.init(
{
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
},
email: {
type: DataTypes.STRING,
allowNull: false,
unique: true,
validate: {
isEmail: true,
},
},
username: {
type: DataTypes.STRING(50),
allowNull: false,
unique: true,
validate: {
len: [3, 50],
},
},
password: {
type: DataTypes.STRING,
allowNull: false,
validate: {
len: [8, 100],
},
},
name: {
type: DataTypes.STRING,
allowNull: true,
},
bio: {
type: DataTypes.TEXT,
allowNull: true,
},
age: {
type: DataTypes.INTEGER,
validate: {
min: 0,
max: 150,
},
},
role: {
type: DataTypes.ENUM("USER", "ADMIN", "MODERATOR"),
defaultValue: "USER",
},
isActive: {
type: DataTypes.BOOLEAN,
defaultValue: true,
},
metadata: {
type: DataTypes.JSON,
allowNull: true,
},
website: {
type: DataTypes.STRING,
validate: {
isUrl: true,
},
},
phoneNumber: {
type: DataTypes.STRING,
},
createdAt: {
type: DataTypes.DATE,
allowNull: false,
},
updatedAt: {
type: DataTypes.DATE,
allowNull: false,
},
},
{
sequelize,
modelName: "User",
tableName: "users",
timestamps: true,
}
);
class Post extends Model {}
Post.init(
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
title: {
type: DataTypes.STRING,
allowNull: false,
validate: {
len: [1, 200],
},
},
content: {
type: DataTypes.TEXT,
allowNull: true,
},
published: {
type: DataTypes.BOOLEAN,
defaultValue: false,
},
views: {
type: DataTypes.INTEGER,
defaultValue: 0,
validate: {
min: 0,
},
},
tags: {
type: DataTypes.ARRAY(DataTypes.STRING),
defaultValue: [],
},
userId: {
type: DataTypes.INTEGER,
allowNull: false,
references: {
model: User,
key: "id",
},
},
},
{
sequelize,
modelName: "Post",
tableName: "posts",
}
);
// Define associations
User.hasMany(Post, { foreignKey: "userId" });
Post.belongsTo(User, { foreignKey: "userId" });Full Example
import { DynamicFormCore, defineConfig } from "@hero-dynamic-form/core";
import { SequelizeEntityInspector } from "@hero-dynamic-form/sequelize-inspector";
import { Sequelize, DataTypes, Model } from "sequelize";
// Setup Sequelize
const sequelize = new Sequelize({
dialect: "postgres",
host: "localhost",
database: "mydb",
username: "user",
password: "password",
});
// Define models (User, Post, etc.)
// ...
// Create inspector
const inspector = new SequelizeEntityInspector();
const config = defineConfig({
dataSource: sequelize, // Pass Sequelize instance
entityInspector: inspector,
resources: [
{
name: "users",
entity: User,
label: "Users",
},
{
name: "posts",
entity: Post,
label: "Posts",
},
],
auth: {
enabled: true,
adapter: myAuthAdapter,
},
backend: {
port: 3030,
},
});
const core = new DynamicFormCore(config);
await core.init();
await core.start();Type Mappings
| Sequelize Type | Normalized Type | | ---------------- | --------------- | | STRING/TEXT/CHAR | text | | INTEGER | int | | BIGINT | bigint | | FLOAT/REAL | float | | DOUBLE | double | | DECIMAL | decimal | | BOOLEAN | boolean | | DATE | timestamp | | DATEONLY | date | | TIME | time | | JSON | json | | JSONB | jsonb | | BLOB | bytea | | UUID | uuid | | ENUM | enum | | ARRAY | array | | GEOMETRY | geometry | | GEOGRAPHY | geography |
Validation Rules
The inspector automatically generates validation rules from:
Field Options:
allowNull: false→ requireddefaultValue→ skips required if presentvalues(for ENUM) → enum validation
Sequelize Validate Options:
isEmail: true→ email patternisUrl: true→ URL patternisUUID: true→ UUID patternlen: [min, max]→ minLength/maxLengthmin: number→ numeric minimummax: number→ numeric maximum
Type-based Validation:
- Numeric types: min/max ranges
- DATE: valid date check
- BOOLEAN: boolean check
- JSON/JSONB: valid JSON check
- UUID: UUID pattern check
Convention-based Validation:
- Fields named
email: email pattern - Fields named
url: URL pattern - Fields named
phone/tel: phone pattern
- Fields named
API
SequelizeEntityInspector
class SequelizeEntityInspector implements EntityInspector {
// Inspect Sequelize model (required by EntityInspector interface)
inspectEntity(entity: any): ResourceSchema;
}Notes
- Works with Sequelize v6+
- Supports all dialects: PostgreSQL, MySQL, SQLite, MSSQL, MariaDB
- Extracts metadata from
model.rawAttributesormodel.tableAttributes - Respects Sequelize's built-in validation rules
License
MIT
