Backend Architecture
Relevant source files
The following files were used as context for generating this wiki page:
The backend of the Analog project is a modern, TypeScript-based system designed within a monorepo architecture managed by Turborepo. It provides API services, database management, and authentication for the main web application. The architecture emphasizes a clear separation of concerns, with distinct packages for the API layer, database interactions, authentication, and external service clients.
This architecture leverages tRPC for type-safe client-server communication, Drizzle ORM for database interaction with a PostgreSQL database, and Better Auth for handling OAuth-based authentication with providers like Google and Microsoft. The entire backend is designed to be run and developed alongside the Next.js frontend within the same monorepo, facilitating a streamlined development workflow.
High-Level Architecture
The backend is composed of several key components that work together to serve the Analog web application. The Next.js application (@repo/web) communicates with the backend services, which include a tRPC API, an authentication handler, and a database. The backend also integrates with external services like Google Calendar and Google Tasks.
The following diagram illustrates the major components and their interactions.
Core Technologies
The backend is built on a foundation of modern TypeScript tools and libraries.
| Technology | Package/Role | Description |
|---|---|---|
| Turborepo | Root (/) | Manages the monorepo, orchestrates builds, development tasks, and dependencies across workspaces. |
| Next.js | @repo/web | While primarily a frontend framework, it hosts the API routes that expose the backend services. |
| tRPC | @repo/api | Enables building fully type-safe APIs without schemas or code generation. |
| Drizzle ORM | @repo/db | A TypeScript ORM used for querying the PostgreSQL database. |
| PostgreSQL | Database | The relational database for storing application data. Managed via Docker Compose. |
| Better Auth | @repo/auth | Handles OAuth 2.0 authentication flows with providers like Google and Microsoft. |
| Upstash Redis | @repo/auth, @repo/api | Used for rate limiting and potentially other caching/session management tasks. |
Sources: README.md:83-85, package.json:17-21, packages/api/package.json:20,33, packages/db/package.json:18,21, packages/auth/package.json:15,18
Monorepo and Build System
The project uses a monorepo structure managed by Turborepo to handle the web application and various packages.
Sources: package.json:21, turbo.json
Workspace Structure
The repository is organized into two main directories: apps for deployable applications and packages for shared code.
apps/web: The main Next.js application.packages/api: The core tRPC-based API.packages/auth: Authentication logic using Better Auth.packages/db: Drizzle ORM setup and database schemas.packages/google-calendar: Client for Google Calendar API.packages/google-tasks: Client for Google Tasks API.
Sources: package.json:19-20
Build & Development Tasks
Turborepo is configured in turbo.json to manage tasks like build, dev, and lint. The dev and build tasks are configured to be aware of numerous environment variables required by the backend services.
The root package.json provides convenient scripts that use dotenv-cli to load environment variables before running Turborepo commands.
| Script | Description |
|---|---|
bun run dev | Starts the development server for all workspaces. |
bun run build | Builds all applications and packages for production. |
bun run db:push | Pushes Drizzle schema changes to the database. |
bun run docker:up | Starts the PostgreSQL database using Docker Compose. |
Sources: turbo.json:3-70, package.json:4-13, README.md:70-76
API Layer (@repo/api)
The API layer is encapsulated in the @repo/api package. It uses tRPC to create a type-safe interface between the frontend and backend.
Package Exports
The package.json for @repo/api defines the main entry points for the package, indicating a structured API design.
| Export Path | Purpose |
|---|---|
. (./src/root.ts) | The main tRPC app router, combining all other routers. |
./trpc (./src/trpc.ts) | The tRPC initialization file, defining procedures and middleware. |
./providers (./src/providers/index.ts) | Likely contains routers related to third-party providers. |
./openapi (./src/openapi.ts) | Exports an OpenAPI specification generated from the tRPC router. |
Sources: packages/api/package.json:5-11
API Routes
The Next.js configuration in apps/web/next.config.ts defines several API routes and applies specific CORS headers to them, making them accessible from external clients. This is particularly relevant for features like the MCP (Marble Content Platform) integration and the public OpenAPI spec.
/api/mcp/:path*/api/auth/mcp/:path*/api/v1/:path*/api/openapi.json
Sources: apps/web/next.config.ts:16-39
Database Layer (@repo/db)
The @repo/db package manages all database interactions. It uses Drizzle ORM to communicate with a PostgreSQL database.
Setup and Migrations
The database is intended to be run in a Docker container, as defined by a docker-compose.yml file. The README.md provides instructions for starting the database and initializing it.
The following diagram shows the data flow from the application to the database.
Sources: README.md:64-76, packages/db/package.json:12
Drizzle Kit is used for schema management, with scripts available for generating, migrating, and pushing schema changes.
db:generate: Generates Drizzle ORM files from the schema.db:push: Pushes schema changes directly to the database (for development).db:studio: Opens the Drizzle Studio for browsing data.
Sources: packages/db/package.json:10-13
Authentication (@repo/auth)
Authentication is a critical backend component, handled by the @repo/auth package. It relies on better-auth to implement OAuth flows.
Providers and Configuration
The system is configured to support Google and Microsoft OAuth. This requires setting several environment variables, as detailed in the README.md.
GOOGLE_CLIENT_ID&GOOGLE_CLIENT_SECRETMICROSOFT_CLIENT_ID&MICROSOFT_CLIENT_SECRETBETTER_AUTH_SECRETBETTER_AUTH_URL
The setup involves creating OAuth applications in the Google Cloud Console and Microsoft Azure Portal and configuring the correct redirect URIs and API permissions. For Google, Google Calendar API must be enabled, and for Microsoft, permissions like Calendars.ReadWrite and User.Read are required.
Sources: README.md:29-59, CONTRIBUTING.md:29-59, turbo.json:8,12,17,18
Authentication Flow
The following sequence diagram illustrates a typical sign-in flow using Google.
External Service Integrations
The backend integrates with several external Google APIs via dedicated client packages.
Google Calendar & Tasks Clients
The @repo/google-calendar and @repo/google-tasks packages contain auto-generated API clients for interacting with the respective Google services.
The GoogleCalendar and GoogleTasks classes are instantiated with an accessToken obtained during the OAuth flow. They provide methods for all API operations, with built-in support for retries, timeouts, and error handling.
The diagram below shows the class structure for these clients.
Google Places API
The backend also uses the Google Places API, which requires a GOOGLE_MAPS_API_KEY. This is used for location-based features within the calendar.
Sources: README.md:61-62