Fastify boilerplate webservice is a starting point for creating microservice projects. It's designed to help you get up and running quickly, reducing the time spent on boilerplate setup.
- TypeScript Classes: The project leverages TypeScript classes and decorators for a clean and intuitive design.
- Logging Decorator: A custom logging decorator is used to reduce repetitive logging code and keep things DRY (Don't Repeat Yourself).
- Dependency Injection: Fastify plugins are used for dependency injection, providing a simple and effective way to manage dependencies.
- Style Guide: The project adheres to the Airbnb style guide for consistent and readable code.
- Database Management: Knex.js is used for database connection management, with PostgreSQL as the default database.
- Logging: Winston is used for logging, providing features like log rotation out of the box.
- Testable: The use of dependency injection makes the project highly testable. Jest and Supertest are used for unit testing.
- Swagger Docs: The project includes Swagger documentation, which reuses the JSON Schema files created for validation.
- Set up a PostgreSQL database.
- Rename
.env.example
to.env
and value your settings. - Install the dependencies with
npm install
. - Build the project with
npm run build
. - Start the project with
npm run start
.
This project uses dotenv
for managing its environment variables, a practice that is industry standard and works seamlessly with Docker Compose. The .env
file containing these variables is located at the root of the project.
Here are a few key points to keep in mind:
-
Application Startup: The environment variables are loaded into the application during startup, as handled in the
src/config.ts
file. -
Test Suite Startup: The environment variables are also loaded before the tests are run, as handled in the
jest.config.js
setupFiles section. -
Knex Commands: When running Knex commands via an npm script, the environment variables must be available. This is achieved by preloading
dotenv/config
using the-r
flag in Node.js. For example, a Knex command in an npm script might look like this:node -r dotenv/config ./node_modules/.bin/knex migrate:latest
.
- Performance: Fastify is designed to be the fastest framework in town - it's significantly faster than Express.js.
- Schema-based: Fastify uses JSON Schema to validate routes and serialize outputs, improving performance and reliability.
- Extendable: Fastify is fully extensible via its hooks, plugins, and decorators.
This is a boilerplate for a user management system, providing basic CRUD operations. It's designed to be flexible and adaptable to your needs.
- Feel free to swap out components. For example, you might prefer Pino over Winston, or MariaDB over PG.
- Contributions are welcome! Please fork this repository and submit a pull request.
- While the tests mock the data layer, this might be overkill for a simple project. It's included here for completeness.
- This boilerplate aims to accelerate your development process.
This project uses @fastify/awilix
for dependency injection, providing a robust and flexible way to manage dependencies. The data layer accepts the database, and the service layer takes in the data layer, all managed through the Awilix container and integrated into a Fastify plugin. This approach is suitable for both smaller microservices and more complex projects.
We have also adopted a named parameter best practice, ensuring that dependencies are clearly identified and injected by name, which enhances readability and maintainability.
For more information, refer to the fastify-awilix documentation.
Our logging system includes a special feature for data protection. It uses a 'sanitizer' in combination with a 'logging decorator'. The sanitizer refers to a list of property names that should be hidden in logs to protect sensitive information. You can find and modify this list in the sensitiveProps.ts
file.