Skip to content

Latest commit

 

History

History
30 lines (22 loc) · 900 Bytes

advanced-pg-support.html.md

File metadata and controls

30 lines (22 loc) · 900 Bytes
chapter title
SQL
Advanced PostgreSQL support

JSON(B) data types

One of the nicest features of PostgreSQL nowadays, is support for semi-structured data using the JSON format which makes it possible to use this database in scenarios where you don't know the schema of the data beforehand.

Once you defined schema attributes as having the JSONB type, you can call the methods specific to this type on the attributes.

class Users < ROM::Relation[:sql]
  schema do
    attribute :id, Types::Serial
    attribute :properties, Types::PG::JSONB
  end
end

# .has_key will be translated to the '?' operator call
users_with_emails = users.where { properties.has_key('email') }

# equivalent to "properties" @> '{"name": "John"}'::jsonb
johns = users.where { properties.contain(name: 'John') }

Learn more