Mysql: Nest tables format

Created on 26 Mar 2018  路  3Comments  路  Source: mysqljs/mysql

Sorry for asking questions here but i couldn't find the answer in docs. I am joining two tables with nestTables: true and getting

[{
    table1: {
      fieldA: '...',
      fieldB: '...',
    },
    table2: {
      fieldA: '...',
      fieldB: '...',
    },
  }, ...]

but i would like to get it in this format because it is more suitable for RESTful api:

[{
    fieldA: '...',
    fieldB: '...',
    table2: {
      fieldA: '...',
      fieldB: '...',
    },
}, ...]

is this possible with the api or i have to use Array.map to match desidered format?

Most helpful comment

I got it working. Passed object to nestTables with name of the table(object) that is parent(top level object).

If we, for example, JOIN two tables products and collections and pass

..{sql: ..., nestTables: {parent : 'products'}}..

we will get:

[{
    fieldA: '...', 
    fieldB: '...', 
    fieldC: '...', 
    collections: {
      fieldA: '...',
      fieldB: '...',
    },
}, ...]

this will also work with multiple selected tables as one will be parent and rest are nested inside this top level object.

I can make a pull request if this satisfies module design. @dougwilson

All 3 comments

It is currently not possible with the existing nestTables API. If you have an idea for how to implement this in the module, let us know your proposal :)

I'll see what I can do :)

I got it working. Passed object to nestTables with name of the table(object) that is parent(top level object).

If we, for example, JOIN two tables products and collections and pass

..{sql: ..., nestTables: {parent : 'products'}}..

we will get:

[{
    fieldA: '...', 
    fieldB: '...', 
    fieldC: '...', 
    collections: {
      fieldA: '...',
      fieldB: '...',
    },
}, ...]

this will also work with multiple selected tables as one will be parent and rest are nested inside this top level object.

I can make a pull request if this satisfies module design. @dougwilson

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jeremyrambo picture jeremyrambo  路  5Comments

EdoardoPedrotti picture EdoardoPedrotti  路  3Comments

johnrc picture johnrc  路  3Comments

Rhapsody-Sky picture Rhapsody-Sky  路  3Comments

nanom1t picture nanom1t  路  3Comments