应用场景

CabloyJS采用MySQL进行数据存储。为了提升SQL的运行性能,我们需要对业务模块中用到的数据表添加合适的索引。针对这个应用场景,CabloyJS同样提供了便利的配置方式

业务模块中的索引配置

比如,模块test-party有一个数据表:testParty。由于我们需要对该表中的字段partyTypeCode(宴会类型)进行检索,因此需要在模块的meta文件中添加该字段的索引配置:

src/suite-vendor/test-party/modules/test-party/backend/src/meta.js

  1. 1index: {
  2. 2 indexes: {
  3. 3 testParty: 'createdAt,updatedAt,atomId,partyTypeCode',
  4. 4 },
  5. 5},
  • index.indexes:需要设置索引的数据表清单

  • testParty:数据表名,其值就是需要添加索引的字段清单

项目中的索引配置

当然,在实际业务开发当中,业务模块默认的索引配置不一定能满足实际的需要,这时,我们就通过项目配置来覆盖业务模块的索引配置

比如,我们需要对数据表testParty中的另外一个字段partyCity(城市)添加索引,可以这样操作:

src/backend/config/config.default.js

  1. 1 // module config
  2. 2 config.modules = {
  3. 3 'a-index': {
  4. 4 indexesExtend: {
  5. 5 'test-party': {
  6. 6 testParty: 'createdAt,updatedAt,atomId,partyTypeCode,partyCity',
  7. 7 },
  8. 8 },
  9. 9 },
  10. 10 };
  • indexesExtend:是模块a-index提供的配置参数,这里的配置会自动覆盖业务模块中的默认配置