LoopbackJS / AngularJS find with relation hasOne include separated attribute

三世轮回 提交于 2020-01-06 19:38:12

问题


i'm making a find query in angularjs using loopbackapi , in mysql db.

I have a model with a hasOne relation with another one.

Empresa hasOne Operadora

When i do a find in Empresa using include args, it brings me Operadora attributes, but it include the attributes inside Empresa, but i would like to have it separated.

I tested using those examples:

Empresa.findOne({ include: {"relation" : 'Operadora', 'as': 'Operadora'}
Empresa.findOne({ include: {model: 'Operadora', 'as': 'Operadora'} 
Empresa.findOne({ include: {Operadora: 'Operadora'}
Empresa.findOne({ include:  'Operadora'
Empresa.find({ include: {"relation": 'Operadora'}

It brings me like that:

Empresa.nome
Empresa.nomeOperador

But I would like to have :

Empresa.nome
Empresa.Operadora.nomeOperador

empresa.json

{
  "name": "Empresa",
  "base": "PersistedModel",
  "idInjection": false,
  "options": {
    "validateUpsert": true
  },
  "mysql": {
    "schema": "netexamesapp_prod",
    "table": "Empresa"
  },
  "properties": {
    "id": {
      "type": "Number",
      "id": true,
      "length": null,
      "precision": 10,
      "scale": 0,
      "mysql": {
        "columnName": "id",
        "dataType": "int",
        "dataLength": null,
        "dataPrecision": 10,
        "dataScale": 0,
        "nullable": "N"
      },
      "_selectable": false
    },
    "usuario": {
      "type": "Number",
      "required": true,
      "length": null,
      "precision": 10,
      "scale": 0,
      "mysql": {
        "columnName": "usuario",
        "dataType": "int",
        "dataLength": null,
        "dataPrecision": 10,
        "dataScale": 0,
        "nullable": "N"
      },
      "_selectable": false
    },
    "numcartprof": {
      "type": "String",
      "required": false,
      "length": 255,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "numCartProf",
        "dataType": "varchar",
        "dataLength": 255,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "Y"
      },
      "_selectable": true
    },
    "numregcons": {
      "type": "String",
      "required": false,
      "length": 255,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "numRegCons",
        "dataType": "varchar",
        "dataLength": 255,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "Y"
      },
      "_selectable": true
    },
    "tipocontratacao": {
      "type": "String",
      "required": false,
      "length": 255,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "tipoContratacao",
        "dataType": "varchar",
        "dataLength": 255,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "Y"
      },
      "_selectable": true
    },
    "numcnes": {
      "type": "String",
      "required": false,
      "length": 255,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "numCNES",
        "dataType": "varchar",
        "dataLength": 255,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "Y"
      },
      "_selectable": true
    },
    "dataexpcro": {
      "type": "Date",
      "required": false,
      "length": null,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "dataExpCRO",
        "dataType": "datetime",
        "dataLength": null,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "Y"
      },
      "_selectable": true
    },
    "operadora": {
      "type": "Number",
      "length": null,
      "precision": 10,
      "scale": 0,
      "mysql": {
        "columnName": "operadora",
        "dataType": "int",
        "dataLength": null,
        "dataPrecision": 10,
        "dataScale": 0,
        "nullable": "N"
      },
      "_selectable": false
    },
    "datacredenciamento": {
      "type": "Date",
      "required": false,
      "length": null,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "dataCredenciamento",
        "dataType": "datetime",
        "dataLength": null,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "Y"
      },
      "_selectable": true
    },
    "datadescredenciamento": {
      "type": "Date",
      "required": false,
      "length": null,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "dataDescredenciamento",
        "dataType": "datetime",
        "dataLength": null,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "Y"
      },
      "_selectable": true
    },
    "created": {
      "type": "Date",
      "required": false,
      "length": null,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "created",
        "dataType": "datetime",
        "dataLength": null,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "Y"
      },
      "_selectable": true
    },
    "lastupdated": {
      "type": "Date",
      "required": false,
      "length": null,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "lastUpdated",
        "dataType": "datetime",
        "dataLength": null,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "Y"
      },
      "_selectable": true
    },
    "nomefantasia": {
      "type": "String",
      "required": false,
      "length": 255,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "nomefantasia",
        "dataType": "varchar",
        "dataLength": 255,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "Y"
      },
      "_selectable": true
    },
    "cnpj": {
      "type": "String",
      "required": false,
      "length": 255,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "cnpj",
        "dataType": "varchar",
        "dataLength": 255,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "Y"
      },
      "_selectable": true
    },
    "razaosocial": {
      "type": "String",
      "required": false,
      "length": 255,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "razaosocial",
        "dataType": "varchar",
        "dataLength": 255,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "Y"
      },
      "_selectable": true
    }
  },
  "validations": [],
  "relations": {
        "User": {
         "type": "belongsTo",
         "model": "User",
         "foreignKey": "usuario"
       },
       "Local": {
         "type": "belongsTo",
         "model": "Local",
         "foreignKey": "endereco"
       },
       "Operadora": {
         "type": "hasOne",
         "model": "Operadora",
         "foreignKey": "empresa"
       }
  },
  "acls": [],
  "methods": {}
}

