Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

Say I have the following postgreSQL composite type:

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

and a stored procedure that excepts that type:

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

I would like to call this procedure from Node-js using 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 do i create such a type in JS? Is there a way to pass a type from Node to a Postgres stored procedure?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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

The join will return this: 12345,you. When adding the bracts it will create a string that will look like this '(12345,'you')', in the Postgres DB it will get cast to myType.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.