pgrel 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -1
- data/.rubocop.yml +0 -4
- data/README.md +108 -2
- data/lib/pgrel/active_record/store_chain/hstore_chain.rb +1 -1
- data/lib/pgrel/version.rb +2 -2
- data/spec/pgrel/hstore_spec.rb +7 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9b99f0c11a5e624145d178ed3bdb97dd9345ccad
|
4
|
+
data.tar.gz: 7db4f0a1086111e10ded6e6f9af923179ddc2012
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4ef4b9230787c49c3ccb1b24d68113cf90b57c0ae8281e6814eb6d01c01925fc04031ee3f02e1cc09abf60386b2d3c12f6b2f46674f6b75ccc27cd34d3a08619
|
7
|
+
data.tar.gz: 8aaeaab12dd78aac0ad21a49b68af1a5efb83aeb261e0cfd431f24662b5f8b0dc88dcc4b93c8115b3a0d4999ddf3ca842c32d75f92270ebab7113eac31b71acf
|
data/.gitignore
CHANGED
data/.rubocop.yml
CHANGED
data/README.md
CHANGED
@@ -2,6 +2,112 @@
|
|
2
2
|
|
3
3
|
## Pgrel
|
4
4
|
|
5
|
-
|
5
|
+
ActiveRecord extension for querying hstore, array and jsonb.
|
6
|
+
|
7
|
+
Compatible with **Rails** >= 4.0 (and even with upcoming **Rails 5**!).
|
8
|
+
|
9
|
+
### General
|
10
|
+
|
11
|
+
The functionality is based on ActiveRecord `WhereChain`.
|
12
|
+
To start querying call `where(:store_name)` and chain it with store-specific call (see below).
|
13
|
+
|
14
|
+
#### Install
|
15
|
+
|
16
|
+
In your Gemfile:
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
gem "pgrel", "~>0.1"
|
20
|
+
```
|
21
|
+
|
22
|
+
### HStore
|
23
|
+
|
24
|
+
Query by key value:
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
Hstore.where.store(:tags, a: 1, b: 2)
|
28
|
+
#=> select * from hstores where tags->'a' = '1' and tags->'b' = '2'
|
29
|
+
|
30
|
+
Hstore.where.store(:tags, a: [1, 2])
|
31
|
+
#=> select * from hstores where tags->'a' in ('1', '2')
|
32
|
+
```
|
33
|
+
|
34
|
+
Keys existence:
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
# Retrieve items that have key 'a' in 'tags'::hstore
|
38
|
+
Hstore.where.store(:tags).key(:a)
|
39
|
+
#=> select * from hstores where tags ? 'a'
|
40
|
+
|
41
|
+
# Retrieve items that have both keys 'a' and 'b' in 'tags'::hstore
|
42
|
+
Hstore.where.store(:tags).keys('a', 'b')
|
43
|
+
#=> select * from hstores where tags ?& array['a', 'b']
|
44
|
+
|
45
|
+
# Retrieve items that have either key 'a' or 'b' in 'tags'::hstore
|
46
|
+
Hstore.where.store(:tags).any('a', 'b')
|
47
|
+
#=> select * from hstores where tags ?| array['a', 'b']
|
48
|
+
```
|
49
|
+
|
50
|
+
Containment:
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
Hstore.where.store(:tags).contains(a: 1, b: 2)
|
54
|
+
#=> select * from hstores where tags @> '\"a\"=>\"1\", \"b\"=>\"2\"'
|
55
|
+
|
56
|
+
Hstore.where.store(:tags).contained(a: 1, b: 2)
|
57
|
+
#=> select * from hstores where tags <@ '\"a\"=>\"1\", \"b\"=>\"2\"'
|
58
|
+
```
|
59
|
+
|
60
|
+
### JSONB
|
61
|
+
|
62
|
+
All queries for Hstore also available for JSONB.
|
63
|
+
|
64
|
+
**NOTE**. Querying by array value always resolves to `IN (...)` statement.
|
65
|
+
Thus it's impossible to query json array value, e.g.:
|
66
|
+
|
67
|
+
```ruby
|
68
|
+
Model.create!(tags: {main: ['a', 'b']})
|
69
|
+
|
70
|
+
Model.where.store(:tags, main: ['a', 'b']).empty? == true
|
71
|
+
#=> select * from models where tags->'main' in ('a', 'b')
|
72
|
+
```
|
73
|
+
|
74
|
+
Path query:
|
75
|
+
|
76
|
+
```ruby
|
77
|
+
Model.create!(tags: {main: ['a', 'b'], user: { name: 'john' } })
|
78
|
+
|
79
|
+
# You can use object to query by simple value
|
80
|
+
Model.where.store(:tags).path(user: { name: 'john' })
|
81
|
+
#=> select * from hstores where tags#>>'{\"user\",\"name\"}' = 'john'
|
82
|
+
# or passing path parts as args one by one with value at the end
|
83
|
+
Model.where.store(:tags).path(:user, :name, 'john')
|
84
|
+
|
85
|
+
# Match by complex value (array or object)
|
86
|
+
Model.where.store(:tags).path(:main, ['a', 'b'])
|
87
|
+
#=> select * from hstores where tags#>'{\"main\"}' = '[\"a\",\"b\"]'
|
88
|
+
```
|
89
|
+
|
90
|
+
### Array
|
91
|
+
|
92
|
+
Array stores support containment queries (just like Hstore and JSONB) and also `overlap` operator.
|
93
|
+
|
94
|
+
**NOTE**. There are some other array operators ('ANY', 'ALL', querying by index - value) which I'm not going to implement – PRs are welcomed!
|
95
|
+
|
96
|
+
Overlap:
|
97
|
+
```ruby
|
98
|
+
Model.where.store(:tags).overlap('a', 'b')
|
99
|
+
#=> select * from hstores where tags && '{\"a\",\"b\"}'
|
100
|
+
```
|
101
|
+
|
102
|
+
### Negation
|
103
|
+
|
104
|
+
Use `not` before operator to constuct negation or pass arguments to `not` to run key-value query.
|
105
|
+
|
106
|
+
```ruby
|
107
|
+
Model.where.store(:tags).not.overlap('a', 'b')
|
108
|
+
#=> select * from hstores where not (tags && '{\"a\",\"b\"}')
|
109
|
+
|
110
|
+
Hstore.where.store(:tags).not(a: 1)
|
111
|
+
#=> select * from hstores where tags->'a' != '1'
|
112
|
+
```
|
6
113
|
|
7
|
-
ActiveRecord extension for querying hstore and jsonb.
|
data/lib/pgrel/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
module Pgrel
|
2
|
-
VERSION = "0.1.
|
1
|
+
module Pgrel # :nodoc:
|
2
|
+
VERSION = "0.1.1"
|
3
3
|
end
|
data/spec/pgrel/hstore_spec.rb
CHANGED
@@ -24,6 +24,7 @@ describe Hstore do
|
|
24
24
|
Hstore.create!(name: 'c', tags: { f: true, d: 'b' })
|
25
25
|
Hstore.create!(name: 'd', tags: { f: false })
|
26
26
|
Hstore.create!(name: 'e', tags: { a: 2, c: 'x', d: 'c', g: 'c' })
|
27
|
+
Hstore.create!(tags: { 1 => 2 })
|
27
28
|
end
|
28
29
|
|
29
30
|
context '#where' do
|
@@ -33,6 +34,10 @@ describe Hstore do
|
|
33
34
|
expect(Hstore.where.store(:tags, f: false).first.name).to eq 'd'
|
34
35
|
end
|
35
36
|
|
37
|
+
it 'integer keys' do
|
38
|
+
expect(Hstore.where.store(:tags, 1 => 2).size).to eq 1
|
39
|
+
end
|
40
|
+
|
36
41
|
it 'arrays' do
|
37
42
|
expect(Hstore.where.store(:tags, a: [1, 2, 3]).size).to eq 3
|
38
43
|
end
|
@@ -115,11 +120,11 @@ describe Hstore do
|
|
115
120
|
end
|
116
121
|
|
117
122
|
it '#any' do
|
118
|
-
expect(Hstore.where.store(:tags).not.any('a', 'f').size).to eq
|
123
|
+
expect(Hstore.where.store(:tags).not.any('a', 'f').size).to eq 1
|
119
124
|
end
|
120
125
|
|
121
126
|
it '#keys' do
|
122
|
-
expect(Hstore.where.store(:tags).not.keys('a', 'f').size).to eq
|
127
|
+
expect(Hstore.where.store(:tags).not.keys('a', 'f').size).to eq 5
|
123
128
|
end
|
124
129
|
end
|
125
130
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pgrel
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- palkan
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-04-
|
11
|
+
date: 2015-04-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|