Search code examples
javascriptpostgresqlpostgresql-9.1pg-promise

pg-promise method for PostgreSQL void function


When executing a PostgreSQL function that RETURNS void, db.none() rejects with "No return data was expected.".

I then use the db.one() method, which resolves with the object { PS_name: '' }.

Should I expect PostgreSQL void functions to actually return an empty object? What would be the most appropriate way of handling void functions with pg-promise?


Solution

  • The answer below is no longer valid, as PostgreSQL v11 added support for proper stored procedures, and method proc now can only invoke a stored procedure, using the new CALL syntax.


    What would be the most appropriate way of handling void functions with pg-promise?

    Database method proc.

    db.proc('proc_name', [param1, param2,...])
        .then(data => {
            /* data = either null or an object */
        })
        .catch(error => {
            /* error */
        });
    

    UPDATE

    How do you specify the parameter types when using proc? i.e., to fix function foo(unknown) does not exist? If I do db.proc('foo($1::int2[])', x), then it throws syntax error at or near "(".

    You can't do it with method proc, which was created to keep things very simple. If your call requires SQL type casting, you need to execute it as a regular query with the casting inside the template:

    db.oneOrNone('SELECT * FROM proc_name($1::int2[], $2, ...)', [param1, param2,...])
        .then(data => {
            /* data = either null or an object */
        })
        .catch(error => {
            /* error */
        });
    

    Method oneOrNone in this context is equivalent in result expectation to method proc.