Tag: pg promise

用pg-promise跳过更新列

我已经有一个使用pg-promise为Postgres的节点上的API,这工作得很好,但我想如何修改PUT语句来处理input中的NULLS好一点。 以下是PUT语句的代码: //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,' + 'date3=$12,' + 'currency1=$13,' + 'currency2=$14' + 'WHERE id = $15', [req.body.string1, req.body.string2, req.body.string3, req.body.string4, req.body.string5, req.body.string6, parseInt(req.body.integer1), parseInt(req.body.integer2), parseInt(req.body.integer3), […]

在pg-promise中跨多个控制器的多个查询之间共享事务或任务对象

我对node.js,postgresql,Promise和事实上的stackoverflow都比较陌生,所以如果事情听起来有些不协调的话,我会提前道歉的! 我目前正在尝试在各种控制器中分布的链式承诺内运行多个查询。 我想在同一个事务或任务中运行所有的查询,以消除多个连接和断开连接到数据库。 我已经尝试了以下方法,在这里添加一名学生并为该学生分配两名导师。 HTTP请求被路由到学生控制器,该控制器通过学生存储库添加学生。 学生存储库是任务启动的地方,并返回到控制器,该控制器将其转发给导师控制器,并沿着链路进入… @ HttpPost("/api/students/create") addStudent( @ Req()request) { var studentid; var mentorids= []; //Add the student return this.studentRepository.addStudent(request.body.student) .then(newStudentId => { studentid = newStudentId; //Add the mentors, passing the transaction object received back from studentRepository return this.mentorController.addMentor(request.body.schoolid, request.body.mentors, newStudentId.transaction) .then(result => { var data = []; console.log(result); for (var role in […]

如何避免在postgres数据库中插入空值

我有以下查询插入到多对多连接表中的数据 INSERT INTO playlist_genre(playlist_id, genre_id) VALUES (${playlistId}, ${genre1Id}), (${playlistId}, ${genre2Id}), (${playlistId}, ${genre3Id}) ); 但是我遇到的问题是genre2Id和genre3Id值不是用户所需要的,所以可以是INTEGER或NULL 。 我正在试图find一种方法来写这个相同的查询,但它只能插入如果存在一个值。 两列都有NOT NULL约束。 编辑 : 这是我的播放列表类 class Playlist { constructor(playlist) { // required fields this.title = playlist.title; this.playlistType = playlist.playlistType; this.userId = playlist.userId; this.numberOfTracks = playlist.numberOfTracks; this.lengthInSeconds = playlist.lengthInSeconds; this.genre1Id = playlist.genre1Id; // not required fields this.genre2Id = playlist.genre2Id || […]

使用pg-promiselogging特定的postgresql查询

我使用pg-promise包与Nodejs来执行PostgreSQL查询。 我想看看执行的查询。 只有特定的查询,比如说,我只想debugging一个查询。 我可以看到,一个推荐的方法是使用pg-monitor来捕获事件并按照这里在示例文档中提到的那样logging它们。 没有使用pg-monitor ,是否有一个简单的方法来打印执行准备好的查询。 我无法在文档中看到它。 例: db.query("SELECT * FROM table WHERE id = $/id/", {id: 2}) 如何打印这个查询产生? SELECT * FROM table WHERE id = 2

与pg-promise的嵌套查询

我需要使用pg-promise进行查询,使用结果做出3个其他查询,但执行时出现这个错误: 未处理的拒绝types错误:方法'批'需要一个值的数组。 在C:\ apps \ pfc \ node_modules \ spex \ lib \ ext \ batch.js:149:26 at batch(C:\ apps \ pfc \ node_modules \ spex \ lib \ ext \ batch.js:61:26) Task.batch(C:\ apps \ pfc \ node_modules \ pg-promise \ lib \ task.js:120:39)………….. 这是我的代码: db.task(t => { return t.one('select gid, idgrupo from orden where gid […]

如何使pg-promise将数据返回为数组?

我们如何让pg-promise从查询中返回一个行数组,而不是行对象数组呢?

Pg-promise性能提升:具有多个更新参数的多个插入

正如我在这里和那里所build议的那样,我正在实施Vitaly的pg-promise性能模式。 这是我的代码: for (var i=0;i<chunkedData.length;i++){ var insertData = chunkedData[i].map(function (d) { return { application_id: d.application_id, country_id: d.country_id, collection_id: collectionId }; }); // Would need to make a loop here, and thus turning the result into an array var updateData = { application_id: chunkedData[i][j].application_id, country_id: chunkedData[i][j].country_id, collection_id: collectionId }; var query = h.insert(insertData, cs) + " […]

为什么AWS Lambda执行时间长,使用pg-promise

我开始使用AWS Lambda执行一个非常简单的任务,即执行SQL查询以从RDS postgres数据库检索logging,并在结果上创buildSQS消息库。 因为Amazon默认只提供aws-sdk模块(使用node 4.3引擎),所以我们需要执行这个SQL查询,所以我们必须创build一个包含pg-promise的自定义部署包。 这是我正在使用的代码: console.info('Loading the modules…'); var aws = require('aws-sdk'); var sqs = new aws.SQS(); var config = { db: { username: '[DB_USERNAME]', password: '[DB_PASSWORD]', host: '[DB_HOST]', port: '[DB_PORT]', database: '[DB_NAME]' } }; var pgp = require('pg-promise')({}); var cn = `postgres://${config.db.username}:${config.db.password}@${config.db.host}:${config.db.port}/${config.db.database}`; if (!db) { console.info('Connecting to the database…'); var db = pgp(cn); […]

Node / Express&Postgresql – 当没有行匹配时

你好我是Postgresql的新手,我想知道如何处理0结果作为错误被抛出。 基本上我想获得一个用户,如果它不存在,返回null,如果没有,并有一个error handling程序。 以下是我正在使用的当前代码。 任何提示,以更好的方式来做到这一点,表示赞赏! var options = { // Initialization Options promiseLib: promise }; var pgp = require('pg-promise')(options); var connectionString = 'postgres://localhost:5432/myDbName'; var db = pgp(connectionString); function getUser(id) { let user = new Promise(function(resolve, reject) { try { db.one('select * from users where loginName = $1', id).then(function(data) { console.log(data); resolve(data); }).catch (function (e) { […]

使用pg-promise嵌套查询对象映射

我正在通过pg-promise的一个例子来说明方法映射 : // Build a list of active users, each with the list of user events: db.task(t => { return t.map('SELECT id FROM Users WHERE status = $1', ['active'], user => { return t.any('SELECT * FROM Events WHERE userId = $1', user.id) .then(events=> { user.events = events; return user; }); }).then(t.batch); }) .then(data => { […]