Skip to content

Starting template to build Rest APIs, using Fastify, PrimsaJS and TypeScript. Providing OpenAPI/ SwaggerUI for docs. Also, Blazingly fast (breh!)

License

Notifications You must be signed in to change notification settings

danielm/fastify-prisma-swagger-rest-boilerplate

Repository files navigation

🚀 RESTfull APIs boilerplate using Fastify

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.

Key Components

Tyepscript - Fastify - PrismaJS - Docker - Swagger - MongoDB - PM2 - FakerJS

Fastify plugins included

@fastify/compress - @fastify/cors - @fastify/env - @fastify/helmet - @fastify/swagger - @fastify/swagger-ui

Table of contents


Setup & Configuration

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 👇


Running locally

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:

Note: Inside the docs/ you can download a Postman collection to import and play with the example APIs

Other useful commands

# Very nice UI for data visualization of our database
npx prisma studio
# Synchronize your Prisma schema with your database
npx prisma db push

Seed the local Database

# Seed our database with a bunch of random data
npx prisma db seed

Databases & MongoDB for development

Since the project is using by default MongoDB, and It can be a little tricky to setup a replica set for just development:

Read more about Prisma and MongoDB

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.


File Structure

├── 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 example project

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:

Running as a Docker container

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"
# ...

Building for production

# Build the image
make prod

The production image uses PM2 for process management, see the content of pm2.config.json for settings.


Debugging

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

Hot-Reloading

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.

About

Starting template to build Rest APIs, using Fastify, PrimsaJS and TypeScript. Providing OpenAPI/ SwaggerUI for docs. Also, Blazingly fast (breh!)

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published