postgres复合types在node-postgres上
说我有以下postgreSQL复合types:
CREATE TYPE myType AS( id bigint, name text, );
和一个除了那个types的存储过程:
CREATE FUNCTION myFunction(mt myType){ //do some stuff }
我想使用node-postgres模块从Node-js调用这个过程。
var pg = require('pg'); var connectionString = "connection string"; pg.connect(connectionString, function(err, client, done) { client.query('SELECT myFunction($1)', [some value], function(err, result) { // do stuff done(); }); });
我如何在JS中创build这样的types? 有没有办法将节点types传递给Postgres存储过程?
经过一些工作,我find了解决这个问题的办法。
var pg = require('pg'); var connectionString = "connection string"; var myType = [ 12345, 'you' ]; pg.connect(connectionString, function(err, client, done) { client.query('SELECT myFunction($1::myType)', ['(' + myType.join(',') + ')' ], function(err, result) { // do stuff done(); }); });
join将返回这个: 12345,you
。 当添加苞片时,它会创build一个string,看起来像这样'(12345,'you')'
,在Postgres数据库中它将被转换为myType
。
- 如何结束“pg-promise”应用程序
- 将自定义types的数组传递给来自node-pg和SQL注入的postgres函数
- 在node.js中导入sql文件并针对PostgreSQL执行
- node-postgres:如何在不执行查询的情况下准备一个语句?
- ECONNFUSED在应用程序中进行GET请求时,API成功返回JSON
- 使用node-postgres将文件存储在postgres中
- webpack与node-postgres导入错误('pg'.Client)
- node-postgres:如何执行“WHERE col IN(<dynamic值列表>)”查询?
- Node-postgresdate不准确保存