feat(db): support drizzle orm v1 & relations v2 - #825
Conversation
|
@Sqh3rd is attempting to deploy a commit to the NuxtLabs Team on Vercel. A member of the Team first needs to authorize it. |
|
I love this concept! |
|
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
left a comment
There was a problem hiding this comment.
Thanks for this PR! 💚 Left one small comment, but otherwise looks great.
|
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 |
|
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" 😄 |
|
@RihanArfan hey, could you enable https://pkg.pr.new for this PR? :) |
commit: |
…hub-core into add-support-for-relations-v2
|
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 |
feat(db): upgrade drizzle to v1 rc.2
…hub-core into add-support-for-relations-v2
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. |
|
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. :) |
|
PR for introducing mustache is open as draft, fyi: #900 |
onmax
left a comment
There was a problem hiding this comment.
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 :)
| 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 } |
There was a problem hiding this comment.
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.
| 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} })`, |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Good point, I am not a official maintainer of this repo, so I don't have a take.
| * 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'}>> |
There was a problem hiding this comment.
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.
| "aws4fetch": "^1.0.20", | ||
| "drizzle-kit": "^0.31.9", | ||
| "drizzle-orm": "^0.45.2", | ||
| "drizzle-kit": "1.0.0-rc.2", |
There was a problem hiding this comment.
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 @@ | |||
| { | |||
There was a problem hiding this comment.
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.
| : generateLazyDbTemplate( | ||
| `import { drizzle } from 'drizzle-orm/${driver}'`, | ||
| ` const hyperdrive = process.env.${bindingName} || globalThis.__env__?.${bindingName} || globalThis.${bindingName} | ||
| `import { drizzle } from 'drizzle-orm/${driver}'${relationsImport}`, |
Adds support for drizzle relations v2 introduced in drizzle v1-beta
Closes #764