I am developing my first application with node and mongoose and trying to structure the application with a repository pattern.
I have my application module, a router module using journey and a mongoose layer. I created a separate module for each of my mongo entities and exporting methods for CRUD operations acting like a repository.
Is this a nice way for structuring the application or should I follow another approach?
Mongoose is an ODM that provides a straightforward and schema-based solution to model your application data on top of MongoDB's native drivers.
It's not mandatory to use Mongoose over the MongoDB Native API. However, there are some benefits to doing so.
Talking about the Repository pattern, it is a representation where you can keep all operations of your database (like a Create, Read, Update and Delete Operations) in one local per Business Entity, when you need to do operations with Database, don't call directly database drivers and if you have more one database, or ...
Mongoose, a neat ODM library for MongoDB used in Node. js projects, has plenty of useful features that make developers' lives easier. It manages relationships between data, has schema validation, and overall allows coding 3-5 times faster.
That's a nice way to go, although I encourage you to put more layers:
Create controllers for common resources, eg: UserController.
You should also create an UserService instead of calling the repository directly from your controller. This will be really helpful if you need to do extra tasks besides using the repository only.
For example, you might need to add the user to an indexing service or save another data that's not related to the user repository.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With