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 JSON serialized Array columns #949

Merged
merged 2 commits into from
Jun 26, 2023
Merged

Adding JSON serialized Array columns #949

merged 2 commits into from
Jun 26, 2023

Conversation

jwoertink
Copy link
Member

Fixes #845

This PR allows you to convert your json arrays in to an array of serialized objects.. One thing to note about this PR is that it's only supporting '[]'::jsonb and not []::jsonb[]. The distinction here is that the column is still stored as jsonb, but it allows that json string to be parsed as an array VS using postgres's ARRAY column to store an array of jsonb objects.

I think we could eventually do that one too by just saying that the column isn't serialized, but that can be for a future PR.

With this, you can now do something like:

struct Server
  include JSON::Serializable
  property host : String
end

class Monitor < BaseModel
  table do
    column input_server : Server
    column output_servers : Array(Server)
  end
end

in_server = Server.from_json(%({"host":"x.y.z"}))
out_servers = Array(Server).from_json(%([{"host":"a.b.c"}, {"host":"d.e.f"}]))

SaveMonitor.create!(input_servers: in_server, output_servers: out_servers)

m = MonitorQuery.new.first
m.output_servers.first.host == "a.b.c"

@jwoertink jwoertink merged commit 401356e into main Jun 26, 2023
8 of 9 checks passed
@jwoertink jwoertink deleted the issues/845 branch June 26, 2023 15:04
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.

JSON serialize with Array does not work
2 participants