在 Loopback 版本 LB4 中使用嵌套对象和数组创建模型

Creating model with nested objects and arrays in Loopback version LB4

我刚刚第一次开始使用 Loopback,并且开始使用他们的最新版本 LB4。我希望根据我的 JSON 模式创建一个包含嵌套对象和数组的模型,我遵循 documentation 允许我创建我的模式的基值,但我需要创建对象和数组中的字段,但我找不到帮助我理解这一点的文档或文章...

这是我的 JSON 模式,我正在尝试创建一个 LB4 模型:

"socialProfiles": {
    "facebook": {
        "linked": 1,
        "pullData": 1,
        "linkID": 4434343,
        "profile": "https://www.facebook.com/FBURL",
        "registered": {
            "date": "2018-05-04T12:41:27.838Z",
            "verified": "2018-05-04T12:41:27.838Z",
            "by": {
                "id": 1,
                "user": "USER"
            }
        }
    },
}

使用 LB4 文档,我可以创建我的主字段 socialProfiles 但是我找不到在这个对象中创建我的字段的位置...这是我的 LB4 模型代码

import {Entity, model, property} from '@loopback/repository';

@model()
export class Users extends Entity {

 @property({
    type: 'object',
 })
 socialProfiles?: object;

 constructor(data?: Partial<Users>) {
    super(data)
 }
}

如何操作?

如果你想将对象存储在模型本身中(而不是关系),你可以创建一个类似这样的接口:

export interface ISocialProfile {
    "linked": number,
    "pullData": number,
    "linkID": number,
    "profile": string,
    "registered": {
        "date": Timestamp,
        "verified": Timestamp,
        "by": {
            "id": number,
            "user": string
        }
    }
}

然后在您的模型中,您只需添加类型:

socialProfiles?: {[name: string]: ISocialProfile};