Home > database >  How to query data from `PostgresSql`?
How to query data from `PostgresSql`?

Time:11-16

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'}    
        ]
    }
]

How can I query? Could you help me?

CodePudding user response:

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.

CodePudding user response:

Query the database to get the desired results and map them through the json return model The query result cannot be returned as json.

  • Related