pinia-orm: Mutators appear to be broken. Works on initial insert/save but not on updates
Environment
No response
Reproduction
Please see description below
Describe the bug
I’ve created a very simple mutator that encrypts my string. The method of encryption itself does not matter in the context of this bug report.
static mutators() {
return {
content: {
set: (value) => {
const encryptedValue = encryptData(value);
console.log('content.set', value, encryptedValue);
return encryptedValue;
},
},
};
}
When I create a new record:
await useRepo(Requirement).save({
id: 1,
content: this.form.content,
});
Then everything works fine. I can trace that my set mutator is being visited, the plainText is encrypted, and if I view my content with
const requirement = useRepo(Requirement).find(1);
console.log('requirement.content', requirement.content);
Then the value is indeed encrypted and life is good.
If I then update this existing requirement
Whether like this:
await useRepo(Requirement)
.find(1)
.update({
content: this.form.content,
});
Or like this
await useRepo(Requirement).save({
id: 1,
content: this.form.content,
});
Then again I can trace that my set mutator is being visited. The set mutator does the encryption, however unlike when I created a new record, this time if I inspect the value with
const requirement = useRepo(Requirement).find(1);
console.log('requirement.content', requirement.content);
This time the value is plainText. It is not the encrypted value. So the mutator is “used” but somehow not applied on updates, only on inserts.
Additional context
No response
Logs
No response
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 24 (11 by maintainers)
Commits related to this issue
- fix(pinia-orm): Get mutators were wrong for updated models (#1824) * fix(pinia-orm): Get mutators were wrong for updated models * refactor(pinia-orm): linting related #1818 — committed to CodeDredd/pinia-orm by CodeDredd 3 years ago
- fix(pinia-orm): Get mutator was called twice (#1827) related #1818 — committed to CodeDredd/pinia-orm by CodeDredd 3 years ago
- fix(pinia-orm): Mutators still don't apply on update consistently (#1828) * fix(pinia-orm): Get mutator was called twice * fix(pinia-orm): Mutators still don't apply on update consistently * re... — committed to CodeDredd/pinia-orm by CodeDredd 3 years ago
- fix(pinia-orm): Updates were wrong for hydrated cache for casts & mutators (#1823) * fix(pinia-orm): Updates were wrong for hydrated cache for casts & mutators * refactor(pinia-orm): linting cl... — committed to dev1437/pinia-orm by CodeDredd 3 years ago
🚀 Release triggered! You can now install pinia-orm@npm:pinia-orm-edge@1.9.0-28580088.76eb4a5
@vesper8 this bug is annoying…but well now i have great hope that it’s fixed. You can try out the new patch.
And again thanks for testing.
Hey @CodeDredd. Thanks for staying on top of this!
I can confirm that
1.9.0-28580767.64b3b57introduces a new problem. It appears to be going through the getter twice now.So in this example (Custom Cast)
After doing an initial insert with the string “test”, you would expect the value to be
test (encrypted) (decrypted)But instead you get
test (encrypted) (decrypted) (decrypted)In my previous example, with a encrypting/decrypting method
Well I’m just getting an empty value every time because it correctly encrypts on set, and then it tries to decrypt twice, the 2nd time fails and returns empty.
I guess you’re not catching this because you’re using toLowerCase/toUpperCase in your tests.
@CodeDredd it seems this issue may not be entirely solved yet.
In trying to decide how to approach my specific problem (which I’d appreciate your advice on, see below) I may have uncovered another issue.
I’m trying to make pinia-orm handle my encryption/decryption of specific fields on specific models so that the value inside pinia-orm is always stored encrypted, but is automatically decrypted when displayed on the web app.
Another very important point is that whenever this data is persisted to the backend, it should always transfer the encrypted values.
I’m using a custom cast to achieve this:
This works on create, but it makes it very hard for me to A) confirm that the data is stored encrypted and B) access the encrypted value. Is there a way to circumvent the getter here if I want to access the raw value, for example when it’s time to persist to the backend using the pinia-orm axios plugin?
This also exposes the remaining issue… it seems to be the same issue as you fixed with the setter… but it’s also happening with the getter. The getter will return the decrypted value correctly on create, but on update it just seems to ignore the getter and returns the encrypted value instead.
This led me to experiment with a different approach:
But in this case the same issue occurs and the getter only returns the object format
{ "encrypted": "3Xelj+wXrRlrDjaRjf0ewNoOFYtETHK46gPjEnOTs8E=", "decrypted": "test" }on the initial create and not after an update.Thank you! It works
Going to do some testing now see if this introduced any regressions. I can confirm the problem raised in this issue is now working though so that’s great.
@vesper8 Don’t take the edge directly for testing. Here is way how to do it…i added it also to the docs. Just need to rebuild the docs again.
Update
pinia-ormdependency insidepackage.json:Remove lockfile (
package-lock.json,yarn.lock, orpnpm-lock.yaml) and reinstall dependencies.Opting out from the edge channel
Update
pinia-ormdependency insidepackage.json:@CodeDredd Thanks! Really so happy that you’re back at work on this. Looking forward to 1.9.0 stable!
For now I tried installing the edge releases:
But now I’m getting inundated with these errors when I try to start my project
Is that because I would have to replace all my imports from
pinia-ormtopinia-orm-edge? That’s hundreds of replacements… my Git history wouldn’t love that…You are right. The hydration cache is not correctly updating for updates. That’s the reason why all this is not working. As a workaround you could have done this before every update ->
useRepo(Model).hydratedDataCache.clear().Fix is on the way. You can test it out with the new edge release if you like. 😉(Posting it here after merge) Release will be later