Skip to content

Commit 328912d

Browse files
committed
refactor(indexes): store named and unnamed indexes separately
1 parent 7ce50c6 commit 328912d

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

src/model/index/index-service.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,28 @@ export interface IndexFieldOptions {
1111
collate?: string;
1212
}
1313

14+
export interface IndexesMeta {
15+
named: { [name: string]: IndexOptions };
16+
unnamed: IndexOptions[];
17+
}
18+
1419
export type IndexOptions = Pick<SequelizeIndexOptions, Exclude<keyof SequelizeIndexOptions, 'fields'>>;
1520

1621
/**
1722
* Returns model indexes from class by restoring this
1823
* information from reflect metadata
1924
*/
20-
export function getIndexes(target: any): any | undefined {
21-
const indexes = Reflect.getMetadata(INDEXES_KEY, target);
25+
export function getIndexes(target: any): IndexesMeta {
26+
const { named = {}, unnamed = [] }: IndexesMeta =
27+
Reflect.getMetadata(INDEXES_KEY, target) || {};
2228

23-
// tslint:disable-next-line:prefer-object-spread
24-
return indexes && Object.assign([], indexes);
29+
return { named: {...named}, unnamed: [...unnamed] };
2530
}
2631

2732
/**
2833
* Sets indexes
2934
*/
30-
export function setIndexes(target: any, indexes: any): void {
35+
export function setIndexes(target: any, indexes: IndexesMeta): void {
3136
Reflect.defineMetadata(INDEXES_KEY, indexes, target);
3237
}
3338

@@ -39,14 +44,17 @@ export function addFieldToIndex(target: any,
3944
fieldOptions: IndexFieldOptions,
4045
indexOptions: IndexOptions,
4146
indexId?: string | number): string | number {
42-
const indexes = getIndexes(target) || [];
47+
const indexes = getIndexes(target);
4348

4449
const chosenId = typeof indexId !== 'undefined'
4550
? indexId
46-
: indexOptions.name || indexes.length;
47-
if (!indexes[chosenId]) indexes[chosenId] = {...indexOptions};
51+
: indexOptions.name || indexes.unnamed.length;
52+
const indexStore = typeof chosenId === 'string'
53+
? indexes.named
54+
: indexes.unnamed;
55+
if (!indexStore[chosenId]) indexStore[chosenId] = {...indexOptions};
4856

49-
const index = indexes[chosenId];
57+
const index = indexStore[chosenId];
5058
if (!index.fields) index.fields = [];
5159
index.fields.push(fieldOptions);
5260

src/sequelize/sequelize/sequelize.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,11 @@ export class Sequelize extends OriginSequelize {
9090

9191
if (!modelOptions) throw new Error(`@Table annotation is missing on class "${model['name']}"`);
9292

93+
const indexArray = Object.keys(indexes.named)
94+
.map(key => indexes.named[key])
95+
.concat(indexes.unnamed);
9396
const initOptions: InitOptions & { modelName } = {
94-
indexes: indexes && Object.keys(indexes).map(key => indexes[key]),
97+
...(indexArray.length > 0 && { indexes: indexArray }),
9598
...modelOptions,
9699
modelName,
97100
sequelize: this,

0 commit comments

Comments
 (0)