Search code examples
loopbackjs

Loopback inserts object id values as sequence and not random - how can i change it?


Loopback inserts object id values for my MongoDB as sequence and not random. for security reason, how can i change it to be random by default for all my models?

Will it affect the DB performance?


Solution

  • You can change the way the ID is generated in the description file of your model: https://loopback.io/doc/en/lb3/Model-definition-JSON-file.html#properties

    model.json:
    
    {
      "name": "model",
      "base": "PersistedModel",
      "strict": true,
      "idInjection": false, // disable default id!
      "properties": {
        "id": {
          "id": true,
          "required": true,
          "type": "string",
          "defaultFn": "uuid"   // "guid" / "uuid" / "uuidv4" / "now"
        },
      },
      "validations": [],
      "relations": {},
      "acls": [],
      "methods": {}
    }