0.4.0 • Published 1 year ago

user-identity-service v0.4.0

Weekly downloads
-
License
ISC
Repository
-
Last release
1 year ago

Microservice

Microservice boilerplate built with Fastify

Table of contents

TOC

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:

  1. Install the packages with pnpm i
  2. Create .env file from .env.example (you can also check envalid plugin to see the default envs for development)
  3. 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 app
  • start: to start the app

Development

For development, we're using ts-node-dev

  • dev: to run the app in development
  • dev:debug: to run the app in the debug mode

Test

For testing, we're using Vitest

Lint

For linting, we're using ESlint

  • lint: to show the linter errors and warnings using eslint
  • lint: to fix the auto-fixable linter errors and warnings
  • lint-staged: to run lint-staged scripts which is useful in husky's pre-commit hook

Type check

  • typecheck: to run type checking on the whole codebase using tsc

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