pggraphql 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 310b215ed3294f252ba1a2c446438e1ef2eb36b6
4
- data.tar.gz: f9e4f9aa456475b48f65053cff6859018113d8a2
3
+ metadata.gz: fdf0b9b66504b4787e8ec3e6928426228b531d61
4
+ data.tar.gz: c1435f3aaa8d96b2e8d420494d378072a6cded71
5
5
  SHA512:
6
- metadata.gz: 9ea6027265481e57382bddd85d8cc1c737e8da6e31c9696802bc726ede7cd46e243dbea37b41087e9410cec981de57e32a6c8fdcae1b7aa1f9cca93cd5d7ca29
7
- data.tar.gz: 52987c14f1c1427d0a242dbc3826c15815d387fa76617a566aedf161acdf2e40753df529b07590dd60b7edaf3869c6e66f14a819cb24c9448e28954019cc70e1
6
+ metadata.gz: 3d3b0514e72ca3c1dbdc2f92902c1b67537192c0970d82e950b8137b7a115eb2dc03ae13b4159041a890a028849d9df303bd9b7128658ab6b4a61c789ba934e2
7
+ data.tar.gz: 7f9dc9e43010a5cf019ba8567077b2751e8de9d22dbe16beb4e21afd0f547cf3df8a881a9b9caff547a9ca651e8078abf42811804331ec9527f43b81fc929a8f
@@ -1,3 +1,3 @@
1
1
  module Pggraphql
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
data/lib/pggraphql.rb CHANGED
@@ -30,15 +30,22 @@ module PgGraphQl
30
30
  type = link ? link.type : self.types[e[0].to_s.singularize.to_sym]
31
31
  ids = e[1][:id]
32
32
 
33
+ # puts "#{e[0].inspect}, #{link_name.inspect} ... type: #{type.inspect}"
34
+
33
35
  raise "missing :fk on link #{link.name.inspect}" if link && !link.fk
34
36
 
35
37
  columns = e[1].map do |f|
36
- column_name = f[0]
38
+ nested_link_name = f[0]
39
+ field_name = f[0]
40
+
41
+ raise "unknown field #{field_name.inspect} on type #{type.name.inspect}" if !f[1].is_a?(Hash) && !type.fields.include?(field_name)
42
+ raise "unknown link #{field_name.inspect} on type #{type.name.inspect}" if f[1].is_a?(Hash) && !type.links.include?(field_name)
37
43
 
38
- raise "unknown field #{column_name.inspect} on type #{type.name.inspect}" if !f[1].is_a?(Hash) && !type.fields.include?(column_name)
39
- raise "unknown link #{column_name.inspect} on type #{type.name.inspect}" if f[1].is_a?(Hash) && !type.links.include?(column_name)
44
+ column_name = field_name.to_s.index("__") ? field_name.to_s.gsub(/__/, ".").to_sym : field_name
40
45
 
41
- (f[1].is_a?(Hash) ? "(" + to_sql([f].to_h, level + 1, type, column_name) + ") as #{column_name}" : column_name.to_s)
46
+ column_expr = type.mappings[field_name] || column_name
47
+
48
+ (f[1].is_a?(Hash) ? "(" + to_sql([f].to_h, level + 1, type, nested_link_name) + ") as #{field_name}" : ((column_name == field_name && column_name == column_expr) ? column_name.to_s : "#{column_expr} as #{field_name}"))
42
49
  end.join(",")
43
50
 
44
51
  is_many = (link && link.many?) || !ids || ids.is_a?(Array)
@@ -66,6 +73,16 @@ module PgGraphQl
66
73
  sql += "x.*"
67
74
  sql += "), '[]'::json)" if is_many
68
75
  sql += ") from (select #{columns} from #{type.table}"
76
+
77
+
78
+ unless type.subtypes.empty?
79
+ sql += "\n" + type.subtypes.map do |f|
80
+ subtype = f[1]
81
+ "left join #{subtype.table} as #{subtype.name} on (#{subtype.fk})"
82
+ end.join("\n")
83
+ end
84
+
85
+
69
86
  sql += " where #{wheres.join(' and ')}" unless wheres.empty?
70
87
  sql += " order by #{order_by}" if order_by
71
88
  sql += " limit 1" if !is_many
@@ -85,8 +102,8 @@ module PgGraphQl
85
102
  end
86
103
 
87
104
  class Type
