Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Embeddable array of objects cannot be searched by properties #1887

Open
tudddorrr opened this issue May 31, 2021 · 4 comments
Open

Embeddable array of objects cannot be searched by properties #1887

tudddorrr opened this issue May 31, 2021 · 4 comments
Assignees
Labels
bug Something isn't working

Comments

@tudddorrr
Copy link

Describe the bug
Embeddable arrays cannot be searched in mysql (haven't tested other drivers). I think the mysql driver manually creates a query for searching json objects but doesn't take into account that embeddables can be arrays of objects.

To Reproduce
Create an Embeddable array property in an entity:

@Embeddable()
class Prop {
  @Property()
  key: string
  
  @Property()
  value: string
}

...

@Embedded(() => Prop, { array: true })
props: Prop[] = []

Try to search by a property inside the array of objects:

const { search } = request.query
const players = await em.find({ props: {
  value: {
    $like: `%${search}%`
  }
 })

This will produce an sql query looking something like this:

select `e0`.* from `player` as `e0`
where `e0`.`props`->'$.value' like '%xyz%'

Expected behavior
If the embeddable is set to array mode then the query should use the json array search syntax. I'm not sure if there's any fancy syntax for doing that but currently I'm using the QueryBuilder to manually use json_extract:

em.createQueryBuilder(Player, 'p')
  .where('json_extract(props, \'$[*].value\') like ?', [`%${search}%`])
@B4nan B4nan added the bug Something isn't working label May 31, 2021
@tudddorrr tudddorrr changed the title Embeddable array of objects cannot be search by properties Embeddable array of objects cannot be searched by properties Jun 6, 2021
@mbvissers
Copy link

mbvissers commented Dec 8, 2022

Is there any update on working with an array of embeddables? I'm facing a similar issue.

E: I have been able to resolve my issue by using the last snippet of tudddorrr. It's not the cleanest but it works great that way.

@B4nan
Copy link
Member

B4nan commented Dec 8, 2022

The problem with this is that only MySQL offers feasible solution, I haven't found a way to do it with other drivers (that would fit how the QB is designed).

@jcrben
Copy link

jcrben commented Oct 7, 2023

Is it worth submitting feedback upstream to postgres? Curious about the blocker in how postgres works versus MySQL.

@B4nan
Copy link
Member

B4nan commented Oct 7, 2023

More context here, I got some suggestions on how this could be done, but the problem is that it's not possible to go that way without a lot of added abstractions, or it would be very limited in general.

https://stackoverflow.com/questions/67785282/how-can-i-filter-by-jsonb-object-value-inside-array

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants