-1

我有一个这样的模型定义(为了便于阅读而剪掉了):

{
  "name": "PlannerArchive",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true,
    "strictObjectIDCoercion": true
  },
  "properties": {
    "planner": {
      "type": "object",
      "required": true
    },
    "event": {
      "type": "string",
      "required": true
    },
    "user": {
      "type": "string",
      "required": true
    }
  },
  "indexes": {                    <<<<<<<<<<<<<<<<<<<<<<<<
    "event_idx": {
      "keys": {
        "event": 1
      }
    },
    "user_idx": {
      "keys": {
        "user": 1
      }
    }
  }
}

我的期望是,当服务器启动并创建集合时 - 索引也会被创建。

现实情况是创建了集合,并创建了生成的“id”上的 PK 索引 - 但我的自定义索引都没有。

我可以在服务器启动时运行看起来像mongoDS.autoupdate()这样会创建它们的代码,但我希望不需要这样做。

我对环回索引处理的理解是否存在缺陷——或者我还能做些什么?

为仍在运行 Loopback 3.x 道歉 - 迁移正在进行中。

4

1 回答 1

1

添加索引时,您需要一个自动更新脚本。

将其添加到/server/boot/autoupdate.js

'use strict';
var Promise = require('bluebird');

module.exports = function (server) {

    Promise.each(server.models(), function (model) {

        if (model.dataSource) {
            var autoupdate = Promise.promisify(model.dataSource.autoupdate);
            if (autoupdate) {
                return autoupdate.call(model.dataSource, model.modelName);
            }
        }
    });
};
于 2021-04-30T06:36:22.837 回答