pg-promise

How to insert jsonb[] data into column using pg-promise

大憨熊 提交于 2019-12-11 05:43:06
问题 Given a table with a column of type jsonb[] , how do I insert a json array into the column? Using the provided formatters :array , :json won't work in this instance - unless I am missing the correct combination or something. const links = [ { title: 'IMDB', url: 'https://www.imdb.com/title/tt0076759' }, { title: 'Rotten Tomatoes', url: 'https://www.rottentomatoes.com/m/star_wars' } ]; const result = await db.none(`INSERT INTO tests (links) VALUES ($1:json)`, [links]); 回答1: You do not need the

rebuilding connections in Nodejs, pg-promise

不问归期 提交于 2019-12-11 05:34:43
问题 In the scenario where master/replica postgres connections are built using pg-promise , is there a way to rebuild these connections in case of replica outages? Instead of doing process.exitCode = 1; in the error function passed with the initOptions and rebuilding only working connections on service start-up... Is there a better way to remove the failing connection (even better if its a replica and process.exitCode if its a primary)? const initOptions = { // global event notification; error:

Best way to query a Many to Many Relationship using pg-promise

余生长醉 提交于 2019-12-11 02:36:34
问题 For example I want to get the user info, emails and it's roles from db and create an object like : { "id": 1, "firstname": "John", "lastname": "Johnny", "emails": [ { "type": "work", "email": "work@work.com" }, { "type": "personal", "email": "personal@personal.com" } ], "roles": [ { "role": "ADM", "title": "Admin" }, { "role": "PUB", "title": "Publisher" } ] } There are three tables I need to query: Users table has id , firstname , lastname . Emails table has type , email , user_id . Roles

How to turn nested callback into promise?

浪子不回头ぞ 提交于 2019-12-11 01:38:12
问题 Recently I started using pg-promise with bluebird library. I have always been nesting callback and handling err in each callback. I find that the catch statement in promise looks really neat. I am not sure if it possible to turn this code to promise base? username = username.toUpperCase(); let text = "SELECT * FROM users WHERE username = $1"; let values = [username]; database.one(text, values).then(function (userObject) { // ANY WAY TO TURN this nested bycrypt into promise chain? bcrypt

How to insert into table name as alias using pg-promise insert helper?

拜拜、爱过 提交于 2019-12-11 01:18:19
问题 This is a follow-up question from this comment The use case is for such query below: INSERT INTO "GamingLogs" AS GL ("GameName", "TimeSpent") VALUES ('LOL', '2'), ('DOTA2', '1'), ('Mobius Final Fantasy', '3') ON CONFLICT ("GameName") DO UPDATE SET "TimeSpent" = GL."TimeSpent" + EXCLUDED."TimeSpent" Assume the data table contains primary string key on GameName , and an integer column TimeSpent . The purpose let's assume it logs my lifetime total hours of gaming time on given GameName . UPDATE:

Pg-promise performance boost : ON CONFLICT

☆樱花仙子☆ 提交于 2019-12-10 19:57:42
问题 I'm trying to follow the performance pattern recommended by the pg-promise library author here. Basically Vitaly recommends to do so with inserts : var users = [['John', 23], ['Mike', 30], ['David', 18]]; // We can use Inserts as an inline function also: db.none('INSERT INTO Users(name, age) VALUES $1', Inserts('$1, $2', users)) .then(data=> { // OK, all records have been inserted }) .catch(error=> { // Error, no records inserted }); Using the following helper function : function Inserts

How do you connect to a Postgres Database with no password using pg-promise?

て烟熏妆下的殇ゞ 提交于 2019-12-10 16:19:22
问题 I am trying to initialize a connection to my PG database doing this: pgp = require('pg-promise')({ // Initialization Options }), cn = { host: 'activity.postgres.activity', // server name or IP address; port: 5432, database: 'activity', user: 'postgres', password: '' }, db = pgp(cn), But I keep getting: Error: connect ECONNREFUSED 172.20.0.3:5432 Any idea why? RESOLVED: set the listen_addresses = '*' in the postgresql.conf file 回答1: The issue is not with the library you are using or the

WHERE col IN Query with empty array as parameter

两盒软妹~` 提交于 2019-12-08 21:45:36
问题 From example where-col-in example and this answer, WHERE IN clauses should have query with parameters with following syntax const response = await db.any('SELECT * FROM table WHERE id IN ($1:csv)', [data]) where data is an array. Now, when data is an empty array, it produces the following query SELECT * FROM users WHERE id IN () which is a syntax error. Consider following statements: this works const x = await db.any('SELECT * FROM table WHERE id IN ($1:csv)', [[1, 2, 3]]); this does not work

How to set columns when using helpers in pg-promise

核能气质少年 提交于 2019-12-08 05:46:40
问题 Developed by node.js. I am using pg-promise. There is a problem with inserting the following data. I want to insert multiple rows of data into the table below. create table info ( id varchar(20) not null, name varchar(20) not null, createdate timestamp with time zone not null ) I inserted the data below. let info = [ { myid: '0001', myname: 'name1' }, { myid: '0002', myname: 'name2' }, { myid: '0003', myname: 'name3' }, ] I originally inserted the following. for (let i = 0; i <info.length; i

How to set columns when using helpers in pg-promise

时光总嘲笑我的痴心妄想 提交于 2019-12-06 14:59:05
Developed by node.js. I am using pg-promise . There is a problem with inserting the following data. I want to insert multiple rows of data into the table below. create table info ( id varchar(20) not null, name varchar(20) not null, createdate timestamp with time zone not null ) I inserted the data below. let info = [ { myid: '0001', myname: 'name1' }, { myid: '0002', myname: 'name2' }, { myid: '0003', myname: 'name3' }, ] I originally inserted the following. for (let i = 0; i <info.length; i ++) { db.none (`INSERT INTO info (id, name, createdate ') VALUES ($1, $2, CURRENT_TIMESTAMP)`, [info[i