Adds typed jsonb backed fields as first class citizens to your ActiveRecord models. This gem is similar in spirit to HstoreAccessor, but the jsonb column in PostgreSQL has a few distinct advantages, mostly around nested documents and support for collections.
This gem is under heavy development. Please use cautiously and help us with feedback by opening issues for defects and feature requests. The current API is subject to change.
Add this line to your application's Gemfile:
gem "jsonb_accessor"And then execute:
$ bundle install
First we must create a model which has a jsonb column available to store data into it:
class CreateProductsTable < ActiveRecord::Migration
def change
create_table :products do |t|
t.jsonb :options
end
end
endWe can then declare the jsonb fields we wish to expose via the accessor:
class Product < ActiveRecord::Base
jsonb_accessor(
:options,
:count, # => value type
title: :string,
id_value: :value,
external_id: :integer,
reviewed_at: :date_time
)
endJSONb Accessor accepts both untyped and typed key definitions. Untyped keys are treated as-is and no additional casting is performed. This allows the freedom of dynamic values alongside the power types, which is especially convenient when saving nested form attributes. Typed keys will be cast to their respective values using the same mechanism ActiveRecord uses to coerce standard attribute columns. It's as close to a real column as you can get and the goal is to keep it that way.
All untyped keys must be defined prior to typed columns. You can declare a typed column with type value for explicit dynamic behavior. For reference, the jsonb_accessor macro is defined thusly.
def jsonb_accessor(jsonb_attribute, *value_fields, **typed_fields)
...
endThere's quite a bit more to do do and document but we're excited to get this out there while we work on it some more.
class Product < ActiveRecord::Base
jsonb_accessor :data, field: :string
endfieldfield=field?field_changed?field_wasfield_changereset_field!restore_field!field_will_change!
The following types are supported:
- big_integer
- binary
- bit
- bit_varying
- boolean
- bytea
- cidr
- date
- date_time
- decimal
- decimal_without_scale
- enum
- float
- hstore
- inet
- integer
- json
- jsonb
- money
- point
- range
- specialized_string
- string
- text
- time
- unsigned_integer
- uuid
- value
- vector
- xml
Typed arrays are also supported by specifying :type_array (i.e. :float_array). :array is interpreted as an array of value types.
Support for nested types is also available but experimental at this point. If you must, you may try something like this for nested objects.
class Product < ActiveRecord::Base
jsonb_accessor(
:options,
nested_object: { key: :integer }
)
end
p = Product.new
p.nested_object.key = "10"
puts p.nested_object.key #=> 10Because this gem promotes attributes nested into the JSON column to first level attributes, most validations should just work. We still have to add some testing and support around this feature but feel free to try and leave us feedback if they're not working as expected.
You can use it for STI in the same spirit as hstore_accessor, which is documented here..
Coming soon...
Coming soon...
jsonb supports GIN, GIST, btree and hash indexes over json column. We have plans to add migrations helpers for generating these indexes for you.
- ActiveRecord 4.2
- Postgres 9.4 (in order to use the jsonb column type).
After checking out the repo, run bin/setup to install dependencies (make sure postgres is running first).
Run bin/console for an interactive prompt that will allow you to experiment.
rake will run Rubocop and the specs.
- Fork it
- Create your feature branch (
git checkout -b my-new-feature) - Add tests and changes (run the tests with
rake) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create a new Pull Request