使用一次 API 调用在多个表中创建多个记录的最有效方法是什么(sequelize/postgres/node.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

最新更新