pg-promise

Query timeout in pg-promise

你说的曾经没有我的故事 提交于 2019-11-29 17:22:38
I want to add timeout to pg-promise queries so they will fail after some amount of time if database have not yet responded. Is there any recommended way to do that or should I make custom wrapper that will handle timer and reject promise if it's too late? From the author of pg-promise ... pg-promise doesn't support query cancellation, because it is a hack to work-around incorrect database design or bad query execution. PostgreSQL supports events that should be used when executing time-consuming queries, so instead of waiting, one can set an event listener to be triggered when specific data

Combine nested loop queries to parent array result - pg-promise

拥有回忆 提交于 2019-11-29 14:30:26
I'm new to node(express) and pg-promise, and have not been able to figure out how to add the result of each nested query(loop) into the main json array result query. I have two tables: Posts and comments. CREATE TABLE post( id serial, content text not null, linkExterno text, usuario VARCHAR(50) NOT NULL REFERENCES usuarios(alias) ON UPDATE cascade ON DELETE cascade, multimedia text, ubicacation VARCHAR(100), likes integer default 0, time VARCHAR default now(), reported boolean default false, PRIMARY KEY (id) ); CREATE TABLE comment( id serial, idPost integer NOT NULL REFERENCES post(id) ON

How to make pg-promise return rows as arrays?

↘锁芯ラ 提交于 2019-11-29 08:43:16
How can we make pg-promise return an array of rows from a query, as opposed to array of row objects? Version 4.0.5 of pg-promise started to support advanced parameters for Prepared Statements and Parameterized Queries , exactly as they are in node-postgres . One such parameter - rowMode , can be set to array to make the driver return rows as arrays. // Prepared Statement: db.query({ name: 'my-prep-statement', text: 'select ...', // a query or a QueryFile object (see PreparedStatement) values: [], rowMode: 'array' }).then(data=>{}).catch(reason=>{}); // Parameterized Query: db.query({ text:

Get a parents + children tree with pg-promise

五迷三道 提交于 2019-11-28 11:43:46
I use the pg-promise library with bluebird for making dependent queries. I have two tables, a and b, looking like this: | a | | b | |-------| |-------| | a_id | | b_id | | prop1 | | prop2 | | b_a | where b.b_a is a reference to a.a_id . I want to select all entries matching a given prop1 and the result should contain all matching a -rows plus the corresponding b -rows for each a . This should be doable with two dependent queries. Both queries may return multiple results. If table a only returns one row I can do this: function getResult(prop1) { return db.task(function (t) { return t.one(

Query timeout in pg-promise

帅比萌擦擦* 提交于 2019-11-28 10:31:45
问题 I want to add timeout to pg-promise queries so they will fail after some amount of time if database have not yet responded. Is there any recommended way to do that or should I make custom wrapper that will handle timer and reject promise if it's too late? 回答1: From the author of pg-promise... pg-promise doesn't support query cancellation, because it is a hack to work-around incorrect database design or bad query execution. PostgreSQL supports events that should be used when executing time

Optional INSERT statement in transaction chain using NodeJS and Postgres

旧巷老猫 提交于 2019-11-28 09:58:17
问题 I'm building a simple webapp using NodeJS/Postgres that needs to make 3 insertions in the database. To control the chain of statements I'm using pg-transaction . My problem is that I have to always run the 2 first INSERTS, but I have a condition to run the 3rd one. Maybe my code could be built in a better manner (suggestions are welcome). Here's a pseudo code: function(req, res) { var tx = new Transaction(client); tx.on('error', die); tx.begin(); tx.query('INSERT_1 VALUES(...) RETURNING id',

Verify database connection with pg-promise when starting an app

≡放荡痞女 提交于 2019-11-28 06:21:04
I am building an express application that connects to a postgres database using the pg-promise module. I would like to ensure that the database connection is successful when starting the application server. In other words, if the connection to the database fails, I'd like to throw an error. My server.js file is as follows: const express = require("express"); const databaseConfig= { "host": "localhost", "port": 5432, "database": "library_app", "user": "postgres" }; const pgp = require("pg-promise")({}); const db = pgp(databaseConfig); const app = express(); const port = 5000; app.listen(port,

How to make pg-promise return rows as arrays?

吃可爱长大的小学妹 提交于 2019-11-28 01:55:48
问题 How can we make pg-promise return an array of rows from a query, as opposed to array of row objects? 回答1: pg-promise supports advanced parameters for Prepared Statements and Parameterized Queries , exactly as they are in node-postgres. One such parameter - rowMode, can be set to array to make the driver return rows as arrays. // Prepared Statement: await db.query({ name: 'my-prep-statement', text: 'select ...', // a query or a QueryFile object (see PreparedStatement) values: [], rowMode:

pg-promise returns integers as strings

谁都会走 提交于 2019-11-27 21:18:22
问题 I have this simple query to a table that contains a column of type bigint . However when I query it, pg-promise returns this column's values as a string. I can't find info about that in the documentation. Is that standard behavior? var ids = [180, 120]; db.any('SELECT id_brand, brand from catalog_brand WHERE id_brand in ($1:csv)', [ids]) .then((data) => { // return results }); data takes the following form, with id as string instead of int: [{id_brand: "180", brand: "Ford"}, {id_brand: "120",

Get a parents + children tree with pg-promise

巧了我就是萌 提交于 2019-11-27 06:31:29
问题 I use the pg-promise library with bluebird for making dependent queries. I have two tables, a and b, looking like this: | a | | b | |-------| |-------| | a_id | | b_id | | prop1 | | prop2 | | b_a | where b.b_a is a reference to a.a_id . I want to select all entries matching a given prop1 and the result should contain all matching a -rows plus the corresponding b -rows for each a . This should be doable with two dependent queries. Both queries may return multiple results. If table a only