user-identity-service v0.4.0
Microservice
Microservice boilerplate built with Fastify
Table of contents
Folder structure
Stack
- Fastify Node.js web framework to manage server and routes.
- MongoDB NoSQL database to store and use data.
- OpenTelemetry A tracer to instrument, generate, collect, and export telemetry data.
- RabbitMQ A message-broker to be used for messaging.
- Redis In-memeory data structure to use as a cache database.
- Swagger Suite of tools for developing and describing RESTful APIs.
Setup
Requirements
Installation
First, make sure you have all the requirements installed and then follow these instructions:
- Install the packages with
pnpm i
- Create
.env
file from.env.example
(you can also check envalid plugin to see the default envs for development) - Run docker compose to setup the services with
docker compose up
Scripts
Build
For building the app, we're using tsc and tsc-alias
build
: to build the appstart
: to start the app
Development
For development, we're using ts-node-dev
dev
: to run the app in developmentdev:debug
: to run the app in the debug mode
Test
For testing, we're using Vitest
test
: to run the tests in development modetest:ui
: to run Vitest's UI on http://localhost:51204/__vitest__/test:coverage
: to have the tests with coveragetest:coverage:check
: to only export the coverage in text which is useful in husky's pre-commit hook
Lint
For linting, we're using ESlint
lint
: to show the linter errors and warnings usingeslint
lint
: to fix the auto-fixable linter errors and warningslint-staged
: to runlint-staged
scripts which is useful in husky's pre-commit hook
Type check
typecheck
: to run type checking on the whole codebase usingtsc
Publish a new version
For publishing a new version, we're using standard-version to generate a changelog and bump the package version based on the commit messages (Conventional Commits).
To publish a new version, run release
script.
Plugins
With Fastify plugins, we have a way to extend the functionalities of the app.
You can access the plugins in two ways:
// if you have access to app
app.pluginName
// if are inside a request
request.server.pluginName
Envalid
Validate environment variables and make them accessible on the app instance:
const { PORT } = app.getEnv();
or
// you can also get them with importing env directly from the plugin
import { env } from "./plugins/envalid";
const { PORT } = env
MongoDB
Connect to mongodb server and make the mongoose models' methods available on the app instance:
await app.db.User.find({ name: "john" });
OpenTelemetry
Connect to the tracer an make it available on the app instance:
const span = app.tracer.startSpan('baba boo');
// don't forget to end your span after starting one
span.end()
Redis
Connect to redis server and make it available on the app instance:
await app.redis.set(...)
Swagger
Set default options and make the documentation available on /documentation
1 year ago