pg-promise

Log specific postgresql query using pg-promise

爱⌒轻易说出口 提交于 2019-12-06 03:29:48
I am using pg-promise package with Nodejs to execute PostgreSQL queries. I want to see the queries executed. Only specific queries, say, just one query that I want to debug. I can see that one recommended way is to use the pg-monitor to catch the events and log them as mentioned here in the examples documentation . Without using pg-monitor , is there a simple way to just print the prepared query that is executed. I can't see it in the docs. Example: db.query("SELECT * FROM table WHERE id = $/id/", {id: 2}) How to print this query to yield? SELECT * FROM table WHERE id = 2 is there a simple way

pg-promise: use result of one query in next query within a transaction

不想你离开。 提交于 2019-12-06 01:33:08
I am node.js with pg-promise for postgres, trying to do a transaction with 2 inserts in sequence. The result id of the 1st insert should be used in the next insert in the transaction. Rollback if any of the query fails. Nesting 2nd db.none inside .then() of 1st db.one will NOT rollback 1st query. So using a transaction here. I am stuck at using the result of 1st query in 2nd. Here is what I have now. db.tx(function (t) { return t.sequence([ // generated using pgp.helpers.insert for singleData query t.one("INSERT INTO table(a, b) VALUES('a', 'b') RETURNING id"), t.none("INSERT INTO another

Conditional task with pg-promise

孤街浪徒 提交于 2019-12-04 18:18:47
I am trying to simply read a value from a table and based on the return value call for additional queries and return the combined results. let's take a simple example: table Users has id , name and emailid and let's say if emailid is not null we want to call the email table and return a results like { id:[id], name:[name], email:[email]} . Using the latest syntax supported by pg-promise : db.task(t => { return t.map('SELECT * FROM Users', [], user => { return user.emailid ? t.one('SELECT * FROM Emails WHERE id = $1', user.emailid, e=> { user.email = e.email; return user; }) : user; }).then(t

How to get results from multiple queries at once with pg-promise?

∥☆過路亽.° 提交于 2019-12-03 07:17:22
Currently I have the following code to get the results of two queries dbro.many("SELECT geoname_id, country_name FROM paises WHERE locale_code=$1 LIMIT 10",data.lang) .then(function(countriesData){ data.countries=countriesData; dbro.many("SELECT * FROM categorias") .then(function(categoriesData){ data.categories=(categoriesData) console.log(data); res.render('layout', data); res.end(); }) .catch(function(err){ console.log("error while fetching categories data"); }) }) .catch(function(err){ console.log("error while fetching countries data",err); }); Somehow I think this is not right. What if I

nodeJS inserting Data into PostgreSQL error

时光怂恿深爱的人放手 提交于 2019-12-02 09:17:28
问题 I have a weird error using NodeJS with a PostgreSQL and I hope you can maybe help me out. I have a huge amount of data sets, about 2 Million entries that I want to insert into my DB. One data consists of 4 columns: id: string, points: float[][] mid: float[] occurences: json[] I am inserting data like so: let pgp = require('pg-promise')(options); let connectionString = 'postgres://archiv:archiv@localhost:5432/fotoarchivDB'; let db = pgp(connectionString); cityNet.forEach((arr) => { db .none(

Interdependent Transactions with pg-promise

独自空忆成欢 提交于 2019-12-02 01:29:01
问题 I am trying to build an app involves posts and tags for posts. For these I have a post , tags and post_tag table. tags has the tags I have defined before hand and in somewhere in the app is suggested to the user on the front-end. post_tag table holds the post and tag ids as pairs on each row. I use express.js and postgreql and pg-promise. As far as I know I need a transactional query(ies) for a create post operation. Also I need a mechanism to detect if a tag was not in tags table when the

Interdependent Transactions with pg-promise

a 夏天 提交于 2019-12-01 20:59:39
I am trying to build an app involves posts and tags for posts. For these I have a post , tags and post_tag table. tags has the tags I have defined before hand and in somewhere in the app is suggested to the user on the front-end. post_tag table holds the post and tag ids as pairs on each row. I use express.js and postgreql and pg-promise. As far as I know I need a transactional query(ies) for a create post operation. Also I need a mechanism to detect if a tag was not in tags table when the user created the post, so that I can insert it on the fly, and I have a tag_id for each tag that is

Inserting multiple records with pg-promise

我只是一个虾纸丫 提交于 2019-11-30 11:57:30
问题 I have a scenario in which I need to insert multiple records. I have a table structure like id (it's fk from other table), key(char), value(char). The input which needs to be saved would be array of above data. example: I have some array objects like: lst = []; obj = {}; obj.id= 123; obj.key = 'somekey'; obj.value = '1234'; lst.push(obj); obj = {}; obj.id= 123; obj.key = 'somekey1'; obj.value = '12345'; lst.push(obj); In MS SQL, I would have created TVP and passed it. I don't know how to

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

寵の児 提交于 2019-11-30 09:18:53
问题 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,

Inserting multiple records with pg-promise

て烟熏妆下的殇ゞ 提交于 2019-11-30 03:18:33
I have a scenario in which I need to insert multiple records. I have a table structure like id (it's fk from other table), key(char), value(char). The input which needs to be saved would be array of above data. example: I have some array objects like: lst = []; obj = {}; obj.id= 123; obj.key = 'somekey'; obj.value = '1234'; lst.push(obj); obj = {}; obj.id= 123; obj.key = 'somekey1'; obj.value = '12345'; lst.push(obj); In MS SQL, I would have created TVP and passed it. I don't know how to achieve in postgres. So now what I want to do is save all the items from the list in single query in