Skip to content

feat(db): support drizzle orm v1 & relations v2 - #825

Open
Sqh3rd wants to merge 27 commits into
nuxt-hub:mainfrom
Sqh3rd:add-support-for-relations-v2
Open

feat(db): support drizzle orm v1 & relations v2#825
Sqh3rd wants to merge 27 commits into
nuxt-hub:mainfrom
Sqh3rd:add-support-for-relations-v2

Conversation

@Sqh3rd

@Sqh3rd Sqh3rd commented Feb 9, 2026

Copy link
Copy Markdown

Adds support for drizzle relations v2 introduced in drizzle v1-beta

Closes #764

@vercel

vercel Bot commented Feb 9, 2026

Copy link
Copy Markdown

@Sqh3rd is attempting to deploy a commit to the NuxtLabs Team on Vercel.

A member of the Team first needs to authorize it.

@RihanArfan
RihanArfan self-requested a review February 10, 2026 03:31
@branislavjuhaas

Copy link
Copy Markdown
Contributor

I love this concept!

@RihanArfan RihanArfan changed the title Add support for relations v2 feat(db): support drizzle orm v1 & relations v2 Feb 10, 2026
@Sqh3rd

Sqh3rd commented Feb 11, 2026

Copy link
Copy Markdown
Author

Just noticed that the type definition I added does not properly handle the case when there's multiple relationParts defined. I'll take a look today and tomorrow in order to fix that

@RihanArfan RihanArfan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this PR! 💚 Left one small comment, but otherwise looks great.

Comment thread playground/server/db/schema/author.postgresql.ts Outdated
@RihanArfan

Copy link
Copy Markdown
Member

Right now we're waiting for the Drizzle Team to add API support for Drizzle Studio MySQL and SQLite in v1.

Then we'll need to fix a bug with nuxt db squash now that there's no more journal, and document the requireddrizzle-kit up command for upgrading.

@Sqh3rd

Sqh3rd commented Feb 15, 2026

Copy link
Copy Markdown
Author

thanks a lot for your support! If you need/want any support from me please ping me, otherwise I'll mark this PR in my mind as "basically completed" 😄

@adamkasper

Copy link
Copy Markdown
Contributor

@RihanArfan hey, could you enable https://pkg.pr.new for this PR? :)

@pkg-pr-new

pkg-pr-new Bot commented Feb 16, 2026

Copy link
Copy Markdown
npm i https://pkg.pr.new/@nuxthub/core@825

commit: 55e4f03

@adamkasper

adamkasper commented May 10, 2026

Copy link
Copy Markdown
Contributor

Hey @Sqh3rd, just a heads up — Drizzle v1.0.0-rc.1 shipped a breaking change to the casing API (it's no longer set on the db instance but on the table/view/schema), so the casing option in the generated drizzle({...}) templates will need a rework before this can be merged. 🙂

Sqh3rd#1

@Sqh3rd

Sqh3rd commented May 12, 2026

Copy link
Copy Markdown
Author

Hey @Sqh3rd, just a heads up — Drizzle v1.0.0-rc.1 shipped a breaking change to the casing API (it's no longer set on the db instance but on the table/view/schema), so the casing option in the generated drizzle({...}) templates will need a rework before this can be merged. 🙂

Sqh3rd#1

Hey, thanks a lot for supporting @adamkasper. I'm starting to think that with the increases in template modification it may make more sense to use a templating engine (e.g. Mustache) to handle these things. Would it be fine to introduce sth like that? I'm not a 100% certain yet, whether that'd make it better. I'll try to write a poc in a few hours to get a feel for how that could look.

@Sqh3rd

Sqh3rd commented May 19, 2026

Copy link
Copy Markdown
Author

update regarding me wanting to introduce mustache: It seems that this is quite promising, but I don't want to further complicate this PR. I'll introduce another PR during this/next week, to tackle this. :)

@Sqh3rd

Sqh3rd commented May 22, 2026

Copy link
Copy Markdown
Author

PR for introducing mustache is open as draft, fyi: #900

@onmax onmax left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested the pkg.pr.new builds against Nuxt Better Auth's generated auth schema: repro. The SQLite/libSQL path passes, but I found the remaining driver and migration issues below

If there is anything I can do to move this PR forward let me know :)

Comment thread src/db/setup.ts
type SchemaType = typeof schema
export type Tables = ExtractTablesFromSchema<SchemaType>
type RelationsKeys = keyof SchemaType extends infer Key ? Key extends keyof SchemaType ? SchemaType[Key] extends ExtractTablesWithRelationsParts<any, any> ? Key : never : never : never
type FlatRelations = { [Key in keyof SchemaType[RelationsKeys]]: keyof SchemaType extends infer T ? T extends keyof SchemaType ? Key extends keyof SchemaType[T] ? SchemaType[T][Key] : never : never : never }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SchemaType[RelationsKeys] is a union, so keyof keeps only the keys shared by every relation file. With separate usersRelations and postsRelations, that can become never, and with queries stop typechecking. Turning the union into an intersection and adding a two-file type test would cover this.

Comment thread src/db/setup.ts
if (!url) throw new Error('DATABASE_URL, POSTGRES_URL, or POSTGRESQL_URL required')
const sql = neon(url)
_db = drizzle(sql, { schema${casingOption} })`
_db = drizzle(sql, { ${casingOption}${relationsOption} })`,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

relationsOption already includes the comma, so Neon, D1, and D1 HTTP generate { , relations }. These paths also lose schema when v2 is off. Keeping schema in all three templates and adding a syntax check for each driver would cover both cases.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah thx, do you think it'd be reasonable to get some reviews/progress on #900 first? I'm finding it hard to correctly adapt the generated files with the current generation logic :)

If that's not reasonable, I'll do the adaptations anyways tho

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, I am not a official maintainer of this repo, so I don't have a take.

Comment thread src/db/setup.ts
* The ${driver} database client.
*/
export const db: ReturnType<typeof drizzleCore<typeof schema>>
export const db: ReturnType<typeof drizzleCore<${useRelationsV2 ? 'typeof schema, typeof relations' : 'typeof schema'}>>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This generic order works for SQLite and MySQL, but not the RC.2 PostgreSQL drivers. PGlite, postgres-js, and Neon HTTP need drizzleCore<typeof relations> here.

Comment thread playground/package.json
"aws4fetch": "^1.0.20",
"drizzle-kit": "^0.31.9",
"drizzle-orm": "^0.45.2",
"drizzle-kit": "1.0.0-rc.2",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

drizzle-kit@1.0.0-rc.2 no longer exports drizzle-kit/api, but Studio still imports it in src/devtools.ts and launch-studio.post.dev.ts. Studio now fails with ERR_PACKAGE_PATH_NOT_EXPORTED. The imports need the dialect-specific api-* entry points.

@@ -1,20 +0,0 @@
{

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nuxt db squash still reads meta/_journal.json, so removing these journals breaks the command. Supporting the v1 layout and documenting the drizzle-kit up step would keep it working.

Comment thread src/db/setup.ts
: generateLazyDbTemplate(
`import { drizzle } from 'drizzle-orm/${driver}'`,
` const hyperdrive = process.env.${bindingName} || globalThis.__env__?.${bindingName} || globalThis.${bindingName}
`import { drizzle } from 'drizzle-orm/${driver}'${relationsImport}`,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pnpm lint fails here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

drizzle v1 support

5 participants