Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS and PostgreSQL function returns tuple, not row

I'm running PostgreSQL and NodeJS.

In PostgreSQL I have a custom function dummy:

... RETURNS RECORD AS $$
    ...
    DECLARE ret RECORD
    ...
    RETURN ret;

  END;
$$ LANGUAGE plpgsql;

In NodeJS this returns a tuple, as a string:

(x,y,z)

So I have to manually split the string and read the parts... Is it possible to have PostgreSQL return the tuple as a rowor similar, so I can use data.x, data.y and data.zin NodeJS instead?

like image 542
Kasper Nielsen Avatar asked Aug 10 '26 23:08

Kasper Nielsen


1 Answers

fn():

t=# create or replace function f() returns record as $$ declare ret record; begin for ret in (select oid::int,datname::text from pg_database limit 1) loop return ret; end loop; end; $$ language plpgsql;
CREATE FUNCTION

your call:

t=# select f();
        f
------------------
 (13505,postgres)
(1 row)

expected call:

t=# select * from f() as r(o int,n text);
   o   |    n
-------+----------
 13505 | postgres
(1 row)

and if you want to predefine the structure of record to be called implicitely, you can create a dummy fn():

t=# create or replace function dummy() returns table (a int,b text) as $$ begin return query select * from f() as r(o int,n text); end; $$ language plpgsql;
CREATE FUNCTION
t=# select * from dummy();
   a   |    b
-------+----------
 13505 | postgres
(1 row)

of if you want to avoid FROM:

t=# select (dummy()).*;
   a   |    b
-------+----------
 13505 | postgres
(1 row)
like image 187
Vao Tsun Avatar answered Aug 12 '26 16:08

Vao Tsun