rasti-db 0.2.9 → 0.3.0

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: e586b1a5bcf834e6a79251e952217118cf6c6f53
4
- data.tar.gz: b856445b50aa29d89cac9ed1249b214b652709b3
3
+ metadata.gz: 898bf83981559ff4a054d73ad018a7f07e65d1a3
4
+ data.tar.gz: dda154478784631a4683281364c43d249db3356c
5
5
  SHA512:
6
- metadata.gz: 6accf5482a38984a81ce068a90c1d1dbf959573b04b6e197d9e444f25d3bf0bff48a2365114352dc26d9e27656a7aed4702142977d97237026609f1dc16268da
7
- data.tar.gz: 0a2ccdbfa430014b52a4af8fa7cf1abd8a56b07dc3ed2fa4611c280247d118bd1c6ac5f63380e34d245f977343799ed316989498b6f940cc51b437b010853027
6
+ metadata.gz: cf9dacf0e63b028ad3ac4a32fcaff83dbb3b355ee02f60782d85203c2d931015e5ec1aa7cbcbc35b54ab943a3fb78f62970ebde2680d603d5998547ba3c347a4
7
+ data.tar.gz: ad67c48927b98ae619bd7c80fd044f518d26d906ff238972ab07871d987f2ccd616230b34c6816c0e5f49a928ba6bc77436c1834a71a21675cb8f546299d72a4
data/README.md CHANGED
@@ -130,6 +130,9 @@ DB.transaction do
130
130
  users.bulk_insert [{name: 'User 1'}, {name: 'User 2'}]
131
131
  users.bulk_update(name: 'User updated') { where id: [1,2] }
132
132
  users.bulk_delete { where id: [1,2] }
133
+
134
+ posts.insert_relations 1, categories: [2,3]
135
+ posts.delete_relations 1, categories: [2,3]
133
136
  end
