Skip to content

Commit 2b77cfc

Browse files
Updated:
- onIndex config params now can accept 1 param without array, or multiple params in array - Added toArray function which will check if value array -> return value, if not -> return [value] - Updated mysql-common tests > Added tests to check sql query include correct USE INDEX options for 1 and multiple indexes - Updated type-tests
1 parent 5d53945 commit 2b77cfc

File tree

4 files changed

+272
-119
lines changed

4 files changed

+272
-119
lines changed

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import type { ValueOrArray } from '~/utils.ts';
2525
import { applyMixins, getTableColumns, getTableLikeName, haveSameKeys, orderSelectedFields } from '~/utils.ts';
2626
import { ViewBaseConfig } from '~/view-common.ts';
2727
import type { IndexBuilder } from '../indexes.ts';
28-
import { convertIndexToString } from '../utils.ts';
28+
import { convertIndexToString, toArray } from '../utils.ts';
2929
import { MySqlViewBase } from '../view-base.ts';
3030
import type {
3131
AnyMySqlSelect,
@@ -50,9 +50,9 @@ import type {
5050
export type IndexForHint = IndexBuilder | string;
5151

5252
export type IndexConfig = {
53-
useIndex?: IndexForHint[];
54-
forceIndex?: IndexForHint[];
55-
ignoreIndex?: IndexForHint[];
53+
useIndex?: IndexForHint | IndexForHint[];
54+
forceIndex?: IndexForHint | IndexForHint[];
55+
ignoreIndex?: IndexForHint | IndexForHint[];
5656
};
5757

5858
export class MySqlSelectBuilder<
@@ -122,13 +122,13 @@ export class MySqlSelectBuilder<
122122
let ignoreIndex: string[] = [];
123123
if (is(source, MySqlTable) && onIndex && typeof onIndex !== 'string') {
124124
if (onIndex.useIndex) {
125-
useIndex = convertIndexToString(onIndex.useIndex);
125+
useIndex = convertIndexToString(toArray(onIndex.useIndex));
126126
}
127127
if (onIndex.forceIndex) {
128-
forceIndex = convertIndexToString(onIndex.forceIndex);
128+
forceIndex = convertIndexToString(toArray(onIndex.forceIndex));
129129
}
130130
if (onIndex.ignoreIndex) {
131-
ignoreIndex = convertIndexToString(onIndex.ignoreIndex);
131+
ignoreIndex = convertIndexToString(toArray(onIndex.ignoreIndex));
132132
}
133133
}
134134

@@ -273,13 +273,13 @@ export abstract class MySqlSelectQueryBuilderBase<
273273
let ignoreIndex: string[] = [];
274274
if (is(table, MySqlTable) && onIndex && typeof onIndex !== 'string') {
275275
if (onIndex.useIndex) {
276-
useIndex = convertIndexToString(onIndex.useIndex);
276+
useIndex = convertIndexToString(toArray(onIndex.useIndex));
277277
}
278278
if (onIndex.forceIndex) {
279-
forceIndex = convertIndexToString(onIndex.forceIndex);
279+
forceIndex = convertIndexToString(toArray(onIndex.forceIndex));
280280
}
281281
if (onIndex.ignoreIndex) {
282-
ignoreIndex = convertIndexToString(onIndex.ignoreIndex);
282+
ignoreIndex = convertIndexToString(toArray(onIndex.ignoreIndex));
283283
}
284284
}
285285

drizzle-orm/src/mysql-core/utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,7 @@ export function convertIndexToString(indexes: IndexForHint[]) {
7474
return typeof idx === 'object' ? idx.config.name : idx;
7575
});
7676
}
77+
78+
export function toArray<T>(value: T | T[]): T[] {
79+
return Array.isArray(value) ? value : [value];
80+
}

drizzle-orm/type-tests/mysql/select.ts

Lines changed: 86 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -655,46 +655,59 @@ await db
655655
const table1 = mysqlTable('table1', {
656656
id: int().primaryKey(),
657657
name: text().notNull(),
658-
}, () => {
659-
return { table1NameIndex };
660-
});
658+
}, () => [table1NameIndex]);
661659
const table1NameIndex = index('table1_name_index').on(table1.name);
662660

663661
const table2 = mysqlTable('table2', {
664662
id: int().primaryKey(),
665663
age: int().notNull(),
666664
table1Id: int().references(() => table1.id).notNull(),
667-
}, () => {
668-
return { table2AgeIndex };
669-
});
670-
const table2AgeIndex = index('table1_name_index').on(table2.age);
665+
}, () => [table2AgeIndex, table2Table1Index]);
666+
const table2AgeIndex = index('table2_name_index').on(table2.age);
667+
const table2Table1Index = index('table2_table1_index').on(table2.table1Id);
671668

672669
const view = mysqlView('view').as((qb) => qb.select().from(table2));
673670
const sq = db.select().from(table2, { useIndex: ['posts_text_index'] }).as('sq');
674671

