Search code examples
javascriptdecoratortypeorm

What's the difference between class index decorator and column decorator?


I'm using typeorm but i'm still newbee and it's interesting to me what is different between class index decorator and column decorator. They both say that email is unique. But what is the best solution? Where is the best place (property decorator or class decorator) to define indexes whey they are much or there is just 1 index in the table?

@Index('email', ['email'], { unique: true })
@Column({ unique: true })

Solution

  • The @Index decorator can do the same the @Column({ unique: true }) does, but it can do more. Use the simplest solution that works for you:

    • If you just need mark a column as unique - use @Column({ unique: true }), because you anyway need to use the @Column decorator.

    • If you need to somehow customize the index, use the @Index decorator. It allows you to specify, for example, a name of the index: @Index('index-name', { unique: true }).