This is a starter template for building a tRPC API with Drizzle ORM, MySQL, Jest and Fastify. It provides a basic structure for setting up an API with tRPC and integrating it with Drizzle ORM for database operations using MySQL, along with the Fastify framework for handling HTTP requests and Jest for testing.
Before using this template, ensure that you have the following installed:
- Node.js v18 and above
- MySQL
- Clone the repository.
- Install the dependencies using
pnpm i
. - Set up your MySQL database and configure the connection in the application.
- Generate the initial migration file using Drizzle's CLI.
- Apply the initial migration to the database.
- Start the server using
pnpm run dev
.
src/
db.ts
- Drizzle ORM connection configuration to the MySQL database with mysql2 driver.schema.ts
- SQL schema.router.ts
- tRPC router with a procedure to use in our application.
migrations/
- Folder to store database migration files.
To create and apply database migrations, use the following commands:
- Generate a migration:
pnpm run generate-migration
- Apply migrations:
pnpm run migrate
For detailed instructions on using Drizzle ORM for migrations, refer to the Drizzle ORM documentation.
The API endpoints are defined in the src/router.ts
file. You can define your tRPC API handlers and schemas in this directory.
To start the hot reload development server, run the following command:
pnpm run dev
# try http://localhost:2000/v1/greeting
# try http://localhost:2000/v1/userById?input=1
To run tests, run the following command:
pnpm run test
# pnpm run test src/index.test.ts