Hey there! I have some difficulties with the property setters and getters in my model.
I want to use the getters and setters defined in the models to encrypt and decrypt some data in my database. But during implementation I noticed, that the setters get triggered on update as well as creation but only have an real effect on creation. So creating an entry calls the setter and the value of (see the code) "secret" stands encrypted in the database. But if i want to update this entry, the setter gets called but nothing gets updated.
If I update also values which have no setter function defined for them only this values get updated.
This is my setter:
secret: {
type: DataTypes.STRING,
defaultValue: null,
async get() {
try {
return await crypt.decryptGetter(this, 'secret');
} catch (err) {
throw new Error("Error while decrypting secret: " + err);
}
},
async set(value) {
try {
return await crypt.encryptSetter(this, 'secret', value);
} catch (err) {
throw new Error("Error while encrypting secret: " + err);
}
}
}
I use this setters and getters more often, that's why I outsourced them. I pass the current instance of the model as well as the key and the value if necessary:
async decryptGetter(sequelizeModel, key) {
try {
return await module.exports.decrypt(sequelizeModel.getDataValue(key),config.internal.secret)
} catch (err) {
throw new Error("GETTER: " + err, __filename);
}
},
async encryptSetter(sequelizeModel, key, value) {
try {
let encValue = await module.exports.encrypt(value, config.internal.secret);
return sequelizeModel.setDataValue(key, encValue);
} catch (err) {
throw new Error("SETTER: " + err, __filename);
}
}
Can anybody help me out here? Did I miss something along the way? Thank you in advance!