gpt4 book ai didi

json - 如何将 JSON 数组从 NodeJS 流式传输到 postgres

转载 作者:搜寻专家 更新时间:2023-10-31 22:49:50 25 4
gpt4 key购买 nike

我试图通过每次批量插入尝试接收来自客户端的请求 10,000 条记录(使用 sequelize.js 和 bulkCreate( ))

这显然是个坏主意,所以我尝试查看 node-pg-copy-streams

但是,我不想在客户端发起更改,在客户端发送一个 json 数组

# python
data = [
{
"column a":"a values",
"column b":"b values",
},
...
# 10,000 items
...
]
request.post(data=json.dumps(data), url=url)

在 nodejs 的服务器端,我如何在以下框架中流式传输接收到的 request.body

.post(function(req, res){

// old sequelize code
/* table5.bulkCreate(
req.body, {raw:true}
).then(function(){
return table5.findAll();
}).then(function(result){
res.json(result.count);
});*/

// new pg-copy-streams code
pg.connect(function(err, client, done) {
var stream = client.query(copyFrom('COPY my_table FROM STDIN'));
// My question is here, how would I stream or pipe the request body ?
// ?.on('error', done);
// ?.pipe(stream).on('finish', done).on('error', done);
});
});

最佳答案

这是我解决问题的方法,

首先是将我的 req.body dict 转换为 TSV 的函数(不是初始问题的一部分)

/**
* Converts a dictionary and set of keys to a Tab Separated Value blob of text
* @param {Dictionary object} dict
* @param {Array of Keys} keys
* @return {Concatenated Tab Separated Values} String
*/
function convertDictsToTSV(dicts, keys){
// ...
}

其次是我原来的 .post 函数的其余部分

.post(function(req, res){
// ...
/* requires 'stream' as
* var stream = require('stream');
* var copyFrom = require('pg-copy-streams').from;
*/
var read_stream_string = new stream.Readable();
read_stream_string.read = function noop() {};
var keys = [...]; // set of dictionary keys to extract from req.body
read_stream_string.push(convertDictsToTSV(req.body, keys));
read_stream_string.push(null);
pg.connect(connectionString, function(err, client, done) {
// ...
// error handling
// ...
var copy_string = 'Copy tablename (' + keys.join(',') + ') FROM STDIN'
var pg_copy_stream = client.query( copyFrom( copy_string ) );
read_stream_string.pipe(pg_copy_stream).on('finish', function(finished){
// handle finished and done appropriately
}).on('error', function(errored){
// handle errored and done appropriately
});
});
pg.end();
});

关于json - 如何将 JSON 数组从 NodeJS 流式传输到 postgres,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34687387/

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