What is a Monorepos and quickly create a Monorepos with Nx

Tram Ho

Microservices are currently mentioned in the world of software and technology that are highly expected and evaluated as a trend for the future (Open API, service provider, …). Some opinions say that microservices are nothing new, it is just a polished, renamed SOA (service-oriented architecture). Although implementing microservices as a multi-repo has many benefits, in some ways implementing this code can be difficult in some cases and instead of implementing the underlying code structure. divided into many separate repos, they choose to organize only in a single repository or monorepo. This article will cover these two implementations of the source code as well as compare their characteristics, and finally an introduction to Nx – an introduced development tool that is commonly used for extensibility. Monorepos.

Multiple repositories and Mono repositories

Image from the article: https://levelup.gitconnected.com/moving-from-multiple-repositories-to-a-lerna-js-mono-repo-faa97aeee35b

Not every application is developed as microservices. However, whether we have to refactor the code from a monolith or not, we will have to choose how to store and manage the code in the form of multiple repositories (multirepo) or mono repositories (monorepo). A summary of these two types of storage and management is as follows:

  • Monorepo is a type of project structure in which all modules (or subprojects) are in the same git repository.
  • Multirepo, in contrast, is a type of project structure in which each module (or subproject) is contained in individual git repositories.

Undeniably, the storage and management of source code has a number of advantages as follows:

  • Flexible source code styles and easy testing for each repositories
  • Clear decentralization for development and maintenance participants
  • Codebase is not so large that it is easy to manage

However, software development is often geared towards an overall application. Then, breaking the application into separate parts can cause confusion about the scope as well as the purpose and the way modules work between teams in the entire organization. Teams then become fragmented and may not know the main goal of the project they are participating in and therefore don’t mind what others are doing as long as their work is done. Besides, some claims that this way of storing and managing this takes a lot of time and resources along with that, in some cases, parts of the application use the same language. Flexible source styles are no longer important. Therefore, they believe that this problem as well as some of the above and some others not mentioned can be solved by using a different method – monorepo.

Over the past few years Babel, Angular, React, Jest and many other open source projects have switched to monorepos. Monorepos is not only useful for open source projects. They are even more suitable for organizations that are developing real-world applications. Google, Facebook, Uber, and Twitter all do that, and they all have easy-to-customize developer tools that offer a ton of functionality. Not natural, monorepo is used so much and the following are some of the advantages that can be mentioned:

  • Seamless organization: With mono repo, projects can be organized and grouped together in whatever way you find to be the most logically consistent. Using a single repo also reduces the cost of managing your dependencies.
  • Improve teamwork culture: With the adoption of a mono repo, every individual in the organization is aware of the application’s goals and this makes the whole organization a coherent team. that can contribute more specifically to the goals and objectives of the organization.
  • Better coordination between developers: Developers can easily run the entire platform on their machine, and this helps them understand all services and how they work together. This caused the developers to find more local errors before sending a pull request.
  • Easy Refactoring: Anytime we want to rename something, refactoring becomes incredibly easy. Restructuring is also easier because everything is neat in one place and easier to understand.

Note that the implementation as a mono-repo is not identical to the monolithic architecture.

In short, monorepos not only maintains most of the advantages when deployed as microservices but also simplifies code sharing and cross project refactoring, they significantly reduce the cost of creating libs, microservices, and microfrontends. . Therefore, adopting monorepo often allows for more flexible deployment.

Tools Nx

General introduction

Nx is a set of extensible dev tools for monorepos, which helps you develop like Google, Facebook, and Microsoft.

Through the brief introduction we can know through that Nx is a set of tools used to create monorepos. According to the development team, Nx learns to develop based on their experiences in the process of working in large corporations such as Google, Facebook and Microsoft. Therefore, they expect that Nx will provide a powerful tool to help programmers create and develop systems easily with a variety of frontend and backend technologies available today.

Due to specific development for Javascript language, Nx currently supports Angular, React and Node.js. However, it is possible that in the near future, Nx may support other frameworks such as Vue or Svelte, …

Use

Publishing uses the concept of workspace to refer to an entire application that is deployed in monorepos. So when starting to use we create a new workspace using npx as follows:

In addition to using npx we can use npm init using npm init nx-workspace or yarn create with yarn create nx-workspace . Immediately after running the above command, Nx will ask us some things such as if we want to create a workspace according to any available template, … as follows:

After choosing an existing template that suits our needs, Nx will create a new workspace for us. Here I choose wokspace under react model to try using Nx CLI – a tool provided by Nx to manage workspaces that people can install by using the command npm install -g nx To be able to run the application locally, we can use the npx nx serve demo command

Add plugins for your workspace

Nx is an open platform with plugins for many modern frameworks and tools. So we can use the npx nx list to find out if Nx already supports the frameworks you want to use.

The results show that not only does Nx itself support a lot of frameworks, but it also has plugins made by the community to be able to use other frameworks, but Nx doesn’t have an official plugin yet. Usually each application comes with a ‘backend’ so in this example we’ll use express by running npm install --save-dev @nrwl/express and using the following command:

Right after that, Nx CLI will give us the api directory and the result is as follows:

It can be seen that the above process in addition to creating the directory api also modifies the contents of some files such as the workspace.json file. Open the workspace.json file, we can see that a number of entries have been added, one of which is proxyConfig pointing to the proxy.conf.json file. You can see that this section circulates the proxy configuration for the part of the frontend code that can communicate with the backend.

Manage plugins with Dep graph

Usually an application has only one frontend and one backend part or add some backend like e2e testing. However, the development team directs users to break down their application into modules in a workspace (possibly in the form of micro frontend and micoservices) as well as create libraries and share code between modules for optimization. source code, avoid repetition. Therefore, when the number of modules will be very large, manual management is no longer effective. Therefore, Nx CLI provides Dep Graph or Dependencies Graph in order to visualize the relationships between modules. After using the following command npx nx dep-graph we can see a dependencies graph shown in the browser as follows:

summary

This article talks about these two implementations of the source code as well as comparing their characteristics, and finally an introduction to Nx – a commonly used development tool Monorepos. It can be seen that framing a complete web application usually takes a lot of time and effort. Therefore, a number of tools such as Nx were created to simplify the process as well as to manage the application in the future, and to structure according to the current optimal methods. Thank you everyone for taking the time to read.

Share the news now

Source : Viblo