rom-sql 0.5.2 → 0.5.3

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: 0b429ab1e11613744f6b0626ea822abe070424c0
4
- data.tar.gz: 86220401f55baec6bc56a40545379971f74ed17e
3
+ metadata.gz: a3b5357a86ca00174aa17cce24a077a1eb843238
4
+ data.tar.gz: 4e7165c1dc95b56b1823def507746fefc0c0c74e
5
5
  SHA512:
6
- metadata.gz: f0169d4b08b6e037dbb7c18b76e41e14a6cff5f2d1edb0bdaba4705c216a6f52e57b04de4bb3ef14b5e329ba45e55205be7afe18c85f3179f345612158cbbc0d
7
- data.tar.gz: beceab2da1ed5cea31bf03224cf30752a6b1c2a922e88f6a5f61875c03bf4239b1626d8b5d9d51930244492957362f2efab80f87773e2e1118b50d354e36c8db
6
+ metadata.gz: fe54d5beae0bf5cb3b249ff8a6c17b445b351ebcf61651b02903dda3205d4d1696162be311b82ff68a08b6d9496f735529e7d0807f200a88f3b5445b11787314
7
+ data.tar.gz: 5458d5da39115d940d9c01989723bbee280ad6f50710c3324e78936c41b91d1c2b2a642e1a5ed58d2bf04320c9b6b1fb1beff26f5795f2806082f0f635dc241b
@@ -1,3 +1,19 @@
1
+ ## v0.5.3 2015-07-23
2
+
3
+ ### Added
4
+
5
+ * `Relation#multi_insert` (draxxxeus)
6
+
7
+ ### Changed
8
+
9
+ * Command that receives many tuples will use `multi_insert` now (draxxxeus)
10
+
11
+ ### Fixed
12
+
13
+ * Relation name and join key fixes for `many_to_one` macro (jamesmoriarty)
14
+
15
+ [Compare v0.5.2...v0.5.3](https://github.com/rom-rb/rom-sql/compare/v0.5.2...v0.5.3)
16
+
1
17
  ## v0.5.2 2015-06-22
2
18
 
3
19
  ### Added
data/README.md CHANGED
@@ -35,110 +35,7 @@ Or install it yourself as:
35
35
 
36
36
  $ gem install rom-sql
37
37
 
38
- ## Setup
39
-
40
- ROM uses [Sequel](http://sequel.jeremyevans.net) under the hood and exposes its
41
- [Dataset API](http://sequel.jeremyevans.net/rdoc/files/doc/dataset_basics_rdoc.html)
42
- in relation objects. For schema migrations you can use its
43
- [Migration API](http://sequel.jeremyevans.net/rdoc/files/doc/migration_rdoc.html)
44
- which is available via repositories.
45
-
46
- ``` ruby
47
- require "rom-sql"
48
- require "sqlite3"
49
-
50
- setup = ROM.setup(:sql, "sqlite::memory")
51
-
52
- setup.default.connection.create_table(:users) do
53
- primary_key :id
54
- String :name
55
- Boolean :admin
56
- end
57
-
58
- setup.default.connection.create_table(:tasks) do
59
- primary_key :id
60
- Integer :user_id
61
- String :title
62
- end
63
- ```
64
-
65
- ## Relations
66
-
67
- ``` ruby
68
-
69
- class Users < ROM::Relation[:sql]
70
- def by_name(name)
71
- where(name: name)
72
- end
73
- end
74
-
75
- class Tasks < ROM::Relation[:sql]
76
- end
77
-
78
- rom = ROM.finalize.env
79
-
80
- users = rom.relations.users
81
- tasks = rom.relations.tasks
82
-
83
- users.insert(id: 1, name: "Piotr")
84
- tasks.insert(user_id: 1, title: "Be happy")
85
-
86
- puts rom.relation(:users).by_name("Piotr").to_a.inspect
87
- # => [{:id=>1, :name=>"Piotr", :user_id=>1}]
88
- ```
89
-
90
- ## Mapping joins to aggregates
91
-
92
- ROM doesn't have a relationship concept like in ActiveRecord or Sequel. Instead
93
- it provides a convenient interface for building joined relations that can be
94
- mapped to [aggregate objects](http://martinfowler.com/bliki/Aggregate.html).
95
-
96
- There's no lazy-loading, eager-loading or any other magic happening behind the
97
- scenes. You're in full control of how data are fetched from the database and it's
98
- an explicit operation.
99
-
100
- Sequel's association DSL is available in relation definitions which enables
101
- `association_join` interface inside relations. To map joined results to
102
- aggregate objects `wrap` and `group` mapping transformation can be used
103
-
104
- ``` ruby
105
- ROM.setup(:sql, "sqlite::memory")
106
-
107
- class Users < ROM::Relation[:sql]
108
- one_to_many :tasks, key: :user_id
109
-
110
- def by_name(name)
111
- where(name: name)
112
- end
113
-
114
- def with_tasks
115
- association_join(:tasks, select: [:title])
116
- end
117
- end
118
-
119
- class UserMapper < ROM::Mapper
120
- relation :users
121
- register_as :model
122
-
123
- model name: 'User'
124
-
125
- group tasks: [:title]
126
- end
127
-
128
- rom = ROM.finalize.env
129
-
130
- users = rom.relations.users
131
- tasks = rom.relations.tasks
132
-
133
- users.insert(id: 1, name: "Piotr")
134
- tasks.insert(user_id: 1, title: "Be happy")
135
-
136
- rom.relation(:users).with_tasks.by_name("Piotr").to_a
137
- # => [{:id=>1, :name=>"Piotr", :admin=>nil, :title=>"Be happy"}]
138
-
139
- rom.relation(:users).as(:model).with_tasks.by_name("Piotr").to_a
140
- # => [#<User:0x007fb31542a098 @id=1, @name="Piotr", @tasks=[{:title=>"Be happy"}]>]
141
- ```
38
+ Check out [SQL guide](http://rom-rb.org/guides/adapters/sql/) for usage examples.
142
39
 
143
40
  ## ROADMAP
144
41
 
@@ -26,7 +26,11 @@ module ROM
26
26
  attributes.to_h
27
27
  end
28
28
 
29
- insert(insert_tuples)
29
+ if insert_tuples.length > 1
30
+ multi_insert(insert_tuples)
31
+ else
32
+ insert(insert_tuples)
33
+ end
30
34
  end
31
35
 
32
36
  private
@@ -39,6 +43,14 @@ module ROM
39
43
  relation.where(relation.primary_key => pks)
40
44
  end
41
45
 
46
+ # Executes multi_insert statement and returns inserted tuples
47
+ #
48
+ # @api private
49
+ def multi_insert(tuples)
50
+ pks = relation.multi_insert(tuples, return: :primary_key)
51
+ relation.where(relation.primary_key => pks).to_a
52
+ end
53
+
42
54
  # Yields tuples for insertion or return an enumerator
43
55
  #
44
56
  # @api private
@@ -11,6 +11,10 @@ module ROM
11
11
  relation.dataset.returning(*relation.columns).insert(tuple)
12
12
  end.flatten
13
13
  end
14
+
15
+ def multi_insert(tuples)
16
+ relation.dataset.returning(*relation.columns).multi_insert(tuples)
17
+ end
14
18
  end
15
19
 
16
20
  module Update
@@ -348,6 +348,20 @@ module ROM
348
348
  dataset.insert(*args, &block)
349
349
  end
350
350
 
351
+ # Multi insert tuples into relation
352
+ #
353
+ # @example
354
+ # users.multi_insert([{name: 'Jane'}, {name: 'Jack'}])
355
+ #
356
+ # @param [Array] tuples
357
+ #
358
+ # @return [Relation]
359
+ #
360
+ # @api public
361
+ def multi_insert(*args, &block)
362
+ dataset.multi_insert(*args, &block)
363
+ end
364
+
351
365
  # Update tuples in the relation
352
366
  #
353
367
  # @example
@@ -97,12 +97,12 @@ module ROM
97
97
  end
98
98
 
99
99
  def graph_join_other(name, assoc, type, join_type, options)
100
- key = assoc[:key]
100
+ key = assoc[:key]
101
101
  on_conditions = assoc[:on] || {}
102
102
 
103
103
  join_keys =
104
104
  if type == :many_to_one
105
- { primary_key => key }
105
+ { assoc[:class].primary_key => key }
106
106
  else
107
107
  { key => primary_key }
108
108
  end.merge(on_conditions)
@@ -88,9 +88,7 @@ module ROM
88
88
  #
89
89
  # @api public
90
90
  def many_to_one(name, options = {})
91
- relation_name = Inflector.pluralize(name).to_sym
92
- new_options = options.merge(relation: relation_name)
93
- associations << [__method__, name, new_options]
91
+ associations << [__method__, name, { relation: name }.merge(options)]
94
92
  end
95
93
 
96
94
  # Finalize the relation by setting up its associations (if any)
@@ -1,5 +1,5 @@
1
1
  module ROM
2
2
  module SQL
3
- VERSION = '0.5.2'.freeze
3
+ VERSION = '0.5.3'.freeze
4
4
  end
5
5
  end
@@ -51,6 +51,16 @@ describe 'Commands / Create' do
51
51
  expect(result.value).to eq(id: 1, name: 'Jane')
52
52
  end
53
53
 
54
+ it 'creates multiple records if nothing was raised' do
55
+ result = users.create.transaction {
56
+ users.create_many.call([{name: 'Jane'}, {name: 'Jack'}])
57
+ }
58
+
59
+ expect(result.value).to match_array([
60
+ { id: 1, name: 'Jane' }, { id: 2, name: 'Jack' }
61
+ ])
62
+ end
63
+
54
64
  it 'allows for nested transactions' do
55
65
  result = users.create.transaction {
56
66
  users.create.transaction {
@@ -43,4 +43,18 @@ describe 'Defining many-to-one association' do
43
43
  [{ id: 1, title: 'Finish ROM', user: { name: 'Piotr' } }]
44
44
  )
45
45
  end
46
+
47
+ it "joins on specified key" do
48
+ setup.relation(:task_tags) do
49
+ many_to_one :tags, key: :tag_id
50
+
51
+ def with_tags
52
+ association_left_join(:tags)
53
+ end
54
+ end
55
+
56
+ expect(rom.relation(:task_tags).with_tags.to_a).to eq(
57
+ [{ tag_id: 1, task_id: 1, id: 1, name: "important" }]
58
+ )
59
+ end
46
60
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rom-sql
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.2
4
+ version: 0.5.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Piotr Solnica
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-06-22 00:00:00.000000000 Z
11
+ date: 2015-07-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sequel