Tag: pg promise

如何通过pg-promise传递表名和参数

我是新来的节点,随后pg-promise。 我正在尝试在我的节点快车应用程序中实现的是一个path,将作为一个API在数据库中的表中执行单个字段更新。 这只是一个单独的路由,将使用表名称,列名称,列值和列标识值来标识表中要更新的行。 这是我到目前为止所尝试的代码,但我似乎无法做到这一点: router.post('/update_db_field', function (req, res, next) { db.one('update $table~ set $dbcol~=$dbcolval~ where $dbcolid~=$dbcolidval~ returning $colid~', [ req.body.table, req.body.dbcol, req.body.dbcolid, req.body.dbcolval, req.body.dbcolidval ]).then(function (data) { console.log(data) res.send(data) }).catch(function (error) { console.log("ERROR:", error.message || error); // print error; }); }) 不幸的是,它抛出了以下错误: ERROR: syntax error at or near "$" 是否还有一种方法来打印来自catch()函数的查询string,以便debugging可以更容易一些?

NodeJS和pg-promise,从JSON对象dynamic插入

我正在运行NodeJS和pg-promise,并正在尝试完成诸如: db.none('INSERT INTO my-table (JSON-object-keys) VALUES ($1)', [JSON-object-values]) .catch(function(err) { console.log('Error on insert into my-table: ' + err); }); 我有JSON对象可以看起来像: {"column1":"value1", "column2":"value2", "column3":"value3"} {"column2":"value2", "column3":"value3"} {"column1":"value1", "column3":"value3"} 我希望INSERTS根据JSON对象包含的内容自动生成。 这是否可能以优雅的方式? 更多的解释,在JSON的3个例子中应该产生以下内容: db.none('INSERT INTO my-table (column1, column2, column3) VALUES ($1, $2, $3)', [value1, value2, value3]) .catch(function(err) { console.log('Error on insert into my-table: ' + err); }); db.none('INSERT […]

连接池使用pg-promise

我使用Node js和Postgresql,并试图在连接实现中最有效率。 我看到pg-promisebuild立在node-postgres之上,node-postgres使用pg-pool来pipe理共享池。 我还读到,“一次有100多个客户是非常糟糕的”( node-postgres )。 我正在使用pg-promise并想知道: 对于非常大的数据负载,推荐的poolSize是多less? 如果poolSize = 100并且应用程序同时获得101请求(甚至更多),会发生什么情况? Postgres是否处理这个命令并且使得这个请求等到它可以运行?

在语句中使用pgp.helpers.insert和RETURNING

在前面的问题中,我非常好的dynamic创buildINSERT语句: NodeJS和pg-promise,从JSON对象中dynamic插入 我试图看看文档,并希望在INSERT语句中包含RETURNING,所以我得到插入的logging作为pg-promise的输出。 我正在使用pg-promise的NodeJS。 是否有可能返回所有列插入的logging?

pg-promise:在事务中的下一个查询中使用一个查询的结果

我是postgres的pg-promise的node.js,试图用2个插入按顺序进行事务。 第一个插入的结果ID应该在事务的下一个插入中使用。 如果任何查询失败,则回滚。 嵌套第二个db.none里面db.none .then()第一个db.one不会回滚第一个查询。 所以在这里使用一个交易。 我坚持在第二次使用第一个查询的结果。 这是我现在拥有的。 db.tx(function (t) { return t.sequence([ // generated using pgp.helpers.insert for singleData query t.one("INSERT INTO table(a, b) VALUES('a', 'b') RETURNING id"), t.none("INSERT INTO another_table(id, a_id) VALUES(1, <above_id>), (2, <above_id>)") ]); })… 使用pgp.helpers.insert为多数据查询生成第二个查询。 但是,想要使用以前的查询结果是不可行的。 是不是! 有没有办法从第一个INSERT获得id ie <above_id> ?

如何将单独的pg-promise查询合并到pg-promise中

我是新来的节点和pg-promise,并没有能够弄清楚如何得到三个相关查询的结果成一个json结果。 给出三个相关的表格: 父母实体 create table parent ( id bigint, name character varying ); 一个小孩实体 create table entity_2 ( id bigint, parent_id bigint, name character varying ); 孩子和同事之间的许多桌子 create table entity_2_entity_3 ( id bigint, child_id bigint, associate_id bigint ); 子表的关联表 create associate ( id bigint, name character varying ); 而我的服务url是/ api / family / 1(其中1是子标识) 第一个查询是(返回一个结果): […]

在过期的连接之外放松请求

我正在尝试执行以下pg-promise查询,但收到此错误: 在过期的连接之外放松请求 查询实际上成功,但是,即使如此,我想摆脱错误消息。 pg-promise查询 db.task(t => { t.oneOrNone(queries.insertUser, [profile.id]) .then(id =>{ if (id) { t.none(queries.insertUserGoogle, [ profile.id, profile.emails[0].value, profile.name.givenName, profile.name.familyName, profile.displayName ]) }}) .catch(err => console.log(err)) SQL const insertUser = `INSERT INTO users (google_id) VALUES ($1) ON CONFLICT (google_id) DO NOTHING RETURNING user_id`; const insertUserGoogle = `INSERT INTO users_google (google_id, email, first_name, last_name, display_name) VALUES […]

使用Express创build反应应用程序

我需要查询一个数据库,我正在使用create-react-app 。 连接到数据库( pg-promise )的库不能与Webpack一起使用,需要在节点服务器上运行。 所以我安装了Express并且有这个: app.get('*', (req, res) => { res.sendFile(path.resolve(__dirname, '..', 'build', 'index.html')); }) 如何从React页面加载数据库中的数据? 我虽然使用request但我怎样才能请求我自己的服务器? 我应该添加到上面的代码行? 我认为它会是这样的: app.get('/query/:querybody', (req, res) => { // process and return query }) 这是正确的吗? 我怎样才能使它与SPA一起工作?

nodeJS将数据插入PostgreSQL错误

女士们,先生们, 我有一个奇怪的错误使用PostgreSQL的NodeJS,我希望你也许可以帮助我。 我有大量的数据集,大约200万个条目,我想插入到我的数据库。 一个数据由4列组成: id: string, points: float[][] mid: float[] occurences: json[] 我插入像这样的数据: let pgp = require('pg-promise')(options); let connectionString = 'postgres://archiv:archiv@localhost:5432/fotoarchivDB'; let db = pgp(connectionString); cityNet.forEach((arr) => { db .none( "INSERT INTO currentcitynet(id,points,mid,occurences) VALUES $1", Inserts("${id},${points}::double precision[],${mid}::double precision[],${occurences}::json[]",arr)) .then(data => { //success }) .catch(error => { console.log(error); //error }); }) function Inserts(template, data) { if […]

在Upsert上跳过空值

我正在使用pg-promise来处理我的Postgres查询,而且我遇到了麻烦,find下面的查询的解决scheme: 我正在尝试创build一个批量插入多行的方法,这是我的代码: massUpsert: (orgId, entities) => db.tx((t) => { const queries = []; entities.forEach((entity) => { const { id, name, age, type } = entity; queries.push( t.one(`INSERT INTO public.table (id, name, age, type) VALUES ($1, $2, $3, $4) ON CONFLICT (id) DO update SET name = $2, age = $3, type = $4 RETURNING *`, […]