Timothy Lavelle
Timothy Lavelle

Reputation: 23

Creating model with nested objects and arrays in Loopback version LB4

I've just started with Loopback for the first time and I've started with LB4, their newest release. I'm looking to create a model with nested objects and arrays as per my JSON schema to which I followed the documentation which allowed me to create the base values of my schema, but I need to create the fields inside the objects and arrays, but I can't find the documentation or articles to help me understand this...

This is my JSON schema I'm trying to create a LB4 model with:

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

Using the LB4 documentation, I can create my main field socialProfiles but I can't find where I go to create my fields inside this object... Here's my LB4 model code

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

@model()
export class Users extends Entity {

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

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

How to do this?

Upvotes: 2

Views: 1708

Answers (1)

hanego
hanego

Reputation: 1635

If you want to store the object in the model itself (not with a relation), you can create an interface with something like:

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

and then in your model, you can just add the type:

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

Upvotes: 1

Related Questions