How to get rid of nesting as a result adonis js


#1
const tour_type = await TourType
        .query()
        .with('tourTypeDescriptions', (builder) => {
          builder.setVisible(['title'])
            .where('lang_id', lang_id.id)
            .limit(1)
        })
        .with('tours', (builder) => {
          builder
            .setVisible(['id'])
            .with('tourDescriptions', (ins_builder) => { 
              ins_builder.setVisible(['title'])
              ins_builder.where('lang_id', lang_id.id) })
            .where('is_status', 1)
        })
        .select('id')
        .where('is_status', 1)
        .whereExists(function () {
          this.from('tours').whereRaw('tour_types.id = tours.tour_type_id')
        })
        .fetch()
      
      
      
      return  response.send({
        type: 'success',
        tour_type: tour_type
      })

And I have this result :grin::grin::
image


#2

But that is the defined behaviour of with method.

Maybe just don’t use it then and do your own queries manually?


#3

You can simply restructure your object or create a new object before sending the response.


#4

Do I need to create an object in which I define its properties? Or how can restructure?:thinking:


#5

I did so but I do not like it


#6

Yes, that’s what I’m thinking

See this for an example:
I’m returning users with profile relationship.

let allUsers = await User.query().with('profile').fetch()
{
  "data": [
    {
      "id": 1,
      "name": "Aaliyah Schmidt",
      "email": "Orin.Klein87@gmail.com",
      "password": "$2a$10$H5c836JIFmpg4wMTql49QOOBsEr1StBj3p40gL6ovG5k0MlDsaFhq",
      "created_at": "2018-02-28 17:21:59",
      "updated_at": "2018-02-28 17:21:59",
      "profile": {
        "id": 3,
        "user_id": 1,
        "stripe_token": "cus_CPSEuspzVx4Xmt",
        "created_at": "2018-02-28 17:22:25",
        "updated_at": "2018-02-28 17:22:26"
      }
    },
    {
      "id": 2,
      "name": "Rasheed Pollich",
      "email": "Isadore.Runte@hotmail.com",
      "password": "$2a$10$2.0706obp9UrFZlUXxSGq.xFYwQuEA1nima4aUr/XW.yPZh.TQr32",
      "created_at": "2018-02-28 17:22:01",
      "updated_at": "2018-02-28 17:22:01",
      "profile": {
        "id": 5,
        "user_id": 2,
        "stripe_token": "cus_CPSE0irSD3fjjg",
        "created_at": "2018-02-28 17:22:39",
        "updated_at": "2018-02-28 17:22:40"
      }
    },
    {
      "id": 3,
      "name": "Casimir Metz",
      "email": "Elijah.Gutmann@hotmail.com",
      "password": "$2a$10$znpLaxYvtdjT4i6OhckuJuHb.AS6lCalekxBfg2/hX/2Mc.kwB.BW",
      "created_at": "2018-02-28 17:22:02",
      "updated_at": "2018-02-28 17:22:02",
      "profile": {
        "id": 2,
        "user_id": 3,
        "stripe_token": "cus_CPSEX1OfIg0024",
        "created_at": "2018-02-28 17:22:21",
        "updated_at": "2018-02-28 17:22:23"
      }
    }
  ]
}

Let’s say you only wanted the stripe_token from the nested profile relationship; you may consider something like:

let users = allUsers.toJSON().map(user => {
  return {
    name: user.name,
    email: user.email,
    stripe_token: user.profile.stripe_token
  }
})

response.send({ data: users })

which yields:

{
  "data": [
    {
      "name": "Aaliyah Schmidt",
      "email": "Orin.Klein87@gmail.com",
      "stripe_token": "cus_CPSEuspzVx4Xmt"
    },
    {
      "name": "Rasheed Pollich",
      "email": "Isadore.Runte@hotmail.com",
      "stripe_token": "cus_CPSE0irSD3fjjg"
    },
    {
      "name": "Casimir Metz",
      "email": "Elijah.Gutmann@hotmail.com",
      "stripe_token": "cus_CPSEX1OfIg0024"
    }
  ]
}

You can do this as needed, but if you have multiple controllers need to return the same format it may turn into a mess. I’d recommend the package
Adonis Bumblebee for transforming data.

Hope this helps :smiley:


Format response in one level of depth
#7

Thank you very much :grinning::grinning: