Search code examples
javascriptloopbackjsstrongloop

How to override and rename properties of User built-in model in Loopback Framework


I'm using the loopback framework to create a RESTful API for my application.

Following the documentation, I create my own Customer Model extending the built-in model User.

What I'm trying to achieve is:

How can I rename and remove some properties from this built-in model?

    {
      "name": "Cliente",
      "plural": "Clientes",
      "base": "User",
      "idInjection": false,
      "strict":"true",
    ...
    }

    {
      "name": "User",
      "properties": {
        "realm": {
          "type": "string"
        },
        "username": {
          "type": "string"
        },
        "password": {
          "type": "string",
          "required": true
        },
        "email": {
          "type": "string",
          "required": true
        },
        "emailVerified": "boolean",
        "verificationToken": "string"
      },
     ...
  }

I reached the results modyfing the loopbacks models inside the node modules, but this solution does not seem the right way, is there a way to config this in my code instead change loopback base models?


Solution

  • I think what you are trying to do is "rename" a property, am I correct? If so, you can do the following:

    "senha": {
          "type": "string",
          "id": true,
          "required": true,
          "index": true,
          "postgresql": {
            "columnName": "password"
          }
        }
    

    Notice that I have a "postgresql" attribute, which depends on your database connector. Check it here. Inside that attribute I have a "columnName", which is the real name of that column in my database. So "senha" is the new name of that attribute.

    For hiding the username property, you could do the following in the root object:

    "hidden":["username"]
    

    Your final file should look something like this:

    {
        "name": "Cliente",
        "plural": "Clientes",
        "base": "User",
        "idInjection": false,
        "strict": "true",
        "properties": {
            "realm": {
                "type": "string"
            },
            "username": {
                "type": "string"
            },
            "senha": {
                "type": "string",
                "required": true,
                "postgresql": {
                    "columnName": "password"
                }
            },
            "email": {
                "type": "string",
                "required": true
            },
            "emailVerified": "boolean",
            "verificationToken": "string"
        },
        "hidden": ["username"]
    }