Skip to content

Public Class Fields in Sequelize V6 #719

@mfvargo

Description

@mfvargo

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;
  ^^^^^^
...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions