Skip to content

Commit 434d36e

Browse files
authored
V0.43.0 (#546)
* Cross, Lateral joins * Fixed resulting SQL * Removed `on` clause from cross join lateral
1 parent 17baaf9 commit 434d36e

File tree

1 file changed

+102
-1
lines changed

1 file changed

+102
-1
lines changed

src/content/docs/joins.mdx

Lines changed: 102 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Join clause in SQL is used to combine 2 or more tables, based on related columns
77
Drizzle ORM joins syntax is a balance between the SQL-likeness and type safety.
88

99
## Join types
10-
Drizzle ORM has APIs for `INNER JOIN`, `FULL JOIN`, `LEFT JOIN` and `RIGHT JOIN`.
10+
Drizzle ORM has APIs for `INNER JOIN [LATERAL]`, `FULL JOIN`, `LEFT JOIN [LATERAL]`, `RIGHT JOIN`, `CROSS JOIN [LATERAL]`.
1111
Lets have a quick look at examples based on below table schemas:
1212
```typescript copy
1313
export const users = pgTable('users', {
@@ -46,6 +46,31 @@ const result: {
4646
```
4747
</Section>
4848

49+
### Left Join Lateral
50+
<Section>
51+
```typescript copy
52+
const subquery = db.select().from(pets).where(gte(users.age, 16)).as('userPets')
53+
const result = await db.select().from(users).leftJoinLateral(subquery, sql`true`)
54+
```
55+
```sql
56+
select ... from "users" left join lateral (select ... from "pets" where "users"."age" >= 16) "userPets" on true
57+
```
58+
```typescript
59+
// result type
60+
const result: {
61+
user: {
62+
id: number;
63+
name: string;
64+
};
65+
userPets: {
66+
id: number;
67+
name: string;
68+
ownerId: number;
69+
} | null;
70+
}[];
71+
```
72+
</Section>
73+
4974
### Right Join
5075
<Section>
5176
```typescript copy
@@ -69,6 +94,7 @@ const result: {
6994
}[];
7095
```
7196
</Section>
97+
7298
### Inner Join
7399
<Section>
74100
```typescript copy
@@ -92,6 +118,32 @@ const result: {
92118
}[];
93119
```
94120
</Section>
121+
122+
### Inner Join Lateral
123+
<Section>
124+
```typescript copy
125+
const subquery = db.select().from(pets).where(gte(users.age, 16)).as('userPets')
126+
const result = await db.select().from(users).innerJoinLateral(subquery, sql`true`)
127+
```
128+
```sql
129+
select ... from "users" inner join lateral (select ... from "pets" where "users"."age" >= 16) "userPets" on true
130+
```
131+
```typescript
132+
// result type
133+
const result: {
134+
user: {
135+
id: number;
136+
name: string;
137+
};
138+
userPets: {
139+
id: number;
140+
name: string;
141+
ownerId: number;
142+
};
143+
}[];
144+
```
145+
</Section>
146+
95147
### Full Join
96148
<Section>
97149
```typescript copy
@@ -116,6 +168,55 @@ const result: {
116168
```
117169
</Section>
118170

171+
### Cross Join
172+
<Section>
173+
```typescript copy
174+
const result = await db.select().from(users).crossJoin(pets)
175+
```
176+
```sql
177+
select ... from "users" cross join "pets"
178+
```
179+
```typescript
180+
// result type
181+
const result: {
182+
user: {
183+
id: number;
184+
name: string;
185+
};
186+
pets: {
187+
id: number;
188+
name: string;
189+
ownerId: number;
190+
};
191+
}[];
192+
```
193+
</Section>
194+
195+
### Cross Join Lateral
196+
<Section>
197+
```typescript copy
198+
const subquery = db.select().from(pets).where(gte(users.age, 16)).as('userPets')
199+
const result = await db.select().from(users).crossJoinLateral(subquery)
200+
```
201+
```sql
202+
select ... from "users" cross join lateral (select ... from "pets" where "users"."age" >= 16) "userPets"
203+
```
204+
```typescript
205+
// result type
206+
const result: {
207+
user: {
208+
id: number;
209+
name: string;
210+
};
211+
userPets: {
212+
id: number;
213+
name: string;
214+
ownerId: number;
215+
};
216+
}[];
217+
```
218+
</Section>
219+
119220
## Partial select
120221
If you need to select a particular subset of fields or to have a flat response type, Drizzle ORM
121222
supports joins with partial select and will automatically infer return type based on `.select({ ... })` structure.

0 commit comments

Comments
 (0)