javascriptnode.jsarraysmodeladonis.js

How can I make query model relation has to many easy to read?


My query has to many like this :

const query = Buyer.query()
.select('id', 'buyer_name', 'description')
query.preload('products', (query) => {
    query.select('id', 'product_name', 'buyer_id')
})
query.orderBy('buyer_name', 'desc')

If the query run, the result like this ::

{
    "data": [
        {
            "id": 1,
            "buyer_name": "John Terry",
            "description": "Voluptates ducimus ut ducimus perferendis placeat omnis qui voluptas omnis.",
            "products": [
                {
                    "id": 1,
                    "buyer_id": 1,
                    "category_name": "category 1",
                    "category_id": 1,
                },
                {
                    "id": 2,
                    "buyer_id": 1,
                    "category_name": "category 1",
                    "category_id": 1,
                }
            ]
        },
        {
            "id": 2,
            "buyer_name": "Frank Lampard",
            "description": "Eligendi itaque velit placeat nulla.",
            "products": [
                {
                    "id": 3,
                    "buyer_id": 2,
                    "category_name": "category 2",
                    "category_id": 2,
                },
                {
                    "id": 4,
                    "buyer_id": 2,
                    "category_name": "category 2",
                    "category_id": 2,
                }
            ]
        }
    ]
}

I want the result of the model relation query like this ::

{
    "data": [
        {
            "id": 1, // this is buyer id
            "buyer_name": "John Terry",
            "description": "Voluptates ducimus ut ducimus perferendis placeat omnis qui voluptas omnis."
            "category_name": "category 1",
            "category_id": 1,
        },
        {
            "id": 2, // this is buyer id
            "buyer_name": "Frank Lampard",
            "description": "Eligendi itaque velit placeat nulla."
            "category_name": "category 2",
            "category_id": 2,
        },
    ]
}

So it's easy to use when this api/query is called

I had try query builder and it works. But I want to use the model because it's cleaner

How can I solve this problem?

Please help. Thanks

Note : The docs : https://docs.adonisjs.com/guides/models/relationships#preload-relationship

Every buyer only have 1 category id


Solution

  • This assumes that every buyer has at least one project, and uses the fact that "Every buyer only have 1 category id"

    const query = null; // your query, but has to be null to run in this snippet
    const result = // await query
    {data:[{id:1,buyer_name:"John Terry",description:"Voluptates ducimus ut ducimus perferendis placeat omnis qui voluptas omnis.",products:[{id:1,buyer_id:1,category_name:"category 1",category_id:1},{id:2,buyer_id:1,category_name:"category 1",category_id:1}]},{id:2,buyer_name:"Frank Lampard",description:"Eligendi itaque velit placeat nulla.",products:[{id:3,buyer_id:2,category_name:"category 2",category_id:2},{id:4,buyer_id:2,category_name:"category 2",category_id:2}]}]};
    
    const formattedResult = {
      data: result.data.map((buyer) => ({
        id: buyer.id,
        buyer_name: buyer.buyer_name,
        description: buyer.description,
        category_name: buyer.products[0].category_name,
        category_id: buyer.products[0].category_id,
      })),
    };
    
    console.log(formattedResult);