-
-
Notifications
You must be signed in to change notification settings - Fork 711
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
JSON functions #1436
JSON functions #1436
Conversation
bb2d296
to
308bb03
Compare
@myyra, this PR is based on the foundations you have grounded in #1423. Above all, thank you! I would be happy if you could review the user-facing, public, API of this PR, in order to check if it fits your needs. Files of interest, documentation-wise, are: New tests can look like some actual use cases, even if they sometimes are a little bit low-level: In our previous conversations, we had a few problems to solve: How to expose the SQLite JSON functions? My solution is to expose all of them as static // JSON('[1, 2, 3]')
Database.json("[1, 2, 3]") This way:
Which subset of JSON features should ship? All (but This also made it possible to write the JSON documentation tour, How to expose As all SQLite JSON functions,
I have favored
What is
struct Player: Codable {
var address: Address
enum Columns1 {
static let address = JSONColumn(CodingKeys.address)
}
enum Columns2 {
static let address = Column(CodingKeys.address)
}
enum Columns3: String, ColumnExpression {
case address
}
}
// 100% identical behavior
Player.Columns1.address
Player.Columns2.address.asJSONValue
Player.Columns3.address.asJSONValue
JSONColumn("address")
Column("address").asJSONValue In this PR,
|
308bb03
to
bf348cf
Compare
e635b10
to
de12337
Compare
This looks awesome and solves so many more use cases than I had planned! I have a busier than usual week, so it'll likely take until the weekend before I can test this in my app and review it properly. Feel free to merge this earlier if you feel like it. Meanwhile, I had a quick read-through and added some comments below.
What a neat solution you ended up with. I like how
🎉 I had started writing a doc about handling JSON with SQLite and GRDB, but it's a bit more practical and end-to-end, maybe I can expand the docs once I finish it.
After playing around with your earlier experimental branch, I realized that this is probably the best choice (vs.
This turned out nicely. The subscript syntax has really grown on me, it nicely indicates the sub-queryish nature of accessing JSON.
I think
Having a separate Overall, I really like the improvements on ensuring that values are proper JSON, and how that's indicatated in the APIs. I think I was missing the larger context earlier. |
Glad it seems to fit 👍
I'll be happy to hear your suggestions on the GRDB doc!
I'm relieved. Your opinion on this is very important to me, because you'll be an actual user of those features :-)
You're right.
I'm relieved, again :-) OK, I'll rename |
428cd71
to
ca7bbf1
Compare
Users must be able to store JSON data as text in the database.
ca7bbf1
to
d64de2b
Compare
The PR was just shipped in v6.19.0 :-) |
This pull request adds support for SQLite JSON Functions And Operators.
Added the full set of SQLite json functions
Database.json
,Database.jsonExtract
,Database.jsonArray
, ... match the SQL functionsjson
,json_extract
,json_array
, etc.The only notable omission is the support for
json_each
andjson_tree
. These are not supported, as GRDB has not yet learned to handle table-valued functions.The
JSONColumn
type and theSQLJSONExpressible
protocol grant convenience methods such as access to JSON subcomponentsUse JSON expressions when defining the database schema
Codable record that want to manipulate JSON data as Foundation
Data
can save it as database stringsSQLite can only manipulate JSON when it is stored as text at the database level, and that's why it is necessary to convert
Data
JSON columns into text in the database: