Skip to content

Commit 6c106d1

Browse files
Claudehotlong
andauthored
fix: use TursoDriver instead of InMemoryDriver, remove unnecessary better-sqlite3
- Replace InMemoryDriver with TursoDriver (remote mode for Vercel) - Add @libsql/client and @objectstack/driver-turso dependencies - Remove better-sqlite3 dependency (not needed for Turso remote mode) - Remove better-sqlite3 from esbuild externals and vercel.json includeFiles - Remove better-sqlite3 copy logic from build-vercel.sh - Add .env.example with Turso configuration - Update DEPLOYMENT.md with Turso environment variables and troubleshooting Turso remote mode uses HTTP-only connection via @libsql/client, which doesn't require native bindings like better-sqlite3. This makes the deployment simpler and more suitable for serverless environments. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com> Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
1 parent fbf4686 commit 6c106d1

File tree

7 files changed

+37
-37
lines changed

7 files changed

+37
-37
lines changed

examples/app-host/.env.example

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Turso Database Configuration
2+
# Required for Vercel deployment
3+
TURSO_DATABASE_URL=libsql://your-database.turso.io
4+
TURSO_AUTH_TOKEN=your-auth-token-here

examples/app-host/DEPLOYMENT.md

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ This example demonstrates how to deploy the ObjectStack app-host to Vercel using
1212
Set the following environment variables in your Vercel project:
1313

