Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problems when trying to use TypeScript #11

Open
Guergeiro opened this issue Feb 24, 2020 · 5 comments
Open

Problems when trying to use TypeScript #11

Guergeiro opened this issue Feb 24, 2020 · 5 comments
Labels
enhancement New feature or request

Comments

@Guergeiro
Copy link

Guergeiro commented Feb 24, 2020

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;
};
@nodejh nodejh added bug Something isn't working and removed bug Something isn't working labels Feb 24, 2020
@Guergeiro
Copy link
Author

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
Copy link
Owner

nodejh commented Feb 26, 2020

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

@Guergeiro
Copy link
Author

Do you an ETA on the fix?

@nodejh
Copy link
Owner

nodejh commented May 11, 2020

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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants