brianc / node-postgres

PostgreSQL client for node.js.

Home Page:https://node-postgres.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to pass a number as parameter to the database?

jaads opened this issue · comments

As stated in the docs here a parameter which i of type number is converted to a string by node-postges before passing it to the database.

Is there a way to not convert the parameter and instead pass it to the database as actual number?

I want to use the parameter to select an item from an array stored in a JSON field. Only when it's an actually number is it considered an index, otherwise it is considered as a property of an object which is not what I want. Due to the conversion to string, the query gives me the wrong results currently.

I realised that I don't have to pass the number as parameter since in my use case it's not user input. so problem solved :)

It’s hard to say for sure without seeing an example of the type of query you’re making, but it sounds like (if you wanted to use a parameter) your query should include $1::integer, be casting from a JSON value to integer, or something along those lines. (pg doesn’t forward any type information to PostgreSQL by default, so types are determined entirely by PostgreSQL – the same way they would be in a PREPARE statement, for example.)