This project is a boilerplate codebase for building a RESTful API using Fastify, Prisma, Zod, and TypeScript. This project includes user and product management endpoints, authentication, request and response validation, and Swagger documentation.
- Fastify - Web Server: Employed for its exceptional speed and low overhead.
- Prisma - Database ORM: Serves as the Database Object-Relational Mapping (ORM) tool.
- Zod - Request and Response Validation: Integrated for comprehensive validation of requests and responses.
- Swagger - API Docs: Utilized for automatically generating comprehensive API documentation.
- TypeScript : Enhances code quality by introducing static typing and other advanced features.
Before getting started with the project, ensure you have the following tools installed:
- Postman: Used for making API requests and testing the functionality of the endpoints.
- Prisma Studio: Essential for viewing and interacting with the data stored in the database.
- PostgreSQL Database: PostgreSQL as the database for this application.
- Docker: Used for containerization, including Docker Compose for running the PostgreSQL instance.
The project boasts the following features:
- User Management: Create a user, login, and list users.
- Product Management: Create a product and list products.
- Authentication: Implement secure JWT authentication mechanisms for user-related operations.
- Request & Response Validation: Zod is employed to validate incoming requests and ensure accurate and secure responses.
- Swagger Docs: Automatically generated API documentation using Swagger for easy reference.
To get started with the project, follow the instructions below:
-
Clone the repository:
https://github.com/remediios/fastify-prisma-rest-api.git
-
Navigate to the project directory:
cd fastify-prisma-rest-api
-
Install the dependencies:
yarn install
-
Create a
.env
file with the following credentials:
# .env Example
# PostgreSQL Database URL
DATABASE_URL="postgresql://postgres:<yourDBPassword>@localhost:5432/fastify-prisma-rest-api?schema=public"
# JWT Secret for Authentication
JWT_SECRET="Your random JWT Secret up to your choice"
-
Check the
docker-compose.yml
file and change any properties you'd like, e.g.POSTGRES_PASSWORD
for any of your choice. (it should match the.env
Database URL password) -
Start your Docker PostgreSQL container with
docker compose up -d
(-d to run in the background) -
Push the Prisma schema to reflect on the PostgreSQL Database with
npx prisma db push
ornpx prisma migrate dev --name init
(for historical migration's records) -
Run the application with
yarn dev
-
Lastly, if needed, import the Postman collection file example
Fastify Prisma API.postman_collection.json
into your Postman environment to facilitate API testing.
After completing these steps, you should be able to run this project locally. If you encounter any issues or have any questions, please don't hesitate to reach out.
After running the application, you can access it at http://localhost:3000/documentation
. Explore the API using Postman, test the functionality, and refer to the Swagger documentation for endpoint details.
Contributions to the project are more than welcome. Please submit a pull request with a detailed description of your changes.
Fastify-Prisma-Rest-API is open-source software licensed under the MIT License.
For any questions or feedback, please reach out to [email protected]
.