回送索引-如何在模型定义中指定不同的索引类型?

时间:2019-04-05 22:30:41

标签: postgresql loopbackjs

在Loopback(v3)中,当在我的model.json文件中定义索引时,如何指定不同类型的索引(例如BRIN)?另外,如何指定索引条件(例如是否要创建部分索引)?如果相关的话,我正在使用postgres作为数据库。

3 个答案:

答案 0 :(得分:1)

您可以通过type字段配置索引类型。

{
  "name": "MyModel",
  "properties": {
    // ...
  },
  "indexes": {
    "myindex": {
      "columns": "name, email",
      "type": "BRIN",
      // ...
    }
  }
} 

恐怕LoopBack还不支持索引条件(部分索引)。随时在https://github.com/strongloop/loopback-connector-postgresql/issues中打开新一期。

答案 1 :(得分:0)

在PostgreSQL和Loopback 3中,您可以像这样为多列指定索引。

以下环回JSON代码在Postgres中创建索引,其中字段messagetype唯一。

{
  "name": "notification",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "message": {
      "type": "string",
      "required": true
    },
    "type": {
      "type": "string",
      "required": true
    },
    "seen": {
      "type": "boolean",
      "required": true,
      "default": false
    }
  },
  "validations": [],
  "relations": {},
  "acls": [],
  "methods": {},
  "indexes": {
    "message_type_index": {
      "keys": "message, type",
      "options": {"unique": true}
    }
  }
}

答案 2 :(得分:0)

我试图添加 Lb4。它在那里非常简单(我希望 lb3 也应该相同)

@model({ 
name: 'tablename', 
settings: {
 indexes: {
  idx_tablename: {
    columnA : '',
    columnB : '',
    columnC: ''
    }
   }
 } 
})

构建完成后,将创建具有 3 列的索引名称 idx_tablename