Postgres + Sequelize:如何阅读功能结果?

时间:2017-11-18 05:13:18

标签: json node.js postgresql stored-procedures sequelize.js

我有一个函数payment_summary(),如下所示:

CREATE OR REPLACE FUNCTION payment_summary()
  RETURNS SETOF PAYMENT_SUMMARY_TYPE
LANGUAGE plpgsql
AS $$
DECLARE
  payment_sum payment_summary_type%ROWTYPE;
BEGIN
  FOR payment_sum IN SELECT
                       pay.application_no,
                       project.title,
                       pay.payment_rec,
                       customer.cust_name,
                       project.estimated_cost,
                       (project.estimated_cost - pay.payment_rec) AS outstanding_amt
                     FROM project
                       INNER JOIN customer
                         ON project.customer_cust_id = customer.cust_id
                       INNER JOIN
                       (SELECT
                          project.application_no,
                          sum(payment.amount) AS payment_rec
                        FROM payment
                          INNER JOIN project
                            ON payment.project_id = project.project_id
                        WHERE payment.drcr_flg = 'Cr'
                        GROUP BY project.application_no) AS pay
                         ON pay.application_no = project.application_no
  LOOP
    RETURN NEXT payment_sum;
  END LOOP;
END;
$$;

PAYMENT_SUMMARY_TYPE定义为:

CREATE TYPE PAYMENT_SUMMARY_TYPE AS
(
  application_no  VARCHAR(150),
  title           VARCHAR(500),
  payment_rec     INTEGER,
  customer_name   VARCHAR(500),
  estimated_cost  INTEGER,
  outstanding_amt INTEGER
);

使用以下代码执行该功能并获得结果:

sequelize.query('SELECT payment_summary()').then(function(data) {
        res.json(data);
 });

获得以下回复:

[
    [
        {
            "payment_summary": "(716,\"C1\",100000,\"C1 - city\",0,-100000)"
        },
        {
            "payment_summary": "(716,\"C2\",100000,\"C2 - city\",0,-100000)"
        }
    ],
    {
        "command": "SELECT",
        "rowCount": 2,
        "oid": null,
        "rows": [
        {
            "payment_summary": "(716,\"C1\",100000,\"C1 - city\",0,-100000)"
        },
        {
            "payment_summary": "(716,\"C2\",100000,\"C2 - city\",0,-100000)"
        }
        ],
        "fields": [
            {
                "name": "payment_summary",
                "tableID": 0,
                "columnID": 0,
                "dataTypeID": 17453,
                "dataTypeSize": -1,
                "dataTypeModifier": -1,
                "format": "text"
            }
        ],
        "_parsers": [
            null
        ],
        "rowAsArray": false
    }
]

我需要以下格式的回复:

[
    {
        application_no: 716,
        title: "C1",
        payment_rec : 100000,    
        customer_name : "C1 - city"
        estimated_cost : 0 
        outstanding_amt : -100000
    },
    {
        application_no: 717,
        title: "C2",
        payment_rec : 100000,    
        customer_name : "C2 - city"
        estimated_cost : 0 
        outstanding_amt : -100000
    }
]

如何以所需格式阅读/转换回复?

0 个答案:

没有答案