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

fix: use custom decorator on column have a property descriptor #1070

Merged
merged 3 commits into from
Jan 3, 2022

Conversation

caolvchong
Copy link
Contributor

@caolvchong caolvchong commented Aug 6, 2021

we add custom decorator(@CustomField) for a column to change it to be an accessor. for example:

function CustomField(target: any, key: string): void {
  Object.defineProperty(target, key, {
    configurable: true,
    enumerable: true,
    get() {
      const value = this.getDataValue(key);
      return `prefix/${value}`;
    },
    set(value: string): void {
      const _value = value?.replace(/^prefix/, '');
      this.setDataValue(key, _value);
    }
  });
}
@Table
export class ModelA extends Model {
  @Column
  name: string;

  @Column({
    type: DataType.VIRTUAL
  })
  @CustomField
  bio: string;
}
@Table
export class ModelB extends Model {
  @PrimaryKey
  @AutoIncrement
  @Column
  id: number;

  @Column({
    type: DataType.VIRTUAL
  })
  @CustomField
  bio: string;
}

the @Column decorator will lose propertyDescriptor(because class field decorator does not hava descriptor)

@caolvchong caolvchong changed the title fix: use custom decorator to change a column to be an accessor will l… fix: use custom decorator to change a column to be an accessor will lose property descriptor Aug 6, 2021
@jagged3dge
Copy link

Apologies for the errant review.

Tagging @RobinBuschmann for review

Copy link
Contributor

@theoludwig theoludwig left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!
Thanks for your contribution! 👍

@codecov
Copy link

codecov bot commented Jan 3, 2022

Codecov Report

Merging #1070 (11487e9) into master (f16baa4) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #1070   +/-   ##
=======================================
  Coverage   95.11%   95.11%           
=======================================
  Files         117      117           
  Lines        1125     1126    +1     
  Branches      128      129    +1     
=======================================
+ Hits         1070     1071    +1     
  Misses         23       23           
  Partials       32       32           
Impacted Files Coverage Δ
src/model/column/column.ts 96.00% <100.00%> (+0.16%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update f16baa4...11487e9. Read the comment docs.

@theoludwig theoludwig changed the title fix: use custom decorator to change a column to be an accessor will lose property descriptor fix: use custom decorator on column have a property descriptor Jan 3, 2022
@theoludwig theoludwig merged commit 7ce03de into sequelize:master Jan 3, 2022
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 this pull request may close these issues.

3 participants