rasti-db 1.3.0 → 2.0.1
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/.travis.yml +7 -12
- data/README.md +77 -21
- data/lib/rasti/db.rb +2 -1
- data/lib/rasti/db/collection.rb +70 -46
- data/lib/rasti/db/data_source.rb +18 -0
- data/lib/rasti/db/environment.rb +32 -0
- data/lib/rasti/db/model.rb +4 -0
- data/lib/rasti/db/query.rb +71 -37
- data/lib/rasti/db/relations/base.rb +22 -8
- data/lib/rasti/db/relations/graph.rb +129 -0
- data/lib/rasti/db/relations/many_to_many.rb +59 -25
- data/lib/rasti/db/relations/many_to_one.rb +17 -12
- data/lib/rasti/db/relations/one_to_many.rb +27 -16
- data/lib/rasti/db/version.rb +1 -1
- data/rasti-db.gemspec +3 -8
- data/spec/collection_spec.rb +223 -52
- data/spec/minitest_helper.rb +50 -14
- data/spec/model_spec.rb +9 -1
- data/spec/query_spec.rb +199 -77
- data/spec/relations_spec.rb +27 -7
- metadata +25 -10
- data/lib/rasti/db/helpers.rb +0 -16
- data/lib/rasti/db/relations/graph_builder.rb +0 -60
data/spec/relations_spec.rb
CHANGED
@@ -30,14 +30,19 @@ describe 'Relations' do
|
|
30
30
|
relation.many_to_one?.must_equal false
|
31
31
|
relation.many_to_many?.must_equal false
|
32
32
|
relation.one_to_one?.must_equal false
|
33
|
+
|
34
|
+
relation.from_one?.must_equal true
|
35
|
+
relation.from_many?.must_equal false
|
36
|
+
relation.to_one?.must_equal false
|
37
|
+
relation.to_many?.must_equal true
|
33
38
|
end
|
34
39
|
|
35
40
|
it 'Graph' do
|
36
41
|
user_id = db[:users].insert name: 'User 1'
|
37
|
-
1.upto(2) { |i| db[:posts].insert user_id: user_id, title: "Post #{i}", body: '...' }
|
42
|
+
1.upto(2) { |i| db[:posts].insert user_id: user_id, title: "Post #{i}", body: '...', language_id: 1 }
|
38
43
|
rows = db[:users].all
|
39
44
|
|
40
|
-
Users.relations[:posts].
|
45
|
+
Users.relations[:posts].fetch_graph environment, rows
|
41
46
|
|
42
47
|
rows[0][:posts].must_equal posts.where(user_id: user_id).all
|
43
48
|
end
|
@@ -72,14 +77,19 @@ describe 'Relations' do
|
|
72
77
|
relation.many_to_one?.must_equal true
|
73
78
|
relation.many_to_many?.must_equal false
|
74
79
|
relation.one_to_one?.must_equal false
|
80
|
+
|
81
|
+
relation.from_one?.must_equal false
|
82
|
+
relation.from_many?.must_equal true
|
83
|
+
relation.to_one?.must_equal true
|
84
|
+
relation.to_many?.must_equal false
|
75
85
|
end
|
76
86
|
|
77
87
|
it 'Graph' do
|
78
88
|
user_id = db[:users].insert name: 'User 1'
|
79
|
-
db[:posts].insert user_id: user_id, title: 'Post 1', body: '...'
|
89
|
+
db[:posts].insert user_id: user_id, title: 'Post 1', body: '...', language_id: 1
|
80
90
|
rows = db[:posts].all
|
81
91
|
|
82
|
-
Posts.relations[:user].
|
92
|
+
Posts.relations[:user].fetch_graph environment, rows
|
83
93
|
|
84
94
|
rows[0][:user].must_equal users.first
|
85
95
|
end
|
@@ -120,12 +130,17 @@ describe 'Relations' do
|
|
120
130
|
relation.many_to_one?.must_equal false
|
121
131
|
relation.many_to_many?.must_equal true
|
122
132
|
relation.one_to_one?.must_equal false
|
133
|
+
|
134
|
+
relation.from_one?.must_equal false
|
135
|
+
relation.from_many?.must_equal true
|
136
|
+
relation.to_one?.must_equal false
|
137
|
+
relation.to_many?.must_equal true
|
123
138
|
end
|
124
139
|
|
125
140
|
it 'Graph' do
|
126
141
|
user_id = db[:users].insert name: 'User 1'
|
127
142
|
|
128
|
-
1.upto(2) { |i| db[:posts].insert user_id: user_id, title: "Post #{i}", body: '...' }
|
143
|
+
1.upto(2) { |i| db[:posts].insert user_id: user_id, title: "Post #{i}", body: '...', language_id: 1 }
|
129
144
|
|
130
145
|
1.upto(4) { |i| db[:categories].insert name: "Category #{i}" }
|
131
146
|
|
@@ -136,7 +151,7 @@ describe 'Relations' do
|
|
136
151
|
|
137
152
|
rows = db[:posts].all
|
138
153
|
|
139
|
-
Posts.relations[:categories].
|
154
|
+
Posts.relations[:categories].fetch_graph environment, rows
|
140
155
|
|
141
156
|
rows[0][:categories].must_equal categories.where(id: [1,2]).all
|
142
157
|
rows[1][:categories].must_equal categories.where(id: [3,4]).all
|
@@ -172,6 +187,11 @@ describe 'Relations' do
|
|
172
187
|
relation.many_to_one?.must_equal false
|
173
188
|
relation.many_to_many?.must_equal false
|
174
189
|
relation.one_to_one?.must_equal true
|
190
|
+
|
191
|
+
relation.from_one?.must_equal true
|
192
|
+
relation.from_many?.must_equal false
|
193
|
+
relation.to_one?.must_equal true
|
194
|
+
relation.to_many?.must_equal false
|
175
195
|
end
|
176
196
|
|
177
197
|
it 'Graph' do
|
@@ -186,7 +206,7 @@ describe 'Relations' do
|
|
186
206
|
|
187
207
|
rows = db[:users].all
|
188
208
|
|
189
|
-
Users.relations[:person].
|
209
|
+
Users.relations[:person].fetch_graph environment, rows
|
190
210
|
|
191
211
|
2.times do |i|
|
192
212
|
rows[i][:person].must_equal people.find("document_#{i}")
|
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:
|
4
|
+
version: 2.0.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: 2020-10-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: sequel
|
@@ -113,33 +113,47 @@ dependencies:
|
|
113
113
|
- !ruby/object:Gem::Version
|
114
114
|
version: '1.0'
|
115
115
|
- !ruby/object:Gem::Dependency
|
116
|
-
name:
|
116
|
+
name: hierarchical_graph
|
117
117
|
requirement: !ruby/object:Gem::Requirement
|
118
118
|
requirements:
|
119
119
|
- - "~>"
|
120
120
|
- !ruby/object:Gem::Version
|
121
|
-
version: '1.
|
122
|
-
type: :
|
121
|
+
version: '1.0'
|
122
|
+
type: :runtime
|
123
|
+
prerelease: false
|
124
|
+
version_requirements: !ruby/object:Gem::Requirement
|
125
|
+
requirements:
|
126
|
+
- - "~>"
|
127
|
+
- !ruby/object:Gem::Version
|
128
|
+
version: '1.0'
|
129
|
+
- !ruby/object:Gem::Dependency
|
130
|
+
name: hash_ext
|
131
|
+
requirement: !ruby/object:Gem::Requirement
|
132
|
+
requirements:
|
133
|
+
- - "~>"
|
134
|
+
- !ruby/object:Gem::Version
|
135
|
+
version: '0.5'
|
136
|
+
type: :runtime
|
123
137
|
prerelease: false
|
124
138
|
version_requirements: !ruby/object:Gem::Requirement
|
125
139
|
requirements:
|
126
140
|
- - "~>"
|
127
141
|
- !ruby/object:Gem::Version
|
128
|
-
version: '
|
142
|
+
version: '0.5'
|
129
143
|
- !ruby/object:Gem::Dependency
|
130
144
|
name: rake
|
131
145
|
requirement: !ruby/object:Gem::Requirement
|
132
146
|
requirements:
|
133
147
|
- - "~>"
|
134
148
|
- !ruby/object:Gem::Version
|
135
|
-
version: '
|
149
|
+
version: '12.3'
|
136
150
|
type: :development
|
137
151
|
prerelease: false
|
138
152
|
version_requirements: !ruby/object:Gem::Requirement
|
139
153
|
requirements:
|
140
154
|
- - "~>"
|
141
155
|
- !ruby/object:Gem::Version
|
142
|
-
version: '
|
156
|
+
version: '12.3'
|
143
157
|
- !ruby/object:Gem::Dependency
|
144
158
|
name: minitest
|
145
159
|
requirement: !ruby/object:Gem::Requirement
|
@@ -263,7 +277,8 @@ files:
|
|
263
277
|
- lib/rasti-db.rb
|
264
278
|
- lib/rasti/db.rb
|
265
279
|
- lib/rasti/db/collection.rb
|
266
|
-
- lib/rasti/db/
|
280
|
+
- lib/rasti/db/data_source.rb
|
281
|
+
- lib/rasti/db/environment.rb
|
267
282
|
- lib/rasti/db/model.rb
|
268
283
|
- lib/rasti/db/nql/invalid_expression_error.rb
|
269
284
|
- lib/rasti/db/nql/nodes/binary_node.rb
|
@@ -293,7 +308,7 @@ files:
|
|
293
308
|
- lib/rasti/db/nql/syntax.treetop
|
294
309
|
- lib/rasti/db/query.rb
|
295
310
|
- lib/rasti/db/relations/base.rb
|
296
|
-
- lib/rasti/db/relations/
|
311
|
+
- lib/rasti/db/relations/graph.rb
|
297
312
|
- lib/rasti/db/relations/many_to_many.rb
|
298
313
|
- lib/rasti/db/relations/many_to_one.rb
|
299
314
|
- lib/rasti/db/relations/one_to_many.rb
|
data/lib/rasti/db/helpers.rb
DELETED
@@ -1,16 +0,0 @@
|
|
1
|
-
module Rasti
|
2
|
-
module DB
|
3
|
-
module Helpers
|
4
|
-
module WithSchema
|
5
|
-
|
6
|
-
private
|
7
|
-
|
8
|
-
def with_schema(table, field=nil)
|
9
|
-
qualified_table = schema ? Sequel[schema][table] : Sequel[table]
|
10
|
-
field ? Sequel[qualified_table][field] : qualified_table
|
11
|
-
end
|
12
|
-
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
@@ -1,60 +0,0 @@
|
|
1
|
-
module Rasti
|
2
|
-
module DB
|
3
|
-
module Relations
|
4
|
-
class GraphBuilder
|
5
|
-
class << self
|
6
|
-
|
7
|
-
def graph_to(rows, relations, collection_class, db, schema=nil)
|
8
|
-
return if rows.empty?
|
9
|
-
|
10
|
-
parse(relations).each do |relation_name, nested_relations|
|
11
|
-
relation = get_relation collection_class, relation_name
|
12
|
-
relation.graph_to rows, db, schema, nested_relations
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
def joins_to(dataset, relations, collection_class, schema=nil)
|
17
|
-
ds = recursive_joins dataset, recursive_parse(relations), collection_class, schema
|
18
|
-
qualified_collection_name = schema ? Sequel[schema][collection_class.collection_name] : Sequel[collection_class.collection_name]
|
19
|
-
ds.distinct.select_all(qualified_collection_name)
|
20
|
-
end
|
21
|
-
|
22
|
-
private
|
23
|
-
|
24
|
-
def get_relation(collection_class, relation_name)
|
25
|
-
raise "Undefined relation #{relation_name} for #{collection_class}" unless collection_class.relations.key? relation_name
|
26
|
-
collection_class.relations[relation_name]
|
27
|
-
end
|
28
|
-
|
29
|
-
def parse(relations)
|
30
|
-
relations.each_with_object({}) do |relation, hash|
|
31
|
-
tail = relation.to_s.split '.'
|
32
|
-
head = tail.shift.to_sym
|
33
|
-
hash[head] ||= []
|
34
|
-
hash[head] << tail.join('.') unless tail.empty?
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
def recursive_parse(relations)
|
39
|
-
parse(relations).each_with_object({}) do |(key, value), hash|
|
40
|
-
hash[key] = recursive_parse value
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
def recursive_joins(dataset, joins, collection_class, schema, prefix=nil)
|
45
|
-
joins.each do |relation_name, nested_joins|
|
46
|
-
relation = get_relation collection_class, relation_name
|
47
|
-
|
48
|
-
dataset = relation.join_to dataset, schema, prefix
|
49
|
-
|
50
|
-
dataset = recursive_joins dataset, nested_joins, relation.target_collection_class, schema, relation.join_relation_name(prefix) unless nested_joins.empty?
|
51
|
-
end
|
52
|
-
|
53
|
-
dataset
|
54
|
-
end
|
55
|
-
|
56
|
-
end
|
57
|
-
end
|
58
|
-
end
|
59
|
-
end
|
60
|
-
end
|