Roeurb Navy
Roeurb Navy

Reputation: 91

How to query data from `PostgresSql`?

I use NestJj,PostgreSQL and Graphql for Back-End, Flutter,graphql_flutter for Front End.

I have a collection store like this :

enter image description here

I want to get the following result:

[
    {
        type:'A',
        details:[
            {name:'Food1'}    
        ]
    },
    {
        type:'Expense',
        details:[
            {name:'gym'},
            {name:'Dinner'}
        ]
    },
    {
        type:'Revenue',
        details:[
            {name:'Revenue'}    
        ]
    }
]

For show on the device : enter image description here How can I query? Could you help me?

Upvotes: 0

Views: 72

Answers (1)

Dinu Mihnea
Dinu Mihnea

Reputation: 31

I'm not sure if you'll be able to build that structure at the level of SQL. What you can do is to extract the data from the table wit the structure as it is and then map it at the level of JS.

// here's an example with TypeORM
const data = await Collection.findBy({...});
const result = data.map(item => ({
    type: item.type,
    details: [{
        name: item.name
    }]
}));

P.S. I'm pretty sure that's not the answer you've expected but it will solve your issue.

Upvotes: 0

Related Questions