Tag: pg promise

在Node.js中进行数据库查询分析

我正在build立一个JSON API使用express (好吧,也许会移动到koa )。 我将数据存储在PostgreSQL数据库中,并使用pg-promise从数据中获取数据(通过babel进行async/await )。 我对node.js完全陌生,在这个环境下我找不到关于性能测量的任何信息。 再具体一点: module.exports.get_hierarchy = async function () { const rows = await postgres.any('SELECT id, parent, title, permission FROM heading'); var result = []; // some black magic goes here… return result; } 我想知道( 如果可能,编程)多less时间SELECT消耗。 (不是承诺从构build到解决的时间,这可以通过取两个时间戳来实现,而是由DB服务器用来处理查询的实际时间)。 这可以实现吗? 如果是这样,怎么样?

大量插入pg-promise

我正在使用pg-promise ,我想对多个表进行插入。 我已经看到一些解决scheme,如pg-promise的多行插入,以及如何正确地将多行插入PG和node-postgres? ,我可以使用pgp.helpers.concat来连接多个select。 但现在,我需要在一个表中插入大量的测量数据,超过10,000条logging,并在https://github.com/vitaly-t/pg-promise/wiki/Performance-Boost中说:“有多less条logging你可以像这样连接 – 取决于logging的大小,但是我不会用这种方法超过10,000条logging,所以如果你需要插入更多的logging,你可能想把它们拆分成连续的批处理,然后执行它们逐个。” 我读了所有的文章,但我不知道如何“分割”我的插入批处理,然后逐个执行它们。 谢谢!

Pg-promise:交易问题

我用pg-promise与发生器交易得到了一些奇怪的东西。 这就是我要的 : 获取或注册一个用户(getOrRegisterUser) 做批次的东西(4插入发生器) 最后,使用getOrRegisterCurrentParkingIfProvided生成器的最后一个插入(发生器registerCall) 这是我的代码: db.tx(function (t) { return t.task.call(params, getOrRegisterUser).then(function (user) { params.masterId = user.id; // NOTICE : MY USER ID DATABASE return t.batch([ t.task.call(params, registerNewPhones), t.task.call(params, registerNewPlate), t.task.call(params, registerNewSubscriptions), t.task.call(params, getOrRegisterCurrentParkingIfProvided) ]).then(function (result) { params.ParkingId = (result[3] !== undefined) ? result[3].id : null; return t.task.call(params, registerCall); }) }); }).then(function () { […]

pg-promise任务并映射多个相同级别的嵌套查询

我使用node和pg-promise来创build一个基本的rest API,并且在查询特定用户的所有数据时遇到了一些问题。 以下是返回的数据应该是什么样的。 地址,电话号码和技能都住在不同的表格中。 我没有问题检索地址或电话号码,只是我似乎无法得到的技能。 不太确定如何在主查询后获得多个查询,以获取所有这些其他字段,请参阅附加的代码以供参考,我将很乐意回答任何问题。 { "user_id": 1, "first_name": "Eugene", "last_name": "Hanson", "display_name": "Eugene Hanson", "email": "ehanson0@typepad.com", "hash": "88a6aa27235d2e39dd9cb854cc246487147050f265578a3e1aee35be5db218ef", "privilege_id": 14, "seniority": 1, "birthday": "19-11-1940 00:00:00.0", "shift_count_total": 587, "shift_count_year": 62, "address_id": 1, "street": "92 Schmedeman Lane", "city": "Fort Smith", "state": "AR", "zip": 72905, "phone_numbers": [ { "phone_number": "62-(705)636-2916", "name": "PRIMARY" } ], "skills": [ […]

如何修复多行node-postgres插入 – 在$ 4或附近的语法错误

我有一个多行node-postgres插入,给我的问题。 这是一个参数化查询,它使用公用表expression式来用外键来更新主表和两个表。 该查询与主表的参数一起工作,但是,当我尝试参数化多行的外键表时,会引发语法错误。 首先,我有这个函数接受一个数组并返回一串值。 const buildValues = (id, values) => { return values .map(val => "(" + id + ", '" + val + "', " + false + ")") .join(", "); }; 这是我的查询: app.post('/api/saveperson', function (req, res) { pg.connect(connectionString, (err, client, done) => { const insertPerson = ` WITH x AS ( INSERT INTO […]

pg-promise更新的行数

我正在使用pg-promise NodeJS模块。 在某个时候,我正在更新我的数据库的一个表。 我想知道查询有多less行已被更新,是否有可能? 以下是我的代码的一部分: var queryText = "UPDATE public.mytable SET whatever = $1 WHERE criteria1=$2 AND criteria2=$3;", queryParam = [ value1, value2, value3 ]; postgres.none(queryText, queryParam) .then((value) => { // how can I know at this point how many rows have been updated resolve(result); }) .catch(err => { // it doesn't go here when […]

node.js pg-promise和来自API的分页

看看https://github.com/vitaly-t/pg-promise/wiki/Data-Imports有一个关于如何使用它进行导入的非常详细的文档。 但是,虽然这适用于演示场景,但我不知道如何将其应用于我的案例。 当我进行networking调用时,我得到了实际的JSON数据和标题中的一个参数,它给了我下一页的值(可以是date或string或数字值)。 在这个例子中,它说: db.tx('massive-insert', t => { return t.sequence(index => { return getNextData(index) .then(data => { if (data) { const insert = pgp.helpers.insert(data, cs); return t.none(insert); } }); }); }) .then(data => { console.log('Total batches:', data.total, ', Duration:', data.duration); }) .catch(error => { console.log(error); }); 在这种情况下, sequence(index将使用似乎增加+1的索引,但在我的情况下, function getNextData(nextPage) { //get the data for […]

带有json数组的Postgresql中的参数化查询

我想调用array_prepend到一个json []使用参数化查询。 我正在使用pg-promise npm包,但是它使用正常的node-postgres适配器。 我的查询是: db.query(`update ${schema}.chats set messages = array_prepend('{"sender":"${sender}","tstamp":${lib.ustamp()},"body":$1}',messages) where chat_id = ${chat_id}` , message)); 与“$ 1”相同。 它适用于非参数化查询。 以上代码产生: {[错误:在“hiya”或附近的语法错误] 主要原因是为了避免sql注入(文档说,使用参数化查询时,他们充分逃脱)。

pg-promise为select查询创build自定义filter

我正在工作的function是获取有7个不同的键值的input对象,他们每个可能是未定义或不。 我想根据input中存在的键值过滤我的数据库。 例如,如果只有input.userID存在我想运行这个查询: db.query("…WHERE userID = ${userID}", {userID: input.userID}); 否则,如果input.userID和input.startTime都存在,我想这样做: db.query("…WHERE userID = ${userID} and startTime= ${startTime}", {userID: input.userID, startTime: input.startTime}); 我所做的是我创build了一个params和keys对象,像这样: if(input.userID) { keys.push('userID'); params.push(input.userID); query = addFilterToTheQuery(query, 'userID', input.userID, filteredItemsCount); filteredItemsCount = filteredItemsCount +1; } addFilterToTheQuery是一个我自己实现的简单函数。 我基本上做了7个,如果情况。 然后,我必须使用这些键和参数值以可能需要另一个巨大的开关大小写代码的方式传递给查询函数。 这是唯一的方法来做到这一点? 有没有更好的方法来摆脱这个代码中的冗余?

如何解决pg-promise错误“必须指定promise库”。

我在ExpressJS中使用pg-promise创build了一个基本的API来与我的PostgreSQL数据库进行交互。 在Windows上运行时,它工作正常。 然后我把它移动到Ubuntu 15.04,但是当我尝试启动它时会得到以下错误: /node_modules/pg-promise/lib/promise.js:46 抛出新的TypeError(“必须指定Promise库”);