Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
e1bed03
Introduce support for drizzle relations v2
Sqh3rd Feb 9, 2026
d42bab9
Update lockfile
Sqh3rd Feb 9, 2026
75e09cd
feat: auto enable useRelationsV2
RihanArfan Feb 10, 2026
5fb6451
fix(db): correct template variables in drizzle client generation
RihanArfan Feb 10, 2026
2798d6c
chore: update comments
RihanArfan Feb 10, 2026
bcb1f0a
chore: improvements
RihanArfan Feb 10, 2026
bb15745
Merge branch 'nuxt-hub:main' into add-support-for-relations-v2
Sqh3rd Feb 11, 2026
4a91887
Fix Relation Types
Sqh3rd Feb 11, 2026
8f80142
docs: document relations v2
RihanArfan Feb 11, 2026
13c3604
chore: use @nuxthub/db/schema
RihanArfan Feb 11, 2026
8af4897
Fix copy pasta oversight
Sqh3rd Feb 11, 2026
4f9935d
Merge branch 'main' into add-support-for-relations-v2
Sqh3rd Feb 15, 2026
55e4f03
Merge branch 'main' into add-support-for-relations-v2
RihanArfan Feb 16, 2026
bf0fd50
Explicitly filter for relation keys in types
Sqh3rd Feb 18, 2026
7af4582
Merge branch 'add-support-for-relations-v2' of github.com:Sqh3rd/nuxt…
Sqh3rd Feb 18, 2026
d68868f
Filter for relation keys in mjs
Sqh3rd Feb 18, 2026
8451114
Merge branch 'main' into add-support-for-relations-v2
Sqh3rd Feb 20, 2026
83efd2a
Expose relations in separate file
Sqh3rd Feb 27, 2026
b5366e7
Merge branch 'main' into add-support-for-relations-v2
Sqh3rd Feb 27, 2026
c33d16f
Update docs
Sqh3rd Feb 27, 2026
3ca2728
Merge branch 'main' into add-support-for-relations-v2
Sqh3rd Mar 15, 2026
76842fc
Merge branch 'main' into add-support-for-relations-v2
Sqh3rd Apr 28, 2026
8c3838c
feat(db): upgrade drizzle to v1 rc.2
adamkasper May 10, 2026
a0066ac
Merge pull request #1 from adamkasper/add-support-for-relations-v2
Sqh3rd May 12, 2026
081a7a9
Merge branch 'add-support-for-relations-v2' of github.com:Sqh3rd/nuxt…
Sqh3rd May 12, 2026
d9fe3c1
Merge branch 'main' into add-support-for-relations-v2
Sqh3rd May 22, 2026
52d3a59
Merge branch 'main' into add-support-for-relations-v2
Sqh3rd Jul 28, 2026
dadfae4
wip: fix issues
Sqh3rd Aug 24, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
{
"eslint.useFlatConfig": true
"eslint.useFlatConfig": true,
"editor.codeActionsOnSave": {
"source.fixAll.prettier": "never",
"source.sortImports": "never"
}
}
108 changes: 98 additions & 10 deletions docs/content/docs/2.database/1.index.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ NuxtHub Database provides a type-safe SQL database powered by [Drizzle ORM](http
::steps{level="3"}
### Install dependencies

Install Drizzle ORM, Drizzle Kit, and the appropriate driver(s) for the database you are using:
Install Drizzle ORM and Drizzle Kit alongside the appropriate driver(s) for your database:

::tabs{sync="database-dialect"}
:::tabs-item{label="PostgreSQL" icon="i-simple-icons-postgresql"}
:pm-install{name="drizzle-orm drizzle-kit postgres @electric-sql/pglite"}
:pm-install{name="drizzle-orm@beta drizzle-kit@beta postgres @electric-sql/pglite"}
::callout
NuxtHub automatically detects your database connection using environment variables:
- Uses `PGlite` (embedded PostgreSQL) if no environment variables are set.
Expand All @@ -24,15 +24,15 @@ Install Drizzle ORM, Drizzle Kit, and the appropriate driver(s) for the database
::
:::
:::tabs-item{label="MySQL" icon="i-simple-icons-mysql"}
:pm-install{name="drizzle-orm drizzle-kit mysql2"}
:pm-install{name="drizzle-orm@beta drizzle-kit@beta mysql2"}
::callout
NuxtHub automatically detects your database connection using environment variables:
- Uses `mysql2` driver if you set `DATABASE_URL` or `MYSQL_URL` environment variable.
- Requires environment variable (no local fallback).
::
:::
:::tabs-item{label="SQLite" icon="i-simple-icons-sqlite"}
:pm-install{name="drizzle-orm drizzle-kit @libsql/client"}
:pm-install{name="drizzle-orm@beta drizzle-kit@beta @libsql/client"}
::callout
NuxtHub automatically detects your database connection using environment variables:
- Uses `libsql` driver for [Turso](https://turso.tech) if you set `TURSO_DATABASE_URL` and `TURSO_AUTH_TOKEN` environment variables.
Expand All @@ -50,7 +50,7 @@ For containerized deployments, you can defer environment variables to runtime.

### Set SQL dialect

Enable the database in your `nuxt.config.ts` by setting the `db` property to your desired SQL dialect:
Enable the database in your `nuxt.config.ts` by setting the `db` property with your SQL dialect:

::tabs{sync="database-dialect"}
:::tabs-item{label="PostgreSQL" icon="i-simple-icons-postgresql"}
Expand Down Expand Up @@ -133,8 +133,93 @@ Create your database schema with full TypeScript support using Drizzle ORM:
:::
::

### Define relations

Define relations between your tables using [`defineRelationsPart`](https://orm.drizzle.team/docs/relations-v2#relations-parts) from `drizzle-orm`. Name each export descriptively (e.g., `postsRelations`) to avoid conflicts with other schema exports:

::tabs{sync="database-dialect"}
:::tabs-item{label="PostgreSQL" icon="i-simple-icons-postgresql"}
```ts [server/db/schema/posts.ts]
import { users } from '@nuxthub/db/schema'
import { defineRelationsPart } from 'drizzle-orm'
import { pgTable, text, serial, integer, timestamp } from 'drizzle-orm/pg-core'

export const posts = pgTable('posts', {
id: serial().primaryKey(),
title: text().notNull(),
content: text().notNull(),
authorId: integer('author_id').references(() => users.id),
createdAt: timestamp().notNull().defaultNow(),
})

export const postsRelations = defineRelationsPart({ posts, users }, r => ({
posts: {
author: r.one.users({
from: r.posts.authorId,
to: r.users.id
})
}
}))
```
:::

:::tabs-item{label="MySQL" icon="i-simple-icons-mysql"}
```ts [server/db/schema/posts.ts]
import { users } from '@nuxthub/db/schema'
import { defineRelationsPart } from 'drizzle-orm'
import { mysqlTable, text, serial, int, timestamp } from 'drizzle-orm/mysql-core'

export const posts = mysqlTable('posts', {
id: serial().primaryKey(),
title: text().notNull(),
content: text().notNull(),
authorId: int('author_id').references(() => users.id),
createdAt: timestamp().notNull().defaultNow(),
})

export const postsRelations = defineRelationsPart({ posts, users }, r => ({
posts: {
author: r.one.users({
from: r.posts.authorId,
to: r.users.id
})
}
}))
```
:::

:::tabs-item{label="SQLite" icon="i-simple-icons-sqlite"}
```ts [server/db/schema/posts.ts]
import { users } from '@nuxthub/db/schema'
import { defineRelationsPart, sql } from 'drizzle-orm'
import { sqliteTable, text, integer } from 'drizzle-orm/sqlite-core'

export const posts = sqliteTable('posts', {
id: integer().primaryKey({ autoIncrement: true }),
title: text().notNull(),
content: text().notNull(),
authorId: integer('author_id').references(() => users.id),
createdAt: integer({ mode: 'timestamp' }).notNull().default(sql`(unixepoch())`),
})

export const postsRelations = defineRelationsPart({ posts, users }, r => ({
posts: {
author: r.one.users({
from: r.posts.authorId,
to: r.users.id
})
}
}))
```
:::
::

::callout{icon="i-lucide-lightbulb"}
Import tables from other schema files using `@nuxthub/db/schema`. NuxtHub auto-merges all your schema files and makes them available through this import.
::

::callout{to="/docs/database/schema"}
Learn more about defining the **database schema files**.
Learn more about **schema files**, **relations patterns**, and **sharing types** with your Vue app.
::

### Generate migrations
Expand Down Expand Up @@ -460,7 +545,7 @@ Use direct database connection string instead of pooling when configuring Hyperd

## AI Agents

If you work with an IDE that supports AI agents, you can add the following text in your `Agents.md` or `.cursor/rules` file:
If you work with an IDE that supports AI agents, add the following text in your `AGENTS.md` or `.cursor/rules` file:

```md
# Agent Instructions
Expand All @@ -469,15 +554,18 @@ If you work with an IDE that supports AI agents, you can add the following text

## Database

- **Database Dialect**: The database dialect is set in the `nuxt.config.ts` file, within the `hub.db` option or `hub.db.dialect` property.
- **Database Dialect**: The database dialect is set in the `nuxt.config.ts` file under `hub.db.dialect`.
- **Drizzle ORM v1 Beta**: This project uses `drizzle-orm@beta` and `drizzle-kit@beta` with relations v2 enabled (`useRelationsV2: true`).
- **Drizzle Config**: Don't generate the `drizzle.config.ts` file manually, it is generated automatically by NuxtHub.
- **Generate Migrations**: Use `npx nuxt db generate` to automatically generate database migrations from schema changes
- **Never Write Manual Migrations**: Do not manually create SQL migration files in the `server/db/migrations/` directory
- **Generate Migrations**: Use `npx nuxt db generate` to automatically generate database migrations from schema changes.
- **Never Write Manual Migrations**: Do not manually create SQL migration files in the `server/db/migrations/` directory.
- **Relations**: Use `defineRelationsPart` from `drizzle-orm` to define relations in each schema file. Name exports descriptively (e.g., `postsRelations`) to avoid conflicts.
- **Workflow**:
1. Create or modify the database schema in `server/db/schema.ts` or any other schema file in the `server/db/schema/` directory
2. Run `npx nuxt db generate` to generate the migration
3. Run `npx nuxt db migrate` to apply the migration to the database, or run `npx nuxt dev` to apply the migration during development
- **Access the database**: Use the `db` instance from `@nuxthub/db` (or `hub:db` for backwards compatibility) to query the database, it is a Drizzle ORM instance.
- **Relational queries**: Use the v2 query API with object-based `where` and `orderBy` (e.g., `db.query.posts.findMany({ where: { authorId: 1 }, orderBy: { createdAt: 'desc' } })`).
```

## Migrating from v0.9
Expand Down
Loading