This repo contain a fullstack example to build on Cloudflare with the following stack:
- RedwoodSDK: A React framework to run react 19 with SSR/RSC/ServerFunctions/etc.. on Cloudflare
- Drizzle ORM: Lightweight, type-safe SQL ORM with migrations
- Better-auth: Simple, flexible authentication library - The example is setup to use OTP
- Alchemy: Infrastructure-as-Code without the dead weight
- Shadcn: A set of beautifully-designed, accessible components to build your component library
- Bun: a fast JavaScript all-in-one toolkit
- D1 (as main DB)
- KV (for sessions)
- Website running on workers using RedwoodSDK
All the required resources are configured via Alchmey in alchemy.run.ts
- MJ Meyer: this example was eavily inspired and borrow alot from his repo, adding little things here and there, mainly Alchemy as IaC.
- Check /types/env.d.ts to see how our IaC help defining our types (no need to generate types with Wrangler)
- Check ./alchemy.run.ts to see how the whole infra is defined as code via Alchemy
git clone https://github.com/nickbalestra/fullstack-cf-example
cd fullstack-cf-example
bun install
Create an .env file (look at the provided env.example for reference)
bun dev
The application will be available at the URL displayed in your terminal (typically http://localhost:5173
)
This will provision all the resources needed like db, ... The application will be available at the Cloudflare URL displayed in your terminal.
bun infra:up
This example includes several key routes:
- / - The landing page with a link to the protected home page
- /home - A protected page that requires authentication (redirects to login if not authenticated)
- /user/login - The login page where users can authenticate
This example includes a complete authentication system with:
- OTP for signup and login
- Session management using a seperate KV database
- Protected routes
- Logout functionality
The project uses Cloudflare D1 (SQLite) with Drizzle ORM. A local development database will automatically setup when you first run bun dev
in ./wrangler
The authentication schema is defined in src/db/schema
and includes tables for:
- Users
- Sessions
- Accounts
When you need to update your database schema:
- Modify the schema files in
src/db/schema
- Generate a new migration:
bun migrate:new --name="your_migration_name"
- Apply the migration:
bun migrate:dev
To deploy the whole application (app, db, ecc) to Cloudflare:
- Run the infra:up command to spin up and deploy:
bun infra:up
- Run the infra:destroy to tear it down
bun infra:destroy
Everytime you change anything to the infra definition and run infra:up
your whole infra will be updated, that's it.
├── src/
│ ├── app/ # UI components
│ │ ├── pages/ # Page components
│ │ ├── shared/ # Shared components
│ │ └── document/ # Root document/headers/css
│ ├── db/ # Database configuration
│ │ ├── migrations/ # Database migrations
│ │ ├── schema/ # Drizzle schema definitions
│ │ ├── scripts/ # Database scripts for ie seeding
│ │ └── db.ts # Database connection
│ ├── lib/ # Application logic
│ │ ├── auth.ts # Server-side auth configuration
│ │ └── auth-client.ts # Client-side auth configuration
│ ├── types/ # Project wide & system types
│ ├── client.tsx # Client entry point
│ └── worker.tsx # Server entry point
├── infra.run.ts # Alchemy main script for orchestrating infrastructure's resources
└── *.config # Various configuration files (drizzle, vite, wrangler, typescript)