Indexes

Sequelize supports adding indexes to the model definition which will be created on sequelize.sync().

  1. const User = sequelize.define('User', { /* attributes */ }, {
  2. indexes: [
  3. // Create a unique index on email
  4. {
  5. unique: true,
  6. fields: ['email']
  7. },
  8. // Creates a gin index on data with the jsonb_path_ops operator
  9. {
  10. fields: ['data'],
  11. using: 'gin',
  12. operator: 'jsonb_path_ops'
  13. },
  14. // By default index name will be [table]_[fields]
  15. // Creates a multi column partial index
  16. {
  17. name: 'public_by_author',
  18. fields: ['author', 'status'],
  19. where: {
  20. status: 'public'
  21. }
  22. },
  23. // A BTREE index with an ordered field
  24. {
  25. name: 'title_index',
  26. using: 'BTREE',
  27. fields: [
  28. 'author',
  29. {
  30. attribute: 'title',
  31. collate: 'en_US',
  32. order: 'DESC',
  33. length: 5
  34. }
  35. ]
  36. }
  37. ]
  38. });