node-postgres

Webpack can not use __dirname?

最后都变了- 提交于 2019-12-30 18:45:20
问题 I am trying to use node-postgres to hook my app up to Postgres. The code I use is: import React from 'react'; import pg from 'pg'; import fs from 'fs'; var cn = { host: 'localhost', // server name or IP address; port: 5432, database: 'my_db', user: 'myname', password: 'mypass' }; and it produces the error: index.js:5 Uncaught ReferenceError: __dirname is not defined up @ index.js:5 __webpack_require__ @ bootstrap 539ecc7…:19 (anonymous function) @ client.js:4 __webpack_require__ @ bootstrap

How to make a synchronous query (blocking call) with node-postgres?

蓝咒 提交于 2019-12-23 08:49:07
问题 While booting my Node.js app, I want to make a couple of synchronous calls to the PostgreSQL database to check some things before continuing the control flow. How can I achieve this using the node-postgres package? 回答1: The only way to synchronize calls is to nest them in callbacks: function init() { pg.connect('tcp://user@host/db', function(err, client, done) { client.query("SELECT column FROM table", function(err, result) { // check some things... client.query("SELECT column FROM other

postgres:get executable query from query with parameters

巧了我就是萌 提交于 2019-12-21 22:49:16
问题 Is there any way to get executable query from query with $ parameters.Actually its weird but i want to store executable query in database.A complete query without parameters($1,$2,$3) i am using node-postgres pg.connect(conString, function(err, client, done) { console.log('Executing Insert query'); client.query('insert into tablename(column1,column2,column3) values($1,$2,$3)',["data1","data2","data3"], function(err, result) { done(); console.log('finished executing Insert query'); }); });

query.on is not a function

折月煮酒 提交于 2019-12-21 03:34:21
问题 I am trying to learn how to use javascript to connect to a postgresql database but when I try to log a query to the console using query.on(...), I get a type error that says "query.on is not a function". I have searched extensively on how to resolve this but can't seem to find any documentation on the .on function. I know that the connection is successful because when I query the db from terminal, the two new rows have been added. jsontest.js var pg = require('pg'); var conString = "postgres:

Manually promisifying pg.connect with Bluebird

前提是你 提交于 2019-12-18 02:54:48
问题 I want to promisify node-postgres' pg.connect method along with the inner connection.query method provided in the callback. I can .promisify the latter, but I need to implement the first one manually (if I'm missing something here, please explain). The thing is, I'm not sure if this code is correct or should be improved? The code is working, I just want to know if I'm using Bluebird as meant. // aliases var asPromise = Promise.promisify; // save reference to original method var connect = pg

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

How to convert MySQL-style question mark `?` bound parameters to Postgres-style `$1` bound parameter

二次信任 提交于 2019-12-12 10:13:49
问题 I am converting an existing project from MySQL to Postgres. There are quite a few raw SQL literals in the code that use ? as a placeholder, e.g. SELECT id FROM users WHERE name = ? But I get this error: DB query error: error: operator does not exist: character varying = ? I don't want to convert all my existing SQL from ? to postgres-style operators like $1 . Is there some way of having node-postgres accept the question marks instead, or an utility that can convert to postgres style params?

Why do I need to use async/await twice in node-postgres

走远了吗. 提交于 2019-12-11 19:06:39
问题 I wrote this code that seems to be working: database.js const {Pool} = require('pg'); const pool = new Pool({ connectionString: process.env.DATABASE_URL, }); module.exports = { query: (text, params) => pool.query(text, params) }; auth_facade.js const database = require('../../utils/database'); module.exports.findPersonByEmail = async function(email) { const query = 'SELECT * FROM Person WHERE email = $1'; const values = [email]; try { console.log(1); const {rows} = await database.query(query,

querying postgres db with node-postgres

怎甘沉沦 提交于 2019-12-11 10:26:45
问题 Do I need to use pg.connect() every time I query the database? After looking over the githhub page and wiki, the examples show a query inside the pg.connect callback like this (the comments are from the github example, i did not write them) //this initializes a connection pool //it will keep idle connections open for a (configurable) 30 seconds //and set a limit of 20 (also configurable) pg.connect(conString, function(err, client, done) { if(err) { return console.error('error fetching client

Asynchronous Database Queries with PostgreSQL in Node not working

非 Y 不嫁゛ 提交于 2019-12-10 12:18:47
问题 Using Node.js and the node-postgres module to communicate with a database, I'm attempting to write a function that accepts an array of queries and callbacks and executes them all asynchronously using the same database connection. The function accepts a two-dimensional array and calling it looks like this: perform_queries_async([ ['SELECT COUNT(id) as count FROM ideas', function(result) { console.log("FUNCTION 1"); }], ["INSERT INTO ideas (name) VALUES ('test')", function(result) { console.log