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 +4 -4
- data/README.md +4 -0
- data/lib/rasti/db/collection.rb +14 -13
- data/lib/rasti/db/query.rb +10 -5
- data/lib/rasti/db/relations.rb +30 -4
- data/lib/rasti/db/version.rb +1 -1
- data/lib/rasti/db.rb +1 -1
- data/spec/collection_spec.rb +28 -5
- data/spec/query_spec.rb +1 -1
- 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: 898bf83981559ff4a054d73ad018a7f07e65d1a3
|
4
|
+
data.tar.gz: dda154478784631a4683281364c43d249db3356c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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]
|
data/lib/rasti/db/collection.rb
CHANGED
@@ -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
|
-
|
62
|
-
|
63
|
-
|
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
|
-
|
70
|
-
|
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
|
data/lib/rasti/db/query.rb
CHANGED
@@ -28,7 +28,7 @@ module Rasti
|
|
28
28
|
end
|
29
29
|
|
30
30
|
def all
|
31
|
-
|
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.
|
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(
|
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(
|
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
|
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
|
data/lib/rasti/db/relations.rb
CHANGED
@@ -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
|
data/lib/rasti/db/version.rb
CHANGED
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'
|
data/spec/collection_spec.rb
CHANGED
@@ -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)
|
317
|
-
|
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.
|
345
|
+
result_1.primary_keys.must_equal [1,2,3]
|
323
346
|
|
324
347
|
result_2 = posts.created_by(2)
|
325
|
-
result_2.
|
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.
|
353
|
+
result.primary_keys.must_equal [4]
|
331
354
|
end
|
332
355
|
|
333
356
|
end
|
data/spec/query_spec.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: 0.
|
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-
|
11
|
+
date: 2017-12-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: sequel
|