rails_event_store 0.2.2 → 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/CHANGELOG.md +6 -0
- data/README.md +76 -16
- data/lib/rails_event_store/client.rb +18 -10
- data/lib/rails_event_store/constants.rb +1 -0
- data/lib/rails_event_store/event.rb +1 -17
- data/lib/rails_event_store/repositories/event_repository.rb +62 -16
- data/lib/rails_event_store/version.rb +1 -1
- data/rails_event_store.gemspec +9 -9
- metadata +23 -23
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2d9826553e1956e364b65709bd8e29e713869a6b
|
4
|
+
data.tar.gz: 2f30ed66122ba0a9a30b5bd3053f03652eaf94ab
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5b2e74bd009c8369d43507147d7c0c64748787e3a6207f2482b8c14b1671853d003b9aa1647276f404400e0bebe2a201dee8e39b5d26c9c0c44ccf931c6783bb
|
7
|
+
data.tar.gz: 1200e2ab68017a246835588778bee2b0a577af0aec61935b1f27c008f19274508b7f56b5a215788ddf5ec24ccd99251eff63d5d8dc4e045c139ca5f032e96cfd
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,11 @@
|
|
1
1
|
### HEAD
|
2
2
|
|
3
|
+
### 0.3.0 (03.03.2016)
|
4
|
+
|
5
|
+
* Update to ruby_event_store 0.3.0 - see ruby_event_store changelog for more details
|
6
|
+
* Implement reading forward & backward (aliasold methods to read forward)
|
7
|
+
* Implement paging when reading from all streams
|
8
|
+
|
3
9
|
### 0.2.2 (25.02.2016)
|
4
10
|
|
5
11
|
* Restore AggregateRoot in RES, but this time as a dependency on aggregate_root gem
|
data/README.md
CHANGED
@@ -38,16 +38,18 @@ Firstly you have to define own event model extending `RailsEventStore::Event` cl
|
|
38
38
|
```ruby
|
39
39
|
class OrderCreated < RailsEventStore::Event
|
40
40
|
end
|
41
|
+
|
42
|
+
# or
|
43
|
+
|
44
|
+
OrderCreated = Class.new(RailsEventStore::Event)
|
41
45
|
```
|
42
46
|
|
43
47
|
```ruby
|
44
48
|
stream_name = "order_1"
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
event = OrderCreated.new(event_data)
|
50
|
-
|
49
|
+
event = OrderCreated.new(
|
50
|
+
data: "sample",
|
51
|
+
event_id: "b2d506fd-409d-4ec7-b02f-c6d2295c7edd"
|
52
|
+
)
|
51
53
|
#publishing event for specific stream
|
52
54
|
client.publish_event(event, stream_name)
|
53
55
|
|
@@ -64,39 +66,97 @@ end
|
|
64
66
|
|
65
67
|
```ruby
|
66
68
|
stream_name = "order_1"
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
event = OrderCreated.new(event_data)
|
69
|
+
event = OrderCreated.new(
|
70
|
+
data: "sample",
|
71
|
+
event_id: "b2d506fd-409d-4ec7-b02f-c6d2295c7edd"
|
72
|
+
)
|
72
73
|
expected_version = "850c347f-423a-4158-a5ce-b885396c5b73" #last event_id
|
73
74
|
client.publish_event(event, stream_name, expected_version)
|
74
75
|
```
|
75
76
|
|
76
|
-
#### Reading stream's events forward in batch
|
77
|
+
#### Reading stream's events forward in batch - starting from first event
|
77
78
|
|
78
79
|
```ruby
|
79
80
|
stream_name = "order_1"
|
80
|
-
start = "850c347f-423a-4158-a5ce-b885396c5b73"
|
81
81
|
count = 40
|
82
|
-
client.
|
82
|
+
client.read_events_forward(stream_name, :head, count)
|
83
83
|
```
|
84
84
|
|
85
|
+
In this case `:head` means first event of the stream.
|
86
|
+
|
87
|
+
#### Reading stream's events forward in batch - staring from given event
|
88
|
+
|
89
|
+
```ruby
|
90
|
+
# last_read_event is any domain event read or published by rails_event_store
|
91
|
+
|
92
|
+
stream_name = "order_1"
|
93
|
+
start = last_read_event.event_id
|
94
|
+
count = 40
|
95
|
+
client.read_events_forward(stream_name, start, count)
|
96
|
+
```
|
97
|
+
|
98
|
+
#### Reading stream's events backward in batch
|
99
|
+
|
100
|
+
As in examples above, just use `read_events_backward` instead of `read_events_forward`.
|
101
|
+
In this case `:head` means last event of the stream.
|
102
|
+
|
85
103
|
#### Reading all events from stream forward
|
86
104
|
|
87
105
|
This method allows us to load all stream's events ascending.
|
88
106
|
|
89
107
|
```ruby
|
90
108
|
stream_name = "order_1"
|
91
|
-
client.
|
109
|
+
client.read_stream_events_forward(stream_name)
|
110
|
+
```
|
111
|
+
|
112
|
+
#### Reading all events from stream forward
|
113
|
+
|
114
|
+
This method allows us to load all stream's events descending.
|
115
|
+
|
116
|
+
```ruby
|
117
|
+
stream_name = "order_1"
|
118
|
+
client.read_stream_events_backward(stream_name)
|
92
119
|
```
|
93
120
|
|
94
121
|
#### Reading all events forward
|
95
122
|
|
96
123
|
This method allows us to load all stored events ascending.
|
97
124
|
|
125
|
+
This will read first 100 domain events stored in event store.
|
126
|
+
|
127
|
+
```ruby
|
128
|
+
client.read_all_streams_forward(:head, 100)
|
129
|
+
```
|
130
|
+
|
131
|
+
When not specified it reads events starting from `:head` (first domain event
|
132
|
+
stored in event store) and reads up to `RailsEventStore::PAGE_SIZE`
|
133
|
+
domain events.
|
134
|
+
|
135
|
+
```ruby
|
136
|
+
client.read_all_streams_forward
|
137
|
+
```
|
138
|
+
|
139
|
+
You could also read batch of domain events starting from any read or published event.
|
140
|
+
|
141
|
+
```ruby
|
142
|
+
client.read_all_streams_forward(last_read_event.event_id, 100)
|
143
|
+
```
|
144
|
+
|
145
|
+
#### Reading all events backward
|
146
|
+
|
147
|
+
This method allows us to load all stored events descending.
|
148
|
+
|
149
|
+
This will read last 100 domain events stored in event store.
|
150
|
+
```ruby
|
151
|
+
client.read_all_streams_backward(:head, 100)
|
152
|
+
```
|
153
|
+
|
154
|
+
When not specified it reads events starting from `:head` (last domain event
|
155
|
+
stored in event store) and reads up to `RailsEventStore::PAGE_SIZE`
|
156
|
+
domain events.
|
157
|
+
|
98
158
|
```ruby
|
99
|
-
client.
|
159
|
+
client.read_all_streams_backward
|
100
160
|
```
|
101
161
|
|
102
162
|
#### Deleting stream
|
@@ -1,30 +1,38 @@
|
|
1
1
|
module RailsEventStore
|
2
2
|
class Client
|
3
3
|
|
4
|
-
def initialize(repository = Repositories::EventRepository.new)
|
4
|
+
def initialize(repository = Repositories::EventRepository.new, page_size = PAGE_SIZE)
|
5
5
|
@repository = repository
|
6
|
+
@page_size = page_size
|
6
7
|
end
|
7
|
-
attr_reader :repository
|
8
|
+
attr_reader :repository, :page_size
|
8
9
|
|
9
|
-
def publish_event(
|
10
|
-
event_store.publish_event(
|
10
|
+
def publish_event(event, stream_name = GLOBAL_STREAM, expected_version = nil)
|
11
|
+
event_store.publish_event(event, stream_name, expected_version)
|
12
|
+
end
|
13
|
+
|
14
|
+
def appent_to_stream(event, stream_name = GLOBAL_STREAM, expected_version = nil)
|
15
|
+
event_store.appent_to_stream(stream_name, event, expected_version)
|
11
16
|
end
|
12
17
|
|
13
18
|
def delete_stream(stream_name)
|
14
19
|
event_store.delete_stream(stream_name)
|
15
20
|
end
|
16
21
|
|
17
|
-
def
|
18
|
-
event_store.
|
22
|
+
def read_events_forward(stream_name, start = :head, count = page_size)
|
23
|
+
event_store.read_events_forward(stream_name, start, count)
|
19
24
|
end
|
25
|
+
alias :read_events :read_events_forward
|
20
26
|
|
21
|
-
def
|
22
|
-
event_store.
|
27
|
+
def read_stream_events_forward(stream_name)
|
28
|
+
event_store.read_stream_events_forward(stream_name)
|
23
29
|
end
|
30
|
+
alias :read_all_events :read_stream_events_forward
|
24
31
|
|
25
|
-
def
|
26
|
-
event_store.
|
32
|
+
def read_all_streams_forward(start = :head, count = page_size)
|
33
|
+
event_store.read_all_streams_forward(start, count)
|
27
34
|
end
|
35
|
+
alias :read_all_streams :read_all_streams_forward
|
28
36
|
|
29
37
|
def subscribe(subscriber, event_types)
|
30
38
|
event_store.subscribe(subscriber, event_types)
|
@@ -1,19 +1,3 @@
|
|
1
1
|
module RailsEventStore
|
2
|
-
|
3
|
-
def initialize(**args)
|
4
|
-
attributes = args.except(:event_type, :event_id, :metadata)
|
5
|
-
singleton_class = (class << self; self; end)
|
6
|
-
attributes.each do |key, value|
|
7
|
-
singleton_class.send(:define_method, key) { value }
|
8
|
-
end
|
9
|
-
|
10
|
-
event_data = {
|
11
|
-
event_type: args[:event_type] || self.class.name,
|
12
|
-
data: attributes,
|
13
|
-
}
|
14
|
-
event_data[:event_id] = args[:event_id] if args.key?(:event_id)
|
15
|
-
event_data[:metadata] = args[:metadata] if args.key?(:metadata)
|
16
|
-
super(event_data)
|
17
|
-
end
|
18
|
-
end
|
2
|
+
Event = RubyEventStore::Event
|
19
3
|
end
|
@@ -7,35 +7,82 @@ module RailsEventStore
|
|
7
7
|
end
|
8
8
|
attr_reader :adapter
|
9
9
|
|
10
|
-
def
|
11
|
-
|
10
|
+
def create(event, stream_name)
|
11
|
+
data = event.to_h.merge!(stream: stream_name)
|
12
|
+
adapter.create(data)
|
13
|
+
event
|
12
14
|
end
|
13
15
|
|
14
|
-
def
|
15
|
-
|
16
|
-
rescue ActiveRecord::RecordNotUnique
|
17
|
-
raise EventCannotBeSaved
|
18
|
-
end
|
19
|
-
|
20
|
-
def delete(condition)
|
16
|
+
def delete_stream(stream_name)
|
17
|
+
condition = {stream: stream_name}
|
21
18
|
adapter.destroy_all condition
|
22
19
|
nil
|
23
20
|
end
|
24
21
|
|
25
|
-
def
|
26
|
-
adapter.
|
22
|
+
def has_event?(event_id)
|
23
|
+
adapter.exists?(event_id: event_id)
|
27
24
|
end
|
28
25
|
|
29
26
|
def last_stream_event(stream_name)
|
30
27
|
build_event_entity(adapter.where(stream: stream_name).last)
|
31
28
|
end
|
32
29
|
|
33
|
-
def
|
34
|
-
adapter.
|
30
|
+
def get_all_events
|
31
|
+
adapter.order('id ASC').order('stream').all.map(&method(:build_event_entity))
|
32
|
+
end
|
33
|
+
|
34
|
+
def read_events_forward(stream_name, start_event_id, count)
|
35
|
+
stream = adapter.where(stream: stream_name)
|
36
|
+
unless start_event_id.equal?(:head)
|
37
|
+
starting_event = adapter.find_by(event_id: start_event_id)
|
38
|
+
stream = stream.where('id > ?', starting_event.id)
|
39
|
+
end
|
40
|
+
|
41
|
+
stream.order('id ASC').limit(count)
|
42
|
+
.map(&method(:build_event_entity))
|
43
|
+
end
|
44
|
+
|
45
|
+
def read_events_backward(stream_name, start_event_id, count)
|
46
|
+
stream = adapter.where(stream: stream_name)
|
47
|
+
unless start_event_id.equal?(:head)
|
48
|
+
starting_event = adapter.find_by(event_id: start_event_id)
|
49
|
+
stream = stream.where('id < ?', starting_event.id)
|
50
|
+
end
|
51
|
+
|
52
|
+
stream.order('id DESC').limit(count)
|
53
|
+
.map(&method(:build_event_entity))
|
54
|
+
end
|
55
|
+
|
56
|
+
def read_stream_events_forward(stream_name)
|
57
|
+
adapter.where(stream: stream_name).order('id ASC')
|
58
|
+
.map(&method(:build_event_entity))
|
59
|
+
end
|
60
|
+
|
61
|
+
def read_stream_events_backward(stream_name)
|
62
|
+
adapter.where(stream: stream_name).order('id DESC')
|
63
|
+
.map(&method(:build_event_entity))
|
35
64
|
end
|
36
65
|
|
37
|
-
def
|
38
|
-
|
66
|
+
def read_all_streams_forward(start_event_id, count)
|
67
|
+
stream = adapter
|
68
|
+
unless start_event_id.equal?(:head)
|
69
|
+
starting_event = adapter.find_by(event_id: start_event_id)
|
70
|
+
stream = stream.where('id > ?', starting_event.id)
|
71
|
+
end
|
72
|
+
|
73
|
+
stream.order('id ASC').limit(count)
|
74
|
+
.map(&method(:build_event_entity))
|
75
|
+
end
|
76
|
+
|
77
|
+
def read_all_streams_backward(start_event_id, count)
|
78
|
+
stream = adapter
|
79
|
+
unless start_event_id.equal?(:head)
|
80
|
+
starting_event = adapter.find_by(event_id: start_event_id)
|
81
|
+
stream = stream.where('id < ?', starting_event.id)
|
82
|
+
end
|
83
|
+
|
84
|
+
stream.order('id DESC').limit(count)
|
85
|
+
.map(&method(:build_event_entity))
|
39
86
|
end
|
40
87
|
|
41
88
|
private
|
@@ -45,7 +92,6 @@ module RailsEventStore
|
|
45
92
|
::RailsEventStore::Event.new(record.data.merge(
|
46
93
|
event_type: record.event_type,
|
47
94
|
event_id: record.event_id,
|
48
|
-
stream: record.stream,
|
49
95
|
metadata: record.metadata))
|
50
96
|
end
|
51
97
|
|
data/rails_event_store.gemspec
CHANGED
@@ -9,9 +9,9 @@ Gem::Specification.new do |spec|
|
|
9
9
|
spec.authors = ['rybex', 'mpraglowski']
|
10
10
|
spec.email = ['tomek.rybka@gmail.com', 'm@praglowski.com']
|
11
11
|
|
12
|
-
spec.summary = %q{
|
12
|
+
spec.summary = %q{Event Store in Ruby}
|
13
13
|
spec.description = %q{Implementation of Event Store in Ruby}
|
14
|
-
spec.homepage = ''
|
14
|
+
spec.homepage = 'https://github.com/arkency/rails_event_store'
|
15
15
|
|
16
16
|
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
17
|
spec.bindir = 'exe'
|
@@ -20,14 +20,14 @@ Gem::Specification.new do |spec|
|
|
20
20
|
|
21
21
|
spec.add_development_dependency 'bundler', '~> 1.9'
|
22
22
|
spec.add_development_dependency 'rake', '~> 10.0'
|
23
|
-
spec.add_development_dependency 'pry'
|
24
|
-
spec.add_development_dependency 'rspec'
|
25
|
-
spec.add_development_dependency 'rails', '~> 4.2
|
26
|
-
spec.add_development_dependency 'webmock', '~> 1.21
|
27
|
-
spec.add_development_dependency 'sqlite3'
|
23
|
+
spec.add_development_dependency 'pry', '~> 0'
|
24
|
+
spec.add_development_dependency 'rspec', '~> 0'
|
25
|
+
spec.add_development_dependency 'rails', '~> 4.2'
|
26
|
+
spec.add_development_dependency 'webmock', '~> 1.21'
|
27
|
+
spec.add_development_dependency 'sqlite3', '~> 0'
|
28
28
|
|
29
|
-
spec.add_dependency 'ruby_event_store', '
|
30
|
-
spec.add_dependency 'aggregate_root', '
|
29
|
+
spec.add_dependency 'ruby_event_store', '~> 0.3'
|
30
|
+
spec.add_dependency 'aggregate_root', '~> 0'
|
31
31
|
spec.add_dependency 'activesupport', '>= 3.0'
|
32
32
|
spec.add_dependency 'activemodel', '>= 3.0'
|
33
33
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails_event_store
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- rybex
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: exe
|
11
11
|
cert_chain: []
|
12
|
-
date: 2016-
|
12
|
+
date: 2016-03-03 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -43,28 +43,28 @@ dependencies:
|
|
43
43
|
name: pry
|
44
44
|
requirement: !ruby/object:Gem::Requirement
|
45
45
|
requirements:
|
46
|
-
- - "
|
46
|
+
- - "~>"
|
47
47
|
- !ruby/object:Gem::Version
|
48
48
|
version: '0'
|
49
49
|
type: :development
|
50
50
|
prerelease: false
|
51
51
|
version_requirements: !ruby/object:Gem::Requirement
|
52
52
|
requirements:
|
53
|
-
- - "
|
53
|
+
- - "~>"
|
54
54
|
- !ruby/object:Gem::Version
|
55
55
|
version: '0'
|
56
56
|
- !ruby/object:Gem::Dependency
|
57
57
|
name: rspec
|
58
58
|
requirement: !ruby/object:Gem::Requirement
|
59
59
|
requirements:
|
60
|
-
- - "
|
60
|
+
- - "~>"
|
61
61
|
- !ruby/object:Gem::Version
|
62
62
|
version: '0'
|
63
63
|
type: :development
|
64
64
|
prerelease: false
|
65
65
|
version_requirements: !ruby/object:Gem::Requirement
|
66
66
|
requirements:
|
67
|
-
- - "
|
67
|
+
- - "~>"
|
68
68
|
- !ruby/object:Gem::Version
|
69
69
|
version: '0'
|
70
70
|
- !ruby/object:Gem::Dependency
|
@@ -73,70 +73,70 @@ dependencies:
|
|
73
73
|
requirements:
|
74
74
|
- - "~>"
|
75
75
|
- !ruby/object:Gem::Version
|
76
|
-
version: 4.2
|
76
|
+
version: '4.2'
|
77
77
|
type: :development
|
78
78
|
prerelease: false
|
79
79
|
version_requirements: !ruby/object:Gem::Requirement
|
80
80
|
requirements:
|
81
81
|
- - "~>"
|
82
82
|
- !ruby/object:Gem::Version
|
83
|
-
version: 4.2
|
83
|
+
version: '4.2'
|
84
84
|
- !ruby/object:Gem::Dependency
|
85
85
|
name: webmock
|
86
86
|
requirement: !ruby/object:Gem::Requirement
|
87
87
|
requirements:
|
88
88
|
- - "~>"
|
89
89
|
- !ruby/object:Gem::Version
|
90
|
-
version: 1.21
|
90
|
+
version: '1.21'
|
91
91
|
type: :development
|
92
92
|
prerelease: false
|
93
93
|
version_requirements: !ruby/object:Gem::Requirement
|
94
94
|
requirements:
|
95
95
|
- - "~>"
|
96
96
|
- !ruby/object:Gem::Version
|
97
|
-
version: 1.21
|
97
|
+
version: '1.21'
|
98
98
|
- !ruby/object:Gem::Dependency
|
99
99
|
name: sqlite3
|
100
100
|
requirement: !ruby/object:Gem::Requirement
|
101
101
|
requirements:
|
102
|
-
- - "
|
102
|
+
- - "~>"
|
103
103
|
- !ruby/object:Gem::Version
|
104
104
|
version: '0'
|
105
105
|
type: :development
|
106
106
|
prerelease: false
|
107
107
|
version_requirements: !ruby/object:Gem::Requirement
|
108
108
|
requirements:
|
109
|
-
- - "
|
109
|
+
- - "~>"
|
110
110
|
- !ruby/object:Gem::Version
|
111
111
|
version: '0'
|
112
112
|
- !ruby/object:Gem::Dependency
|
113
113
|
name: ruby_event_store
|
114
114
|
requirement: !ruby/object:Gem::Requirement
|
115
115
|
requirements:
|
116
|
-
- - "
|
116
|
+
- - "~>"
|
117
117
|
- !ruby/object:Gem::Version
|
118
|
-
version: 0.
|
118
|
+
version: '0.3'
|
119
119
|
type: :runtime
|
120
120
|
prerelease: false
|
121
121
|
version_requirements: !ruby/object:Gem::Requirement
|
122
122
|
requirements:
|
123
|
-
- - "
|
123
|
+
- - "~>"
|
124
124
|
- !ruby/object:Gem::Version
|
125
|
-
version: 0.
|
125
|
+
version: '0.3'
|
126
126
|
- !ruby/object:Gem::Dependency
|
127
127
|
name: aggregate_root
|
128
128
|
requirement: !ruby/object:Gem::Requirement
|
129
129
|
requirements:
|
130
|
-
- - "
|
130
|
+
- - "~>"
|
131
131
|
- !ruby/object:Gem::Version
|
132
|
-
version: 0
|
132
|
+
version: '0'
|
133
133
|
type: :runtime
|
134
134
|
prerelease: false
|
135
135
|
version_requirements: !ruby/object:Gem::Requirement
|
136
136
|
requirements:
|
137
|
-
- - "
|
137
|
+
- - "~>"
|
138
138
|
- !ruby/object:Gem::Version
|
139
|
-
version: 0
|
139
|
+
version: '0'
|
140
140
|
- !ruby/object:Gem::Dependency
|
141
141
|
name: activesupport
|
142
142
|
requirement: !ruby/object:Gem::Requirement
|
@@ -192,7 +192,7 @@ files:
|
|
192
192
|
- lib/rails_event_store/repositories/event_repository.rb
|
193
193
|
- lib/rails_event_store/version.rb
|
194
194
|
- rails_event_store.gemspec
|
195
|
-
homepage:
|
195
|
+
homepage: https://github.com/arkency/rails_event_store
|
196
196
|
licenses: []
|
197
197
|
metadata: {}
|
198
198
|
post_install_message:
|
@@ -211,8 +211,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
211
211
|
version: '0'
|
212
212
|
requirements: []
|
213
213
|
rubyforge_project:
|
214
|
-
rubygems_version: 2.2.
|
214
|
+
rubygems_version: 2.2.3
|
215
215
|
signing_key:
|
216
216
|
specification_version: 4
|
217
|
-
summary:
|
217
|
+
summary: Event Store in Ruby
|
218
218
|
test_files: []
|