Tag: pg promise

在pg-promise中查询超时

我想为pg-promise查询添加超时,所以如果数据库还没有响应,它们会在一段时间后失败。 有没有build议的方式来做到这一点,或者我应该做自定义包装将处理计时器和拒绝承诺如果为时已晚?

在Nodejs中重build连接,pg-promise

在使用pg-promise构buildmaster / replica postgres连接的情况下,有没有办法在出现副本中断的情况下重build这些连接? 而不是做process.exitCode = 1; 在与initOptions传递的错误函数,并重build服务启动时只有工作连接…有没有更好的方式来删除失败的连接(如果它的副本和process.exitCode更好,如果它的主要)? const initOptions = { // global event notification; error: (error, e) => { if (e.cn) { //log } process.exitCode =1; } }; //singleton const pgp = require('pg-promise')(initOptions); // then for each database in config, we connect and start the service

pg-promise mulitple或在where子句中

我在pg-promise上遇到了一些麻烦。 我想在WHERE子句中指定一个未定义数目的参数。 所有参数将与OR连接。 db.any( 'SELECT genre FROM genres'+ 'WHERE ( (genres.genre LIKE \'Animation\' OR genres.genre LIKE \'Action\') ') .then(function(data) { // success; }) .catch(function(error) { // error; }); }); 我没有一个一个的指定参数,我有一个由用户传递的stream派的数组。 我想做这样的事情… var genres = ["Action", "Adventure", "Romantic"]; db.any( 'SELECT genre FROM genres'+ 'WHERE ( (genres.genre LIKE $1) '), [genres]) .then(function(data) { // success; }) .catch(function(error) […]

pg-promise中的dynamic命名参数

我在我的REST API中有一个补丁端点,其中每个身体参数都是可选的。 没有手动检查每个参数,实现它的最好方法是什么? 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); });

使用sinon和mocha来保存pg-promise

假设我有一个以下模块,作为database.js const initOptions = {} const pgp = require('pg-promise')(initOptions) const config = require('../../config') const db = pgp({ host: config.database.host, port: config.database.port, database: config.database.database, user: config.database.user, password: config.database.password }) module.exports = db 和下面的模块一样create.js const db = require('./database') function create (name) { return new Promise((resolve, reject) => { db.func('create', name) .then(data => { return resolve(data) }) .catch(err […]

pg-promise helpers.update不起作用

我无法得到helpers.update方法的工作。 我几乎100%肯定,这是简单的,我俯瞰。 以下是代码中涉及的表中的列: user_social_profile_id user_id user_social_profile_platform user_social_profile_link 以下是我对ColumnSet定义。 (注意我添加了一些不在我的实际代码中的空格,我只是不想滚动这些窗口。) const usersSocialProfiles = new pgp.helpers.ColumnSet( [ 'user_id', 'user_social_profile_platform', 'user_social_profile_link' ], {table: 'users_social_profiles'} ); const usersSocialProfilesUpdate = new pgp.helpers.ColumnSet( [ '?user_social_profile_id', '?user_id', 'user_social_profile_platform', 'user_social_profile_link' ], {table:'users_social_profiles'} ); 然后一个函数来切换我想要使用的ColumnSet var pgpConstantSet = function(setName){ if(setName === "usersSocialProfiles") { return usersSocialProfiles; } if(setName === "updateUsersSocialProfiles") { return usersSocialProfilesUpdate; } } […]

使用Object更新准备的语句

我有一个Object ,将列名称映射到值。 预先不知道要更新的列,并在运行时决定。 例如map = {col1: "value1", col2: "value2"} 。 我想执行一个UPDATE查询,将这些列的表更新为相应的值。 我可以做以下吗? 如果没有,是否有一个优雅的方式,而不需要手动构build查询? db.none('UPDATE mytable SET $1 WHERE id = 99', map)

转义JSON以避免更改插入string

我有下面的jsonstring,应该直接插入到一个列中的数据库中: const jsString = JSON.stringify({"escaping":"d\"on't"}); const insertion = [{"a":2,"json":jsString}]; const query = pgp.helpers.insert(insertion,["a","json"],"tbl"); 然而实际上结果在数据库中的是: {"escaping":"d"on't"} 删除在\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\' 这将是有益的,因为如果我的有效json仍然如此,那将是很好的。

使用Node.js从其他文件调用方法

我没有要求我的./db/index.js方法到我的server.js文件从数据库中select数据并显示它。 /db/index.js是这样的: 'use strict'; const pgp = require('pg-promise')(); const pg = pgp(process.env.DATABASE_URL); let select = () => { pg.any('SELECT username, status FROM status') .then(function(data){ for (var item of data) { return item.username + "'s status is " + item.status; } }) .catch(function(err) { return 'Error: ' + err.message || err; }); }; module.exports = () […]

使用asynchronousdb编程来sorting动作

新的asynchronous和挣扎。 在下面的例子中,我希望初始化一个表,然后通过以下方式处理它的内容:a)删除旧数据b)插入新loggingc)将表读入数组d)显示数组 'use strict'; // ================================================================================ // Module dependencies var pgp = require('pg-promise')(); // ================================================================================ //Configure the database connection var config = { user: 'user', //env var: PGUSER database: 'database', //env var: PGDATABASE password: 'password', //env var: PGPASSWORD }; var db = pgp(config); // ================================================================================ // Initialise rhe variables var customers = []; // ================================================================================ […]