Skip to content

theogravity/bun-fastify-starter-turbo-monorepo

Repository files navigation

Bun Fastify Turbo Monorepo Starter

A starter project for building an API server using Bun, Typescript, Fastify v5, and Kysely with Postgres.

This project uses Bun as its runtime and package manager. Bun provides faster installs, native TypeScript execution, and improved performance over Node.js.

Requirements

  • Bun >= 1.0.0
  • Docker (for local Postgres and testing)

Features

  • Bun-powered - Uses Bun for package management, script execution, and runtime.
  • Fastify v5 with Typescript.
  • Monorepo setup using turbo and Bun workspaces.
  • Outputs OpenAPI schema for the API and has a web UI for viewing it.
  • A sample REST test is included using Vitest.
  • Sample database migrations / repositories are included using Kysely.
  • An client SDK package is included to generate typescript client code from the API schema.
  • An error handler package is included to handle errors and return a consistent response.
  • Code generators using turbo gen to create new API endpoints and database tables.

Libraries and tools used

  • typescript
  • bun for package management and runtime
  • commitlint for commit message linting
  • turbo for monorepo management
  • fastify for the API server framework
  • hash-runner for caching builds
  • kysely for the database query builder
  • postgres + pgAdmin for the database
  • testcontainers for testing with a sandboxed postgres instance
  • vitest for endpoint testing
  • loglayer for formatted logging
  • biome for linting and formatting
  • syncpack for keeping package versions in sync
  • Hey API for generating the backend SDK using the generated OpenAPI schema from the backend

Setup

  1. Install Bun (if not already installed):

    curl -fsSL https://bun.sh/install | bash
  2. Install dependencies:

    bun install
  3. Copy apps/backend/.env.example to apps/backend/.env

Start local postgres server:

docker compose up -d

Perform database migrations:

bun run db:migrate:latest

Development

turbo watch dev

Testing

Make sure docker is running as it uses testcontainers to spin up a temporary postgres database.

turbo test

Build

turbo build

Generate scaffolding

Generators for the following:

  • New API endpoints + tests
  • Database tables and repositories

turbo gen

Database migrations

  • Create a migration: bun run db:migrate:create
  • Run migrations: bun run db:migrate:latest
  • Rollback migrations: bun run db:migrate:undo

Update all dependencies

bun run syncpack:update

Troubleshooting

turbo watch dev failing

• Packages in scope: @internal/backend, @internal/backend-client, @internal/backend-errors, @internal/tsconfig
• Running dev in 4 packages
• Remote caching disabled
  × failed to connect to daemon
  ╰─▶ server is unavailable: channel closed

Try:

turbo daemon clean

Then try running turbo watch dev again.

If you get:

• Packages in scope: @internal/backend, @internal/backend-client, @internal/backend-errors, @internal/tsconfig
• Running dev in 4 packages
• Remote caching disabled
  × discovery failed: bad grpc status code: The operation was cancelled

Wait a few minutes and try again.

Related:

About

A starter project for building an API service using Fastify v5, kysely / postgres, turbo, and bun with sample tests and OpenAPI endpoint / database generators to help you build quickly

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors