Lucid: Query rows for pagination returns non-serialized results (as in object, not JSON)

Created on 6 Jun 2018  路  2Comments  路  Source: adonisjs/lucid

So, why is this an issue? Because relationships do not show easily until toJSON() is called on individual rows and its a PITA once you get to the view.
Controller Model pull:

var results = await ResultModel
            .query()
            .orderBy('created_at','desc')
            .with('test', (builder) => {
                builder.select('id', 'name')
            })
            .with('testset',(builder) => {
                builder.select('id', 'name')
            })
            .paginate(page, 10)

This returns results as an object containing something like below:

{
rows: {
  Result {
   __setters__: [Array],
       '$attributes': [Object],
       '$persisted': true,
       '$originalAttributes': [Object],
       '$relations': [Object],
       '$sideLoaded': {},
       '$parent': null,
       '$frozen': false,
       '$visible': undefined,
       '$hidden': undefined
  }
}
pages: { total: 20, perPage: 10, page: 1, lastPage: 2 },  
isOne: false
}

I'm having to loop through result rows to repack serialized data in the controller. Since Edge doesn't have the ability to use variables (or so i think?) its difficult to solve this cleanly.
For now, I may opt to just use good old SQL and .fetch() to solve this issue.

PS: I do know I can do this: results.rows[0].$relations.testset.id but that's ugly
PPS: Adonis has been very nice! I've done Laravel work before and its so natural!

Most helpful comment

You can simply call results.toJSON() and you will get the following structure back.

{
  page: 1,
  total: 20,
  perPage: 10,
  lastPage: 2,
  data: []
}

The data property will be an array of rows, that you can loop over.

All 2 comments

You can simply call results.toJSON() and you will get the following structure back.

{
  page: 1,
  total: 20,
  perPage: 10,
  lastPage: 2,
  data: []
}

The data property will be an array of rows, that you can loop over.

Closing since no answer from issue reporter.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nwashangai picture nwashangai  路  5Comments

ConsoleTVs picture ConsoleTVs  路  3Comments

Karnith picture Karnith  路  5Comments

hectorgrecco picture hectorgrecco  路  3Comments

SAGV picture SAGV  路  7Comments