简体   繁体   English

如何从`PostgresSql`查询数据?

[英]How to query data from `PostgresSql`?

I use NestJj , PostgreSQL and Graphql for Back-End, Flutter , graphql_flutter for Front End.我使用NestJjPostgreSQLGraphql作为后端, Fluttergraphql_flutter作为前端。

I have a collection store like this:我有一个这样的collection store

在此处输入图像描述

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:在设备上显示: 在此处输入图像描述 How can I query?我该如何查询? Could you help me?你可以帮帮我吗?

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.我不确定您是否能够在 SQL 级别构建该结构。您可以做的是按原样从表中提取数据,然后在 JS 级别 map 中提取数据。

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

PS I'm pretty sure that's not the answer you've expected but it will solve your issue. PS 我很确定这不是您所期望的答案,但它会解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM