drizzle-team / drizzle-orm

Headless TypeScript ORM with a head. Runs on Node, Bun and Deno. Lives on the Edge and yes, it's a JavaScript ORM too 😅
https://orm.drizzle.team
Apache License 2.0
23.52k stars 577 forks source link

[FEATURE]: ATTACH support #2052

Open themixednuts opened 6 months ago

themixednuts commented 6 months ago

Describe what you want

I know this is probably on your radar but wanted to put this in. With the ability to now ATTACH db's in Turso would love the feature to be implemented.

This is my suggestion and would require Turso to have createClient() return the id of the db.

import { db as db_one } from '../server/db/first/client'
import { db as db_two } from '../server/db/two/client'
import { users as users_one } from '../server/db/one/schema'
import { users as users_two } from '../server/db/two/schema'
import { alias } from 'drizzle-orm/sqlite-core'
import { eq } from 'drizzle-orm'

const attached = alias(db_two, 'attached')

// stupid query I know, lets just say ones normal users, the other is managers/admins
const res = await db_one.attach(attached).select().from(users_two).where(eq(users_two.id, users_one.id))

This way we keep the drizzle way of building, apis stay similiar. You also get the win of separate clients/schemas, and can implement attach for other databases that don't support the ATTACH keyword but drizzle can do some magic under the hood etc.

adriangalilea commented 5 months ago

Please drizzle-team :)

predaytor commented 5 months ago

must have! waitin..

natanparmigianoreis commented 5 months ago

+1 on this, only feature missing for us to adopt Drizzle!

Pramuspl commented 5 months ago

+1. Would be great!