event_store_client 0.2.7 → 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (80) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +27 -37
  3. data/lib/event_store_client.rb +0 -2
  4. data/lib/event_store_client/adapters/grpc.rb +21 -0
  5. data/lib/event_store_client/adapters/grpc/Protos/cluster.proto +149 -0
  6. data/lib/event_store_client/adapters/grpc/Protos/gossip.proto +44 -0
  7. data/lib/event_store_client/adapters/grpc/Protos/operations.proto +45 -0
  8. data/lib/event_store_client/adapters/grpc/Protos/persistent.proto +180 -0
  9. data/lib/event_store_client/adapters/grpc/Protos/projections.proto +174 -0
  10. data/lib/event_store_client/adapters/grpc/Protos/shared.proto +22 -0
  11. data/lib/event_store_client/adapters/grpc/Protos/streams.proto +242 -0
  12. data/lib/event_store_client/adapters/grpc/Protos/users.proto +119 -0
  13. data/lib/event_store_client/adapters/grpc/client.rb +119 -0
  14. data/lib/event_store_client/adapters/grpc/command_registrar.rb +32 -0
  15. data/lib/event_store_client/adapters/grpc/commands/command.rb +43 -0
  16. data/lib/event_store_client/adapters/grpc/commands/persistent_subscriptions/create.rb +46 -0
  17. data/lib/event_store_client/adapters/grpc/commands/persistent_subscriptions/delete.rb +34 -0
  18. data/lib/event_store_client/adapters/grpc/commands/persistent_subscriptions/read.rb +65 -0
  19. data/lib/event_store_client/adapters/grpc/commands/persistent_subscriptions/settings_schema.rb +38 -0
  20. data/lib/event_store_client/adapters/grpc/commands/persistent_subscriptions/update.rb +48 -0
  21. data/lib/event_store_client/adapters/grpc/commands/projections/create.rb +45 -0
  22. data/lib/event_store_client/adapters/grpc/commands/projections/delete.rb +34 -0
  23. data/lib/event_store_client/adapters/grpc/commands/projections/update.rb +42 -0
  24. data/lib/event_store_client/adapters/grpc/commands/streams/append.rb +61 -0
  25. data/lib/event_store_client/adapters/grpc/commands/streams/delete.rb +35 -0
  26. data/lib/event_store_client/adapters/grpc/commands/streams/link_to.rb +61 -0
  27. data/lib/event_store_client/adapters/grpc/commands/streams/read.rb +80 -0
  28. data/lib/event_store_client/adapters/grpc/commands/streams/read_all.rb +43 -0
  29. data/lib/event_store_client/adapters/grpc/commands/streams/tombstone.rb +35 -0
  30. data/lib/event_store_client/adapters/grpc/connection.rb +50 -0
  31. data/lib/event_store_client/adapters/grpc/generated/cluster_pb.rb +140 -0
  32. data/lib/event_store_client/adapters/grpc/generated/cluster_services_pb.rb +46 -0
  33. data/lib/event_store_client/adapters/grpc/generated/gossip_pb.rb +53 -0
  34. data/lib/event_store_client/adapters/grpc/generated/gossip_services_pb.rb +26 -0
  35. data/lib/event_store_client/adapters/grpc/generated/operations_pb.rb +49 -0
  36. data/lib/event_store_client/adapters/grpc/generated/operations_services_pb.rb +31 -0
  37. data/lib/event_store_client/adapters/grpc/generated/persistent_pb.rb +213 -0
  38. data/lib/event_store_client/adapters/grpc/generated/persistent_services_pb.rb +29 -0
  39. data/lib/event_store_client/adapters/grpc/generated/projections_pb.rb +193 -0
  40. data/lib/event_store_client/adapters/grpc/generated/projections_services_pb.rb +34 -0
  41. data/lib/event_store_client/adapters/grpc/generated/shared_pb.rb +35 -0
  42. data/lib/event_store_client/adapters/grpc/generated/streams_pb.rb +283 -0
  43. data/lib/event_store_client/adapters/grpc/generated/streams_services_pb.rb +29 -0
  44. data/lib/event_store_client/adapters/grpc/generated/users_pb.rb +126 -0
  45. data/lib/event_store_client/adapters/grpc/generated/users_services_pb.rb +33 -0
  46. data/lib/event_store_client/adapters/http.rb +16 -0
  47. data/lib/event_store_client/adapters/http/README.md +16 -0
  48. data/lib/event_store_client/adapters/http/client.rb +160 -0
  49. data/lib/event_store_client/adapters/http/commands/command.rb +27 -0
  50. data/lib/event_store_client/adapters/http/commands/persistent_subscriptions/ack.rb +15 -0
  51. data/lib/event_store_client/adapters/http/commands/persistent_subscriptions/create.rb +31 -0
  52. data/lib/event_store_client/adapters/http/commands/persistent_subscriptions/read.rb +57 -0
  53. data/lib/event_store_client/adapters/http/commands/projections/create.rb +30 -0
  54. data/lib/event_store_client/adapters/http/commands/streams/append.rb +49 -0
  55. data/lib/event_store_client/adapters/http/commands/streams/delete.rb +16 -0
  56. data/lib/event_store_client/adapters/http/commands/streams/link_to.rb +49 -0
  57. data/lib/event_store_client/adapters/http/commands/streams/read.rb +53 -0
  58. data/lib/event_store_client/adapters/http/commands/streams/tombstone.rb +17 -0
  59. data/lib/event_store_client/adapters/http/connection.rb +46 -0
  60. data/lib/event_store_client/adapters/http/request_method.rb +28 -0
  61. data/lib/event_store_client/adapters/in_memory.rb +142 -0
  62. data/lib/event_store_client/broker.rb +16 -5
  63. data/lib/event_store_client/client.rb +18 -72
  64. data/lib/event_store_client/configuration.rb +28 -14
  65. data/lib/event_store_client/data_decryptor.rb +13 -8
  66. data/lib/event_store_client/data_encryptor.rb +7 -6
  67. data/lib/event_store_client/deserialized_event.rb +10 -1
  68. data/lib/event_store_client/event.rb +2 -2
  69. data/lib/event_store_client/mapper/default.rb +0 -1
  70. data/lib/event_store_client/serializer/json.rb +2 -0
  71. data/lib/event_store_client/subscriptions.rb +4 -13
  72. data/lib/event_store_client/types.rb +3 -1
  73. data/lib/event_store_client/value_objects/read_direction.rb +43 -0
  74. data/lib/event_store_client/version.rb +1 -1
  75. metadata +97 -15
  76. data/lib/event_store_client/store_adapter.rb +0 -10
  77. data/lib/event_store_client/store_adapter/api/client.rb +0 -224
  78. data/lib/event_store_client/store_adapter/api/connection.rb +0 -43
  79. data/lib/event_store_client/store_adapter/api/request_method.rb +0 -30
  80. data/lib/event_store_client/store_adapter/in_memory.rb +0 -160
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'grpc'
4
+ require 'event_store_client/adapters/grpc/generated/projections_pb.rb'
5
+ require 'event_store_client/adapters/grpc/generated/projections_services_pb.rb'
6
+
7
+ require 'event_store_client/adapters/grpc/commands/command'
8
+
9
+ module EventStoreClient
10
+ module GRPC
11
+ module Commands
12
+ module Projections
13
+ class Delete < Command
14
+ use_request EventStore::Client::Projections::DeleteReq
15
+ use_service EventStore::Client::Projections::Projections::Stub
16
+
17
+ def call(name, options: {})
18
+ opts = {
19
+ name: name,
20
+ delete_emitted_streams: true,
21
+ delete_state_stream: true,
22
+ delete_checkpoint_stream: true
23
+ }.merge(options)
24
+
25
+ service.delete(request.new(options: opts), metadata: metadata)
26
+ Success()
27
+ rescue ::GRPC::Unknown => e
28
+ Failure(:not_found) if e.message.include?('OperationFailed')
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'grpc'
4
+ require 'event_store_client/adapters/grpc/generated/projections_pb.rb'
5
+ require 'event_store_client/adapters/grpc/generated/projections_services_pb.rb'
6
+
7
+ require 'event_store_client/adapters/grpc/commands/command'
8
+
9
+ module EventStoreClient
10
+ module GRPC
11
+ module Commands
12
+ module Projections
13
+ class Update < Command
14
+ use_request EventStore::Client::Projections::UpdateReq
15
+ use_service EventStore::Client::Projections::Projections::Stub
16
+
17
+ def call(name, streams)
18
+ data = <<~STRING
19
+ fromStreams(#{streams})
20
+ .when({
21
+ $any: function(s,e) {
22
+ linkTo("#{name}", e)
23
+ }
24
+ })
25
+ STRING
26
+
27
+ options =
28
+ {
29
+ query: data,
30
+ name: name,
31
+ emit_enabled: true
32
+ }
33
+ service.update(request.new(options: options), metadata: metadata)
34
+ Success()
35
+ rescue ::GRPC::AlreadyExists
36
+ Failure(:conflict)
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'grpc'
4
+ require 'event_store_client/adapters/grpc/generated/streams_pb.rb'
5
+ require 'event_store_client/adapters/grpc/generated/streams_services_pb.rb'
6
+
7
+ require 'event_store_client/adapters/grpc/commands/command'
8
+
9
+ module EventStoreClient
10
+ module GRPC
11
+ module Commands
12
+ module Streams
13
+ class Append < Command
14
+ use_request EventStore::Client::Streams::AppendReq
15
+ use_service EventStore::Client::Streams::Streams::Stub
16
+
17
+ # @api private
18
+ # TODO: Add support to verify the expected version
19
+ def call(stream, events, options: {}) # rubocop:disable Lint/UnusedMethodArgument,Metrics/LineLength
20
+ serialized_events = events.map { |event| config.mapper.serialize(event) }
21
+
22
+ serialized_events.each do |event|
23
+ event_metadata = JSON.parse(event.metadata)
24
+ custom_metadata = {
25
+ "type": event.type,
26
+ "created_at": Time.now,
27
+ 'content-type': event_metadata['content-type']
28
+ }
29
+ custom_metadata['encryption'] = event_metadata['encryption'] unless event_metadata['encryption'].nil?
30
+ event_metadata = event_metadata.select { |k| ['type', 'content-type', 'created_at'].include?(k) }
31
+
32
+
33
+ payload = [
34
+ request.new(
35
+ options: {
36
+ stream_identifier: {
37
+ streamName: stream
38
+ },
39
+ any: {}
40
+ }
41
+ ),
42
+ request.new(
43
+ proposed_message: {
44
+ id: {
45
+ string: SecureRandom.uuid
46
+ },
47
+ data: event.data,
48
+ custom_metadata: JSON.generate(custom_metadata),
49
+ metadata: event_metadata
50
+ }
51
+ )
52
+ ]
53
+ service.append(payload, metadata: metadata)
54
+ end
55
+ Success()
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'grpc'
4
+ require 'event_store_client/adapters/grpc/generated/projections_pb.rb'
5
+ require 'event_store_client/adapters/grpc/generated/projections_services_pb.rb'
6
+
7
+ require 'event_store_client/adapters/grpc/commands/command'
8
+
9
+ module EventStoreClient
10
+ module GRPC
11
+ module Commands
12
+ module Streams
13
+ class Delete < Command
14
+ use_request EventStore::Client::Streams::DeleteReq
15
+ use_service EventStore::Client::Streams::Streams::Stub
16
+
17
+ def call(name, options: {}) # rubocop:disable Lint/UnusedMethodArgument
18
+ opts =
19
+ {
20
+ stream_identifier: {
21
+ streamName: name
22
+ },
23
+ any: {}
24
+ }
25
+
26
+ service.delete(request.new(options: opts), metadata: metadata)
27
+ Success()
28
+ rescue ::GRPC::FailedPrecondition
29
+ Failure(:not_found)
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'grpc'
4
+ require 'event_store_client/adapters/grpc/generated/streams_pb.rb'
5
+ require 'event_store_client/adapters/grpc/generated/streams_services_pb.rb'
6
+
7
+ require 'event_store_client/adapters/grpc/commands/command'
8
+
9
+ module EventStoreClient
10
+ module GRPC
11
+ module Commands
12
+ module Streams
13
+ class LinkTo < Command
14
+ include Configuration
15
+
16
+ use_request EventStore::Client::Streams::AppendReq
17
+ use_service EventStore::Client::Streams::Streams::Stub
18
+
19
+ def call(stream_name, events, options: {})
20
+ events.each_with_index do |event, index|
21
+ custom_metadata = JSON.generate(
22
+ "type": '$>',
23
+ "created_at": Time.now,
24
+ "encryption": event.metadata['encryption'] || ''
25
+ )
26
+
27
+ event_metadata = event.metadata.tap do |h|
28
+ h['type'] = '$>'
29
+ h['content-type'] = 'application/json'
30
+ h.delete('encryption')
31
+ end
32
+
33
+ payload = [
34
+ request.new(
35
+ options: {
36
+ stream_identifier: {
37
+ streamName: stream_name
38
+ },
39
+ any: {}
40
+ }
41
+ ),
42
+ request.new(
43
+ proposed_message: {
44
+ id: {
45
+ string: event.id
46
+ },
47
+ data: event.title,
48
+ custom_metadata: custom_metadata,
49
+ metadata: event_metadata
50
+ }
51
+ )
52
+ ]
53
+ service.append(payload, metadata: metadata)
54
+ end
55
+ Success()
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,80 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'grpc'
4
+ require 'event_store_client/adapters/grpc/generated/streams_pb.rb'
5
+ require 'event_store_client/adapters/grpc/generated/streams_services_pb.rb'
6
+
7
+ require 'event_store_client/configuration'
8
+ require 'event_store_client/adapters/grpc/commands/command'
9
+
10
+ module EventStoreClient
11
+ module GRPC
12
+ module Commands
13
+ module Streams
14
+ class Read < Command
15
+ include Configuration
16
+
17
+ use_request EventStore::Client::Streams::ReadReq
18
+ use_service EventStore::Client::Streams::Streams::Stub
19
+
20
+ StreamNotFound = Class.new(StandardError)
21
+
22
+ def call(name, options: {})
23
+ direction =
24
+ EventStoreClient::ReadDirection.new(options[:direction] || 'forwards').to_sym
25
+ opts = {
26
+ stream: {
27
+ stream_identifier: {
28
+ streamName: name
29
+ }
30
+ },
31
+ read_direction: direction,
32
+ resolve_links: options[:resolve_links] || true,
33
+ count: options[:count] || config.per_page,
34
+ uuid_option: {
35
+ string: {}
36
+ },
37
+ no_filter: {}
38
+ }
39
+ options[:start] ||= 0
40
+ if options[:start].zero?
41
+ opts[:stream][:start] = {}
42
+ else
43
+ opts[:stream][:revision] = options[:start]
44
+ end
45
+
46
+ events = service.read(request.new(options: opts), metadata: metadata).map do |res|
47
+ raise StreamNotFound if res.stream_not_found
48
+
49
+ deserialize_event(res.event.event)
50
+ end
51
+ Success(events)
52
+ rescue StreamNotFound
53
+ Failure(:not_found)
54
+ end
55
+
56
+ private
57
+
58
+ def deserialize_event(entry)
59
+ data = (entry.data.nil? || entry.data.empty?) ? '{}' : entry.data
60
+
61
+ metadata =
62
+ JSON.parse(entry.custom_metadata || '{}').merge(
63
+ entry.metadata.to_h || {}
64
+ ).to_json
65
+
66
+ event = EventStoreClient::Event.new(
67
+ id: entry.id.string,
68
+ title: "#{entry.stream_revision}@#{entry.stream_identifier.streamName}",
69
+ type: entry.metadata['type'],
70
+ data: data,
71
+ metadata: metadata
72
+ )
73
+
74
+ config.mapper.deserialize(event)
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'grpc'
4
+ require 'event_store_client/adapters/grpc/generated/projections_pb.rb'
5
+ require 'event_store_client/adapters/grpc/generated/projections_services_pb.rb'
6
+
7
+ require 'event_store_client/configuration'
8
+ require 'event_store_client/adapters/grpc/commands/command'
9
+ require 'event_store_client/adapters/grpc/commands/streams/read'
10
+
11
+ module EventStoreClient
12
+ module GRPC
13
+ module Commands
14
+ module Streams
15
+ class ReadAll < Command
16
+ include Configuration
17
+
18
+ use_request EventStore::Client::Streams::ReadReq
19
+ use_service EventStore::Client::Streams::Streams::Stub
20
+
21
+ def call(stream_name, options: {})
22
+ start ||= options[:start] || 0
23
+ count ||= options[:count] || 20
24
+ events = []
25
+
26
+ loop do
27
+ res = Read.new.call(
28
+ stream_name, options: options.merge(start: start, count: count)
29
+ )
30
+ break if res.failure?
31
+ break if (entries = res.value!).empty?
32
+
33
+ events += entries
34
+ start += count
35
+ end
36
+
37
+ Success(events)
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'grpc'
4
+ require 'event_store_client/adapters/grpc/generated/projections_pb.rb'
5
+ require 'event_store_client/adapters/grpc/generated/projections_services_pb.rb'
6
+
7
+ require 'event_store_client/adapters/grpc/commands/command'
8
+
9
+ module EventStoreClient
10
+ module GRPC
11
+ module Commands
12
+ module Streams
13
+ class Tombstone < Command
14
+ use_request EventStore::Client::Streams::TombstoneReq
15
+ use_service EventStore::Client::Streams::Streams::Stub
16
+
17
+ def call(name, options: {})
18
+ opts =
19
+ {
20
+ stream_identifier: {
21
+ streamName: name
22
+ },
23
+ any: {}
24
+ }
25
+
26
+ service.tombstone(request.new(options: opts), metadata: metadata)
27
+ Success()
28
+ rescue ::GRPC::FailedPrecondition
29
+ Failure(:not_found)
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'grpc'
4
+ require 'base64'
5
+ require 'net/http'
6
+
7
+ module EventStoreClient
8
+ module GRPC
9
+ class Connection
10
+ include Configuration
11
+
12
+ # Initializes the proper stub with the necessary credentials
13
+ # to create working gRPC connection - Refer to generated grpc files
14
+ # @return [Stub] Instance of a given `Stub` klass
15
+ #
16
+ def call(stub_klass, options: {})
17
+ credentials =
18
+ options[:credentials] ||
19
+ Base64.encode64("#{config.eventstore_user}:#{config.eventstore_password}")
20
+ stub_klass.new(
21
+ "#{config.eventstore_url.host}:#{config.eventstore_url.port}",
22
+ channel_credentials,
23
+ channel_args: { 'authorization' => "Basic #{credentials.delete("\n")}" }
24
+ )
25
+ end
26
+
27
+ private
28
+
29
+ attr_reader :cert
30
+
31
+ def initialize
32
+ @cert =
33
+ Net::HTTP.start(
34
+ config.eventstore_url.host, config.eventstore_url.port,
35
+ use_ssl: true,
36
+ verify_mode: verify_ssl,
37
+ &:peer_cert
38
+ )
39
+ end
40
+
41
+ def channel_credentials
42
+ ::GRPC::Core::ChannelCredentials.new(cert.to_s)
43
+ end
44
+
45
+ def verify_ssl
46
+ config.verify_ssl || OpenSSL::SSL::VERIFY_NONE
47
+ end
48
+ end
49
+ end
50
+ end