@Index
@Index records index metadata on an entity. See
Indexes and Foreign Keys for the full guide.
function Index(
options?: Omit<IndexMetadata, 'columns'>,
): PropertyDecorator;
function Index(
fields: string | string[],
options?: Omit<IndexMetadata, 'columns'>,
): ClassDecorator;
As a property decorator, the column list is inferred from the property it's placed on:
class Customer {
@Index({ unique: true })
@Column({ fieldName: 'email' })
declare email: string;
}
As a class decorator, the column list is explicit (required for composite indexes):
@Entity('customers')
@Index(['countryCode', 'city'], { name: 'idx_customer_location' })
class Customer { /* ... */ }
IndexMetadata reference
| Field | Type | Description |
|---|---|---|
columns | string[] | Property names making up the index. Supplied automatically by the property-decorator form; required as the first argument to the class-decorator form. |
name | string | Optional index name. |
unique | boolean | Marks the index as unique. |
primary | boolean | Marks the index as the primary key — set internally by @PrimaryKey; you would not normally set this yourself via @Index. |
@Index purely records metadata (accessible via entity.indexes, see
EntityMetadata) — it does not generate DDL or alter query
planning.