88
- attr_accessor :name, :table, :filter, :links, :order_by, :fields
89
- attr_reader :schema
105
+ attr_accessor :name, :table, :filter, :links, :order_by, :fields, :subtypes
106
+ attr_reader :schema, :mappings
90
107
  def initialize(schema, name)
91
108
  @schema = schema
92
109
  @name = name
@@ -95,6 +112,11 @@ module PgGraphQl
95
112
  @filter = nil
96
113
  @order_by = nil
97
114
  @links = {}
115
+ @subtypes = {}
116
+ @mappings = {}
117
+ end
118
+ def map(field, column_expr)
119
+ @mappings[field] = column_expr
98
120
  end
99
121
  def one(name, opts={})
100
122
  create_link(name, false, opts)
@@ -102,6 +124,13 @@ module PgGraphQl
102
124
  def many(name, opts={})
103
125
  create_link(name, true, opts)
104
126
  end
127
+ def subtype(name, opts={})
128
+ subtype = @subtypes[name] = SubType.new(self, name)
129
+ opts.each_pair do |key, val|
130
+ subtype.send(:"#{key}=", val)
131
+ end
132
+ subtype
133
+ end
105
134
  def create_link(name, many, opts)
106
135
  link = @links[name] = Link.new(self, name, many)
107
136
  opts.each_pair do |key, val|
@@ -111,6 +140,16 @@ module PgGraphQl
111
140
  end
112
141
  end
113
142
 
143
+ class SubType
144
+ attr_accessor :name, :table, :fk
145
+ def initialize(type, name)
146
+ @type = type
147
+ @name = name
148
+ @table = nil
149
+ @fk = nil
150
+ end
151
+ end
152
+
114
153
  class Link
115
154
  attr_accessor :name, :filter, :fk, :order_by
116
155
  def initialize(owner, name, many)
@@ -125,9 +125,74 @@ module PgGraphQl
125
125
  ), token(res)
126
126
  end
127
127
 
128
- # #####################
129
- # # one
130
- # #####################
128
+ #####################
129
+ # inherit
130
+ #####################
131
+
132
+ def test_inherit
133
+ res = to_sql({
134
+ products: {
135
+ id: [],
136
+ type: nil,
137
+ clickout__destination_url: nil,
138
+ download__download_url: nil,
139
+ download__users: {
140
+ id: "id",
141
+ orders: {
142
+ id: "id"
143
+ }
144
+ }
145
+ }
146
+ }) do |s|
147
+ s.root :product
148
+
149
+ s.type :user, fields: [:id, :email] do |t|
150
+ t.many :orders, fk: "user_id = users.id"
151
+ end
152
+
153
+ s.type :order
154
+
155
+ s.type :product, fields: [:id, :type, :clickout__destination_url, :download__download_url] do |t|
156
+
157
+ t.map :id, "products.id"
158
+
159
+ t.subtype :download, table: :product_downloads, fk: "download.id = products.id and products.type = 'download'"
160
+ t.subtype :clickout, table: :product_clickouts, fk: "clickout.id = products.id and products.type = 'clickout'"
161
+
162
+ t.many :download__users, type: :user, fk: "id = download.id"
163
+ end
164
+
165
+ end
166
+
167
+ assert_equal token(<<-SQL
168
+ select 'products'::text as key,
169
+ (select to_json(coalesce(json_agg(x.*), '[]'::json))
170
+ from (select products.id as id,
171
+ type,
172
+ clickout.destination_url as clickout__destination_url,
173
+ download.download_url as download__download_url,
174
+ (select to_json(coalesce(json_agg(x.*), '[]'::json))
175
+ from (select id,
176
+ (select to_json(coalesce(json_agg(x.*), '[]'::json))
177
+ from (select id
178
+ from orders
179
+ where (user_id = users.id)) x) as orders
180
+ from users
181
+ where (id = download.id)) x) as download__users
182
+ from products
183
+ left join product_downloads as download on (download.id = products.id
184
+ and products.type = 'download')
185
+ left join product_clickouts as clickout on (clickout.id = products.id
186
+ and products.type = 'clickout')) x) as value
187
+ SQL
188
+ ), token(res)
189
+
190
+ end
191
+
192
+
193
+ #####################
194
+ # one
195
+ #####################
131
196
 
132
197
 
133
198
  def test_link_one
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pggraphql
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jan Zimmek
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-08 00:00:00.000000000 Z
11
+ date: 2015-03-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json