Indexes - 索引

Sequelize 支持在模型定义上添加索引,该索引将在 sequelize.sync() 上创建

  1. const User = sequelize.define('User', { /* 属性 */ }, {
  2. indexes: [
  3. // 在 email 上创建唯一索引
  4. {
  5. unique: true,
  6. fields: ['email']
  7. },
  8. // 使用 jsonb_path_ops 运算符在 data 上创建 gin 索引
  9. {
  10. fields: ['data'],
  11. using: 'gin',
  12. operator: 'jsonb_path_ops'
  13. },
  14. // 默认情况下,索引名称将为 [table]_[fields]
  15. // 创建多列部分索引
  16. {
  17. name: 'public_by_author',
  18. fields: ['author', 'status'],
  19. where: {
  20. status: 'public'
  21. }
  22. },
  23. // 具有 order 字段的 BTREE 索引
  24. {
  25. name: 'title_index',
  26. using: 'BTREE',
  27. fields: [
  28. 'author',
  29. {
  30. name: 'title',
  31. collate: 'en_US',
  32. order: 'DESC',
  33. length: 5
  34. }
  35. ]
  36. }
  37. ]
  38. });