sequelize / sequelize-auto

Automatically generate bare sequelize models from your database.
2.9k stars 527 forks source link

Typescript Model missing field CreatedAt and UpdatedAt type error #625

Open bzzanui opened 1 year ago

bzzanui commented 1 year ago

Like the doc said: https://sequelize.org/docs/v6/other-topics/typescript/

For typescript even those fields are sequelize managed timestamps you will still need the name in the Table.init function

createdAt: DataTypes.DATE,
updatedAt: DataTypes.DATE

in the node_modules/sequelize-auto/lib/auto-generator.js

        // ignore Sequelize standard fields
        const additional = this.options.additional;
        // if (additional && (additional.timestamps !== false) && (this.isTimestampField(field) || this.isParanoidField(field))) {
        //     return '';
        // }
        // adding the follow code to fix the type error
        if (additional && (additional.timestamps !== false) && this.isTimestampField(field) || this.isParanoidField(field)) {
            const fieldName = (0, types_1.recase)(this.options.caseProp, field);
            return this.space[2] + this.quoteName(fieldName) + ": DataTypes.DATE,\n";
        }

Also as the doc recommend Model property should be declare not real property to prevent accidentally set the value from property not from DB ORM such as this, can we add the declare instead of property?

class Project extends Model<
  InferAttributes<Project>,
  InferCreationAttributes<Project>
> {
  // id can be undefined during creation when using `autoIncrement`
  declare id: CreationOptional<number>;

  // foreign keys are automatically added by associations methods (like Project.belongsTo)
  // by branding them using the `ForeignKey` type, `Project.init` will know it does not need to
  // display an error if ownerId is missing.
  declare ownerId: ForeignKey<User['id']>;
  declare name: string;

  // `owner` is an eagerly-loaded association.
  // We tag it as `NonAttribute`
  declare owner?: NonAttribute<User>;

  // createdAt can be undefined during creation
  declare createdAt: CreationOptional<Date>;
  // updatedAt can be undefined during creation
  declare updatedAt: CreationOptional<Date>;
}
christopher-caldwell commented 1 year ago

Having the same issue. I have to add them manually each time.

lptai commented 1 year ago

@steveschmitt I created a PR for this. Can you help to review? thanks https://github.com/sequelize/sequelize-auto/pull/626

christopher-caldwell commented 1 year ago

@lptai Your PR doesn't seem to address the declare issue.

rachid-debu-prbob commented 1 year ago

currently working on a fork for the same issue https://github.com/rachid-debu-prbob/sequelize-auto-bob i'm also adding a caseAlias property to customize aliases casing.

bzzanui commented 1 year ago

Hi All,

I don't have time to fix all the source code of this repo to create PR for this issue but I do have a patch to fix this issue in version 0.8.8

https://github.com/benzhanghf/sequelize-auto/blob/master/patches/sequelize-auto%2B0.8.8.patch

You can download the patch file and put in your project /patches/sequelize-auto+0.8.8.patch Then let your npm install to patch it.

Hope this will help anyone using this library to auto generate exist DB to sequelize model in typescript