8000 question: support for data extraction and search for a struct/JSON column · Issue #266 · ent/ent · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

question: support for data extraction and search for a struct/JSON column #266

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

Open
chris-rock opened this issue Jan 2, 2020 · 3 comments

Comments

@chris-rock
Copy link
Contributor

It's a great experimental ent feature to be able to store structs as Json in a database table. I have a few questions regarding this feature:

  • How to search for data within JSON?
  • How to extract only specific fields from JSON?
  • Is there a plan to standardize the abstraction for common use cases?
  • What should be the syntax to write nested JSON queries?

In the meantime, I believe that ent should have the primitives to allow ent users to write those queries even without out-of-the-box support.

Examples for JSON queries:

-- search sqlite
SELECT *
FROM users, json_each(user_data)
WHERE 
    json_extract(json_each.value, '$.email') LIKE '%criteria%';

-- filter specific values in sqlite
select value, json_extract(user_data, '$.email') from entry where id = '12345';

-- filter specific values in postgres
SELECT user -> 'email' AS user_email
FROM users;

If I read the documentation right, the way to implement search would be a custom predicate.

I am not sure how a custom select statement with only specific fields can be generated. Is the way to go a custom select Select(t1.Columns("name", "age")...) statement? I think the challenge will be to find a good syntax for JSON extraction (specifically for nested fields) and its unclear to me how the mapping to entities should look like.

@a8m
Copy link
Member
a8m commented Jan 2, 2020

The "JSON type predicates support" is part of our roadmap (see #46). We didn't come up with the design yet, and I'll be happy to start the discussion/design about it here. However, we plan to finish eager-loading and hooks before start working on this.


How to search for data within JSON?

I thought about generating predicates based on the concrete type (if it's known at compile time).
For example: if the Field type is map[string]T, add predicates like: FieldHasKey. If it's a struct type (like url.URL), add predicates for all struct fields like all regular fields (and use JSON_EXTRACT under the hood).
Note that these are just some ideas I have in my head, and I plan to publish a proper design before start implementing this.

What should be the syntax to write nested JSON queries?

I don't have the answer for this. It's open for discussion.

@ernado
Copy link
Contributor
ernado commented Feb 27, 2020
< 8FC6 task-lists disabled sortable>

I propose to support Field type embedding of ent.Schema or something similar to, so it can be generated.

@Mickls
Copy link
Mickls commented Nov 2, 2023

I also encountered the problem of querying json field content when writing crud code. I am very concerned about whether this discussion has a clear answer. Considering that this is already an issue in 2020, is there a more elegant solution that can implement functions like JSON_CONTAINS or JSON_EXTRACT? If so, please reply to me, thank you very much

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants
0