0

How would you execute this query with typeorm?

SELECT * from t_integration_app WHERE settings = JSON_OBJECT("modelType", JSON_ARRAY("ACTION"));

I tried something like:

repository.find({
    where: {
        settings: { modelType: "ACTION" }
    },
})

but it fails with this error message

"errorMessage": "Unknown column 'modelType' in 'where clause'"

1

2 Answers 2

0

This worked!

await repository
  .createQueryBuilder('t_integration_app')
  .where({ ...filter })
  .andWhere(
    `t_integration_app.settings = JSON_OBJECT("modelType", JSON_ARRAY(:modelType))`,
    { modelType: filterBy.modelType }
  )
  .getMany();
Sign up to request clarification or add additional context in comments.

Comments

0

I also wanted to filter using a JSON Object, but your solution didn't really do the Job. Looking at the raw MySQL query there seems to be a way to query using JSON Columns as where clause mysql json where clause.

From that I was able to get this (adapted to your question).

await repository
  .createQueryBuilder('t_integration_app')
  .where({ ...filter })
  .andWhere(
    `t_integration_app.settings ->>"$.modelType" = :modelType`,
    { modelType: filterBy.modelType }
  )
  .getMany();

This worked for me and should work for even further nested JSON Objects.

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.