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

Public Class Fields in Sequelize V6 #719

Open
mfvargo opened this issue Oct 18, 2024 · 2 comments
Open

Public Class Fields in Sequelize V6 #719

mfvargo opened this issue Oct 18, 2024 · 2 comments

Comments

@mfvargo
Copy link

mfvargo commented Oct 18, 2024

Example models in this plugin will not work correctly with Sequelize v6 as per
https://sequelize.org/docs/v6/core-concepts/model-basics/#caveat-with-public-class-fields

Caveat with Public Class Fields
Adding a Public Class Field with the same name as one of the model's attribute is going to cause issues. Sequelize adds a getter & a setter for each attribute defined through Model.init. Adding a Public Class Field will shadow those getter and setters, blocking access to the model's actual data.

The models need a declare before the field name so the type can be specified without colliding with the inferred model getter/setter from the Sequelize base Model.

@Table({ tableName: "users", paranoid: true })
export class User extends Model {
  saltRounds = 10;

  @Column({ primaryKey: true })
  guid: string;

  @AllowNull(false)
  @Column
  firstName: string;

...

should be

@Table({ tableName: "users", paranoid: true })
export class User extends Model {
  saltRounds = 10;

  @Column({ primaryKey: true })
  declare guid: string;
  ^^^^^^
  @AllowNull(false)
  @Column
  declare firstName: string;
  ^^^^^^
...
@evantahler
Copy link
Member

Looks like a conflict between seqelize and sequelize-typescript. I wonder if we don't need sequelize-typescript any more?

@mfvargo
Copy link
Author

mfvargo commented Oct 18, 2024

I did a quick attempt by

  1. comment out the @column line for a simple fields called name on one of my models. That caused other spots in the model definition to crash when trying to access thing.name as undefined.
  2. I then tried to just comment out the @column before the declare but left the declare in. This let me access thing.name but it was shadowing the setter/getter from sequelize so the value was not getting saved to db.

I will try to define a model without sequelize-typescript to see if I still get typed returns etc. I have to pick an easy one without many foreign keys and hooks.

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

No branches or pull requests

2 participants