Parameterized/Prepared Statements usage pg-promise
<p>I'm using <code>koa v2</code> with <code>pg-promise</code>. I try to do a simple <code>SELECT 2 + 2;</code> within a Parameterized/Prepared Statement to test my setup:</p>

<pre class="lang-js prettyprint-override"><code>// http://127.0.0.1:3000/sql/2
router.get('/sql/:id', async (ctx) =&gt; {
  await db.any({
    name: 'addition',
    text: 'SELECT 2 + 2;',
  })
    .then((data) =&gt; {
      console.log('DATA:', data);
      ctx.state = { title: data }; // =&gt; I want to return num 4 instead of [Object Object]
    })
    .catch((error) =&gt; {
      console.log('ERROR:', error);
      ctx.body = '::DATABASE CONNECTION ERROR::';
    })
    .finally(pgp.end);

  await ctx.render('index');
});
</code></pre>

<p>Which is rendering <code>[Object Object]</code> in the templates and returning this to the console from <code>pg-monitor</code>:</p>

<pre class="lang-bash prettyprint-override"><code>17:30:54 connect(postgres@postgres)
17:30:54 name="addition", text="SELECT 2 + 2;"
17:30:54 disconnect(postgres@postgres)
DATA: [ anonymous { '?column?': 4 } ]
</code></pre>

<p><strong>My problem:</strong></p>

<p>I want to store result <code>4</code> in <code>ctx.state</code>. I don't know how can I access it within <code>[ anonymous { '?column?': 4 } ]</code>?</p>

<p>Thank You for your help!</p>

<p><strong>Edit:</strong></p>

<p>I found another <a href="https://github.com/vitaly-t/pg-promise/wiki/Common-Mistakes#named-parameter-formatting-is-not-used-where-it-should-be" rel="nofollow noreferrer">recommended(1)</a> <a href="https://github.com/vitaly-t/pg-promise#named-parameters" rel="nofollow noreferrer">ways(2)</a> to dealing with named parameters in the official wiki.</p>

<pre class="lang-js prettyprint-override"><code>// http://127.0.0.1:3000/sql/2
router.get('/sql/:id', async (ctx) =&gt; {
  const obj = {
    id: parseInt(ctx.params.id, 10),
  };
  await db.result('SELECT ${id} + ${id}', obj)
    .then((data) =&gt; {
      console.log('DATA:', data.rows[0]['?column?']);
      ctx.state = { title: data.rows[0]['?column?'] }; // =&gt; 4
    })
    .catch((error) =&gt; {
      console.log('ERROR:', error);
      ctx.body = '::DATABASE CONNECTION ERROR::';
    })
    .finally(pgp.end);

  await ctx.render('index');
});
</code></pre>

<p>I changed the <code>any</code> object to <code>result</code>, which returning the raw text. Than I access number <code>4</code> like a javascript object. Am I doing something wrong? Is there another way to access this value?</p>

<p>What is the recommended, more faster, safer way of usage?</p>