134
137
  ```
135
138
 
@@ -144,6 +147,7 @@ posts.where{id > 1}.limit(10).offset(20) } # => [Post, ...]
144
147
  posts.graph(:user, :categories, 'comments.user') # => [Post(User, [Categories, ...], [Comments(User)]), ...]
145
148
  posts.created_by(1) # => [Post, ...]
146
149
  posts.created_by(1).entitled('...').commented_by(2) # => [Post, ...]
150
+ posts.with_categories([1,2]) # => [Post, ...]
147
151
  posts.where(id: [1,2]).raw # => [{id:1, ...}, {id:2, ...}]
148
152
  posts.where(id: [1,2]).primary_keys # => [1,2]
149
153
  posts.where(id: [1,2]).pluck(:id) # => [1,2]
@@ -8,6 +8,7 @@ module Rasti
8
8
 
9
9
  class << self
10
10
 
11
+ extend Sequel::Inflections
11
12
  include Sequel::Inflections
12
13
 
13
14
  def collection_name
@@ -58,19 +59,19 @@ module Rasti
58
59
  @model = model
59
60
  end
60
61
 
61
- def one_to_many(name, options={})
62
- relations[name] = Relations::OneToMany.new name, self, options
63
- end
64
-
65
- def many_to_one(name, options={})
66
- relations[name] = Relations::ManyToOne.new name, self, options
67
- end
62
+ [Relations::OneToMany, Relations::ManyToOne, Relations::ManyToMany].each do |relation_class|
63
+ define_method underscore(demodulize(relation_class.name)) do |name, options={}|
64
+ relations[name] = relation_class.new name, self, options
68
65
 
69
- def many_to_many(name, options={})
70
- relations[name] = Relations::ManyToMany.new name, self, options
66
+ query "with_#{pluralize(name)}".to_sym do |primary_keys|
67
+ with_related name, primary_keys
68
+ end
69
+ end
71
70
  end
72
71
 
73
72
  def query(name, lambda=nil, &block)
73
+ raise "Query #{name} already exists" if queries.key? name
74
+
74
75
  queries[name] = lambda || block
75
76
 
76
77
  define_method name do |*args|
@@ -166,10 +167,6 @@ module Rasti
166
167
  where(self.class.primary_key => primary_key).graph(*relations).first
167
168
  end
168
169
 
169
- def query
170
- Query.new self.class, dataset, [], schema
171
- end
172
-
173
170
  QUERY_METHODS.each do |method|
174
171
  define_method method do |*args, &block|
175
172
  query.public_send method, *args, &block
@@ -194,6 +191,10 @@ module Rasti
194
191
  schema.nil? ? self.class.collection_name : Sequel.qualify(schema, self.class.collection_name)
195
192
  end
196
193
 
194
+ def query
195
+ Query.new self.class, dataset, [], schema
196
+ end
197
+
197
198
  def build_query(filter=nil, &block)
198
199
  raise ArgumentError, 'must specify filter hash or block' if filter.nil? && block.nil?
199
200
  if filter
@@ -28,7 +28,7 @@ module Rasti
28
28
  end
29
29
 
30
30
  def all
31
- with_relations(dataset.all).map do |row|
31
+ with_graph(dataset.all).map do |row|
32
32
  collection_class.model.new row
33
33
  end
34
34
  end
@@ -41,7 +41,7 @@ module Rasti
41
41
  DATASET_CHAINED_METHODS.each do |method|
42
42
  define_method method do |*args, &block|
43
43
  Query.new collection_class,
44
- dataset.send(method, *args, &block),
44
+ dataset.public_send(method, *args, &block),
45
45
  relations,
46
46
  schema
47
47
  end
@@ -68,12 +68,12 @@ module Rasti
68
68
 
69
69
  def first
70
70
  row = dataset.first
71
- row ? collection_class.model.new(with_relations(row)) : nil
71
+ row ? collection_class.model.new(with_graph(row)) : nil
72
72
  end
73
73
 
74
74
  def last
75
75
  row = dataset.last
76
- row ? collection_class.model.new(with_relations(row)) : nil
76
+ row ? collection_class.model.new(with_graph(row)) : nil
77
77
  end
78
78
 
79
79
  def detect(*args, &block)
@@ -92,7 +92,12 @@ module Rasti
92
92
  Query.new collection_class, ds, relations, schema
93
93
  end
94
94
 
95
- def with_relations(data)
95
+ def with_related(relation_name, primary_keys)
96
+ ds = collection_class.relations[relation_name].apply_filter dataset, schema, primary_keys
97
+ Query.new collection_class, ds, relations, schema
98
+ end
99
+
100
+ def with_graph(data)
96
101
  rows = data.is_a?(Array) ? data : [data]
97
102
  Relations.graph_to rows, relations, collection_class, dataset.db, schema
98
103
  data
@@ -43,6 +43,14 @@ module Rasti
43
43
  @target_collection_class ||= @options[:collection].is_a?(Class) ? @options[:collection] : Consty.get(@options[:collection] || camelize(pluralize(name)), source_collection_class)
44
44
  end
45
45
 
46
+ def qualified_source_collection_name(schema=nil)
47
+ schema.nil? ? source_collection_class.collection_name : Sequel.qualify(schema, source_collection_class.collection_name)
48
+ end
49
+
50
+ def qualified_target_collection_name(schema=nil)
51
+ schema.nil? ? target_collection_class.collection_name : Sequel.qualify(schema, target_collection_class.collection_name)
52
+ end
53
+
46
54
  def one_to_many?
47
55
  is_a? OneToMany
48
56
  end
@@ -82,6 +90,15 @@ module Rasti
82
90
  end
83
91
  end
84
92
 
93
+ def apply_filter(dataset, schema=nil, primary_keys=[])
94
+ target_name = qualified_target_collection_name schema
95
+
96
+ dataset.join(target_name, foreign_key => source_collection_class.primary_key)
97
+ .where(Sequel.qualify(target_name, target_collection_class.primary_key) => primary_keys)
98
+ .select_all(qualified_source_collection_name(schema))
99
+ .distinct
100
+ end
101
+
85
102
  end
86
103
 
87
104
 
@@ -107,6 +124,10 @@ module Rasti
107
124
  end
108
125
  end
109
126
 
127
+ def apply_filter(dataset, schema=nil, primary_keys=[])
128
+ dataset.where(foreign_key => primary_keys)
129
+ end
130
+
110
131
  end
111
132
 
112
133
 
@@ -124,10 +145,6 @@ module Rasti
124
145
  @relation_collection_name ||= @options[:relation_collection_name] || [source_collection_class.collection_name, target_collection_class.collection_name].sort.join('_').to_sym
125
146
  end
126
147
 
127
- def qualified_target_collection_name(schema=nil)
128
- schema.nil? ? target_collection_class.collection_name : Sequel.qualify(schema, target_collection_class.collection_name)
129
- end
130
-
131
148
  def qualified_relation_collection_name(schema=nil)
132
149
  schema.nil? ? relation_collection_name : Sequel.qualify(schema, relation_collection_name)
133
150
  end
@@ -158,6 +175,15 @@ module Rasti
158
175
  end
159
176
  end
160
177
 
178
+ def apply_filter(dataset, schema=nil, primary_keys=[])
179
+ relation_name = qualified_relation_collection_name schema
180
+
181
+ dataset.join(relation_name, source_foreign_key => target_collection_class.primary_key)
182
+ .where(Sequel.qualify(relation_name, target_foreign_key) => primary_keys)
183
+ .select_all(qualified_source_collection_name(schema))
184
+ .distinct
185
+ end
186
+
161
187
  end
162
188
 
163
189
  end
@@ -1,5 +1,5 @@
1
1
  module Rasti
2
2
  module DB
3
- VERSION = '0.2.9'
3
+ VERSION = '0.3.0'
4
4
  end
5
5
  end
data/lib/rasti/db.rb CHANGED
@@ -4,7 +4,7 @@ require 'consty'
4
4
  require_relative 'db/version'
5
5
  require_relative 'db/helpers'
6
6
  require_relative 'db/query'
7
+ require_relative 'db/relations'
7
8
  require_relative 'db/collection'
8
9
  require_relative 'db/model'
9
- require_relative 'db/relations'
10
10
  require_relative 'db/type_converter'
@@ -312,22 +312,45 @@ describe 'Collection' do
312
312
  describe 'Named queries' do
313
313
 
314
314
  before do
315
+ 1.upto(2) { |i| db[:categories].insert name: "Category #{i}" }
315
316
  1.upto(2) { |i| db[:users].insert name: "User #{i}" }
316
- 1.upto(3) { |i| db[:posts].insert user_id: 1, title: "Post #{i}", body: '...' }
317
- 4.upto(5) { |i| db[:posts].insert user_id: 2, title: "Post #{i}", body: '...' }
317
+ 1.upto(3) do |i|
318
+ db[:posts].insert user_id: 1, title: "Post #{i}", body: '...'
319
+ db[:categories_posts].insert category_id: 1, post_id: i
320
+ end
321
+ 4.upto(5) do |i|
322
+ db[:posts].insert user_id: 2, title: "Post #{i}", body: '...'
323
+ db[:categories_posts].insert category_id: 2, post_id: i
324
+ end
325
+ end
326
+
327
+ describe 'Relations' do
328
+
329
+ it 'Many to Many' do
330
+ posts.order(:id).with_categories(1).primary_keys.must_equal [1,2,3]
331
+ end
332
+
333
+ it 'One to Many' do
334
+ users.with_posts([1,4]).primary_keys.must_equal [1,2]
335
+ end
336
+
337
+ it 'Many to One' do
338
+ posts.with_users(2).primary_keys.must_equal [4,5]
339
+ end
340
+
318
341
  end
319
342
 
320
343
  it 'Global' do
321
344
  result_1 = posts.created_by(1)
322
- result_1.map(&:id).must_equal [1,2,3]
345
+ result_1.primary_keys.must_equal [1,2,3]
323
346
 
324
347
  result_2 = posts.created_by(2)
325
- result_2.map(&:id).must_equal [4,5]
348
+ result_2.primary_keys.must_equal [4,5]
326
349
  end
327
350
 
328
351
  it 'Chained' do
329
352
  result = posts.created_by(2).entitled('Post 4')
330
- result.map(&:id).must_equal [4]
353
+ result.primary_keys.must_equal [4]
331
354
  end
332
355
 
333
356
  end
data/spec/query_spec.rb CHANGED
@@ -37,7 +37,7 @@ describe 'Query' do
37
37
  end
38
38
 
39
39
  it 'Map' do
40
- users_query.map { |u| u.name }.must_equal db[:users].map(:name)
40
+ users_query.map(&:name).must_equal db[:users].map(:name)
41
41
  end
42
42
 
43
43
  it 'Detect' do
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: 0.2.9
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gabriel Naiman
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-12-01 00:00:00.000000000 Z
11
+ date: 2017-12-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sequel