While it’s been quietly available for nearly a year now, I’d like to formally introduce JSONb Accessor to the world. It is similar in spirit to Hstore Accessor but has a couple key features which differentiate it. Overall it provides more utility but requires Postgres 9.4 as well as ActiveRecord 4.2. So if you’re stuck older versions, Hstore might still be the right choice.
Implementation 2.0
If you’ve ever written something twice, you’ll be familiar with how much better it is the second time around. There are no exceptions here. Hstore accessor started as 15 lines in a module I wrote to avoid the awkward syntax of HStore queries. It just happened to work with validations and from there people added support for types, scopes and tons of other features. It worked very well save for one major shortcoming, but it certainly wasn’t the way we would write it given the opportunity to start all over again.
Luckily we carried that knowledge over to Jsob Accessor. And by “we”, I’m referring mostly our Open Source Tsar Michael Crismali. Instead of doing type coersion ourselves, we’re making use of ActiveRecord’s attribute
method which eliminated several edge case behaviors that were inconsistent with what you’d expect from ActiveRecord. Dirty tracking also came along for the ride at no extra cost. With so much less code, our test suite is also easier to manage.
Nested Objects and Arrays
When we introduced types to hstore accessor we chose to support hashes. This proved to be incredibly problematic. It was never the goal of hstore_accessor
to extend Postgres functionality. We simply wanted to make to make Postgres features easier to use. Hstore does not support anything other than simple key-value pairs meant we had to make several assumptions on how collections or complex objects should behave. Ultimately we ended up removing support for objects which proved to be controversial. We didn’t want to provide a suboptimal solution which is actually a bad fit for HStore based on its current capabilities.
Luckly JSONb is like, so totally cool with this. We not only get nested objects, but deep nesting with typed attributes. In addition to nested objects there is support for typed arrays such as integer_array
or string_array
are fully supported by Postgres indexes and rock. Oh so hard! Postgres JSON support as a whole seems superior to HStore.
Additional Reading
See the read me for more details on jsonb accessor. It contains examples and descriptions of every feature available in the gem. I wrote a post many moons ago about how HStore and Single Table Inheritance can avoid the sparsely populated column issues that arise with traditional STI. Jsonb is as good if not better for this use case. Additionally, you should look into the raw SQL functions that you can use. Remember friends, ORMs don’t hurt people and Active Record affords dipping into the powers of the database better than most. You’ve even got btree, hash and GIN indexing at your disposal.
HStore Accessor Support Moving Forward
Because JSON has several advantages over HStore most of our efforts will be put towards jsonb_accssor. We recommend you migrate to it as soon as you can and look forward to feedback and pull requests.
Cheers!
DevMynd – software development companies in Chicago with practice areas in digital strategy, human-centered design, UI/UX, and web application and custom mobile development.