This is a follow-up post to Migrating Our Largest Package into Smaller Libraries, published on December 9, 2021.
In a previous blog post in our 2021 Mixmax Advent series, we talked about how we split up our Salesforce module into smaller, more digestible components as the first step in our quest to accelerate Salesforce development. In this post, we'll talk more about custom Nx generators and how we're using them to create microservices with a single command, which used to be a manual and error-prone process at Mixmax.
What are Custom Nx Generators?
From the Nx documentation:
“Generators provide a way to automate many tasks you regularly perform as part of your development workflow. Whether it is scaffolding out components, features, ensuring libraries are generated and structured in a certain way, or updating your configuration files, generators help you standardize these tasks in a consistent, and predictable manner.”
For Mixmax, Nx generators are powerful because you can chain them together, which we'll talk more about next.
Custom Generators at Mixmax
At the time of writing this post, we've defined five custom generators at Mixmax. The first two are called:
- config-generator, and
- library-config-generator
These two generators yield Jest and TypeScript config files. In practice, we don't actually invoke these generators individually, but rather make use of them inside the next three custom library and service generators, called:
- local-library,
- npm-package, and
- service-with-lb
These generators yield Nx libraries, npm modules, and AWS ECS services, respectively. As described, we make use of the config generators to create config files while simultaneously bootstrapping a module or service. In particular, the service-with-lb generator composes the config-generator and library-config-generator with our terraform-fargate-service-with-lb Terraform module, which lets us initialize microservices with a single command.
service-with-lb
The service-with-lb generator is structured as shown:
Let’s break it down directory by directory.
- deploy: Contains ECS task definition config files
- e2e: Contains integration tests for the service
- src: Contains the service's application code
- terraform: Contains the Terraform files that import the terraform-fargate-service-with-lb module, alongside the various AWS resources that are relevant to each microservice (IAM policy attachment, security group rules, etc.)
Each file inside the generator is templated using EJS template syntax to substitute the new microservice's name. As a result, all we need to do to initialize a microservice is run the following command:
```
npx nx workspace-generator service-with-lb new-salesforce-service --minCapacity=2 --maxCapacity=4
```
Hopefully this post showed you how easy it is to use and compose Nx generators. If you're interested in streamlining development processes just like we did with Nx, check out our open positions!