Skip to content

Commit 50a8b16

Browse files
authored
Fixed type issues with joins with certain variations of tsconfig (fixes #4535, #4457) (#4603)
1 parent 9865e63 commit 50a8b16

File tree

21 files changed

+9727
-7530
lines changed

21 files changed

+9727
-7530
lines changed

changelogs/drizzle-orm/0.44.2.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- [BUG]: Fixed type issues with joins with certain variations of `tsconfig`: [#4535](https://github.com/drizzle-team/drizzle-orm/issues/4535), [#4457](https://github.com/drizzle-team/drizzle-orm/issues/4457)

drizzle-orm/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "drizzle-orm",
3-
"version": "0.44.1",
3+
"version": "0.44.2",
44
"description": "Drizzle ORM package for SQL databases",
55
"type": "module",
66
"scripts": {

drizzle-orm/src/gel-core/query-builders/select.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import type {
4141
CreateGelSelectFromBuilderMode,
4242
GelCreateSetOperatorFn,
4343
GelSelectConfig,
44+
GelSelectCrossJoinFn,
4445
GelSelectDynamic,
4546
GelSelectHKT,
4647
GelSelectHKTBase,
@@ -224,8 +225,10 @@ export abstract class GelSelectQueryBuilderBase<
224225
>(
225226
joinType: TJoinType,
226227
lateral: TIsLateral,
227-
): GelSelectJoinFn<this, TDynamic, TJoinType, TIsLateral> {
228-
return (
228+
): 'cross' extends TJoinType ? GelSelectCrossJoinFn<this, TDynamic, TIsLateral>
229+
: GelSelectJoinFn<this, TDynamic, TJoinType, TIsLateral>
230+
{
231+
return ((
229232
table: GelTable | Subquery | GelViewBase | SQL,
230233
on?: ((aliases: TSelection) => SQL | undefined) | SQL | undefined,
231234
) => {
@@ -300,7 +303,7 @@ export abstract class GelSelectQueryBuilderBase<
300303
}
301304

302305
return this as any;
303-
};
306+
}) as any;
304307
}
305308

306309
/**

drizzle-orm/src/gel-core/query-builders/select.types.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -113,17 +113,22 @@ export type GelSelectJoinFn<
113113
TDynamic extends boolean,
114114
TJoinType extends JoinType,
115115
TIsLateral extends boolean,
116-
> = 'cross' extends TJoinType ? <
117-
TJoinedTable extends (TIsLateral extends true ? Subquery | SQL : GelTable | Subquery | GelViewBase | SQL),
118-
TJoinedName extends GetSelectTableName<TJoinedTable> = GetSelectTableName<TJoinedTable>,
119-
>(table: TJoinedTable) => GelSelectJoin<T, TDynamic, TJoinType, TJoinedTable, TJoinedName>
120-
: <
121-
TJoinedTable extends (TIsLateral extends true ? Subquery | SQL : GelTable | Subquery | GelViewBase | SQL),
122-
TJoinedName extends GetSelectTableName<TJoinedTable> = GetSelectTableName<TJoinedTable>,
123-
>(
124-
table: TJoinedTable,
125-
on: ((aliases: T['_']['selection']) => SQL | undefined) | SQL | undefined,
126-
) => GelSelectJoin<T, TDynamic, TJoinType, TJoinedTable, TJoinedName>;
116+
> = <
117+
TJoinedTable extends (TIsLateral extends true ? Subquery | SQL : GelTable | Subquery | GelViewBase | SQL),
118+
TJoinedName extends GetSelectTableName<TJoinedTable> = GetSelectTableName<TJoinedTable>,
119+
>(
120+
table: TJoinedTable,
121+
on: ((aliases: T['_']['selection']) => SQL | undefined) | SQL | undefined,
122+
) => GelSelectJoin<T, TDynamic, TJoinType, TJoinedTable, TJoinedName>;
123+
124+
export type GelSelectCrossJoinFn<
125+
T extends AnyGelSelectQueryBuilder,
126+
TDynamic extends boolean,
127+
TIsLateral extends boolean,
128+
> = <
129+
TJoinedTable extends (TIsLateral extends true ? Subquery | SQL : GelTable | Subquery | GelViewBase | SQL),
130+
TJoinedName extends GetSelectTableName<TJoinedTable> = GetSelectTableName<TJoinedTable>,
131+
>(table: TJoinedTable) => GelSelectJoin<T, TDynamic, 'cross', TJoinedTable, TJoinedName>;
127132

128133
export type SelectedFieldsFlat = SelectedFieldsFlatBase<GelColumn>;
129134

drizzle-orm/src/mysql-core/query-builders/select.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import type {
3434
LockConfig,
3535
LockStrength,
3636
MySqlCreateSetOperatorFn,
37+
MySqlCrossJoinFn,
3738
MySqlJoinFn,
3839
MySqlJoinType,
3940
MySqlSelectConfig,
@@ -237,7 +238,9 @@ export abstract class MySqlSelectQueryBuilderBase<
237238
>(
238239
joinType: TJoinType,
239240
lateral: TIsLateral,
240-
): MySqlJoinFn<this, TDynamic, TJoinType, TIsLateral> {
241+
): 'cross' extends TJoinType ? MySqlCrossJoinFn<this, TDynamic, TIsLateral>
242+
: MySqlJoinFn<this, TDynamic, TJoinType, TIsLateral>
243+
{
241244
return <
242245
TJoinedTable extends MySqlTable | Subquery | MySqlViewBase | SQL,
243246
>(

drizzle-orm/src/mysql-core/query-builders/select.types.ts

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -119,27 +119,32 @@ export type MySqlJoinFn<
119119
TDynamic extends boolean,
120120
TJoinType extends MySqlJoinType,
121121
TIsLateral extends boolean,
122-
> = 'cross' extends TJoinType ? <
123-
TJoinedTable extends (TIsLateral extends true ? Subquery | SQL : MySqlTable | Subquery | MySqlViewBase | SQL),
124-
TJoinedName extends GetSelectTableName<TJoinedTable> = GetSelectTableName<TJoinedTable>,
125-
>(
126-
table: TJoinedTable,
127-
onIndex?:
128-
| (TJoinedTable extends MySqlTable ? IndexConfig
129-
: 'Index hint configuration is allowed only for MySqlTable and not for subqueries or views')
130-
| undefined,
131-
) => MySqlJoin<T, TDynamic, TJoinType, TJoinedTable, TJoinedName>
132-
: <
133-
TJoinedTable extends (TIsLateral extends true ? Subquery | SQL : MySqlTable | Subquery | MySqlViewBase | SQL),
134-
TJoinedName extends GetSelectTableName<TJoinedTable> = GetSelectTableName<TJoinedTable>,
135-
>(
136-
table: TJoinedTable,
137-
on: ((aliases: T['_']['selection']) => SQL | undefined) | SQL | undefined,
138-
onIndex?:
139-
| (TJoinedTable extends MySqlTable ? IndexConfig
140-
: 'Index hint configuration is allowed only for MySqlTable and not for subqueries or views')
141-
| undefined,
142-
) => MySqlJoin<T, TDynamic, TJoinType, TJoinedTable, TJoinedName>;
122+
> = <
123+
TJoinedTable extends (TIsLateral extends true ? Subquery | SQL : MySqlTable | Subquery | MySqlViewBase | SQL),
124+
TJoinedName extends GetSelectTableName<TJoinedTable> = GetSelectTableName<TJoinedTable>,
125+
>(
126+
table: TJoinedTable,
127+
on: ((aliases: T['_']['selection']) => SQL | undefined) | SQL | undefined,
128+
onIndex?:
129+
| (TJoinedTable extends MySqlTable ? IndexConfig
130+
: 'Index hint configuration is allowed only for MySqlTable and not for subqueries or views')
131+
| undefined,
132+
) => MySqlJoin<T, TDynamic, TJoinType, TJoinedTable, TJoinedName>;
133+
134+
export type MySqlCrossJoinFn<
135+
T extends AnyMySqlSelectQueryBuilder,
136+
TDynamic extends boolean,
137+
TIsLateral extends boolean,
138+
> = <
139+
TJoinedTable extends (TIsLateral extends true ? Subquery | SQL : MySqlTable | Subquery | MySqlViewBase | SQL),
140+
TJoinedName extends GetSelectTableName<TJoinedTable> = GetSelectTableName<TJoinedTable>,
141+
>(
142+
table: TJoinedTable,
143+
onIndex?:
144+
| (TJoinedTable extends MySqlTable ? IndexConfig
145+
: 'Index hint configuration is allowed only for MySqlTable and not for subqueries or views')
146+
| undefined,
147+
) => MySqlJoin<T, TDynamic, 'cross', TJoinedTable, TJoinedName>;
143148

144149
export type SelectedFieldsFlat = SelectedFieldsFlatBase<MySqlColumn>;
145150

drizzle-orm/src/pg-core/query-builders/select.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import type {
4545
LockStrength,
4646
PgCreateSetOperatorFn,
4747
PgSelectConfig,
48+
PgSelectCrossJoinFn,
4849
PgSelectDynamic,
4950
PgSelectHKT,
5051
PgSelectHKTBase,
@@ -231,7 +232,9 @@ export abstract class PgSelectQueryBuilderBase<
231232
>(
232233
joinType: TJoinType,
233234
lateral: TIsLateral,
234-
): PgSelectJoinFn<this, TDynamic, TJoinType, TIsLateral> {
235+
): 'cross' extends TJoinType ? PgSelectCrossJoinFn<this, TDynamic, TIsLateral>
236+
: PgSelectJoinFn<this, TDynamic, TJoinType, TIsLateral>
237+
{
235238
return ((
236239
table: TIsLateral extends true ? Subquery | SQL : PgTable | Subquery | PgViewBase | SQL,
237240
on?: ((aliases: TSelection) => SQL | undefined) | SQL | undefined,

drizzle-orm/src/pg-core/query-builders/select.types.ts

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -117,25 +117,30 @@ export type PgSelectJoinFn<
117117
TDynamic extends boolean,
118118
TJoinType extends JoinType,
119119
TIsLateral extends boolean,
120-
> = 'cross' extends TJoinType ? <
121-
TJoinedTable extends (TIsLateral extends true ? Subquery | SQL : PgTable | Subquery | PgViewBase | SQL),
122-
TJoinedName extends GetSelectTableName<TJoinedTable> = GetSelectTableName<TJoinedTable>,
123-
>(
124-
table: TableLikeHasEmptySelection<TJoinedTable> extends true ? DrizzleTypeError<
125-
"Cannot reference a data-modifying statement subquery if it doesn't contain a `returning` clause"
126-
>
127-
: TJoinedTable,
128-
) => PgSelectJoin<T, TDynamic, TJoinType, TJoinedTable, TJoinedName>
129-
: <
130-
TJoinedTable extends (TIsLateral extends true ? Subquery | SQL : PgTable | Subquery | PgViewBase | SQL),
131-
TJoinedName extends GetSelectTableName<TJoinedTable> = GetSelectTableName<TJoinedTable>,
132-
>(
133-
table: TableLikeHasEmptySelection<TJoinedTable> extends true ? DrizzleTypeError<
134-
"Cannot reference a data-modifying statement subquery if it doesn't contain a `returning` clause"
135-
>
136-
: TJoinedTable,
137-
on: ((aliases: T['_']['selection']) => SQL | undefined) | SQL | undefined,
138-
) => PgSelectJoin<T, TDynamic, TJoinType, TJoinedTable, TJoinedName>;
120+
> = <
121+
TJoinedTable extends (TIsLateral extends true ? Subquery | SQL : PgTable | Subquery | PgViewBase | SQL),
122+
TJoinedName extends GetSelectTableName<TJoinedTable> = GetSelectTableName<TJoinedTable>,
123+
>(
124+
table: TableLikeHasEmptySelection<TJoinedTable> extends true ? DrizzleTypeError<
125+
"Cannot reference a data-modifying statement subquery if it doesn't contain a `returning` clause"
126+
>
127+
: TJoinedTable,
128+
on: ((aliases: T['_']['selection']) => SQL | undefined) | SQL | undefined,
129+
) => PgSelectJoin<T, TDynamic, TJoinType, TJoinedTable, TJoinedName>;
130+
131+
export type PgSelectCrossJoinFn<
132+
T extends AnyPgSelectQueryBuilder,
133+
TDynamic extends boolean,
134+
TIsLateral extends boolean,
135+
> = <
136+
TJoinedTable extends (TIsLateral extends true ? Subquery | SQL : PgTable | Subquery | PgViewBase | SQL),
137+
TJoinedName extends GetSelectTableName<TJoinedTable> = GetSelectTableName<TJoinedTable>,
138+
>(
139+
table: TableLikeHasEmptySelection<TJoinedTable> extends true ? DrizzleTypeError<
140+
"Cannot reference a data-modifying statement subquery if it doesn't contain a `returning` clause"
141+
>
142+
: TJoinedTable,
143+
) => PgSelectJoin<T, TDynamic, 'cross', TJoinedTable, TJoinedName>;
139144

140145
export type SelectedFieldsFlat = SelectedFieldsFlatBase<PgColumn>;
141146

drizzle-orm/src/singlestore-core/query-builders/select.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import type {
4444
SelectedFields,
4545
SetOperatorRightSelect,
4646
SingleStoreCreateSetOperatorFn,
47+
SingleStoreCrossJoinFn,
4748
SingleStoreJoinFn,
4849
SingleStoreSelectConfig,
4950
SingleStoreSelectDynamic,
@@ -210,7 +211,9 @@ export abstract class SingleStoreSelectQueryBuilderBase<
210211
>(
211212
joinType: TJoinType,
212213
lateral: TIsLateral,
213-
): SingleStoreJoinFn<this, TDynamic, TJoinType, TIsLateral> {
214+
): 'cross' extends TJoinType ? SingleStoreCrossJoinFn<this, TDynamic, TIsLateral>
215+
: SingleStoreJoinFn<this, TDynamic, TJoinType, TIsLateral>
216+
{
214217
return (
215218
table: SingleStoreTable | Subquery | SQL, // | SingleStoreViewBase
216219
on?: ((aliases: TSelection) => SQL | undefined) | SQL | undefined,

drizzle-orm/src/singlestore-core/query-builders/select.types.ts

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -111,19 +111,24 @@ export type SingleStoreJoinFn<
111111
TDynamic extends boolean,
112112
TJoinType extends JoinType,
113113
TIsLateral extends boolean,
114-
> = 'cross' extends TJoinType ? <
115-
TJoinedTable extends (TIsLateral extends true ? Subquery | SQL
116-
: SingleStoreTable | Subquery | SQL /* | SingleStoreViewBase */),
117-
TJoinedName extends GetSelectTableName<TJoinedTable> = GetSelectTableName<TJoinedTable>,
118-
>(table: TJoinedTable) => SingleStoreJoin<T, TDynamic, TJoinType, TJoinedTable, TJoinedName>
119-
: <
120-
TJoinedTable extends (TIsLateral extends true ? Subquery | SQL
121-
: SingleStoreTable | Subquery | SQL /* | SingleStoreViewBase */),
122-
TJoinedName extends GetSelectTableName<TJoinedTable> = GetSelectTableName<TJoinedTable>,
123-
>(
124-
table: TJoinedTable,
125-
on: ((aliases: T['_']['selection']) => SQL | undefined) | SQL | undefined,
126-
) => SingleStoreJoin<T, TDynamic, TJoinType, TJoinedTable, TJoinedName>;
114+
> = <
115+
TJoinedTable extends (TIsLateral extends true ? Subquery | SQL
116+
: SingleStoreTable | Subquery | SQL /* | SingleStoreViewBase */),
117+
TJoinedName extends GetSelectTableName<TJoinedTable> = GetSelectTableName<TJoinedTable>,
118+
>(
119+
table: TJoinedTable,
120+
on: ((aliases: T['_']['selection']) => SQL | undefined) | SQL | undefined,
121+
) => SingleStoreJoin<T, TDynamic, TJoinType, TJoinedTable, TJoinedName>;
122+
123+
export type SingleStoreCrossJoinFn<
124+
T extends AnySingleStoreSelectQueryBuilder,
125+
TDynamic extends boolean,
126+
TIsLateral extends boolean,
127+
> = <
128+
TJoinedTable extends (TIsLateral extends true ? Subquery | SQL
129+
: SingleStoreTable | Subquery | SQL /* | SingleStoreViewBase */),
130+
TJoinedName extends GetSelectTableName<TJoinedTable> = GetSelectTableName<TJoinedTable>,
131+
>(table: TJoinedTable) => SingleStoreJoin<T, TDynamic, 'cross', TJoinedTable, TJoinedName>;
127132

128133
export type SelectedFieldsFlat = SelectedFieldsFlatBase<SingleStoreColumn>;
129134

drizzle-orm/src/sqlite-core/query-builders/select.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import type {
4242
SetOperatorRightSelect,
4343
SQLiteCreateSetOperatorFn,
4444
SQLiteSelectConfig,
45+
SQLiteSelectCrossJoinFn,
4546
SQLiteSelectDynamic,
4647
SQLiteSelectExecute,
4748
SQLiteSelectHKT,
@@ -205,7 +206,9 @@ export abstract class SQLiteSelectQueryBuilderBase<
205206

206207
private createJoin<TJoinType extends JoinType>(
207208
joinType: TJoinType,
208-
): SQLiteSelectJoinFn<this, TDynamic, TJoinType> {
209+
): 'cross' extends TJoinType ? SQLiteSelectCrossJoinFn<this, TDynamic>
210+
: SQLiteSelectJoinFn<this, TDynamic, TJoinType>
211+
{
209212
return (
210213
table: SQLiteTable | Subquery | SQLiteViewBase | SQL,
211214
on?: ((aliases: TSelection) => SQL | undefined) | SQL | undefined,

drizzle-orm/src/sqlite-core/query-builders/select.types.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -107,17 +107,21 @@ export type SQLiteSelectJoinFn<
107107
T extends AnySQLiteSelectQueryBuilder,
108108
TDynamic extends boolean,
109109
TJoinType extends JoinType,
110-
> = 'cross' extends TJoinType ? <
111-
TJoinedTable extends SQLiteTable | Subquery | SQLiteViewBase | SQL,
112-
TJoinedName extends GetSelectTableName<TJoinedTable> = GetSelectTableName<TJoinedTable>,
113-
>(table: TJoinedTable) => SQLiteSelectJoin<T, TDynamic, TJoinType, TJoinedTable, TJoinedName>
114-
: <
115-
TJoinedTable extends SQLiteTable | Subquery | SQLiteViewBase | SQL,
116-
TJoinedName extends GetSelectTableName<TJoinedTable> = GetSelectTableName<TJoinedTable>,
117-
>(
118-
table: TJoinedTable,
119-
on: ((aliases: T['_']['selection']) => SQL | undefined) | SQL | undefined,
120-
) => SQLiteSelectJoin<T, TDynamic, TJoinType, TJoinedTable, TJoinedName>;
110+
> = <
111+
TJoinedTable extends SQLiteTable | Subquery | SQLiteViewBase | SQL,
112+
TJoinedName extends GetSelectTableName<TJoinedTable> = GetSelectTableName<TJoinedTable>,
113+
>(
114+
table: TJoinedTable,
115+
on: ((aliases: T['_']['selection']) => SQL | undefined) | SQL | undefined,
116+
) => SQLiteSelectJoin<T, TDynamic, TJoinType, TJoinedTable, TJoinedName>;
117+
118+
export type SQLiteSelectCrossJoinFn<
119+
T extends AnySQLiteSelectQueryBuilder,
120+
TDynamic extends boolean,
121+
> = <
122+
TJoinedTable extends SQLiteTable | Subquery | SQLiteViewBase | SQL,
123+
TJoinedName extends GetSelectTableName<TJoinedTable> = GetSelectTableName<TJoinedTable>,
124+
>(table: TJoinedTable) => SQLiteSelectJoin<T, TDynamic, 'cross', TJoinedTable, TJoinedName>;
121125

122126
export type SelectedFieldsFlat = SelectFieldsFlatBase<SQLiteColumn>;
123127

integration-tests/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "",
55
"type": "module",
66
"scripts": {
7-
"test:types": "tsc",
7+
"test:types": "tsc && cd type-tests/join-nodenext && tsc",
88
"test": "pnpm test:vitest",
99
"test:vitest": "vitest run --pass-with-no-tests",
1010
"test:esm": "node tests/imports.test.mjs && node tests/imports.test.cjs",
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { eq } from 'drizzle-orm';
2+
import { drizzle } from 'drizzle-orm/gel';
3+
import { gelTable, text, uuid } from 'drizzle-orm/gel-core';
4+
import { expectTypeOf } from 'vitest';
5+
6+
const account = gelTable('accounts', {
7+
id: uuid('id').primaryKey().notNull(),
8+
userId: uuid('user_id')
9+
.notNull()
10+
.references(() => users.id),
11+
meta: text().notNull(),
12+
});
13+
14+
const users = gelTable('users', {
15+
id: uuid('id').primaryKey(),
16+
name: text('name').notNull(),
17+
username: text('username').notNull().unique(),
18+
});
19+
20+
const db = drizzle.mock();
21+
22+
(async () => {
23+
const res = await db.select()
24+
.from(users)
25+
.innerJoin(account, eq(users.id, account.id));
26+
27+
expectTypeOf(res).toEqualTypeOf<{
28+
accounts: {
29+
id: string;
30+
userId: string;
31+
meta: string;
32+
};
33+
users: {
34+
id: string;
35+
name: string;
36+
username: string;
37+
};
38+
}[]>();
39+
});

0 commit comments

Comments
 (0)