使用pg promise将多行插入到事务中的多个表中

Multi row insert into multiple tables in a transaction using pg promise

我正在尝试使用 pg promise 执行事务,我打算在事务中的多个 table 中插入多行。我正在使用 pg promise 辅助方法来优化插入每个 table 但无法将所有查询连接在一起以仅执行一个查询。如果有人能指出是否有任何方法可以进一步优化以下代码,也将不胜感激。

目前我收到一个错误 “参数 'queries' 必须是一个数组。” pgp.hlpers.concat

db.tx(async t => {          
            let customerDataArray = [];
            let campaignStatusArray = [];
            let messageDetailsArray = [];

            for(const customer of customerDetails)
            {

                const customerData = {
                    columna1: customer.x,
                    columnb1: customer.y"
                };

                const campaignStatus = {
                    columna2: customer.a,
                    columnb2: customer.b
                };
                
                customerDataArray.push(customerData);
                campaignStatusArray.push(campaignStatus);

                for (const message of customer.messages)
                {
                    const messageStatus = {
                        columna: message.x,
                        columnb: message.y
                    };
                    messageDetailsArray.push(messageStatus);
                }
            }
            
            const customerDataCs = new pgp.helpers.ColumnSet(['columna1', 'columnb1'], {table: 'customerdata'});
            const campaignStatusCs = new pgp.helpers.ColumnSet(['columna2', 'columnb2'], {table: 'campaignstatus'});
            const messageStatusCs = new pgp.helpers.ColumnSet(['columna', 'columnb'], {table: 'messageStatus'});
            
            const customerDataQuery = pgp.helpers.insert(customerDataArray, customerDataCs);
            const campaignStatusQuery = pgp.helpers.insert(campaignStatusArray, campaignStatusCs);
            const messageStatusQuery = pgp.helpers.insert(messageDetailsArray, messageStatusCs);

            const concatAllQueries = pgp.helpers.concat(customerDataQuery, campaignStatusQuery, messageStatusQuery);

            await t.none(concatAllQueries);

        })
            .then(() => {
                // success;  
            })
            .catch(error => {
              
            });
'''

您需要将查询传递给单个数组中的 concat,如下所示:

const concatAllQueries = pgp.helpers.concat([ customerDataQuery, campaignStatusQuery, messageStatusQuery ]);