Last Modified
  2013-07-18 19:10:40 +0000
Requires

## Description

The pg_json_ops extension adds support to Sequel's DSL to make it easier to call PostgreSQL JSON functions and operators (added first in PostgreSQL 9.3).

To load the extension:

    Sequel.extension :pg_json_ops

The most common usage is passing an expression to Sequel.pg_json_op:

    j = Sequel.pg_json_op(:json_column)

If you have also loaded the pg_json extension, you can use Sequel.pg_json as well:

    j = Sequel.pg_json(:json_column)

Also, on most Sequel expression objects, you can call the pg_json method:

    j = Sequel.expr(:json_column).pg_json

If you have loaded the core_extensions extension), or you have loaded the core_refinements extension) and have activated refinements for the file, you can also use Symbol#pg_json:

    j = :json_column.pg_json

This creates a Sequel::Postgres::JSONOp object that can be used for easier querying:

    j[1]                     # (json_column -> 1)
    j[%w'a b']               # (json_column #> ARRAY['a','b'])
    j.get_text(1)            # (json_column ->> 1)
    j.get_text(%w'a b')      # (json_column #>> ARRAY['a','b'])
    j.extract('a', 'b')      # json_extract_path(json_column, 'a', 'b')
    j.extract_text('a', 'b') # json_extract_path_text(json_column, 'a', 'b')

    j.array_length           # json_array_length(json_column)
    j.array_elements         # json_array_elements(json_column)
    j.each                   # json_each(json_column)
    j.each_text              # json_each_text(json_column)
    j.keys                   # json_object_keys(json_column)

    j.populate(:a)           # json_populate_record(:a, json_column)
    j.populate_set(:a)       # json_populate_recordset(:a, json_column)

If you are also using the pg_json extension, you should load it before loading this extension. Doing so will allow you to use JSONHash#op and JSONArray#op to get a JSONOp, allowing you to perform json operations on json literals.