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

Feature Request: Support forFeatureAsync to handle Hooks with injectables #394

Open
nartc opened this issue Dec 14, 2020 · 6 comments
Open

Comments

@nartc
Copy link

nartc commented Dec 14, 2020

Same as nestjs/mongoose https://docs.nestjs.com/techniques/mongodb#hooks-middleware

This will allow for something like the following:

TypegooseModule.forFeatureAsync([
   {
      // some properties here,
      inject: [authConfiguration.KEY],
      useFactory: (authConfig: AuthConfig) => {
         const schema = someMethodToGetSchema();
         schema.pre('save', function(next) {
            if (this.isModified('password')) {
               this.password = someHashFunction(authConfig.salt)
               next()
            }
         })
         return schema;
      }
   }
])
@smolinari
Copy link

Question: isn't the facility in Typegoose for using hooks not enough?

Scott

@nartc
Copy link
Author

nartc commented Mar 5, 2021

@smolinari is the question directed to me? if yes, then no, hooks alone aren’t enough. This feature request allows for using Dependency Injection configurations in the hooks which isn’t possible atm.

@smolinari
Copy link

Ah, Gotcha. I would have put that password hashing logic in a service anyway. But, I guess that's just me. 😁

Scott

@nartc
Copy link
Author

nartc commented Mar 5, 2021

@smolinari Right. I used to do the same but then the logic gets repetitive (while I also tried to DRY it).

There are some different use-cases like post-processing some field changed. Eg: a wysiwyg editor sending raw markdown to the backend, the backend post-processes the markdown and saves html in the db. Whenever this field gets updated, run the post-processing again. Stuffs like this make a lot of sense in a format of hooks and being able to inject services when constructing hooks would be a lot nicer, and the logic is encapsulated.

@87789771
Copy link

@nartc Hello, how did you solve it?

@smolinari
Copy link

smolinari commented Oct 26, 2021

@87789771 - You do the hashing in either the User or Auth services, where it actually belongs.

Scott

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants