|
| 1 | +# Vercel Deployment Example |
| 2 | + |
| 3 | +Deploy an ObjectStack server with Hono to Vercel. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- ✅ Hono adapter for fast, edge-compatible API routes |
| 8 | +- ✅ Turso/LibSQL database driver with in-memory fallback |
| 9 | +- ✅ Authentication with better-auth |
| 10 | +- ✅ Security plugin for RBAC |
| 11 | +- ✅ Optimized serverless function bundling with esbuild |
| 12 | +- ✅ Environment-based configuration |
| 13 | + |
| 14 | +## Prerequisites |
| 15 | + |
| 16 | +1. A [Vercel](https://vercel.com) account |
| 17 | +2. A [Turso](https://turso.tech) database (optional, uses in-memory storage if not configured) |
| 18 | + |
| 19 | +## Local Development |
| 20 | + |
| 21 | +```bash |
| 22 | +# Install dependencies (from monorepo root) |
| 23 | +pnpm install |
| 24 | + |
| 25 | +# Start local development server |
| 26 | +cd examples/vercel |
| 27 | +pnpm dev |
| 28 | +``` |
| 29 | + |
| 30 | +The server will be available at `http://localhost:3000/api/v1`. |
| 31 | + |
| 32 | +## Deployment to Vercel |
| 33 | + |
| 34 | +### Option 1: Deploy via Vercel CLI |
| 35 | + |
| 36 | +```bash |
| 37 | +# Install Vercel CLI |
| 38 | +npm i -g vercel |
| 39 | + |
| 40 | +# Deploy from the examples/vercel directory |
| 41 | +cd examples/vercel |
| 42 | +vercel |
| 43 | +``` |
| 44 | + |
| 45 | +### Option 2: Deploy via Vercel Dashboard |
| 46 | + |
| 47 | +1. Import your GitHub repository in the [Vercel Dashboard](https://vercel.com/new) |
| 48 | +2. Set the **Root Directory** to `examples/vercel` |
| 49 | +3. Configure environment variables (see below) |
| 50 | +4. Click **Deploy** |
| 51 | + |
| 52 | +## Environment Variables |
| 53 | + |
| 54 | +Configure these in your Vercel project settings: |
| 55 | + |
| 56 | +| Variable | Description | Required | Example | |
| 57 | +|----------|-------------|----------|---------| |
| 58 | +| `TURSO_DATABASE_URL` | Turso database connection URL | No* | `libsql://your-db.turso.io` | |
| 59 | +| `TURSO_AUTH_TOKEN` | Turso authentication token | No* | `eyJ...` | |
| 60 | +| `AUTH_SECRET` | Secret key for authentication (min 32 chars) | Yes | Generate with `openssl rand -base64 32` | |
| 61 | + |
| 62 | +*If not set, the server will use an in-memory database (data will be lost on restart). |
| 63 | + |
| 64 | +### Setting up Turso Database |
| 65 | + |
| 66 | +```bash |
| 67 | +# Install Turso CLI |
| 68 | +curl -sSfL https://get.tur.so/install.sh | bash |
| 69 | + |
| 70 | +# Create a new database |
| 71 | +turso db create objectstack-vercel |
| 72 | + |
| 73 | +# Get the database URL |
| 74 | +turso db show objectstack-vercel --url |
| 75 | + |
| 76 | +# Create an auth token |
| 77 | +turso db tokens create objectstack-vercel |
| 78 | + |
| 79 | +# Add both values to Vercel environment variables |
| 80 | +``` |
| 81 | + |
| 82 | +## Project Structure |
| 83 | + |
| 84 | +``` |
| 85 | +examples/vercel/ |
| 86 | +├── api/ |
| 87 | +│ └── [[...route]].js # Vercel serverless function entry point |
| 88 | +├── scripts/ |
| 89 | +│ ├── bundle-api.mjs # esbuild bundler for serverless function |
| 90 | +│ └── build-vercel.sh # Vercel build script |
| 91 | +├── server/ |
| 92 | +│ └── index.ts # Server entrypoint with kernel bootstrap |
| 93 | +├── objectstack.config.ts # ObjectStack configuration |
| 94 | +├── package.json |
| 95 | +├── tsconfig.json |
| 96 | +├── vercel.json # Vercel deployment configuration |
| 97 | +└── README.md |
| 98 | +``` |
| 99 | + |
| 100 | +## How It Works |
| 101 | + |
| 102 | +1. **Build Step**: `scripts/build-vercel.sh` runs on Vercel, which: |
| 103 | + - Builds the monorepo using turbo |
| 104 | + - Bundles `server/index.ts` → `api/_handler.js` using esbuild |
| 105 | + |
| 106 | +2. **Runtime**: Vercel routes requests to `api/[[...route]].js`, which: |
| 107 | + - Lazily boots the ObjectStack kernel on first request |
| 108 | + - Delegates to the Hono adapter for request handling |
| 109 | + - Persists kernel state across warm invocations |
| 110 | + |
| 111 | +3. **Database**: |
| 112 | + - Production: Uses Turso (edge-compatible LibSQL) |
| 113 | + - Local dev: Falls back to in-memory driver |
| 114 | + |
| 115 | +## API Routes |
| 116 | + |
| 117 | +All ObjectStack API routes are available under `/api/v1`: |
| 118 | + |
| 119 | +- `GET /api/v1/meta` - Metadata discovery |
| 120 | +- `GET /api/v1/data/:object` - Query data |
| 121 | +- `POST /api/v1/data/:object` - Insert records |
| 122 | +- `PATCH /api/v1/data/:object/:id` - Update records |
| 123 | +- `DELETE /api/v1/data/:object/:id` - Delete records |
| 124 | +- `POST /api/v1/auth/sign-in` - Authentication |
| 125 | +- And more... |
| 126 | + |
| 127 | +## Testing the Deployment |
| 128 | + |
| 129 | +```bash |
| 130 | +# Health check |
| 131 | +curl https://your-deployment.vercel.app/api/v1/meta |
| 132 | + |
| 133 | +# Example API request (after authentication) |
| 134 | +curl https://your-deployment.vercel.app/api/v1/data/users \ |
| 135 | + -H "Authorization: Bearer YOUR_TOKEN" |
| 136 | +``` |
| 137 | + |
| 138 | +## Troubleshooting |
| 139 | + |
| 140 | +### Build fails with "Module not found" |
| 141 | + |
| 142 | +Make sure you're running the build from the monorepo root, or that Vercel's `installCommand` is set correctly in `vercel.json`. |
| 143 | + |
| 144 | +### Database connection issues |
| 145 | + |
| 146 | +- Verify `TURSO_DATABASE_URL` and `TURSO_AUTH_TOKEN` are set correctly |
| 147 | +- Check Turso database is accessible from Vercel's network |
| 148 | +- For debugging, you can temporarily use `:memory:` as the database URL |
| 149 | + |
| 150 | +### Cold start timeout |
| 151 | + |
| 152 | +- Increase `maxDuration` in `vercel.json` if needed |
| 153 | +- Consider using Vercel Pro for higher limits |
| 154 | + |
| 155 | +## Learn More |
| 156 | + |
| 157 | +- [ObjectStack Documentation](https://docs.objectstack.dev) |
| 158 | +- [Hono Vercel Deployment Guide](https://vercel.com/docs/frameworks/backend/hono) |
| 159 | +- [Turso Documentation](https://docs.turso.tech) |
| 160 | +- [Vercel Serverless Functions](https://vercel.com/docs/functions/serverless-functions) |
| 161 | + |
| 162 | +## License |
| 163 | + |
| 164 | +Apache-2.0 |
0 commit comments