Typeorm: Raw query with json columns (Postgres)

Created on 24 Jul 2017  路  3Comments  路  Source: typeorm/typeorm

Folks, I am trying to build a query like this

`` let query =SELECT "incident".* FROM "incident" "incident"
WHERE incidents::jsonb @> '[{"name":$1}]' ORDER BY "incident"."id" ASC`;

const results: any = await repository.query(query, ['Briga']);
I want to add the value of the json as a parameter, but I am getting an error

`error during executing query:error: invalid input syntax for type json
`

The final query I want is this one:

SELECT "incident".*
FROM "incident" "incident" WHERE incidents::jsonb @> '[{"name":"briga"}]'
ORDER BY "incident"."id" ASC
```

I searched in the documentation how to parametrize queries with json fields but could not find anything. Could you help me?

question

Most helpful comment

repository.query calls underlying pg package's query method and uses its parameters syntax. Refer to pg package how to resolve your problem. But in my opinion you should do:
incidents::jsonb @> $1 and set to parameter the whole json.

All 3 comments

repository.query calls underlying pg package's query method and uses its parameters syntax. Refer to pg package how to resolve your problem. But in my opinion you should do:
incidents::jsonb @> $1 and set to parameter the whole json.

Perfect, @pleerock! It worked!

@pleerock @brunosiqueira This saved me! It was hard to find an example similar to this one. Thank you.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cameronpickham picture cameronpickham  路  3Comments

banduk picture banduk  路  3Comments

juanjalvarez picture juanjalvarez  路  3Comments

arthurvasconcelos picture arthurvasconcelos  路  3Comments

shotor picture shotor  路  3Comments