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

Adding a type for json and jsonb columns #8

Open
CrowdHailer opened this issue Dec 18, 2020 · 7 comments
Open

Adding a type for json and jsonb columns #8

CrowdHailer opened this issue Dec 18, 2020 · 7 comments

Comments

@CrowdHailer
Copy link
Contributor

I need to add a json function for inserting json to the database.

I'm assuming the best thing to do is to add a function like pgo.int

e.g.

let args = [pgo.int(value), pgo.json(data)]

I would need to add a dependency on gleam_json to get a JSON type. is this acceptable?
I guess the other option would be to work with dynamic

@lpil
Copy link
Owner

lpil commented Dec 18, 2020

What type would the pgo.json function take here? I do not want to add a dep on any specific JSON library as we don't yet have any that have been thoroughly designed.

This could also be a function in another library while we work out a more long-term API here.

@ghivert
Copy link
Sponsor Contributor

ghivert commented May 4, 2024

I think we could now add dep to gleam/json no?


If someone wonders how to support Json types from gleam_json, you could simply turn the JSON value in string, and it's working perfectly.

value
|> json.to_string()
|> pgo.text()

@lpil
Copy link
Owner

lpil commented May 4, 2024

I suspect you don't even need to do that, the iodata representation might be supported by pgo.

Sounds like we don't need to do anything then as we can already supply JSON strings. Thanks all

@lpil lpil closed this as completed May 4, 2024
@ghivert
Copy link
Sponsor Contributor

ghivert commented May 4, 2024

I suspect you don't even need to do that, the iodata representation might be supported by pgo.

Yes, but the only way to put a Json value without using some ugly coercion is by turning it in string, before using pgo.text, or am I missing something?

@lpil
Copy link
Owner

lpil commented May 4, 2024

That's how JSON works, I'm not sure why you think it's ugly.

@ghivert
Copy link
Sponsor Contributor

ghivert commented May 4, 2024

My bad, I was unclear. I meant, it could be cool to have a native function to like pgo.json: fn(Json) -> Value. This could ensures you're not putting a String where you want to put a Json (because not all strings are valid JSON). It's more about type-safety. It ensures you're putting a JSON in the DB, and the compiler could help you on that one, not waiting for the SQL transaction to reject saying "incorrect argument".

Pushing json as string is perfectly fine for me.

@lpil
Copy link
Owner

lpil commented May 4, 2024

Let's do that after the migration to OTP27 is done so we can drop the thoas dep in gleam_json.

@lpil lpil reopened this May 4, 2024
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

3 participants