Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

About asynchronous getters/setters again #4227

Open
bigslycat opened this issue Jun 13, 2016 · 13 comments
Open

About asynchronous getters/setters again #4227

bigslycat opened this issue Jun 13, 2016 · 13 comments
Labels
enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature

Comments

@bigslycat
Copy link

I have read the two discussions (#2571 and #517) that relate to this opportunity, and still do not see any reason to abandon it. Why not? Even if Mongoose should maintain compatibility with the ES5, this does not prevent to check that the setter returns to us. Watch this pseudo-code:

if (setterResult.constructor.name === 'Promise' && setterResult.then) {
  promisesArray.push(setterResult.then(
    value => {
      return {
        path: path,
        value: value
      };
    },
    err => {
      return err;
    }
  ));
}

// ...

if (promisesArray.length){
  return Promise.all(promisesArray).then(
    results => {
      for (let result of results) {
        // Do anything with it
      }
      // Do anything with all fields
      return this.save();
    },
    errors => {
      for (let err of errors) {
        // Do anything with it
      }
      return errors;
    }
  );
} else {
  // Do anything with all fields
  return this.save();
}

So why not? This is a very useful feature, and it will join naturally in existing.

@vkarpov15
Copy link
Collaborator

Yeah async setters would be useful. It's more difficult than you think, because the setter would need to resolve before doing validate(), and you'd need to return a promise from .set() because .set() can throw an error if there's a cast error.

@vkarpov15 vkarpov15 added this to the 4.7 milestone Jun 14, 2016
@vkarpov15 vkarpov15 added the enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature label Jun 14, 2016
@vkarpov15 vkarpov15 modified the milestones: 4.12, 4.10 Mar 23, 2017
@maxs15
Copy link

maxs15 commented Jul 24, 2017

Would love to see that coming in Mongoose !
Do you plan to implement that in a next release @vkarpov15 ?

@vkarpov15
Copy link
Collaborator

@maxs15 possibly. Still haven't investigated how we would implement this feature, so uncertain.

@c0d0g3n
Copy link
Contributor

c0d0g3n commented Sep 7, 2017

I've written a little plugin to mock async getters/setters until it lands in the core. It uses different properties (read instead of get and write instead of set) to simplify things and middleware (pre init and post save for getters and pre save for setters). It is by no means perfect (I'm especially not happy how it interacts with validators) but it gets the job done.

You can try it here and leave your opinion.

@vkarpov15
Copy link
Collaborator

I like this plugin but there's a few caveats, we'd also have to integrate this queries, because we also support doing things like findByIdAndUpdate(doc, doc)

@Ulrikop
Copy link

Ulrikop commented Mar 8, 2018

I would be happy to see that feature.

What's the status?

@Zwimber
Copy link

Zwimber commented Jul 10, 2018

+1

@bigslycat
Copy link
Author

May be we ready for PR? Oh I wait this so long. =)

@Matzu89
Copy link

Matzu89 commented Jan 30, 2019

Hi what is status on this?

Library worked great with mongoose 4.

@vkarpov15
Copy link
Collaborator

@Matzu89 what library?

@lvkins
Copy link

lvkins commented Feb 15, 2021

Any update on this?

@gaborszita
Copy link

This would be a great feature to add.

@sol-villar
Copy link

Resurrecting this old thread since it is still the latest for this issue, and trying to bump it for visibility.

For my use case, I have a couple of fields that need to be encrypted with libsodium before saving, and that needs to be done asynchronously. As of now I can't use neither get/set, nor a virtual getter/setter and had to settle on using a pre save method which is far from ideal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature
Projects
None yet
Development

No branches or pull requests

10 participants