drizzle-team / drizzle-team/drizzle-orm

[FEATURE]: ATTACH support

Open
#2,052 8 comments 16 reactions 0 assignees View on GitHub
db/sqlite driver/libsql enhancement qb/crud
Dominant language
TypeScript
Stars
35.8k
Forks
1.6k
Avg merge
2d 7h
Merged PRs (30d)
4

Description

### 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](https://gist.github.com/avinassh/ae33d3a8f73dd64daf9d58aabac03500) would love the feature to be implemented.

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

```ts
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.

Contributor guide

Open the contributing guide

Research direction

Start by tracing the createClient() entry point and the SQLite-related APIs shown in the proposal, including db_one.attach(attached), alias, and the separate client/schema imports. Determine how Turso database IDs and ATTACH support would fit the proposed query-building API, then define what equivalent behavior should mean for other databases before implementation.

Written by the indexing model from the issue text.

Assessment

Tech stack
sqlite, typescript
Domain
backend-api-design, databases
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.