24

Goal: write a select query that returns all rows where state equals "florida".

Entity column:

  @Column({ type: 'json'})
  public address: Address;

Sample column value:

{"city": "miami", "state": "florida"}

Example query (doesn't work):

getManager().getRepository(User)
    .createQueryBuilder('user')
    .select()
    .where('user.address.state =:state', {state: "florida"})

Is this functionality currently supported in typeorm? If so, how would I need to modify my where clause to return the correct rows?

Audwin Oyong
  • 2,247
  • 3
  • 15
  • 32
user2263572
  • 5,435
  • 5
  • 35
  • 57

8 Answers8

19

Another possible solution (raw SQL can not be injected with this):

.where('user.address ::jsonb @> :address', {
    address: {
        state: query.location
    }
})

With this, TypeORM will produce an SQL query ending with

WHERE user.address ::jsonb @> $1

and the query will be given e.g.

{ state: 'florida' }

as a query parameter to replace the right value ($1) in the query.

Sources: Mainly the original question + answer (thank you!) + own testing + https://www.postgresql.org/docs/9.4/functions-json.html

Henri Kellock
  • 190
  • 1
  • 5
  • how to use comparison operators? like this . where('user.address ::jsonb @> :address', { address: { state: LessThan (query.pincode) } }) – Nihal Chandwani Feb 20 '23 at 11:09
16

Got it working.

Correct syntax:

.where(`user.address ::jsonb @> \'{"state":"${query.location}"}\'`)
user2263572
  • 5,435
  • 5
  • 35
  • 57
  • 13
    I should add to be aware of possible SQL injection when using the above syntax. Properly sanitizing any incoming queries is a must. – user2263572 Mar 02 '19 at 16:54
9

It works for me:

.getRepository(User)
.createQueryBuilder('user')
.where(`user.address->>'state' = :state`, {state: "florida"})
2
    return getRepository(User)
    .createQueryBuilder()
    .where('address @> :address', {
      address: { state: "florida" },
    })
    .getMany();
dragansr
  • 417
  • 6
  • 8
Ivan Proskuryakov
  • 1,625
  • 2
  • 23
  • 32
1

try this:

   const users = await getManager()
  .getRepository(UserEntity)
  .createQueryBuilder(`user`)
  .where("address->>'state' = :state")
  .setParameters({ state })
  .getMany();
1

.where("user.address->>'state' = :state", {state: "florida"}).getMany();

nidovic
  • 11
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 05 '23 at 05:33
0
.where(`user.address -> '$.state' = :state`, {state: "florida"})
  • 2
    Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes. – Mark Rotteveel Jul 15 '22 at 15:34
-5

This works for me. I just use TypeORM like function.

.find({
   where: {
      address: Like(`%${add what to search for here}%`),
   },
});
Ivan Simm
  • 43
  • 4
  • 9