Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Repository like pattern with Node and mongoose

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?

like image 292
Élodie Petit Avatar asked May 16 '12 08:05

Élodie Petit


People also ask

Is Mongoose an ORM or ODM?

Mongoose is an ODM that provides a straightforward and schema-based solution to model your application data on top of MongoDB's native drivers.

Is it mandatory to use Mongoose with Node application?

It's not mandatory to use Mongoose over the MongoDB Native API. However, there are some benefits to doing so.

What is Repository pattern in node JS?

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 ...

Is Mongoose a good node?

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.


1 Answers

That's a nice way to go, although I encourage you to put more layers:

  • Service
  • Controller

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.

like image 69
Bruno Fuster Avatar answered Sep 20 '22 08:09

Bruno Fuster