drizzle-team / drizzle-team/drizzle-orm

[FEATURE]: Unwrap chained relations in `with` operator if `columns` is empty or `false`

Open
#2,462 0 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
TypeScript
Stars
35.8k
Forks
1.6k
Avg merge
2d 7h
Merged PRs (30d)
4

Description

### Describe what you want

Let's say we have a `N:N` relation between tables `A` and `B` through the `A_to_B` table.

When I query `A` and want all the `B` without any column from `A_to_B`, what I get is this

``` typescript
{
id: number;
someOtherAColumn: string;
a_to_b: {
b: {
id: number;
someOtherBColumn: string;
}
}[]
}[]
```

Instead it would be better in my opinion to have a result that looks something like this:

``` typescript
{
id: number;
someOtherAColumn: string;
b: {
id: number;
someOtherBColumn: string;
}[]
}[]
```

To get this result, you would have to explicitly state to not be interested in any of the middle table columns like this:

``` typescript
const myARowsWithB = db.query.A.findMany({
with: {
aToB: {
columns: false, // Or even columns: {}
with: {
b: true,
}
},
}
})

// Now you can do this
myARowsWithB[0].b.map(...)
```

This behaviour could be extended not only to a `N:N` relation but in general to any chain of relations when you are only interested in the very last `JOIN`ed table like this:

``` typescript
const myARowsWithD = await db.query.A.findMany({
with: {
aToB: {
columns: false,
with: {
b: {
columns: false,
with: {
c: {
columns: false,
with: {
d: true,
},
},
},
},
}
},
}
})

// Now you can do this
myARowsWithD[0].d.map(...)
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.