operadora.json

{
  "name": "Operadora",
  "base": "PersistedModel",
  "idInjection": false,
  "options": {
    "validateUpsert": true
  },
  "mysql": {
    "schema": "netexamesapp_prod",
    "table": "Operadora"
  },
  "properties": {
    "id": {
      "type": "Number",
      "id": true,
      "length": null,
      "precision": 10,
      "scale": 0,
      "mysql": {
        "columnName": "id",
        "dataType": "int",
        "dataLength": null,
        "dataPrecision": 10,
        "dataScale": 0,
        "nullable": "N"
      },
      "_selectable": false
    },
    "usuario": {
      "type": "Number",
      "length": null,
      "precision": 10,
      "scale": 0,
      "mysql": {
        "columnName": "usuario",
        "dataType": "int",
        "dataLength": null,
        "dataPrecision": 10,
        "dataScale": 0,
        "nullable": "N"
      },
      "_selectable": false
    },
    "datacredenciamento": {
      "type": "Date",
      "required": false,
      "length": null,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "dataCredenciamento",
        "dataType": "datetime",
        "dataLength": null,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "Y"
      },
      "_selectable": true
    },
    "datadescredenciamento": {
      "type": "Date",
      "required": false,
      "length": null,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "dataDescredenciamento",
        "dataType": "datetime",
        "dataLength": null,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "Y"
      },
      "_selectable": true
    },
    "created": {
      "type": "Date",
      "required": false,
      "length": null,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "created",
        "dataType": "datetime",
        "dataLength": null,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "Y"
      },
      "_selectable": true
    },
    "lastupdated": {
      "type": "Date",
      "required": false,
      "length": null,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "lastUpdated",
        "dataType": "datetime",
        "dataLength": null,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "Y"
      },
      "_selectable": true
    },
    "empresa": {
      "type": "Number",
      "required": false,
      "length": null,
      "precision": 10,
      "scale": 0,
      "mysql": {
        "columnName": "empresa",
        "dataType": "int",
        "dataLength": null,
        "dataPrecision": 10,
        "dataScale": 0,
        "nullable": "Y"
      },
      "_selectable": true
    }
  },
  "validations": [],
  "relations": {
       "User": {
         "type": "belongsTo",
         "model": "User",
         "foreignKey": "usuario"
       },
       "Empresa": {
         "type": "belongsTo",
         "model": "Empresa",
         "foreignKey": "empresa"
       }
     },
  "acls": [],
  "methods": {}
}

Any ideas how to do it?

来源:https://stackoverflow.com/questions/34078370/loopbackjs-angularjs-find-with-relation-hasone-include-separated-attribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!