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

Typescript Model missing field CreatedAt and UpdatedAt type error #625

Open
bzzanui opened this issue Oct 28, 2022 · 5 comments · May be fixed by #626
Open

Typescript Model missing field CreatedAt and UpdatedAt type error #625

bzzanui opened this issue Oct 28, 2022 · 5 comments · May be fixed by #626

Comments

@bzzanui
Copy link

bzzanui commented Oct 28, 2022

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

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

lptai added a commit to lptai/sequelize-auto that referenced this issue Nov 2, 2022
@lptai
Copy link

lptai commented Nov 2, 2022

@steveschmitt I created a PR for this. Can you help to review? thanks
#626

@christopher-caldwell
Copy link

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

@rachid-debu-prbob
Copy link

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

bzzanui commented Dec 14, 2022

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

9renpoto pushed a commit to 9renpoto/sequelize-auto that referenced this issue Dec 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants