Fastify provides high-performance web framework capabilities and is well-suited for building scalable APIs. It's lightweight and designed to handle a high volume of requests.
TypeScript provides one of the best developer experiences in ${currentyear}
, developers can catch errors during compilation rather than runtime, making it easier to catch issues early on in the development process. Additionally, It's strict typing system allows for easier maintenance of the codebase, making it easier to read and refactor code.
Tyepscript - Fastify - PrismaJS - Docker - Swagger - MongoDB - PM2 - FakerJS
@fastify/compress - @fastify/cors - @fastify/env - @fastify/helmet - @fastify/swagger - @fastify/swagger-ui
- Setup & Configuration
- Running locally
- Databases & MongoDB for development
- File Structure
- Running as a Docker container
- Building for production
- Debugging
- Hot-Reloading
Clone the repository:
git clone https://github.com/danielm/fastify-prisma-swagger-rest-boilerplate.git
Create a .env
file from .env.example
and tweak it as necessary.
Some options need some tweaking if running locally or using docker. Read more bellow 👇
Make sure your .env
file has the right settings, these in particular:
# ...
BIND_PORT=5000
BIND_ADDR=127.0.0.1
DATABASE_URL=mongodb://USERNAME:PASSWORD@HOST:PORT/DATABASE
# ...
Check this section bellow to quickly spin up locally a MongoDB instance for development
Now, make sure you have installed Node.js in any recent/lts version.
# Install all Dev-included dependencies
npm install
# Generates Prisma cliente metadata/types stuff
npx prisma generate
Running the project is simple as:
npm run start
Now you should be able access the project:
- APIs: http://127.0.0.1:5000/api/v1/*
- SwaggerUI documentation: http://127.0.0.1:5000/docs/
Note: Inside the
docs/
you can download a Postman collection to import and play with the example APIs
# Very nice UI for data visualization of our database
npx prisma studio
# Synchronize your Prisma schema with your database
npx prisma db push
# Seed our database with a bunch of random data
npx prisma db seed
Since the project is using by default MongoDB, and It can be a little tricky to setup a replica set for just development:
I've included in this project a quick way to spin up a single replica node, just by running:
make mongo
This will bring up a MongoDB instance using Docker. See .env.example
for customizing some options.
├── prisma
│ ├── schema.prisma // Prisma JS DB models/schemas
│ └── seed.ts // Random data generator using FakerJS
└── src
├── app.ts
├── config // Lots of config for fastify and plugins
├── controllers
├── index.ts // Main entrypoints
├── lib // Helper functions
├── plugins // Custom plugins
├── routes
└── types // Typescript types and extensions
The boilerplate includes an example of the following schema:
+-------------+ +--------------+
| Category | 1 | Product |
+-------------+---------+--------------+
| id | | id |
| name | | name |
| ... | | ... |
| products |1-------N| category |
+-------------+ +--------------+
2 CRUDS are available, each root:
- Categories: http://127.0.0.1:5000/api/v1/categories/*
- Products: http://127.0.0.1:5000/api/v1/products/*
- Check the Swagger UI for all routes: http://127.0.0.1:5000/docs
During development:
# Build the docker image
make dev
# Start the container
make up
Make sure to have the right settings, these two in particular:
# File: .env
BIND_ADDR=0.0.0.0
# make sure that mongodb host is: 'mongo' instead of '127.0.0.1'
DATABASE_URL="mongodb://USERNAME:PASSWORD@HOST:PORT/DATABASE"
# ...
# Build the image
make prod
The production image uses PM2 for process management, see the content of pm2.config.json
for settings.
npm run debug
This will start the application with code inspection enabled for debugging.
If using VSCode just open the Debug tab, and use the play Button.
If not, use your favourite debugger and connect to: 0.0.0.0:9229
When running by npm run start
or using the dev
docker image, the app runs using nodemon
watching for changes and recompiling the app if necessary.