pg-promise

Pass an array of integers in array of parameters

China☆狼群 提交于 2019-12-14 00:30:41
问题 I am trying to pass an array of parameters in pg-promise's array of parameters, as recommended in pg-promise docs. db.any("SELECT fieldname FROM table WHERE fieldname = $1 AND fieldname2 IN ($2)", [1,[[1730442],[1695256]],[487413],[454336]]]) .then(function (data) { console.log("DATA:", data); // print data; }) .catch(); But it doesn't work, I'm returned a "missing ) after argument list" error. Or an "operator does not exist: integer = integer[]]" error, if I replace the parameters by : [1,

How to avoid inserting null values in postgres database

隐身守侯 提交于 2019-12-13 18:23:58
问题 I have the following query that inserts data into a many-to-many join table INSERT INTO playlist_genre(playlist_id, genre_id) VALUES (${playlistId}, ${genre1Id}), (${playlistId}, ${genre2Id}), (${playlistId}, ${genre3Id}) ); However the problem I've come to is that the values genre2Id and genre3Id are not required by the user so could either be an INTEGER or NULL . I am trying to find a way to write this same query but so it only inserts if a value is present. Both columns have NOT NULL

first transaction blocking DB (SAVEPOINT and connection lost)

妖精的绣舞 提交于 2019-12-13 04:39:21
问题 I can shorten my post if necessary I use a pg-promise transaction to insert a 'device' and all of its part (like system, disk, ...). The transaction works... but only the first time . After it my server can no more interact with the DB (neither insert nor select). Here is a pg-monitor output with these steps Thu Jul 11 2019 14:26:57 GMT+0200 (GMT+02:00) : server is listening on 9000 14:27:11 connect(hidden@hidden); useCount: 0 14:27:11 insert into "public"."roles"("name") values('Test')

How to set schema in pg-promise

拟墨画扇 提交于 2019-12-12 18:44:59
问题 I was searching the documentation of pg-promise specifically in the creation of the client. But I wasn't able to find the option to set the default schema to be used in the connection, it always uses public schema. How do I set it? 回答1: Normally, one sets the default schema(s) for the database or the role, as explained here: Permanently Set Postgresql Schema Path It is only if you want to do so without persisting the change, you might want to set the schema(s) dynamically, just for the

pg-promise set schema NodeJs

你。 提交于 2019-12-12 17:08:47
问题 I am using pg-promise to work with nodeJS and postgres, but when I try to query my User table, it retrieves the superUser. I think it is related to the fact that I don't have a schema set yet, so the code searches on the default 1. How can I set the schema on the code? I tried this: var express = require('express'); var app = express(); const PORT = 8080; var pgp = require('pg-promise')(/*options*/) var db = pgp('postgres://postgres:randompassword@localhost:5432/appname') app.listen(PORT

Return in pg-promise

ぐ巨炮叔叔 提交于 2019-12-12 15:28:36
问题 I created a separate file with all my queries using pg-promise module for Node. While for most of them I just use req, res after a query, for one I want to return a value back. It does not work. It returns undefined . passportLogin: (email)=> { db.one(`SELECT userid FROM user`) .then( result => { return result; }) .catch( error => { return error; }); } 回答1: That code has two problems at the same time: invalid promise use, when inside .catch you do return result , that's not how promise

pg-promise hangs after six queries

北城以北 提交于 2019-12-12 05:27:10
问题 I'm working on a project using pg-promise. I've tried querying a few different ways with pg-promise but they all seem to cause it to hang after 6 queries. It seems to me like the connections aren't being closed but I can't find anything in the documentation about closing a connection after a query. Here's what I have var cn = { host: 'localhost', port: 5432, database: 'db', user: 'user', password: 'password' }; var db = pgp(cn); function query(sql, params) { return db.task(function (t) { //

How pg-promise handles multiple clients in the same app

左心房为你撑大大i 提交于 2019-12-11 21:51:43
问题 I am creating code using just the pg module, where I have a general function for executing queries. This function can create a client on the spot if it doesn't take an already existing client as an argument. Or it can use an already existing client to execute a query. So I can call this function like (pseudocode) const {Client} = require('pg'); const clientobj = {user:...} generalQuery(text, params, client){ if !client{client = new Client(clientobj);} client.query(text , params) client.end();

Unable to query PostgreSQL database in NodeJS using pg-promise - “relation does not exist”

十年热恋 提交于 2019-12-11 14:28:10
问题 I'm trying to get pg-promise working in NodeJS to allow me to connect to my PostgreSQL database. Both node and postgre are running in a codeanywhere node box running Ubuntu. Here's the relevant code: var pgp = require('pg-promise')(); var bot = new TelegramBot(token, { polling: true }); var db = pgp({ host: 'localhost', port: 5432, database: 'users', user: 'postgres', password: '(pass here)' }); db.any("select * from users") .then(function (data) { console.log(data); }) .catch(function (error

Node pg-promise, bind multiple values with type casting

£可爱£侵袭症+ 提交于 2019-12-11 11:34:50
问题 I'm currently using the pg-promise library to insert multiple values into a database in the format: const cs = new pgp.helpers.ColumnSet(['booking_id', {name:'timeslot', cast:'timestamp'}], {table: 'booking'}); // data input values: const values = []; bookings.forEach(slot => { values.push({booking_id: booking_id, timeslot: slot}); }); Where I need timeslot to be a timestamp. However it comes into the API as value like 1515586500.0 Using the above cast property my query gets resolved like so