1414
- `AUTH_SECRET`: A secure random string (minimum 32 characters) for authentication
15+
- `TURSO_DATABASE_URL`: Your Turso database URL (e.g., `libsql://your-database.turso.io`)
16+
- `TURSO_AUTH_TOKEN`: Your Turso authentication token
17+
18+
You can get these credentials from [Turso Dashboard](https://turso.tech/).
1519

1620
## Deployment Steps
1721

@@ -35,6 +39,8 @@ Set the following environment variables in your Vercel project:
3539
4. Set environment variables:
3640
```bash
3741
vercel env add AUTH_SECRET
42+
vercel env add TURSO_DATABASE_URL
43+
vercel env add TURSO_AUTH_TOKEN
3844
```
3945

4046
### Option 2: Using Vercel Dashboard
@@ -50,14 +56,13 @@ The build is configured in `vercel.json`:
5056

5157
- **Install Command**: `cd ../.. && pnpm install` (installs monorepo dependencies)
5258
- **Build Command**: `bash scripts/build-vercel.sh` (builds and bundles the application)
53-
- **Framework**: `null` (uses custom build setup)
59+
- **Framework**: `hono` (uses Vercel's Hono framework preset)
5460

5561
## How It Works
5662

5763
1. **Build Process** (`scripts/build-vercel.sh`):
5864
- Builds the TypeScript project using Turbo
5965
- Bundles the server code using esbuild (`scripts/bundle-api.mjs`)
60-
- Copies native dependencies (like better-sqlite3) to local node_modules
6166

6267
2. **API Handler** (`api/[[...route]].js`):
6368
- Committed catch-all route handler that Vercel detects pre-build
@@ -66,6 +71,7 @@ The build is configured in `vercel.json`:
6671
3. **Server Entrypoint** (`server/index.ts`):
6772
- Boots ObjectStack kernel with Hono adapter
6873
- Uses `@hono/node-server`'s `getRequestListener()` for Vercel compatibility
74+
- Connects to Turso database in remote mode (HTTP-only, no local SQLite)
6975
- Handles Vercel's pre-buffered request body properly
7076

7177
4. **Hono Integration**:
@@ -125,14 +131,15 @@ After deployment, your API will be available at:
125131
### Runtime Errors
126132

127133
- Check function logs in Vercel dashboard
128-
- Verify environment variables are set correctly
134+
- Verify environment variables are set correctly (`TURSO_DATABASE_URL`, `TURSO_AUTH_TOKEN`, `AUTH_SECRET`)
129135
- Ensure `AUTH_SECRET` is at least 32 characters
136+
- Test Turso connection using the provided credentials
130137

131-
### Module Not Found
138+
### Database Connection Issues
132139

133-
- The build script copies native modules to local node_modules
134-
- Check that `better-sqlite3` is in dependencies
135-
- Verify `vercel.json` includeFiles pattern matches the module
140+
- Verify your Turso database URL and auth token are correct
141+
- Check that your Turso database is accessible (not paused)
142+
- The deployment uses TursoDriver in **remote mode** (HTTP-only), which doesn't require native modules like better-sqlite3
136143

137144
## References
138145

examples/app-host/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,16 @@
1717
"@example/app-todo": "workspace:*",
1818
"@example/plugin-bi": "workspace:*",
1919
"@hono/node-server": "^1.19.14",
20+
"@libsql/client": "^0.14.0",
2021
"@objectstack/driver-memory": "workspace:*",
22+
"@objectstack/driver-turso": "workspace:*",
2123
"@objectstack/hono": "workspace:*",
2224
"@objectstack/metadata": "workspace:*",
2325
"@objectstack/objectql": "workspace:*",
2426
"@objectstack/plugin-auth": "workspace:*",
2527
"@objectstack/plugin-hono-server": "workspace:*",
2628
"@objectstack/runtime": "workspace:*",
2729
"@objectstack/spec": "workspace:*",
28-
"better-sqlite3": "^12.9.0",
2930
"hono": "^4.12.12"
3031
},
3132
"devDependencies": {

examples/app-host/scripts/build-vercel.sh

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,4 @@ cd examples/app-host
2323
# 2. Bundle API serverless function
2424
node scripts/bundle-api.mjs
2525

26-
# 3. Copy native/external modules into local node_modules for Vercel packaging.
27-
#
28-
# This monorepo uses pnpm's default strict node_modules structure. Transitive
29-
# native dependencies like better-sqlite3 only exist in the monorepo root's
30-
# node_modules/.pnpm/ virtual store — they're NOT symlinked into
31-
# examples/app-host/node_modules/.
32-
#
33-
# The vercel.json includeFiles pattern references node_modules/ relative to
34-
# examples/app-host/, so we must copy the actual module files here for Vercel
35-
# to include them in the serverless function's deployment package.
36-
echo "[build-vercel] Copying external native modules to local node_modules..."
37-
for mod in better-sqlite3; do
38-
src="../../node_modules/$mod"
39-
if [ -e "$src" ]; then
40-
dest="node_modules/$mod"
41-
mkdir -p "$(dirname "$dest")"
42-
cp -rL "$src" "$dest"
43-
echo "[build-vercel] ✓ Copied $mod"
44-
else
45-
echo "[build-vercel] ⚠ $mod not found at $src (skipped)"
46-
fi
47-
done
48-
4926
echo "[build-vercel] Done. Serverless function in api/[[...route]].js → api/_handler.js"

examples/app-host/scripts/bundle-api.mjs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import { build } from 'esbuild';
1616

1717
// Packages that cannot be bundled (native bindings / optional drivers)
1818
const EXTERNAL = [
19-
'better-sqlite3',
2019
// Optional knex database drivers — never used at runtime, but knex requires() them
2120
'pg',
2221
'pg-native',

examples/app-host/server/index.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
import { ObjectKernel, DriverPlugin, AppPlugin } from '@objectstack/runtime';
1414
import { ObjectQLPlugin } from '@objectstack/objectql';
15-
import { InMemoryDriver } from '@objectstack/driver-memory';
15+
import { TursoDriver } from '@objectstack/driver-turso';
1616
import { createHonoApp } from '@objectstack/hono';
1717
import { AuthPlugin } from '@objectstack/plugin-auth';
1818
import { getRequestListener } from '@hono/node-server';
@@ -54,8 +54,21 @@ async function ensureKernel(): Promise<ObjectKernel> {
5454
// Register ObjectQL engine
5555
await kernel.use(new ObjectQLPlugin());
5656

57-
// Database driver (in-memory for demo)
58-
await kernel.use(new DriverPlugin(new InMemoryDriver()));
57+
// Database driver - Turso (remote mode for Vercel)
58+
const tursoUrl = process.env.TURSO_DATABASE_URL;
59+
const tursoToken = process.env.TURSO_AUTH_TOKEN;
60+
61+
if (!tursoUrl || !tursoToken) {
62+
throw new Error('Missing required environment variables: TURSO_DATABASE_URL and TURSO_AUTH_TOKEN');
63+
}
64+
65+
const tursoDriver = new TursoDriver({
66+
url: tursoUrl,
67+
authToken: tursoToken,
68+
// Remote mode - no local sync needed for Vercel
69+
});
70+
71+
await kernel.use(new DriverPlugin(tursoDriver));
5972

6073
// Auth plugin — uses environment variables for configuration
6174
// Prefer VERCEL_PROJECT_PRODUCTION_URL (stable across deployments)

examples/app-host/vercel.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
"functions": {
77
"api/**/*.js": {
88
"memory": 1024,
9-
"maxDuration": 60,
10-
"includeFiles": "node_modules/better-sqlite3/**"
9+
"maxDuration": 60
1110
}
1211
},
1312
"rewrites": [

0 commit comments

Comments
 (0)