javascript - 通过一次 API 调用在多个表中创建多条记录的最有效方法是什么(sequelize/postgres/node.js)

标签 javascript node.js postgresql sequelize.js

我必须在一次 API 调用中将数据存储在 Postgres 中的多个单独表中。我可以分别调用其中的每一个,但我想知道如何以最有效的方式做到这一点。任何建议将不胜感激,谢谢!

例如:

function create(req, res, next) {
    let people = []
    Male.create({
        name: 'Smith',
        firstname: 'John'
    }).then(john => {
        people.push(john),
            console.log(john.get({
                plain: true
            }))
    })

    Female.create({
        name: 'Smith',
        firstname: 'Jane'
    }).then(jane => {
        people.push(jane),
            console.log(jane.get({
                plain: true
            }))
    })

    Child.create({
        name: 'Smith',
        firstname: 'JayJay'
    }).then(jayjay => {
        people.push(jayjay),
            console.log(jayjay.get({
                plain: true
            }))
    })
    res.send(people[0])
}

最佳答案

尝试调用 postgres 过程,例如:

CREATE OR REPLACE PROCEDURE add_family(father_name varchar(100), mother_name varchar(100), child_name varchar(100))
LANGUAGE plpgsql    
AS $$

BEGIN
Insert into father ( name ) values ( father_name ) ;
insert into mother ( name ) values ( mother_name ) ;
insert into child  ( name ) values ( child_name  ) ;
end

关于javascript - 通过一次 API 调用在多个表中创建多条记录的最有效方法是什么(sequelize/postgres/node.js),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57898177/

相关文章:

javascript - 如何设置现有 moment.js 对象的日期

javascript - 我的浏览器化脚本无法运行

node.js - 查找两个文档的日期差异

postgresql - 列中的 Postgres-pgloader-transformation

javascript - 我如何动态地将对象数组聚合成组

javascript - 将货币值(value)格式化为英镑和便士

javascript - 如何在 Javascript 中仅将第 n 个子字符串存储到变量中

javascript - 将 node.js-mysql 结果返回给函数

在postgresql中使用窗口函数的SQL查询累计总和

ruby-on-rails - PGError : ERROR: relation "delayed_jobs" does not exist (Postgresql, rails 3.04,delayed_job 错误)