Storz icon indicating copy to clipboard operation
Storz copied to clipboard

💡 [REQUEST] - Decoupling services

Open devrrior opened this issue 2 years ago • 0 comments

Start Date

No response

Implementation PR

No response

Reference Issues

No response

Summary

Create new files where we insert business logic from routes. I mean, pull that blocks of code where it's used mongoose's models out and put it in services files. If you agree with this request, I'd appreciate you can assign me this task.

Basic Example

A common example is when a User is created, that logic which we used, encapsulates in a function from service, then call it in the routes/services. In that way, we are just concerned about calling the function and not how it works.

Example:

Before

const user = new User({
            magic_id: magic_id,
            email: email,
            user_name: user_name,
            encryption_key: encryption_key,
            files: []
        })
        await user.save();

After

const user = UserService.create(magic_id, email, user_name, encryption_key, files);

Drawbacks

Decoupling services are essential for the app, in that way we can become the code more scalable and easier for testing, if the controller handled the whole application's logic and the request-handling logic, it would start to become really hard to test. Furthermore if at some point we want to migrate our database just we would have to create a new file and write the logic with the new database.

Unresolved questions

No response

devrrior avatar Oct 11 '22 03:10 devrrior