---
url: https://void.cloud/guide/database.md
description: >-
  Define tables in TypeScript and query them with Drizzle. Use the same schema
  with D1, PostgreSQL, or MySQL.
---

# Database

Define your tables in TypeScript, then query them with `db` from `void/db`. Void includes [Drizzle ORM](https://orm.drizzle.team) and configures the connection, so you can use the same schema and query workflow with D1, PostgreSQL, or MySQL.

## Choosing a Dialect

Choose a database during `void init`, or set `database` in `void.config.ts`:

|            | [D1 (SQLite)](./database/d1) | [PostgreSQL](./database/postgresql) | [MySQL](./database/mysql) |
| ---------- | ---------------------------- | ----------------------------------- | ------------------------- |
| Config     | Default                      | `"database": "pg"`                  | `"database": "mysql"`     |
| Managed by | Void                         | Bring your own database             | Bring your own database   |
| Connection | Automatic D1 binding         | Hyperdrive                          | Hyperdrive                |

A Void platform keeps Hyperdrive query caching off for your database connection, so reads reflect completed writes. An existing connection is updated on the project's next deploy.

## Schema Definition

Define your tables in `db/schema.ts` using column helpers from `void/schema-d1`, `void/schema-pg`, or `void/schema-mysql`.

::: code-group

```ts [SQLite (D1)]
// db/schema.ts
import { sql } from 'void/db';
import { sqliteTable, text, integer, uniqueIndex } from 'void/schema-d1';

export const users = sqliteTable(
  'users',
  {
    id: integer('id').primaryKey({ autoIncrement: true }),
    name: text('name').notNull(),
    email: text('email').notNull(),
    role: text('role').notNull().default('user'),
    createdAt: text('created_at')
      .notNull()
      .default(sql`(datetime('now'))`),
  },
  (table) => [uniqueIndex('users_email_unique').on(table.email)],
);
```

```ts [PostgreSQL]
// db/schema.ts
import { pgTable, serial, text, timestamp } from 'void/schema-pg';

export const users = pgTable('users', {
  id: serial('id').primaryKey(),
  name: text('name').notNull(),
  email: text('email').notNull().unique(),
  role: text('role').notNull().default('user'),
  createdAt: timestamp('created_at').notNull().defaultNow(),
});
```

```ts [MySQL]
// db/schema.ts
import { int, mysqlTable, text, timestamp } from 'void/schema-mysql';

export const users = mysqlTable('users', {
  id: int('id').autoincrement().primaryKey(),
  name: text('name').notNull(),
  email: text('email').notNull(),
  createdAt: timestamp('created_at').notNull().defaultNow(),
});
```

:::

For a unique SQLite column, use `uniqueIndex()` rather than `.unique()`. With `.unique()`, a later `void db generate` can rebuild the whole table, and D1 does not let that rebuild turn off foreign key checks. PostgreSQL and MySQL do not rebuild tables, so `.unique()` is fine there. When you upgrade from drizzle-kit 0.x, [`void db upgrade`](../reference/cli/database.md#void-db-upgrade) lists the SQLite calls to replace.

The schema helpers are included in `void`. You can write tables yourself or generate a starting point with `void gen model`.

You can also split your schema across multiple files under `db/schema/` and re-export from a barrel file:

```ts
// db/schema/users.ts
export const users = ...;

// db/schema/posts.ts
export const posts = ...;

// db/schema.ts (barrel)
export * from "./schema/users";
export * from "./schema/posts";
```

### Column casing

By default, a column declared without a name, such as `createdAt: timestamp()`, uses its key as the column name. To store these columns as `created_at`, define the table with `snakeCase.table()`. Columns with an explicit name, such as `text('created_at')`, keep that name. `camelCase.table()` works the same way.

```ts
// db/schema.ts
import { serial, snakeCase, text, timestamp } from 'void/schema-pg';

export const posts = snakeCase.table('posts', {
  id: serial().primaryKey(),
  title: text().notNull(),
  createdAt: timestamp().notNull().defaultNow(), // created_at
});
```

`snakeCase` and `camelCase` are also exported from `void/schema-d1` and `void/schema-mysql`. If your database already has tables, run `void db generate` after you change a table's casing and review the migration before you apply it.

## Querying

Void re-exports all common [Drizzle query operators](https://orm.drizzle.team/docs/select) from `void/db` so you don't need to install `drizzle-orm` separately. Import `db` from `void/db` and your tables from `@schema`:

```ts
import { users } from '@schema';
import { db } from 'void/db';
```

::: info What is `@schema`?
`@schema` is a Vite path alias that the Void plugin configures automatically. It points to your `db/schema.ts` file, or `db/schema/` if you split tables across files. You can just use it to import table definitions.
:::

### List rows

```ts
const allUsers = await db.select().from(users);
```

### Filter with `where`

```ts
import { users } from '@schema';
import { db, eq, and, or } from 'void/db';

const user = await db.select().from(users).where(eq(users.id, 1));

const admins = await db
  .select()
  .from(users)
  .where(and(eq(users.role, 'admin'), eq(users.name, 'Alice')));

const result = await db
  .select()
  .from(users)
  .where(or(eq(users.role, 'admin'), eq(users.role, 'editor')));
```

### Insert

```ts
await db.insert(users).values({
  name: 'Alice',
  email: 'alice@example.com',
});

const [created] = await db
  .insert(users)
  .values({ name: 'Bob', email: 'bob@example.com' })
  .returning();

await db.insert(users).values([
  { name: 'Alice', email: 'alice@example.com' },
  { name: 'Bob', email: 'bob@example.com' },
]);
```

### Update

```ts
await db.update(users).set({ role: 'admin' }).where(eq(users.id, 1));

const [updated] = await db.update(users).set({ role: 'admin' }).where(eq(users.id, 1)).returning();
```

### Delete

```ts
await db.delete(users).where(eq(users.id, 1));
```

### Column selection

```ts
const names = await db.select({ name: users.name, email: users.email }).from(users);
// names: { name: string; email: string }[]
```

### Ordering and pagination

```ts
import { users } from '@schema';
import { db, desc } from 'void/db';

const page = await db.select().from(users).orderBy(desc(users.createdAt)).limit(10).offset(20);
```

### Joins

```ts
import { users, posts } from '@schema';
import { db, eq } from 'void/db';

const results = await db
  .select({
    id: posts.id,
    title: posts.title,
    author: users.name,
  })
  .from(posts)
  .innerJoin(users, eq(posts.userId, users.id));
```

### Relational queries

Drizzle's relational query API reads the [relations](https://orm.drizzle.team/docs/relations) your schema exports as `relations`. Build them with `defineRelations` from `void/db`:

```ts
// db/schema.ts
import { defineRelations } from 'void/db';

// ... users and posts tables

export const relations = defineRelations({ users, posts }, (r) => ({
  users: {
    posts: r.many.posts(),
  },
  posts: {
    author: r.one.users({ from: r.posts.userId, to: r.users.id }),
  },
}));
```

Without a `relations` export, `db.query` still works for single tables. Then query related rows with `with`:

```ts
const usersWithPosts = await db.query.users.findMany({
  with: { posts: true },
});
```

## Seeding

Use `void db seed` to reset your local database, re-apply migrations, and then run a seed file.

Use `db/seed.ts` for a programmatic seed or `db/seed.sql` for SQL. JavaScript and `.mts` / `.mjs` files also work. If more than one seed file exists, choose one with `--file <path>`.

### Programmatic seeding

Use `db/seed.ts` when you want to generate seed data in code. Export a default function or a named `seed` function.

You can use your app’s TypeScript aliases in seed files and schema modules. For example, import a table from `@schema` just as you would in a route. Dynamic `import()` works too. If your `tsconfig.json` extends `.void/tsconfig.json`, run `void prepare` before seeding a fresh checkout.

```ts
// db/seed.ts
import { defineSeed } from 'void/seed';

export default defineSeed<typeof import('./schema')>(async ({ db, schema }) => {
  const rows = Array.from({ length: 100 }, (_, i) => ({
    text: `Seed message ${i + 1}`,
  }));

  await db.insert(schema.messages).values(rows);
});
```

The seed context includes:

* `dialect`: `"sqlite"`, `"postgresql"`, or `"mysql"`
* `db`: a Drizzle instance for the local database
* `schema`: the exports from your `db/schema.ts` or `db/schema/` modules

On SQLite, `db` is an async Drizzle database on Node's built-in `node:sqlite`, so `await` your queries. Its `transaction()` callback must be synchronous: run statements inside it with `.run()`, `.all()`, `.get()` or `.values()`, without `await`. To type a helper that takes this database, use `SqliteSeedDatabase` from `void/seed`.

### SQL seeding

If you prefer raw SQL, keep using `db/seed.sql`:

```sql
INSERT INTO messages (text) VALUES ('Hello from SQL');
```

## Schema-Derived Validators

Derive request validators from your tables with `void/drizzle-zod`, `void/drizzle-valibot`, or `void/drizzle-arktype`. Add the validator after your table definition:

::: code-group

```ts [Zod]
// db/schema.ts
import { createInsertSchema } from 'void/drizzle-zod';

export const insertUserSchema = createInsertSchema(users, {
  name: (schema) => schema.min(1),
  email: (schema) => schema.email(),
});
```

```ts [Valibot]
// db/schema.ts
import { pipe, minLength, email } from 'valibot';
import { createInsertSchema } from 'void/drizzle-valibot';

export const insertUserSchema = createInsertSchema(users, {
  name: (schema) => pipe(schema, minLength(1)),
  email: (schema) => pipe(schema, email()),
});
```

```ts [ArkType]
// db/schema.ts
import { type } from 'arktype';
import { createInsertSchema } from 'void/drizzle-arktype';

export const insertUserSchema = createInsertSchema(users, {
  name: type('string > 0'),
  email: type('string.email'),
});
```

:::

`createInsertSchema` generates a validator that matches `$inferInsert`. Columns with defaults or auto-increments become optional, while `NOT NULL` columns stay required. The optional second argument lets you refine individual columns, either with a callback that receives the generated schema or by passing a type directly to override a field.

Use the derived schema in your route handlers with [`withValidator()`](./server-routing.md#validation):

```ts
// routes/api/users/index.ts
import { users, insertUserSchema } from '@schema';
import { defineHandler } from 'void';
import { db } from 'void/db';

export const POST = defineHandler.withValidator({
  body: insertUserSchema,
})(async (c, { body }) => {
  const [created] = await db.insert(users).values(body).returning();
  return created;
});
```

Three functions are available:

| Function             | Purpose                                                                       |
| -------------------- | ----------------------------------------------------------------------------- |
| `createInsertSchema` | Validates insert data, excluding auto-generated columns and applying defaults |
| `createSelectSchema` | Matches the shape of selected rows                                            |
| `createUpdateSchema` | Like insert but all fields are optional (partial update)                      |

Void already bundles the Drizzle adapters. Install the validator library itself if you use its direct APIs in your schema file:

```bash
npm install zod
# or
npm install valibot
# or
npm install arktype
```

## CLI Commands

| Command            | Purpose                                              |
| ------------------ | ---------------------------------------------------- |
| `void db push`     | Apply schema changes locally without migration files |
| `void db generate` | Generate SQL migrations                              |
| `void db migrate`  | Apply pending migrations locally                     |
| `void db status`   | Check schema drift and pending migrations            |
| `void db seed`     | Reset the local database and run a seed file         |

See the [CLI reference](../reference/cli/database.md#database) for connection setup, SQL execution, Studio, exports, and other commands.

## Adopt an existing Drizzle PostgreSQL database {#adopt-existing-drizzle}

To use Void with a PostgreSQL database that Drizzle has already migrated, record its applied migrations before your first deployment:

1. Back up the database and pause deployments. Try the procedure on a restored copy first.

2. Set `database: 'pg'` in `void.config.ts`. Keep your existing schema and `db/migrations/` folders. Do not regenerate or rename migrations during adoption. If `db/migrations/` still has a `meta/_journal.json` from drizzle-kit 0.x, run `void db upgrade` once to move it to one `<timestamp>_<name>/migration.sql` folder per migration, named after the time of each journal entry.

3. Read Drizzle's migration history. The default table is `drizzle.__drizzle_migrations`; use your configured schema and table if different.

   ```sql
   SELECT hash, created_at,
     to_char(to_timestamp(created_at / 1000.0) AT TIME ZONE 'UTC', 'YYYYMMDDHH24MISS') AS folder_time
   FROM drizzle.__drizzle_migrations
   ORDER BY created_at;
   ```

   `created_at` is the time drizzle-kit gave the migration, in milliseconds. Match each row's `folder_time` to the timestamp at the start of a migration folder name, such as `20231114221320` in `20231114221320_create_users`, and its `hash` to the SHA-256 of that folder's `migration.sql`. Resolve any differences before continuing. If drizzle-orm 1.0 already migrates this database, the table also has a `name` column that holds the folder name.

4. Save this as `adopt-void.sql`. Replace the example names with **only the migrations you verified as applied**, each written as `<folder>/migration.sql`. Leave pending migrations out.

   ```sql [adopt-void.sql]
   BEGIN;
   CREATE TABLE IF NOT EXISTS _void_migrations (
     name TEXT PRIMARY KEY,
     applied_at TEXT NOT NULL
   );
   INSERT INTO _void_migrations (name, applied_at)
   VALUES
     ('20231114221320_create_users/migration.sql', CURRENT_TIMESTAMP::text),
     ('20231114221321_add_email/migration.sql', CURRENT_TIMESTAMP::text)
   ON CONFLICT (name) DO NOTHING;
   COMMIT;
   ```

5. Run the file with your PostgreSQL client, stopping on errors. Use the database and search path Void will use. This records the migrations without running their SQL or changing Drizzle's history.

6. Connect Void to the database and run `void db status`. Only new migrations should be pending. For direct Cloudflare deployments, set `DATABASE_URL` in the shell or CI environment that runs `void deploy`.

Repeat these steps for each database; development and production may have different migration histories. Keep Drizzle's history table, and use Void for future migrations.

## Hand-written migrations {#hand-written-migrations}

Deploy compares your Drizzle schema with the newest `snapshot.json` in `db/migrations/`. If your SQL migrations have no snapshot, running `void db generate` would create another migration for the same tables.

To keep an existing migration history:

1. Make your Drizzle schema match the tables your SQL migrations create.
2. Put each SQL file in its own folder as `migration.sql`, with folder names that sort in the order the files run, such as `20260101000000_create_users/migration.sql`. For flat `.sql` files, `void db upgrade` does this and keeps each file name as its folder name.
3. In a scratch copy of the project, remove `db/migrations/` and run `void db generate`.
4. Copy the generated `snapshot.json` into the folder of your newest migration.

Review and commit the folders, then run `void deploy` again. If no database has applied the SQL yet, you can instead replace it with migrations from `void db generate`.

For D1 migrations containing triggers, and MySQL migrations containing triggers, procedures, functions or events, add a `--> statement-breakpoint` line after each complete SQL statement, including the trigger's closing `END;`. Keep the trigger body together.

## Scaffolding

The `void gen model` command generates dialect-appropriate `sqliteTable`, `pgTable`, or `mysqlTable` definitions.

```bash
void gen model posts title:string body:text published:boolean
```

This creates:

1. `db/schema/posts.ts`: a Drizzle table definition with `id`, `createdAt`, `updatedAt`, and your columns
2. Updates `db/schema.ts` with `export * from "./schema/posts"`
3. `routes/api/posts/index.ts`: `GET` for list and `POST` for insert with validation
4. `routes/api/posts/[id].ts`: `GET` by id with `404` handling

See the [CLI reference](../reference/cli/generate.md#code-generation) for the full list of generators.
