eggjs / egg

🥚 Born to build better enterprise frameworks and apps with Node.js & Koa
https://eggjs.org
MIT License
18.87k stars 1.81k forks source link

sequalize-auto的ts有什么办法吗? #4782

Open syymo opened 2 years ago

syymo commented 2 years ago

sequalize-auto的ts格式的有什么办法吗?

hyj1991 commented 2 years ago

可以去 https://github.com/sequelize/sequelize-auto/issues?q=typescript 下看看,或者在那边提 issue 咨询下

syymo commented 2 years ago

https://github.com/sequelize/sequelize-auto/issues?q=typescript

sequelize-auto工具是支持ts的,但是egg需要export default app => { app.model.define() }这里应该怎么改造呢?

import * as Sequelize from 'sequelize';
import { DataTypes, Model, Optional } from 'sequelize';

export interface ProductNameAttributes {
  id: number;
  productId: number;
  name: string;
  createdAt?: Date;
  updatedAt?: Date;
  deletedAt?: Date;
}

export type ProductNamePk = "id";
export type ProductNameId = ProductName[ProductNamePk];
export type ProductNameOptionalAttributes = "id" | "createdAt" | "updatedAt" | "deletedAt";
export type ProductNameCreationAttributes = Optional<ProductNameAttributes, ProductNameOptionalAttributes>;

export class ProductName extends Model<ProductNameAttributes, ProductNameCreationAttributes> implements ProductNameAttributes {
  id!: number;
  productId!: number;
  name!: string;
  createdAt?: Date;
  updatedAt?: Date;
  deletedAt?: Date;

  static initModel(sequelize: Sequelize.Sequelize): typeof ProductName {
    ProductName.init({
    id: {
      autoIncrement: true,
      type: DataTypes.INTEGER.UNSIGNED,
      allowNull: false,
      primaryKey: true,
      comment: "主键"
    },
    productId: {
      type: DataTypes.INTEGER,
      allowNull: false,
      comment: "商品ID",
      field: 'product_id'
    },
    name: {
      type: DataTypes.STRING(64),
      allowNull: false,
      comment: "商品名称"
    },
    createdAt: {
      type: DataTypes.DATE,
      allowNull: true,
      comment: "创建时间",
      field: 'created_at'
    },
    updatedAt: {
      type: DataTypes.DATE,
      allowNull: true,
      comment: "更新时间",
      field: 'updated_at'
    }
  }, {
    sequelize,
    tableName: 'product_name',
    timestamps: true,
    paranoid: true,
    deletedAt: 'deleted_at',
    indexes: [
      {
        name: "PRIMARY",
        unique: true,
        using: "BTREE",
        fields: [
          { name: "id" },
        ]
      },
      {
        name: "idx_product_id",
        using: "BTREE",
        fields: [
          { name: "product_id" },
        ]
      },
    ]
  });
  return ProductName;
  }
}
syymo commented 2 years ago

没人回答?