Skip to main content

Function: Index()

Index(columnsOrOptions?, options?): any

Defined in: src/decorators/IndexDecorator.ts:27

Decorator defining an index on an entity table.

Can be used either as a class decorator to define composite or single-column indexes, or as a property decorator on a specific entity field.

Parameters​

ParameterType
columnsOrOptions?string[] | IndexOptions
options?IndexOptions

Returns​

any

Usecase​

Speed up lookups, enforce uniqueness constraints, and declare indexes for Code-First migrations.

Example​

// Class-level composite or named index:
@Index(['email'], { unique: true })
@Index(['firstName', 'lastName'])
class User {
// Or property-level index:
@Index({ unique: true })
username!: string;
}