Search code examples
node.jspostgresqlexpressnode-postgres

Correct way of calling a postgres function using node-postgres


I am trying to workout how to call a postgres function using node-postgres but unfortunately cannot find any examples.

I am seeing examples on the web like this but this is based off a different package (pg-promise), which I am not using:

db.func('inserttask', ['task_name','task_type'])

I am also seeing examples like this as well:

const query= `SELECT inserttask($1, $2)`;        
result= await pool.query(query, ['task_name','task_type']);

I haven't as yet tried these as I wanted to check which approach is correct, when using node-postgres.


Solution

  • From the link you provided:

    const res = await client.query('SELECT $1::text as message', ['Hello world!'])
    

    Isn't it good enough?

    If you have official docs that answer your question, it is usually the one to trust.