rasti-db 2.3.0 → 2.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/rasti/db/type_converters/postgres.rb +32 -36
- data/lib/rasti/db/type_converters/postgres_types/array.rb +11 -9
- data/lib/rasti/db/type_converters/postgres_types/hstore.rb +10 -9
- data/lib/rasti/db/type_converters/postgres_types/json.rb +17 -14
- data/lib/rasti/db/type_converters/postgres_types/jsonb.rb +17 -14
- data/lib/rasti/db/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cbeaaf85ac611e3a8a014720fca0ce2328c240e73b05cae04c4f9c9a3be6b7f6
|
4
|
+
data.tar.gz: 120f2ad079c6ea185bf95d4e74eec3a4d85bbb5bfb6941e8b8f923d5653c5e5f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a9479412975696063c5bf0d62e2f5a41d2f71afe98adb688356cd468a9dc6da04c9b02a5fcc434c8e3964d06bc1f96bfb6206fa6a8032e23587b1d68be9372b6
|
7
|
+
data.tar.gz: 5608471902a30b61971f03ad95e7b6d6d9fd62b8bf350d4bea55f840518e18f32201a9598f533882ee5a0c19d0634824d714fb3361f2f7fbdb976e4125819f46
|
@@ -3,62 +3,58 @@ module Rasti
|
|
3
3
|
module TypeConverters
|
4
4
|
class Postgres
|
5
5
|
|
6
|
-
CONVERTERS = [
|
7
|
-
|
8
|
-
|
6
|
+
CONVERTERS = [
|
7
|
+
PostgresTypes::JSON,
|
8
|
+
PostgresTypes::JSONB,
|
9
|
+
PostgresTypes::HStore,
|
10
|
+
PostgresTypes::Array
|
11
|
+
]
|
9
12
|
|
10
13
|
class << self
|
11
14
|
|
12
15
|
def to_db(db, collection_name, attribute_name, value)
|
13
|
-
|
14
|
-
|
15
|
-
if to_db_mapping.key? attribute_name
|
16
|
-
to_db_mapping[attribute_name][:converter].to_db value, to_db_mapping[attribute_name][:sub_type]
|
17
|
-
else
|
18
|
-
value
|
19
|
-
end
|
16
|
+
converter, type = find_to_db_converter_and_type db, collection_name, attribute_name
|
17
|
+
converter ? converter.to_db(value, type) : value
|
20
18
|
end
|
21
19
|
|
22
|
-
def from_db(
|
23
|
-
|
24
|
-
|
25
|
-
else
|
26
|
-
object
|
27
|
-
end
|
20
|
+
def from_db(value)
|
21
|
+
converter = find_from_db_converter value.class
|
22
|
+
converter ? converter.from_db(value) : value
|
28
23
|
end
|
29
24
|
|
30
25
|
private
|
31
26
|
|
32
|
-
def
|
33
|
-
|
34
|
-
|
35
|
-
@to_db_mapping[key] ||= begin
|
36
|
-
columns = Hash[db.schema(collection_name)]
|
27
|
+
def from_db_converters
|
28
|
+
@from_db_converters ||= {}
|
29
|
+
end
|
37
30
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
31
|
+
def to_db_converters
|
32
|
+
@to_db_converters ||= {}
|
33
|
+
end
|
34
|
+
|
35
|
+
def find_to_db_converter_and_type(db, collection_name, attribute_name)
|
36
|
+
key = [db.opts[:database], collection_name].join('.')
|
42
37
|
|
43
|
-
|
44
|
-
|
45
|
-
|
38
|
+
to_db_converters[key] ||= begin
|
39
|
+
columns = Hash[db.schema(collection_name)]
|
40
|
+
to_db_converters[key] = columns.each_with_object({}) do |(name, schema), hash|
|
41
|
+
converter = CONVERTERS.detect { |c| c.to_db? schema[:db_type] }
|
42
|
+
hash[name] = [converter, schema[:db_type]] if converter
|
46
43
|
end
|
47
44
|
end
|
45
|
+
|
46
|
+
to_db_converters[key].fetch(attribute_name, [])
|
48
47
|
end
|
49
48
|
|
50
|
-
def
|
51
|
-
|
52
|
-
CONVERTERS.
|
53
|
-
converter.db_classes.each do |db_class|
|
54
|
-
result[db_class] = converter
|
55
|
-
end
|
56
|
-
end
|
49
|
+
def find_from_db_converter(klass)
|
50
|
+
if !from_db_converters.key?(klass)
|
51
|
+
from_db_converters[klass] = CONVERTERS.detect { |c| c.from_db? klass }
|
57
52
|
end
|
53
|
+
|
54
|
+
from_db_converters[klass]
|
58
55
|
end
|
59
56
|
|
60
57
|
end
|
61
|
-
|
62
58
|
end
|
63
59
|
end
|
64
60
|
end
|
@@ -3,28 +3,30 @@ module Rasti
|
|
3
3
|
module TypeConverters
|
4
4
|
module PostgresTypes
|
5
5
|
class Array
|
6
|
-
|
7
6
|
class << self
|
8
7
|
|
9
|
-
|
10
|
-
|
8
|
+
DB_TYPE_REGEX = /^([a-z]+)\[\]$/
|
9
|
+
|
10
|
+
def to_db?(type)
|
11
|
+
!type.match(DB_TYPE_REGEX).nil?
|
11
12
|
end
|
12
13
|
|
13
|
-
def to_db(value,
|
14
|
+
def to_db(value, type)
|
15
|
+
sub_type = type[0..-3]
|
14
16
|
array = sub_type == 'hstore' ? value.map { |v| Sequel.hstore v } : value
|
15
17
|
Sequel.pg_array array, sub_type
|
16
18
|
end
|
17
19
|
|
18
|
-
def
|
19
|
-
|
20
|
+
def from_db?(klass)
|
21
|
+
defined?(Sequel::Postgres::PGArray) &&
|
22
|
+
klass == Sequel::Postgres::PGArray
|
20
23
|
end
|
21
24
|
|
22
|
-
def from_db(
|
23
|
-
|
25
|
+
def from_db(value)
|
26
|
+
value.to_a
|
24
27
|
end
|
25
28
|
|
26
29
|
end
|
27
|
-
|
28
30
|
end
|
29
31
|
end
|
30
32
|
end
|
@@ -3,27 +3,28 @@ module Rasti
|
|
3
3
|
module TypeConverters
|
4
4
|
module PostgresTypes
|
5
5
|
class HStore
|
6
|
-
|
7
6
|
class << self
|
8
7
|
|
9
|
-
|
10
|
-
|
8
|
+
DB_TYPE_REGEX = /^hstore$/
|
9
|
+
|
10
|
+
def to_db?(type)
|
11
|
+
!type.match(DB_TYPE_REGEX).nil?
|
11
12
|
end
|
12
13
|
|
13
|
-
def to_db(value,
|
14
|
+
def to_db(value, type)
|
14
15
|
Sequel.hstore value
|
15
16
|
end
|
16
17
|
|
17
|
-
def
|
18
|
-
|
18
|
+
def from_db?(klass)
|
19
|
+
defined?(Sequel::Postgres::HStore) &&
|
20
|
+
klass == Sequel::Postgres::HStore
|
19
21
|
end
|
20
22
|
|
21
|
-
def from_db(
|
22
|
-
|
23
|
+
def from_db(value)
|
24
|
+
value.to_h
|
23
25
|
end
|
24
26
|
|
25
27
|
end
|
26
|
-
|
27
28
|
end
|
28
29
|
end
|
29
30
|
end
|
@@ -3,36 +3,39 @@ module Rasti
|
|
3
3
|
module TypeConverters
|
4
4
|
module PostgresTypes
|
5
5
|
class JSON
|
6
|
-
|
7
6
|
class << self
|
8
7
|
|
9
|
-
|
10
|
-
|
8
|
+
DB_TYPE_REGEX = /^json$/
|
9
|
+
|
10
|
+
def to_db?(type)
|
11
|
+
!type.match(DB_TYPE_REGEX).nil?
|
11
12
|
end
|
12
13
|
|
13
|
-
def to_db(value,
|
14
|
+
def to_db(value, type)
|
14
15
|
Sequel.pg_json value
|
15
16
|
end
|
16
17
|
|
17
|
-
def
|
18
|
-
|
18
|
+
def from_db?(klass)
|
19
|
+
to_hash?(klass) || to_array?(klass)
|
19
20
|
end
|
20
21
|
|
21
|
-
def from_db(
|
22
|
-
|
22
|
+
def from_db(value)
|
23
|
+
to_hash?(value.class) ? value.to_h : value.to_a
|
23
24
|
end
|
24
25
|
|
25
26
|
private
|
26
27
|
|
27
|
-
def
|
28
|
-
|
29
|
-
|
30
|
-
Sequel::Postgres::JSONArray => :to_a
|
31
|
-
}
|
28
|
+
def to_hash?(klass)
|
29
|
+
defined?(Sequel::Postgres::JSONHash) &&
|
30
|
+
klass == Sequel::Postgres::JSONHash
|
32
31
|
end
|
33
32
|
|
34
|
-
|
33
|
+
def to_array?(klass)
|
34
|
+
defined?(Sequel::Postgres::JSONArray) &&
|
35
|
+
klass == Sequel::Postgres::JSONArray
|
36
|
+
end
|
35
37
|
|
38
|
+
end
|
36
39
|
end
|
37
40
|
end
|
38
41
|
end
|
@@ -3,36 +3,39 @@ module Rasti
|
|
3
3
|
module TypeConverters
|
4
4
|
module PostgresTypes
|
5
5
|
class JSONB
|
6
|
-
|
7
6
|
class << self
|
8
7
|
|
9
|
-
|
10
|
-
|
8
|
+
DB_TYPE_REGEX = /^jsonb$/
|
9
|
+
|
10
|
+
def to_db?(type)
|
11
|
+
!type.match(DB_TYPE_REGEX).nil?
|
11
12
|
end
|
12
13
|
|
13
|
-
def to_db(value,
|
14
|
+
def to_db(value, type)
|
14
15
|
Sequel.pg_jsonb value
|
15
16
|
end
|
16
17
|
|
17
|
-
def
|
18
|
-
|
18
|
+
def from_db?(klass)
|
19
|
+
to_hash?(klass) || to_array?(klass)
|
19
20
|
end
|
20
21
|
|
21
|
-
def from_db(
|
22
|
-
|
22
|
+
def from_db(value)
|
23
|
+
to_hash?(value.class) ? value.to_h : value.to_a
|
23
24
|
end
|
24
25
|
|
25
26
|
private
|
26
27
|
|
27
|
-
def
|
28
|
-
|
29
|
-
|
30
|
-
Sequel::Postgres::JSONBArray => :to_a
|
31
|
-
}
|
28
|
+
def to_hash?(klass)
|
29
|
+
defined?(Sequel::Postgres::JSONBHash) &&
|
30
|
+
klass == Sequel::Postgres::JSONBHash
|
32
31
|
end
|
33
32
|
|
34
|
-
|
33
|
+
def to_array?(klass)
|
34
|
+
defined?(Sequel::Postgres::JSONBArray) &&
|
35
|
+
klass == Sequel::Postgres::JSONBArray
|
36
|
+
end
|
35
37
|
|
38
|
+
end
|
36
39
|
end
|
37
40
|
end
|
38
41
|
end
|
data/lib/rasti/db/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rasti-db
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.3.
|
4
|
+
version: 2.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gabriel Naiman
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-02-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: sequel
|