Search code examples
node.jsmongodbnestjsmonorepo

Nest JS mono repo project structure - nest library to store models


I am setting up a new NestJS mono repo project with npm workspaces. I am planning to create various modules as micro-services. I am using MongoDB, and I need a central place to store the database schema so that I don't have to create duplicate schemas in all the individual micro-services. Is it a good idea to store the DB schemas as a nest js library and use it everywhere?


Solution

  • In theory, the microservice should be most independence and it's may use difference other languages, so you don't should centralize all schema in just one library/package. The duplicate code is can't be avoid in microservice system. You can see more other opinions at here.

    But everything policies or rules can change depend on developer and their team. If you really want to centralize your all schemas, I recommend reference Novu. This is open source that fit with your project, it use NestJS in mongorepo structure and centralize all schema in dal library.

    P/S: If you use GraphQL, I highly recommend don't centralize schema at one place. Why? Because you must use decorator @shareable that something make resolvers between other services is not working.