Indices

Column indices

You can create a database index for a specific column by using @Index on a column you want to make an index.You can create indices for any columns of your entity.Example:

  1. import {Entity, PrimaryGeneratedColumn, Column, Index} from "typeorm";
  2. @Entity()
  3. export class User {
  4. @PrimaryGeneratedColumn()
  5. id: number;
  6. @Index()
  7. @Column()
  8. firstName: string;
  9. @Column()
  10. @Index()
  11. lastName: string;
  12. }

You can also specify an index name:

  1. import {Entity, PrimaryGeneratedColumn, Column, Index} from "typeorm";
  2. @Entity()
  3. export class User {
  4. @PrimaryGeneratedColumn()
  5. id: number;
  6. @Index("name1-idx")
  7. @Column()
  8. firstName: string;
  9. @Column()
  10. @Index("name2-idx")
  11. lastName: string;
  12. }

Unique indices

To create an unique index you need to specify { unique: true } in the index options:

Note: CockroachDB stores unique indices as UNIQUE constraints

  1. import {Entity, PrimaryGeneratedColumn, Column, Index} from "typeorm";
  2. @Entity()
  3. export class User {
  4. @PrimaryGeneratedColumn()
  5. id: number;
  6. @Index({ unique: true })
  7. @Column()
  8. firstName: string;
  9. @Column()
  10. @Index({ unique: true })
  11. lastName: string;
  12. }

Indices with multiple columns

To create an index with multiple columns you need to put @Index on the entity itselfand specify all column property names which should be included in the index.Example:

  1. import {Entity, PrimaryGeneratedColumn, Column, Index} from "typeorm";
  2. @Entity()
  3. @Index(["firstName", "lastName"])
  4. @Index(["firstName", "middleName", "lastName"], { unique: true })
  5. export class User {
  6. @PrimaryGeneratedColumn()
  7. id: number;
  8. @Column()
  9. firstName: string;
  10. @Column()
  11. middleName: string;
  12. @Column()
  13. lastName: string;
  14. }

Spatial Indices

MySQL and PostgreSQL (when PostGIS is available) both support spatial indices.

To create a spatial index on a column in MySQL, add an Index with spatial: true on a column that uses a spatial type (geometry, point, linestring,polygon, multipoint, multilinestring, multipolygon,geometrycollection):

  1. @Entity()
  2. export class Thing {
  3. @Column("point")
  4. @Index({ spatial: true })
  5. point: string;
  6. }

To create a spatial index on a column in PostgreSQL, add an Index with spatial: true on a column that uses a spatial type (geometry, geography):

  1. @Entity()
  2. export class Thing {
  3. @Column("geometry", {
  4. spatialFeatureType: "Point",
  5. srid: 4326
  6. })
  7. @Index({ spatial: true })
  8. point: Geometry;
  9. }

Disabling synchronization

TypeORM does not support some index options and definitions (e.g. lower, pg_trgm) because of lot of different database specifics and multipleissues with getting information about exist database indices and synchronizing them automatically. In such cases you should create index manually(for example in the migrations) with any index signature you want. To make TypeORM ignore these indices during synchronization use synchronize: falseoption on @Index decorator.

For example, you create an index with case-insensitive comparison:

  1. CREATE INDEX "POST_NAME_INDEX" ON "post" (lower("name"))

after that, you should disable synchronization for this index to avoid deletion on next schema sync:

  1. @Entity()
  2. @Index("POST_NAME_INDEX", { synchronize: false })
  3. export class Post {
  4. @PrimaryGeneratedColumn()
  5. id: number;
  6. @Column()
  7. name: string;
  8. }