29

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?

9 Answers 9

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

Sign up to request clarification or add additional context in comments.

1 Comment

how to use comparison operators? like this . where('user.address ::jsonb @> :address', { address: { state: LessThan (query.pincode) } })
16

Got it working.

Correct syntax:

.where(`user.address ::jsonb @> \'{"state":"${query.location}"}\'`)

1 Comment

I should add to be aware of possible SQL injection when using the above syntax. Properly sanitizing any incoming queries is a must.
15

More modern approach would be to use JsonContains.

import { JsonContains } from 'typeorm'

repo.find({
    where: { address: JsonContains({ state: 'florida' }) },
});

1 Comment

this should be the accepted answer, thanks!
9

It works for me:

.getRepository(User)
.createQueryBuilder('user')
.where(`user.address->>'state' = :state`, {state: "florida"})

Comments

2
    return getRepository(User)
    .createQueryBuilder()
    .where('address @> :address', {
      address: { state: "florida" },
    })
    .getMany();

Comments

1

try this:

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

Comments

1

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

2 Comments

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.
@nidovic, this doesn't work for camelcase, for example user.myAddress - gives an error.
0
.where(`user.address -> '$.state' = :state`, {state: "florida"})

1 Comment

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.
-6

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

.find({
   where: {
      address: Like(`%${add what to search for here}%`),
   },
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.