nodejh / sequelize-automate

Automatically generate bare sequelize models from your database.
MIT License
116 stars 22 forks source link

Problems when trying to use TypeScript #11

Open Guergeiro opened 4 years ago

Guergeiro commented 4 years ago

First, thanks for your script. I have some problems when trying to incorporate the TS generated by sequelize-automate.

Project structure:

|--- database.ts
|--- models/
     |--- news_revision.ts
     |--- news_revision.d.ts
     |--- ...

The error I get is on the sequelize.define() Screenshot from 2020-02-24 14-23-17 This happens to all models generated and in both attributes and options. I'm just showing one for the sake of simplicity.

Code

// news_revision.ts
import { Sequelize, DataTypes } from "sequelize";
export default function(sequelize: Sequelize) {
    const attributes = {
        id: {
            type: DataTypes.BIGINT.UNSIGNED,
            allowNull: false,
            defaultValue: null,
            primaryKey: true,
            autoIncrement: true,
            comment: null,
            field: "id",
        },
        featured_image: {
            type: DataTypes.BIGINT.UNSIGNED,
            allowNull: true,
            defaultValue: null,
            primaryKey: false,
            autoIncrement: false,
            comment: null,
            field: "featured_image",
            references: {
                key: "id",
                model: "file_model",
            },
        },
        feedback: {
            type: DataTypes.TEXT,
            allowNull: true,
            defaultValue: null,
            primaryKey: false,
            autoIncrement: false,
            comment: null,
            field: "feedback",
        },
        content_id: {
            type: DataTypes.BIGINT.UNSIGNED,
            allowNull: false,
            defaultValue: null,
            primaryKey: false,
            autoIncrement: false,
            comment: null,
            field: "content_id",
            references: {
                key: "id",
                model: "content_model",
            },
        },
        region_id: {
            type: DataTypes.INTEGER.UNSIGNED,
            allowNull: true,
            defaultValue: null,
            primaryKey: false,
            autoIncrement: false,
            comment: null,
            field: "region_id",
            references: {
                key: "id",
                model: "region_model",
            },
        },
        state_id: {
            type: DataTypes.INTEGER.UNSIGNED,
            allowNull: false,
            defaultValue: null,
            primaryKey: false,
            autoIncrement: false,
            comment: null,
            field: "state_id",
            references: {
                key: "id",
                model: "state_model",
            },
        },
        user_id: {
            type: DataTypes.INTEGER.UNSIGNED,
            allowNull: false,
            defaultValue: null,
            primaryKey: false,
            autoIncrement: false,
            comment: null,
            field: "user_id",
            references: {
                key: "id",
                model: "user_model",
            },
        },
        publish_at: {
            type: DataTypes.DATE,
            allowNull: false,
            defaultValue: Sequelize.literal("CURRENT_TIMESTAMP"),
            primaryKey: false,
            autoIncrement: false,
            comment: null,
            field: "publish_at",
        },
        created_at: {
            type: DataTypes.DATE,
            allowNull: false,
            defaultValue: Sequelize.literal("CURRENT_TIMESTAMP"),
            primaryKey: false,
            autoIncrement: false,
            comment: null,
            field: "created_at",
        },
        updated_at: {
            type: DataTypes.DATE,
            allowNull: false,
            defaultValue: Sequelize.literal("CURRENT_TIMESTAMP"),
            primaryKey: false,
            autoIncrement: false,
            comment: null,
            field: "updated_at",
        },
        deleted_at: {
            type: DataTypes.DATE,
            allowNull: true,
            defaultValue: null,
            primaryKey: false,
            autoIncrement: false,
            comment: null,
            field: "deleted_at",
        },
    };
    const options = {
        tableName: "news_revision",
        comment: "",
        indexes: [
            {
                name: "news_revision_featured_image_foreign",
                unique: false,
                type: "BTREE",
                fields: ["featured_image"],
            },
            {
                name: "news_revision_content_id_foreign",
                unique: false,
                type: "BTREE",
                fields: ["content_id"],
            },
            {
                name: "news_revision_state_id_foreign",
                unique: false,
                type: "BTREE",
                fields: ["state_id"],
            },
            {
                name: "news_revision_user_id_foreign",
                unique: false,
                type: "BTREE",
                fields: ["user_id"],
            },
            {
                name: "FK_news_revision_region_id",
                unique: false,
                type: "BTREE",
                fields: ["region_id"],
            },
        ],
    };
    const NewsRevisionModel = sequelize.define("news_revision_model", attributes, options);
    return NewsRevisionModel;
}
// news_revision.d.ts
import { Model, BuildOptions } from "sequelize";
export interface INewsRevisionAttributes {
    id: number;
    featured_image?: number;
    feedback?: string;
    content_id: number;
    region_id?: number;
    state_id: number;
    user_id: number;
    publish_at: Date;
    created_at: Date;
    updated_at: Date;
    deleted_at?: Date;
}
export interface INewsRevisionModel extends INewsRevisionAttributes, Model {}
export type INewsRevisionModelStatic = typeof Model & {
    new (values?: object, options?: BuildOptions): INewsRevisionModel;
};
Guergeiro commented 4 years ago

Oh, and I also changed some things in the code to remove other errors.

  1. sequelize.literal(...) => Sequelize.literal(...)
  2. DataTypes.INTEGER(size).UNSIGNED => DataTypes.INTEGER.UNSIGNED

Honestly, don't know why I had to do this. According to the docs, everything seems correct.

nodejh commented 4 years ago

Thanks for you feedback. Ot is a bug about generating codes, I'll fix it.

Guergeiro commented 4 years ago

Do you an ETA on the fix?

nodejh commented 4 years ago

Hi @Guergeiro , so sorry I haven't reply your issue. Do you have the problems now? I think there are something wrong with sequelize types, maybe you could add @ts-nocheck for model files with tsNoCheck: true options.

Guergeiro commented 4 years ago

That is a solution. I haven't tried anymore since I moved away from using sequelize at all. Using vanilla knexjs for it.