ViteHub

DB

Use one default Drizzle database and optional named databases from Vite server code, with Cloudflare D1 bindings or hosted libSQL connections.

@vitehub/db gives Vite apps a default Drizzle database plus optional named databases behind one runtime import.

Use DB when route code needs joins, relations, transactional writes, or multiple durable datasets that should keep different schema and migration boundaries.

import { defineConfig } from 'vite'
import { hubDb } from '@vitehub/db/vite'

export default defineConfig({
  plugins: [hubDb()],
  db: {
    connection: {
      authToken: process.env.TURSO_AUTH_TOKEN,
      url: process.env.TURSO_DATABASE_URL,
    },
    databases: {
      analytics: {
        connection: {
          authToken: process.env.TURSO_AUTH_TOKEN,
          url: process.env.TURSO_ANALYTICS_DATABASE_URL,
        },
        cloudflare: {
          databaseName: 'vitehub-playground-analytics',
          databaseId: process.env.VITEHUB_D1_ANALYTICS_DATABASE_ID,
        },
      },
    },
  },
})

Default And Named Databases

ViteHub resolves database config in two layers:

  1. Top-level db.connection, db.drizzle, and db.cloudflare configure the default database.
  2. db.databases.<name> adds named databases with their own connection, schema, migration, and Cloudflare binding config.

The runtime surface stays small:

  • db and schema alias databases.default.
  • databases.default is the canonical default entry.
  • databases.<name> exposes { db, schema } for each named database.

Default Conventions

Without overrides, ViteHub uses these defaults:

DatabaseLocal URLSchema discoveryMigrations dirCloudflare binding
defaultfile:.data/db/sqlite.dbsrc/db/schema.ts and src/db/schema/*src/db/migrationsDB
analyticsfile:.data/db/analytics.sqlite.dbsrc/db/analytics/schema.ts and src/db/analytics/schema/*src/db/analytics/migrationsDB_ANALYTICS

If a named entry sets Cloudflare D1 metadata and omits connection.url, ViteHub treats it as D1-only.

Provider Behavior

Cloudflare and Vercel do not share the same database runtime rules:

  • Cloudflare hosted output can resolve an active D1 binding first, then fall back to a remote libSQL URL when one is configured.
  • Vercel hosted output requires a remote libSQL URL for every database that must run there.
  • Local Vite runtime can use file-based SQLite paths for the default or named databases.

Start Here

Start with Quickstart for a local Drizzle setup. Use the primitive comparison when you are deciding between KV, Blob, Queue, Sandbox, or a database.

Next Steps

Quickstart
Configure DB, define a schema, write a note, and read it back.
Usage
Use default aliases, named databases, schema discovery, and hosted fallbacks.
Runtime API
Review the config contract and the `db`, `schema`, and `databases` exports.
Cloudflare D1
Configure bindings, generated `wrangler.json` output, and D1-only entries.
Vercel
Use hosted libSQL URLs and understand the D1-only limitation for Vercel output.
Troubleshooting
Fix missing schema files, unconfigured databases, and hosted output failures.
Copyright © 2026