Monorepo overview
BE Monorepo uses Bun workspaces to organize multiple packages and applications in a single repository. This structure enables code sharing, consistent tooling, and simplified dependency management across all projects.Workspace configuration
The rootpackage.json defines the workspace structure:
package.json
The
workspaces field tells Bun to manage dependencies for all packages in apps/* and packages/* directories.Applications
Theapps/ directory contains deployable applications. Currently, there’s one application:
@workspace/hono
Hono API server
Main backend API built with Hono 4, including authentication, database, and observability.
Directory structure
Directory structure
Key files
Key files
Main application setup with middleware, error handling, and route registration. This is where the OpenAPIHono instance is created and configured.
Entry point for Bun runtime. Exports the app with port configuration for Bun’s native server.
Entry point for Node.js runtime. Uses
@hono/node-server adapter for compatibility.OpenTelemetry SDK initialization. Sets up traces, metrics, logs, and instrumentation for observability.
Drizzle ORM schema definitions. Defines all database tables, columns, and relationships.
Route registration. Combines all route modules and registers them with the Hono app.
Package dependencies
Package dependencies
apps/hono/package.json
workspace:* protocol links to the local @workspace/core package.Shared packages
Thepackages/ directory contains reusable code shared across applications.
@workspace/core
Core utilities
Shared constants, types, utilities, and HTTP services used across the monorepo.
Directory structure
Directory structure
Package exports
Package exports
The package uses path-based exports for tree-shaking and explicit imports:Usage in apps:
packages/core/package.json
Dependencies
Dependencies
packages/core/package.json
@workspace/typescript-config
TypeScript configuration
Shared TypeScript compiler configurations for consistent settings across the monorepo.
Configuration files
Configuration files
base.json):Usage in packages
Usage in packages
Applications and packages extend the shared configuration:
apps/hono/tsconfig.json
Infrastructure
Docker configuration
Thedocker/ directory contains Docker Compose setup for local development:
docker/docker-compose.yml
Root configuration files
Root workspace configuration with scripts, workspaces definition, and shared devDependencies.
Root TypeScript config that extends
@workspace/typescript-config/base.json.Biome linter and formatter configuration. Uses
ultracite/biome/core preset with custom rule overrides.Bun configuration for exact versioning and isolated linking:
Changesets configuration for version management and changelog generation.
GitHub Actions CI/CD workflows for testing, linting, and deployment.
Git hooks for running linters and tests before commits.
How workspaces work
Dependency resolution
When you run
bun install at the root, Bun:- Reads all
package.jsonfiles in the workspace - Resolves dependencies and checks for conflicts
- Creates a single
node_modulesat the root - Links workspace packages using the
workspace:*protocol
Package linking
Workspace packages are linked, not copied:Changes to
@workspace/core are immediately available in @workspace/hono.File organization patterns
- Feature-based
- Layer-based
- Domain-based
Group related files by feature:
BE Monorepo uses a hybrid approach: feature-based for routes and layer-based for infrastructure concerns like database and observability.
Adding new packages
To add a new shared package:Add to workspace
The package is automatically discovered because of the
packages/* workspace pattern.Run bun install to link it.Next steps
Database schema
Learn about the database schema and migrations
Adding routes
Create new API endpoints with OpenAPI docs
Shared utilities
Explore the core package utilities
TypeScript config
Understand the TypeScript setup