672+
await db.select().from(table1, {
673+
useIndex: table1NameIndex,
674+
forceIndex: table1NameIndex,
675+
ignoreIndex: table1NameIndex,
676+
});
675677
await db.select().from(table1, {
676678
useIndex: [table1NameIndex],
677679
forceIndex: [table1NameIndex],
678680
ignoreIndex: [table1NameIndex],
679681
});
682+
await db.select().from(table1, {
683+
useIndex: table1NameIndex,
684+
// @ts-expect-error
685+
table1NameIndex,
686+
forceIndex: table1NameIndex,
687+
ignoreIndex: table1NameIndex,
688+
});
689+
680690
// @ts-expect-error
681691
await db.select().from(view, {
682-
useIndex: [table1NameIndex],
683-
forceIndex: [table1NameIndex],
692+
useIndex: table1NameIndex,
693+
forceIndex: table1NameIndex,
694+
table1NameIndex,
684695
ignoreIndex: [table1NameIndex],
685696
});
697+
686698
// @ts-expect-error
687699
await db.select().from(sq, {
688-
useIndex: [table1NameIndex],
689-
forceIndex: [table1NameIndex],
700+
useIndex: table1NameIndex,
701+
forceIndex: table1NameIndex,
702+
table1NameIndex,
690703
ignoreIndex: [table1NameIndex],
691704
});
692705

693-
const join = await db.select().from(table1)
706+
const join1 = await db.select().from(table1)
694707
.leftJoin(table2, eq(table1.id, table2.table1Id), {
695-
useIndex: [table2AgeIndex],
696-
forceIndex: [table2AgeIndex],
697-
ignoreIndex: [table2AgeIndex],
708+
useIndex: table2AgeIndex,
709+
forceIndex: table2AgeIndex,
710+
ignoreIndex: table2AgeIndex,
698711
});
699712

700713
Expect<
@@ -710,12 +723,58 @@ await db
710723
table1Id: number;
711724
} | null;
712725
}[],
713-
typeof join
726+
typeof join1
714727
>
715728
>;
716729

717-
const sqJoin = await db.select().from(table1, {
718-
useIndex: [table1NameIndex],
730+
const join2 = await db.select().from(table1)
731+
.leftJoin(table2, eq(table1.id, table2.table1Id), {
732+
useIndex: [table2AgeIndex, table2Table1Index],
733+
forceIndex: [table2AgeIndex, table2Table1Index],
734+
ignoreIndex: [table2AgeIndex, table2Table1Index],
735+
});
736+
737+
Expect<
738+
Equal<
739+
{
740+
table1: {
741+
id: number;
742+
name: string;
743+
};
744+
table2: {
745+
id: number;
746+
age: number;
747+
table1Id: number;
748+
} | null;
749+
}[],
750+
typeof join2
751+
>
752+
>;
753+
754+
const sqJoin1 = await db.select().from(table1, {
755+
useIndex: table1NameIndex,
756+
})
757+
.leftJoin(sq, eq(table1.id, sq.table1Id));
758+
759+
Expect<
760+
Equal<
761+
{
762+
table1: {
763+
id: number;
764+
name: string;
765+
};
766+
sq: {
767+
id: number;
768+
age: number;
769+
table1Id: number;
770+
} | null;
771+
}[],
772+
typeof sqJoin1
773+
>
774+
>;
775+
776+
const sqJoin2 = await db.select().from(table1, {
777+
useIndex: [table1NameIndex, table1NameIndex],
719778
})
720779
.leftJoin(sq, eq(table1.id, sq.table1Id));
721780

@@ -732,23 +791,25 @@ await db
732791
table1Id: number;
733792
} | null;
734793
}[],
735-
typeof sqJoin
794+
typeof sqJoin2
736795
>
737796
>;
738797

739798
await db.select().from(table1)
740799
// @ts-expect-error
741800
.leftJoin(view, eq(table1.id, view.table1Id), {
742-
useIndex: [table2AgeIndex],
743-
forceIndex: [table2AgeIndex],
744-
ignoreIndex: [table2AgeIndex],
801+
useIndex: table2AgeIndex,
802+
forceIndex: table2AgeIndex,
803+
table2Table1Index,
804+
ignoreIndex: [table2AgeIndex, table2Table1Index],
745805
});
746806

747807
await db.select().from(table1)
748808
// @ts-expect-error
749809
.leftJoin(sq, eq(table1.id, sq.table1Id), {
750-
useIndex: [table2AgeIndex],
751-
forceIndex: [table2AgeIndex],
752-
ignoreIndex: [table2AgeIndex],
810+
useIndex: table2AgeIndex,
811+
forceIndex: table2AgeIndex,
812+
table2Table1Index,
813+
ignoreIndex: [table2AgeIndex, table2Table1Index],
753814
});
754815
}

0 commit comments

Comments
 (0)