# Use Turso with ViteHub and Drizzle

Use Turso for hosted libSQL with your SQLite Drizzle schema. ViteHub connects
through its libSQL driver. It does not copy local rows to Turso.

First, complete [Save your first Database row](https://vitehub.dev/docs/database/get-started) and
keep its Database Definition and notes routes. Create a new Turso database and
copy its URL and auth token.

## Configure the hosted connection

Read the values from the environment at runtime, not the build config.

### Nuxt

```ts [nuxt.config.ts]
import { env } from 'vite-hub/env'

export default defineNuxtConfig({
  modules: ['vite-hub/nuxt'],
  vitehub: {
  	preset: 'node',
  	database: {
  		connection: {
  			url: env({ source: env.source('TURSO_DATABASE_URL') }),
  			authToken: env({ secret: true, source: env.source('TURSO_AUTH_TOKEN') }),
  		},
  	},
  },
})
```

### Nitro

```ts [vite.config.ts]
import { defineConfig } from 'vite'
import { nitro } from 'nitro/vite'
import { vitehub } from 'vite-hub'
import { env } from 'vite-hub/env'

export default defineConfig({
	plugins: [vitehub({
		preset: 'node',
		database: {
			connection: {
				url: env({ source: env.source('TURSO_DATABASE_URL') }),
				authToken: env({ secret: true, source: env.source('TURSO_AUTH_TOKEN') }),
			},
		},
	}), nitro()],
})
```

### Vite

```ts [vite.config.ts]
import { defineConfig } from 'vite'
import { vitehub } from 'vite-hub'
import { env } from 'vite-hub/env'

export default defineConfig({
	plugins: [vitehub({
		preset: 'node',
		database: {
			connection: {
				url: env({ source: env.source('TURSO_DATABASE_URL') }),
				authToken: env({ secret: true, source: env.source('TURSO_AUTH_TOKEN') }),
			},
		},
	})],
})
```

A connection in the Database Definition wins over these options. If the app
still reads SQLite, remove the old connection from the Definition.

Set the variables in the migration shell and the server environment:

```bash [Terminal]
export TURSO_DATABASE_URL='libsql://your-database.turso.io'
export TURSO_AUTH_TOKEN='your-auth-token'
```

For Vercel, use `preset: 'vercel'` with the same connection and routes. The
provider stays Turso.

## Apply the schema and check a row

Generate a migration if the schema changed. Review the SQL before you apply it:

```bash [Terminal]
pnpm vitehub database generate
pnpm vitehub database migrate --dry-run
pnpm vitehub database migrate
pnpm dev
```

In another terminal, call the notes routes:

```bash [Terminal]
curl --fail-with-body http://localhost:3000/api/notes \
  -H 'content-type: application/json' -d '{"title":"Stored in Turso"}'
curl --fail-with-body http://localhost:3000/api/notes
```

The read includes the note. Restart, repeat the GET, and find the row in Turso.
An older database can return more rows.

## Diagnose connection errors

- Missing table: the database lacks the migration. Check the URL before you
  apply SQL.
- Authentication error: use a valid token for that database.
- Local rows: a Definition probably overrides the hosted connection.

Back up a database before you change its schema. See
[Database configuration](https://vitehub.dev/docs/database/configure) for migration checks and the
[Turso docs](https://docs.turso.tech/) for access and limits.

<!-- vitehub-docs:section-links -->
## In this section

- [Overview](https://vitehub.dev/raw/docs/guides.md)
- [Agents](https://vitehub.dev/raw/docs/guides/deploy-agent-cloudflare-vercel.md)
- [Chat bots](https://vitehub.dev/raw/docs/guides/slack-ai-bot.md)
- [Storage](https://vitehub.dev/raw/docs/guides/upstash-redis.md)
- Databases
  - [Turso](https://vitehub.dev/raw/docs/guides/turso.md) (this page)
  - [Neon Postgres](https://vitehub.dev/raw/docs/guides/neon-postgres.md)
  - [Cloudflare D1](https://vitehub.dev/raw/docs/guides/cloudflare-d1.md)
- [Server work](https://vitehub.dev/raw/docs/guides/resend.md)
- [Better Auth on Cloudflare](https://vitehub.dev/raw/docs/guides/better-auth-cloudflare.md)
- [Concepts](https://vitehub.dev/raw/docs/guides/rate-limiting-algorithms.md)

Next: [Neon Postgres](https://vitehub.dev/raw/docs/guides/neon-postgres.md)

All pages: [llms.txt](https://vitehub.dev/llms.txt)
