Please be aware: This particular answer is no longer applicable following the release of v6.10.0 for pg-promise, as it now has built-in support for Nested Named Parameters.
The code example mentioned in the original question will now function correctly without any alterations.
Specifically for usage with pg-promise versions earlier than v6.10.0
It's important to note that the library solely formats Named Parameters and does not evaluate them, hence direct evaluation, as shown, is not possible within the library. However, you can achieve similar functionality by making sub-properties accessible to the query-formatting engine through defined functions:
var obj = {
name: 'John',
address: {
postcode: 'abc'
},
addrCode: a => a.address.postcode // utilizing an alias for sub-property access
};
You can then utilize WHERE postcode = ${addrCode}
.
Additionally, the older / ES5 syntax using this
is also viable:
var obj = {
name: 'John',
address: {
postcode: 'abc'
},
addrCode: function(/*a*/) {
// a = this (both can be used)
return this.address.postcode;
}
};
UPDATE
It's now feasible to incorporate client-side evaluations, however, only with $1, $2, ...
parameters:
db.query('... postcode = $1 AND name = $2', [obj.name, obj.address.postcode]);
Remember, utilizing evaluations like ${obj.address.postcode}
within the query string directly is not recommended, as it may lead to incorrect escaping of values.