Best approach for nested data?

Hello @Pierre,

You can’t directly obtain nested data inside the content-type, but here is a workaround:
First of all, here is the result that I obtained:

Orders: {
    "_id": "5fa41a5fed71373bb3f4fc66",
    "amount": 1111,
    "status": "pending",
    "order_details": [
        {
            "_id": "5fa41a6ced71373bb3f4fc67",
            "quantity": 2,
            "name": "iPhone 12 x 2",
            "product": "5fa41a4ced71373bb3f4fc65"
        },
        {
            "_id": "5fa4192432803d3af0f5d71b",
            "quantity": 100,
            "name": "Samsung TV x 100",
            "product": "5fa418f832803d3af0f5d719"
        }
    ],
    "user": {
        "_id": "5fa16b953940762a47f2838a",
        "username": "test",
        "email": "testtesttesttest@gmail.com",
        "provider": "local",
        "role": "5fa1584d56e6e917c29eabc2",
    }
}

Orders content-type (User has many Orders, Order has many Order-Details):

image

Order-details content-type (Order-Detail has one Product):

image

Note: order_details.name is generated inside lifecycle, by concatenating product name & quantity so I could obtain this view inside Orders Content-type:
image

async beforeCreate(data, model) {
      let product = await strapi.query('products').findOne({ id: data.product });
      data.name = `${product.name} x ${data.quantity}`;
},
async beforeUpdate(data, model) {
      let product = await strapi.query('products').findOne({ id: model.product });
      model.name = `${product.name} x ${model.quantity}`;
    },
1 Like