Aman Chawla
Aman Chawla

Reputation: 68

loopback4 Project Structure

I come from express.js background and pretty new to loopback framework, especially loopback4 which i am using for my current project. I have gone through the loopback4 documentation few times and got some good progress in setting up the project. As the project is running as expected, I am not much convinced with project structure, Please help me to solve below problem,

As per docs, database operations should be in repositories and routes should be in controllers. Now suppose, My API consist lots of business logic along with database operations say thousand of lines. Which makes controllers routes difficult to maintain. More difficulty would arise, if some API demands version upgrade.

Is there any way to organise the code in controllers in more scalable and reusable manner? What if i add one more service layer between controllers and repositories and put business logic there? how to implement it in the correct way? Is there any official way to do that which is suggested by loopback community only?

Thanks in advance!!

Upvotes: 0

Views: 189

Answers (1)

Rifa Achrinza
Rifa Achrinza

Reputation: 1585

Is there any way to organise the code in controllers in more scalable and reusable manner?

Yes, services can be used to abstract complex logic into its own separate class(es). Once defined, the service can be injected into the dependent controller(s) which can then call the respective service functions.

How the service is designed is dependent on the user requirements as LoopBack 4 does not necessarily enforce a strict design requirement.

Upvotes: 1

Related Questions