pg-promise

Dynamic named parameters in pg-promise

橙三吉。 提交于 2019-12-23 02:18:12
问题 I have a patch endpoint in my REST API where every body parameter is optional. What is the best way of implementing it without checking each parameter manually? db.none("update tasks set title=$1, description=$2, value=$3 where id=$4", [req.body.title, req.body.description, parseInt(req.body.value), req.params.id]) .then(function () { res.status(200) .json({ status: "success", message: "Updated task" }); }) .catch(function (err) { return next(err); }); 回答1: Use methods in the helpers

Massive inserts with pg-promise

╄→гoц情女王★ 提交于 2019-12-22 14:59:23
问题 I'm using pg-promise and I want to make multiple inserts to one table. I've seen some solutions like Multi-row insert with pg-promise and How do I properly insert multiple rows into PG with node-postgres?, and I could use pgp.helpers.concat in order to concatenate multiple selects. But now, I need to insert a lot of measurements in a table, with more than 10,000 records, and in https://github.com/vitaly-t/pg-promise/wiki/Performance-Boost says: "How many records you can concatenate like this

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

雨燕双飞 提交于 2019-12-22 10:43:28
问题 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

Log specific postgresql query using pg-promise

a 夏天 提交于 2019-12-22 09:49:35
问题 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

Conditional task with pg-promise

本秂侑毒 提交于 2019-12-22 00:52:37
问题 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]} . 回答1: Using the latest syntax supported by pg-promise: db.task(t => { return t.map('SELECT * FROM Users', [], user => { return user.emailid ? t.one('SELECT

node.js pg-promise and pagination from API

*爱你&永不变心* 提交于 2019-12-21 20:55:19
问题 Looking at https://github.com/vitaly-t/pg-promise/wiki/Data-Imports there's a very detailed doc on how to use it for importing. However while that works for the demoed scenario I don't know how to apply it on my case. When I do my web call, I get the actual JSON data and a paramter in the header which gives me a value for the next page (could be a date or String or a number value). In the example, it says: db.tx('massive-insert', t => { return t.sequence(index => { return getNextData(index)

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

可紊 提交于 2019-12-20 20:41:57
问题 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){

skip update columns with pg-promise

﹥>﹥吖頭↗ 提交于 2019-12-18 09:14:09
问题 I've got an API up on node using pg-promise for Postgres, this works well but i'm thinking about how to modify the PUT statement to handle NULLS in the input a little better. The following is the code for the PUT statement: //UPDATE a single record function updateRecord(req, res, next) { db.none('update generic1 SET string1=$1,' + 'string2=$2,' + 'string3=$3,' + 'string4=$4,' + 'string5=$5,' + 'string6=$6,' + 'integer1=$7,' + 'integer2=$8,' + 'integer3=$9,' + 'date1=$10,' + 'date2=$11,' +

Verify database connection with pg-promise when starting an app

故事扮演 提交于 2019-12-17 17:56:27
问题 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

Best practice for creating SQL SELECT queries while handling potential undefined values

随声附和 提交于 2019-12-14 02:43:38
问题 I'm currently creating a NodeJS website using PostgreSQL via pg-promise. I have a page with an HTML form with checkboxes to select variables to search the database for using various fields. These are then fed into a SQL query with pg-promise and the intended behaviour is the results are passed back to the user in JSON format. A very minimal working example would be as follows. HTML form: <form action="/search" method="get"> <fieldset> <legend>Variable A</legend> <div> <input type="checkbox"