Composite postgres type on node-postgres

Say I have the following composite postgreSQL type:

CREATE TYPE myType AS(
  id  bigint,
  name  text,
);

and stored procedure, with the exception of this type:

CREATE FUNCTION myFunction(mt myType){
//do some stuff
}

I would like to call this procedure from Node -js using the Node -postgres module.

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();
    });
});

How to create such type in JS? Is there a way to pass a type from Node to a Postgres stored procedure?

+4
source share
1 answer

After some extra work, I found a solution to this problem.

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();
   });
});

Connect return this: 12345,you. When adding bracts it will create a line that will look like '(12345,'you')', in DB Postgres it will be added to myType.

+2
source

All Articles