-1

I have a model definition as such (snipped for sake of readability):

{
  "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
      }
    }
  }
}

My expectation is that when the server has started up and the collection has been created - that the indexes be created as well.

The reality is that the collection is created, and a PK index on generated "id" is created - but none of my custom indexes are.

I can run code on server boot that looks something like mongoDS.autoupdate() that will create them, but was rather hoping not to need to do this.

Is my understanding of loopbacks index handling flawed - or is there something else I can do?

Apologies for still running Loopback 3.x - the migration is in the pipeline.

batman567
  • 642
  • 1
  • 5
  • 17

1 Answers1

1

When adding indexes, you need an auto-update script.

Add it in /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);
            }
        }
    });
};
F3L1X79
  • 2,367
  • 2
  • 25
  • 40
  • 1
    hi. thanks for your response. as i mentioned i am already doing something similar. i guess my expectation was that since i am configuring the indexes in the model - that the framework would actually "take care of them" - instead of me having to write code to do it. – batman567 Apr 30 '21 at 08:39
  • Yes it's not the case, effectively – F3L1X79 Apr 30 '21 at 10:44