hadouken-json 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/hadouken/json.rb +2 -1
- data/lib/hadouken/sql_builder.rb +16 -4
- 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: 5134ad4ac55b205d681920316af6eecf2c2daa787ed32fd6638ed67cafbc3790
|
4
|
+
data.tar.gz: e61fedaa590de97485d3db9d94a56ac734570734b370c5dd2ed17bae353bc8f3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1b14b24f94168ec7f5b30aa6eec22d6253288f9a28dbea7657bcc3fc2d5faffbd4700d694234cd1eab5ce5ff7b42daa6abd874b4d7fc44e0f8ea6eb99bd88f08
|
7
|
+
data.tar.gz: fe6f9cd046066da882f19a39cd3edc64fab4c58ff724d60c56c4b57086ce73883b254e539255eea8acb575040d034adb969f29826650d00fb131990421752e6f
|
data/lib/hadouken/json.rb
CHANGED
@@ -21,7 +21,8 @@ class Hadouken::Json
|
|
21
21
|
main_class: (relation_for(options[:jsonb_field_from]) || relation).klass,
|
22
22
|
scope: options[:for],
|
23
23
|
schema: json_schema,
|
24
|
-
decorator: (options[:decorate_with].is_a?(Hadouken::Decorator) ? options[:decorate_with] : nil)
|
24
|
+
decorator: (options[:decorate_with].is_a?(Hadouken::Decorator) ? options[:decorate_with] : nil),
|
25
|
+
where_condition: options[:where]
|
25
26
|
)
|
26
27
|
end
|
27
28
|
|
data/lib/hadouken/sql_builder.rb
CHANGED
@@ -5,6 +5,7 @@ class Hadouken::SqlBuilder
|
|
5
5
|
attribute :scope
|
6
6
|
attribute :schema, Hash, default: {}
|
7
7
|
attribute :decorator, Hadouken::Decorator
|
8
|
+
attribute :where_condition, Hash
|
8
9
|
|
9
10
|
def self.call(*args)
|
10
11
|
new(*args).call
|
@@ -16,6 +17,7 @@ class Hadouken::SqlBuilder
|
|
16
17
|
@sql = ''
|
17
18
|
@relation = build_relation
|
18
19
|
apply_decorator if decorator&.valid?
|
20
|
+
apply_where_conditions
|
19
21
|
@sql << @relation.select(*columns_to_select).to_sql.gsub(sample_id.to_s, primary_key)
|
20
22
|
|
21
23
|
"SELECT COALESCE(json_agg(a), '[]'::JSON ) FROM (#{@sql}) a"
|
@@ -43,6 +45,13 @@ class Hadouken::SqlBuilder
|
|
43
45
|
EOQ
|
44
46
|
end
|
45
47
|
|
48
|
+
def apply_where_conditions
|
49
|
+
return unless where_condition.is_a?(Hash) && where_condition.any?
|
50
|
+
|
51
|
+
where_condition.deep_transform_keys! { |k| [unwound_jsonb_table_name,k.to_s].join('.') } if scope_is_jsonb_array?
|
52
|
+
@relation.where!(where_condition)
|
53
|
+
end
|
54
|
+
|
46
55
|
def apply_decorator
|
47
56
|
@sql << decorator.data_table_sql
|
48
57
|
@relation.joins!(decorator.join_sql)
|
@@ -59,7 +68,7 @@ class Hadouken::SqlBuilder
|
|
59
68
|
schema.extract!(*schema.select{ |_,v| v.is_a?(String) }.keys)
|
60
69
|
.inject({}) do |h, (field, column)|
|
61
70
|
col = (@relation&.klass&.column_names||[]).include?(column) ? [@relation.klass.table_name, column].join('.') : column
|
62
|
-
scope_is_jsonb_array? ? h.merge(field => "#{unwound_jsonb_table_name}.#{column
|
71
|
+
scope_is_jsonb_array? ? h.merge(field => "#{unwound_jsonb_table_name}.#{column}") : h.merge(field => col)
|
63
72
|
end
|
64
73
|
end
|
65
74
|
|
@@ -116,12 +125,15 @@ class Hadouken::SqlBuilder
|
|
116
125
|
end
|
117
126
|
|
118
127
|
def unwound_jsonb_table_name
|
119
|
-
|
128
|
+
['records_from', scope].join('_')
|
129
|
+
end
|
130
|
+
|
131
|
+
def jsonb_type
|
132
|
+
[main_class.table_name, scope].join('_')
|
120
133
|
end
|
121
134
|
|
122
135
|
def unwound_jsonb_table
|
123
|
-
|
124
|
-
"jsonb_to_recordset(#{main_class.table_name}.#{scope}) AS #{unwound_jsonb_table_name}(#{table_structure})"
|
136
|
+
"jsonb_populate_recordset(null::#{jsonb_type}, #{main_class.table_name}.#{scope}) AS #{unwound_jsonb_table_name}"
|
125
137
|
end
|
126
138
|
|
127
139
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hadouken-json
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Roman Zaytsev
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-09-
|
11
|
+
date: 2021-09-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: virtus
|