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

Add and expand the JSON examples #127

Merged
merged 3 commits into from
Jul 31, 2024
Merged

Conversation

daenney
Copy link
Contributor

@daenney daenney commented Jul 31, 2024

I've been toying a bit with the new JSONB format and was struggling a bit to find some complete working examples. So in this PR:

  • Updates the existing example for the database/sql driver to use JSONB and also show how to retrieve and unmarshal a JSON column in a Go struct.
  • Adds an example on how to do the same with the lowlevel package, but instead using BindJSON() on statements and ColumnJSON() on query.

daenney added 2 commits July 31, 2024 18:07
This changes the database/sql example for doing JSON to instead use a
BLOB column and store the data as JSONB. It also slightly expands the
example to show how to retrieve a column holding JSONB and marshal that
into a Go object.
This adds an example of how to use the low-level package to store and
retrieve JSONB, including binding the statements and decoding the
columns on query.
@ncruces
Copy link
Owner

ncruces commented Jul 31, 2024

This is probably a good idea. I actually tried to convince the SQLite folks to add something similar to their docs:
https://sqlite.org/forum/forumpost/d0cec8d2b722af67

I'm not sure if dropping the "pure" JSON examples is a good idea, or if we should have both…

Copy link
Owner

@ncruces ncruces left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then again, maybe twice the examples is just noise.
What do you think about the comments though?
On both examples, if you agree.

driver/json_test.go Outdated Show resolved Hide resolved
driver/json_test.go Show resolved Hide resolved
driver/json_test.go Outdated Show resolved Hide resolved
@daenney
Copy link
Contributor Author

daenney commented Jul 31, 2024

Then again, maybe twice the examples is just noise.

Mmm yeah, I was wondering about that. My thinking was mostly that JSONB should probably be what people (want to) use going forwards and it took me a second to realise I had to wrap them in jsonb() on insertion. It's a small difference but I thought as an example that might be nice to have.

What do you think about the comments though? On both examples, if you agree.

Sounds good, I'll make the changes.

Addresses the review comments by adding the necessary comments to
explain why we're using the jsonb() and json() functions.
@ncruces ncruces merged commit 6c12a8c into ncruces:main Jul 31, 2024
9 checks passed
@daenney daenney deleted the json-examples branch August 1, 2024 08:16
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

Successfully merging this pull request may close these issues.

2 participants