setter and getter sequelize code example
Example 1: getter and setter sequelize simple way
const someObj = {foo: 'bar'}
someObj.foo
someObj.foo = 'baz'
Example 2: getter and setter sequelize simple way
const User = sequelize.define('user', {
firstName: DataTypes.TEXT,
lastName: DataTypes.TEXT,
fullName: {
type: DataTypes.VIRTUAL,
get() {
return `${this.firstName} ${this.lastName}`;
},
set(value) {
throw new Error('Do not try to set the `fullName` value!');
}
}
});
const user = await User.create({ firstName: 'John', lastName: 'Doe' });
console.log(user.fullName);