sequent 3.1.2 → 3.2.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
  SHA256:
3
- metadata.gz: 8c1a651a4d982304df2f61362bcefc1736a1eecb6a0693f0da66e95319199311
4
- data.tar.gz: '0168575dabe5b65335e56b3feb815c3e8c8abb7b413ae94c271f3480e58fc327'
3
+ metadata.gz: 19b4f1e046b49ceea7b79a7984ba2329905cdec74dc0ab31d02820752f051585
4
+ data.tar.gz: 95cd39748cbe7b474c06f8556cd63e2cf548909d2d7e3aee23b794385fdfd2ca
5
5
  SHA512:
6
- metadata.gz: 39d2faaf084d719c881b9901977bc96b38d8054fd24061d6104ee9036a5e9971565c60cde02f9b874efe722c79d847d544639a08eea6a693d7d027ba8ac49348
7
- data.tar.gz: 3a4d7fdd19a09aad5e603d04c571d1c84ff53a63cfaf5571259dc250a4a5b08e7872a98e2ea367a27d719131d49d102da4dfab621e33c8209c88c24b173409e0
6
+ metadata.gz: d8738ec3b038f058a873db7b8751706cf05112d376fa5053dcb8196c9f96f96a05dc1f61582c98f36415d40899ec05facb572c0f39d03ede1c303b28449614b5
7
+ data.tar.gz: de81d600667fe597142ac81161b806a5eefbc92bc32fea8a3bb7f4f252bf5289d51fc1f55dbdf04ee62ac0eac37cda7175e0fc68ec7eddbe52d658a885b339a9
@@ -29,10 +29,14 @@ CREATE INDEX snapshot_events ON event_records (aggregate_id, sequence_number DES
29
29
  t.string "user_id"
30
30
  t.string "aggregate_id"
31
31
  t.string "command_type", :null => false
32
+ t.string "event_aggregate_id"
33
+ t.integer "event_sequence_number"
32
34
  t.text "command_json", :null => false
33
35
  t.datetime "created_at", :null => false
34
36
  end
35
37
 
38
+ add_index "command_records", ["event_aggregate_id", 'event_sequence_number'], :name => "index_command_records_on_event"
39
+
36
40
  create_table "stream_records", :force => true do |t|
37
41
  t.datetime "created_at", :null => false
38
42
  t.string "aggregate_type", :null => false
@@ -71,7 +71,7 @@ module Sequent
71
71
  # command = Command.from_params(params)
72
72
  #
73
73
  class Command < BaseCommand
74
- attrs aggregate_id: String, user_id: String
74
+ attrs aggregate_id: String, user_id: String, event_aggregate_id: String, event_sequence_number: Integer
75
75
 
76
76
  def initialize(args = {})
77
77
  raise ArgumentError, "Missing aggregate_id" if args[:aggregate_id].nil?
@@ -17,6 +17,8 @@ module Sequent
17
17
  self.user_id = command.user_id if command.respond_to? :user_id
18
18
  self.command_type = command.class.name
19
19
  self.command_json = Sequent::Core::Oj.dump(command.attributes)
20
+ self.event_aggregate_id = command.event_aggregate_id if command.respond_to? :event_aggregate_id
21
+ self.event_sequence_number = command.event_sequence_number if command.respond_to? :event_sequence_number
20
22
  end
21
23
  end
22
24
 
@@ -26,8 +28,26 @@ module Sequent
26
28
 
27
29
  self.table_name = "command_records"
28
30
 
31
+ has_many :event_records
32
+
29
33
  validates_presence_of :command_type, :command_json
30
34
 
35
+ def parent
36
+ EventRecord.find_by(aggregate_id: event_aggregate_id, sequence_number: event_sequence_number)
37
+ end
38
+
39
+ def children
40
+ event_records
41
+ end
42
+
43
+ def origin
44
+ parent.present? ? find_origin(parent) : self
45
+ end
46
+
47
+ def find_origin(record)
48
+ return find_origin(record.parent) if record.parent.present?
49
+ record
50
+ end
31
51
  end
32
52
  end
33
53
  end
@@ -1,4 +1,5 @@
1
1
  require_relative 'transactions/no_transactions'
2
+ require_relative 'current_event'
2
3
 
3
4
  module Sequent
4
5
  module Core
@@ -21,6 +22,12 @@ module Sequent
21
22
  # * If the command is valid all +command_handlers+ that +handles_message?+ is invoked
22
23
  # * The +repository+ commits the command and all uncommitted_events resulting from the command
23
24
  def execute_commands(*commands)
25
+ commands.each do |command|
26
+ if command.respond_to?(:event_aggregate_id) && CurrentEvent.current
27
+ command.event_aggregate_id = CurrentEvent.current.aggregate_id
28
+ command.event_sequence_number = CurrentEvent.current.sequence_number
29
+ end
30
+ end
24
31
  commands.each { |command| command_queue.push(command) }
25
32
  process_commands
26
33
  end
@@ -0,0 +1,13 @@
1
+ module Sequent
2
+ module Core
3
+ class CurrentEvent
4
+ def self.current=(event)
5
+ Thread.current[:sequent_current_event] = event
6
+ end
7
+
8
+ def self.current
9
+ Thread.current[:sequent_current_event]
10
+ end
11
+ end
12
+ end
13
+ end
@@ -82,7 +82,23 @@ module Sequent
82
82
 
83
83
  validates_presence_of :aggregate_id, :sequence_number, :event_type, :event_json, :stream_record, :command_record
84
84
  validates_numericality_of :sequence_number, only_integer: true, greater_than: 0
85
- end
86
85
 
86
+ def parent
87
+ command_record
88
+ end
89
+
90
+ def children
91
+ CommandRecord.where(event_aggregate_id: aggregate_id, event_sequence_number: sequence_number)
92
+ end
93
+
94
+ def origin
95
+ parent.present? ? find_origin(parent) : self
96
+ end
97
+
98
+ def find_origin(record)
99
+ return find_origin(record.parent) if record.parent.present?
100
+ record
101
+ end
102
+ end
87
103
  end
88
104
  end
@@ -1,10 +1,24 @@
1
1
  require_relative 'helpers/message_handler'
2
+ require_relative 'current_event'
2
3
 
3
4
  module Sequent
4
5
  module Core
5
6
  class Workflow
6
7
  include Helpers::MessageHandler
7
8
 
9
+ def self.on(*message_classes, &block)
10
+ decorated_block = ->(event) do
11
+ begin
12
+ old_event = CurrentEvent.current
13
+ CurrentEvent.current = event
14
+ self.instance_exec(event, &block)
15
+ ensure
16
+ CurrentEvent.current = old_event
17
+ end
18
+ end
19
+ super(*message_classes, &decorated_block)
20
+ end
21
+
8
22
  def execute_commands(*commands)
9
23
  Sequent.configuration.command_service.execute_commands(*commands)
10
24
  end
@@ -1,3 +1,3 @@
1
1
  module Sequent
2
- VERSION = '3.1.2'
2
+ VERSION = '3.2.0'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sequent
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.1.2
4
+ version: 3.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lars Vonk
@@ -12,7 +12,7 @@ authors:
12
12
  autorequire:
13
13
  bindir: bin
14
14
  cert_chain: []
15
- date: 2019-04-01 00:00:00.000000000 Z
15
+ date: 2019-05-03 00:00:00.000000000 Z
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency
18
18
  name: activerecord
@@ -274,6 +274,7 @@ files:
274
274
  - lib/sequent/core/command_record.rb
275
275
  - lib/sequent/core/command_service.rb
276
276
  - lib/sequent/core/core.rb
277
+ - lib/sequent/core/current_event.rb
277
278
  - lib/sequent/core/event.rb
278
279
  - lib/sequent/core/event_publisher.rb
279
280
  - lib/sequent/core/event_record.rb