README.md in pgrel-0.1.3 vs README.md in pgrel-0.2.0

- old
+ new

@@ -4,33 +4,39 @@ ActiveRecord extension for querying hstore, array and jsonb. Compatible with **Rails** >= 4.2. -### General - -The functionality is based on ActiveRecord `WhereChain`. -To start querying call `where(:store_name)` and chain it with store-specific call (see below). - #### Install In your Gemfile: ```ruby -gem "pgrel", "~>0.1" +gem "pgrel", "~> 0.2" ``` ### HStore +#### Querying + +The functionality is based on ActiveRecord `WhereChain`. +To start querying call `where(:store_name)` and chain it with store-specific call (see below). + Query by key value: ```ruby Hstore.where.store(:tags, a: 1, b: 2) #=> select * from hstores where tags @> '"a"=>"1","b"=>"2"' Hstore.where.store(:tags, a: [1, 2]) #=> select * from hstores where (tags @> '"a"=>"1"' or tags @> '"a"=>"2"') + +Hstore.where.store(:tags, :a) +#=> select * from hstores where (tags @> '"a"=>NULL') + +Hstore.where.store(:tags, { a: 1 }, { b: 2 }) +#=> select * from hstores where (tags @> '"a"=>"1" or tags @> "b"=>"2"') ``` Keys existence: ```ruby @@ -45,22 +51,58 @@ # Retrieve items that have either key 'a' or 'b' in 'tags'::hstore Hstore.where.store(:tags).any('a', 'b') #=> select * from hstores where tags ?| array['a', 'b'] ``` +Values existence: + +```ruby +# Retrieve items that have value '1' OR '2' +Hstore.where.store(:tags).value(1, 2) +#=> select * from hstores where (avals(tags) @> ARRAY['1'] OR avals(tags) @> ARRAY['2'] ) + +# Retrieve items that have values '1' AND '2' +Hstore.where.store(:tags).values(1, 2) +#=> select * from hstores where (avals(tags) @> ARRAY['1', '2']) +``` + Containment: ```ruby Hstore.where.store(:tags).contains(a: 1, b: 2) #=> select * from hstores where tags @> '\"a\"=>\"1\", \"b\"=>\"2\"' Hstore.where.store(:tags).contained(a: 1, b: 2) #=> select * from hstores where tags <@ '\"a\"=>\"1\", \"b\"=>\"2\"' ``` +#### Update + +Is implemented through `ActiveRecord::Store::FlexibleHstore` and `ActiveRecord::Store::FlexibleJsonb` +objects. You can get them by sending `update_store(store_name)` to relation or class. + +Add key, value pairs: + +```ruby +Hstore.update_store(:tags).merge(new_key: 1, one_more: 2) +Hstore.update_store(:tags).merge([[:new_key, 1], [:one_more, 2]]) +``` + +Delete keys: + +```ruby +Hstore.update_store(:tags).delete_keys(:a, :b) +``` + +Delete key, value pairs: + +```ruby +Hstore.update_store(:tags).delete_pairs(a: 1, b: 2) +``` + ### JSONB -All queries for Hstore also available for JSONB. +All queries and updates for Hstore also available for JSONB. **NOTE**. Querying by array value always resolves to `(... or ...)` statement. Thus it's impossible to query json array value, e.g.: ```ruby