gpt4 book ai didi

postgresql - 使用 node.js 在 Postgres 中更新插入

转载 作者:搜寻专家 更新时间:2023-10-31 23:14:31 24 4
gpt4 key购买 nike

我正在尝试使用带有 pg 扩展名(版本 0.5.4)的 node.js 在 postgres 数据库中进行插入或更新。

到目前为止我有这段代码:(...)

client.query({
text: "update users set is_active = 0, ip = $1 where id=$2",
values: [ip,id]
}, function(u_err, u_result){
debug(socket_id,"update query result: ",u_result);
debug(socket_id,"update query error: ",u_err);

date_now = new Date();
var month = date_now.getMonth() + 1;

if(!u_err){

client.query({
text: 'insert into users (id,first_name,last_name,is_active,ip,date_joined) values' +
'($1,$2,$3,$4,$5,$6)',
values: [
result.id,
result.first_name,
result.last_name,
1,
ip,
date_now.getFullYear() + "-" + month + "-" + date_now.getDate() + " " + date_now.getHours() + ":" + date_now.getMinutes() + ":" + date_now.getSeconds()
]
}, function(i_err, i_result){
debug(socket_id,"insert query result: ",i_result);
debug(socket_id,"insert query error: ",i_err);
});
}
});

问题是,尽管两个查询都有效,但问题始终是同时运行这两个查询,而不是在更新失败时仅运行插入函数。

代码中的调试函数输出如下:

更新

Object { type="update query result: ", debug_value={...}}
home (linha 56)
Object { type="update query error: ", debug_value=null}
home (linha 56)
Object { type="insert query result: "}
home (linha 56)
Object { type="insert query error: ", debug_value={...}}

插入

Object { type="update query result: ", debug_value={...}}
home (linha 56)
Object { type="update query error: ", debug_value=null}
home (linha 56)
Object { type="insert query result: ", debug_value={...}}
home (linha 56)
Object { type="insert query error: ", debug_value=null}

** 编辑 **

来自 node-postgres 开发者的回答:

It's possible to retrieve number of rows affected by an insert and update. It's not fully implemented in the native bindings, but does work in the pure javascript version. I'll work on this within the next week or two. In the mean time use pure javascript version and have a look here:

https://github.com/brianc/node-postgres/blob/master/test/integration/client/result-metadata-tests.js

** 结束编辑**

有人能帮忙吗?

最佳答案

您问题的直接答案是使用存储过程进行更新插入。

http://www.postgresql.org/docs/current/static/plpgsql-control-structures.html#PLPGSQL-UPSERT-EXAMPLE

类似这样的东西在 pg 模块上工作得很好。

client.query({
text: "SELECT upsert($1, $2, $3, $4, $5, $6)"
values: [ obj.id,
obj.first_name,
obj.last_name,
1,
ip,
date_now.getFullYear() + "-" + month + "-" + date_now.getDate() + " " + date_now.getHours() + ":" + date_now.getMinutes() + ":" + date_now.getSeconds()
]
}, function(u_err, u_result){
if(err) // this is a real error, handle it

// otherwise your data is updated or inserted properly
});

当然,这假设您正在使用某种模型对象,它具有您需要的所有值,即使它们没有改变。您必须将它们全部传递到 upsert 中。如果您坚持按照此处显示的方式进行操作,您可能应该在更新后检查实际的错误对象以确定它是否失败,因为该行已经存在,或者由于其他原因(这是真正的数据库错误)需要处理)。

然后您必须处理更新失败和插入完成之间的潜在竞争条件。如果其他一些函数尝试使用相同的 id 插入,那么你就有问题了。交易对此有好处。这就是我现在得到的全部。希望对您有所帮助。

关于postgresql - 使用 node.js 在 Postgres 中更新插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7910174/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com