sqlite_json_ops.rb

lib/sequel/extensions/sqlite_json_ops.rb
Last Update: 2022-04-15 14:39:58 -0700

The sqlite_json_ops extension adds support to Sequel’s DSL to make it easier to call SQLite JSON functions and operators (added first in SQLite 3.38.0).

To load the extension:

Sequel.extension :sqlite_json_ops

This extension works by calling methods on Sequel::SQLite::JSONOp objects, which you can create via Sequel.sqlite_json_op:

j = Sequel.sqlite_json_op(:json_column)

Also, on most Sequel expression objects, you can call the sqlite_json_op method to create a Sequel::SQLite::JSONOp object:

j = Sequel[:json_column].sqlite_json_op

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#sqlite_json_op:

j = :json_column.sqlite_json_op

The following methods are available for Sequel::SQLite::JSONOp instances:

j[1]                     # (json_column ->> 1)
j.get(1)                 # (json_column ->> 1)
j.get_text(1)            # (json_column -> 1)
j.extract('$.a')         # json_extract(json_column, '$.a')

j.array_length           # json_array_length(json_column)
j.type                   # json_type(json_column)
j.valid                  # json_valid(json_column)
j.json                   # json(json_column)

j.insert('$.a', 1)       # json_insert(json_column, '$.a', 1)
j.set('$.a', 1)          # json_set(json_column, '$.a', 1)
j.replace('$.a', 1)      # json_replace(json_column, '$.a', 1)
j.remove('$.a')          # json_remove(json_column, '$.a')
j.patch('{"a":2}')       # json_patch(json_column, '{"a":2}')

j.each                   # json_each(json_column)
j.tree                   # json_tree(json_column)

Related modules: Sequel::SQLite::JSONOp