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

Most upvoted comments

🚀 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.64b3b57 introduces a new problem. It appears to be going through the getter twice now.

So in this example (Custom Cast)


  get(value) {
    return value + ' (decrypted)';
  }

  set(value) {
    return value + ' (encrypted)';
  }

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

  get(value) {
    return decryptData(value);
  }

  set(value) {
    return encryptData(value);
  }

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:


import { CastAttribute } from 'pinia-orm';

import { encryptData, decryptData } from '@/services/encryptionService';

export class EncryptedCast extends CastAttribute {
  constructor(attributes) {
    super(attributes);
  }

  get(value) {
    return decryptData(value);
  }

  set(value) {
    return encryptData(value);
  }
}

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:


import { CastAttribute } from 'pinia-orm';

import { encryptData, decryptData } from '@/services/encryptionService';

export class EncryptedCast extends CastAttribute {
  constructor(attributes) {
    super(attributes);
  }

  get(value) {
    return {
      encrypted: value,
      decrypted: decryptData(value),
    };
  }

  set(value) {
    return encryptData(value);
  }
}

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

"@pinia-orm/axios": "npm:@pinia-orm/axios-edge@latest",
"pinia-orm": "npm:pinia-orm-edge@latest",

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-orm dependency inside package.json:

{
  "devDependencies": {
-   "pinia-orm": "^1.0.0"
+   "pinia-orm": "npm:pinia-orm-edge@latest"
  }
}

Remove lockfile (package-lock.json, yarn.lock, or pnpm-lock.yaml) and reinstall dependencies.

Opting out from the edge channel

Update pinia-orm dependency inside package.json:

{
  "devDependencies": {
-   "pinia-orm": "npm:pinia-orm-edge@latest"
+   "pinia-orm": "^1.0.0"
  }
}

@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:

"@pinia-orm/axios-edge": "latest",

"pinia-orm-edge": "latest",

But now I’m getting inundated with these errors when I try to start my project

1:43:17 PM [vite] Pre-transform error: Failed to resolve import "pinia-orm" from "src/pinia/orm/models/User.js". Does the file exist?

Is that because I would have to replace all my imports from pinia-orm to pinia-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