-1

I am using nest js framework for my demo application. In my demo application I have an entity Category which has two columns (id, description). description is jsonb type.

export class Category extends BaseEntity {
  @PrimaryGeneratedColumn()
  id: number;

  @Column({ type: 'jsonb' })
  description;
}

I inserted some data in my table.

enter image description here Now I want to filter that data and only show those rows which have name = test.

Currently I'm using the code shown here, which returns all rows. But I need only the rows which have name equals to test. Currently there is only one row

@Get('/app')
  async getApp(): Promise<Category[]> {
    return this.categoryRepository.find({});
  }

Here is my full code: https://codesandbox.io/s/nest-nhqyb?file=/src/app.controller.ts:568-670

You can see filter result like

https://nhqyb-5000.sse.codesandbox.io/app

1 Answer 1

0

According to this issue, it's not possible unless you are writing raw SQL and using database specific syntax.

In Postgres, it would look something like

SELECT * 
FROM Category cat
WHERE EXISTS (
  SELECT FROM jsonb_each(cat.description) desc
   WHERE  desc.value->>'name' ILIKE 'test'
   );

Lots more discussion and in depth answers over here

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

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.