How to query data from `PostgresSql`?

68 views Asked by At

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?

1

There are 1 answers

2
Dinu Mihnea On

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.