Skip to content

Commit

Permalink
Fix missing override of the default SequelizeModel.toJSON() method
Browse files Browse the repository at this point in the history
  • Loading branch information
Davide Caroselli committed Apr 20, 2024
1 parent afb46aa commit ca2ed69
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -299,20 +299,20 @@ export class User extends Model<InferAttributes<User>, InferCreationAttributes<U
declare password: string;
}

const jsonthis = new Jsonthis({sequelize});
const jsonthis = new Jsonthis({case: "snake", sequelize});

const user = await User.create({
id: 1,
email: "[email protected]",
password: "s3cret"
});

console.log(user.toJSON());
console.log(user.toJSON()); // or jsonthis.toJson(user)
// {
// id: 1,
// email: 'j******[email protected]',
// updatedAt: 2024-04-13T18:00:20.909Z,
// createdAt: 2024-04-13T18:00:20.909Z
// updated_at: 2024-04-20T12:58:10.229Z,
// created_at: 2024-04-20T12:58:10.229Z
// }
```

Expand Down
5 changes: 5 additions & 0 deletions src/jsonthis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,11 @@ export class Jsonthis {
this.registerGlobalSerializer(model, (model: Model, state: JsonTraversalState, options?: ToJsonOptions) => {
return this.toJson(model.get(), options, state, schema);
});

const jsonthis = this;
model.prototype.toJSON = function () {
return jsonthis.toJson(this);
}
}

}
Expand Down

0 comments on commit ca2ed69

Please sign in to comment.