pg_eventstore 0.9.0 → 0.10.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +14 -0
- data/db/migrations/{3_create_events.sql → 1_create_events.sql} +11 -14
- data/db/migrations/5_partitions.sql +16 -0
- data/docs/configuration.md +1 -1
- data/docs/events_and_streams.md +5 -3
- data/docs/how_it_works.md +80 -0
- data/lib/pg_eventstore/client.rb +11 -7
- data/lib/pg_eventstore/commands/append.rb +17 -8
- data/lib/pg_eventstore/commands/link_to.rb +3 -3
- data/lib/pg_eventstore/commands/read.rb +1 -1
- data/lib/pg_eventstore/errors.rb +17 -6
- data/lib/pg_eventstore/event.rb +5 -1
- data/lib/pg_eventstore/event_deserializer.rb +4 -1
- data/lib/pg_eventstore/queries/event_queries.rb +65 -26
- data/lib/pg_eventstore/queries/links_resolver.rb +31 -0
- data/lib/pg_eventstore/queries/partition_queries.rb +184 -0
- data/lib/pg_eventstore/queries/subscription_queries.rb +12 -15
- data/lib/pg_eventstore/queries/transaction_queries.rb +13 -0
- data/lib/pg_eventstore/queries.rb +5 -6
- data/lib/pg_eventstore/query_builders/events_filtering_query.rb +10 -31
- data/lib/pg_eventstore/rspec/test_helpers.rb +16 -1
- data/lib/pg_eventstore/sql_builder.rb +34 -4
- data/lib/pg_eventstore/stream.rb +3 -8
- data/lib/pg_eventstore/subscriptions/events_processor.rb +10 -2
- data/lib/pg_eventstore/subscriptions/subscription.rb +1 -0
- data/lib/pg_eventstore/subscriptions/subscription_feeder.rb +1 -1
- data/lib/pg_eventstore/subscriptions/subscription_runners_feeder.rb +2 -3
- data/lib/pg_eventstore/version.rb +1 -1
- metadata +10 -11
- data/db/migrations/1_create_streams.sql +0 -13
- data/db/migrations/2_create_event_types.sql +0 -10
- data/lib/pg_eventstore/queries/event_type_queries.rb +0 -74
- data/lib/pg_eventstore/queries/preloader.rb +0 -37
- data/lib/pg_eventstore/queries/stream_queries.rb +0 -77
- /data/db/migrations/{4_create_subscriptions.sql → 2_create_subscriptions.sql} +0 -0
- /data/db/migrations/{5_create_subscription_commands.sql → 3_create_subscription_commands.sql} +0 -0
- /data/db/migrations/{6_create_subscriptions_set_commands.sql → 4_create_subscriptions_set_commands.sql} +0 -0
@@ -1,74 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module PgEventstore
|
4
|
-
# @!visibility private
|
5
|
-
class EventTypeQueries
|
6
|
-
attr_reader :connection
|
7
|
-
private :connection
|
8
|
-
|
9
|
-
# @param connection [PgEventstore::Connection]
|
10
|
-
def initialize(connection)
|
11
|
-
@connection = connection
|
12
|
-
end
|
13
|
-
|
14
|
-
# @param type [String]
|
15
|
-
# @return [Integer] event type's id
|
16
|
-
def find_or_create_type(type)
|
17
|
-
find_type(type) || create_type(type)
|
18
|
-
end
|
19
|
-
|
20
|
-
# @param type [String]
|
21
|
-
# @return [Integer, nil] event type's id
|
22
|
-
def find_type(type)
|
23
|
-
connection.with do |conn|
|
24
|
-
conn.exec_params('SELECT id FROM event_types WHERE type = $1', [type])
|
25
|
-
end.to_a.dig(0, 'id')
|
26
|
-
end
|
27
|
-
|
28
|
-
# @param type [String]
|
29
|
-
# @return [Integer] event type's id
|
30
|
-
def create_type(type)
|
31
|
-
connection.with do |conn|
|
32
|
-
conn.exec_params('INSERT INTO event_types (type) VALUES ($1) RETURNING id', [type])
|
33
|
-
end.to_a.dig(0, 'id')
|
34
|
-
end
|
35
|
-
|
36
|
-
# @param ids [Array<Integer>]
|
37
|
-
# @return [Array<Hash>]
|
38
|
-
def find_by_ids(ids)
|
39
|
-
return [] if ids.empty?
|
40
|
-
|
41
|
-
builder = SQLBuilder.new.from('event_types').where('id = ANY(?)', ids.uniq)
|
42
|
-
connection.with do |conn|
|
43
|
-
conn.exec_params(*builder.to_exec_params)
|
44
|
-
end.to_a
|
45
|
-
end
|
46
|
-
|
47
|
-
# @param types [Array<String>]
|
48
|
-
# @return [Array<Integer, nil>]
|
49
|
-
def find_event_types(types)
|
50
|
-
connection.with do |conn|
|
51
|
-
conn.exec_params(<<~SQL, [types])
|
52
|
-
SELECT event_types.id, types.type
|
53
|
-
FROM event_types
|
54
|
-
RIGHT JOIN (
|
55
|
-
SELECT unnest($1::varchar[]) as type
|
56
|
-
) types ON types.type = event_types.type
|
57
|
-
SQL
|
58
|
-
end.to_a.map { |attrs| attrs['id'] }
|
59
|
-
end
|
60
|
-
|
61
|
-
# Replaces filter by event type strings with filter by event type ids
|
62
|
-
# @param options [Hash]
|
63
|
-
# @return [Hash]
|
64
|
-
def include_event_types_ids(options)
|
65
|
-
options in { filter: { event_types: Array => event_types } }
|
66
|
-
return options unless event_types
|
67
|
-
|
68
|
-
options = Utils.deep_dup(options)
|
69
|
-
options[:filter][:event_type_ids] = find_event_types(event_types).uniq
|
70
|
-
options[:filter].delete(:event_types)
|
71
|
-
options
|
72
|
-
end
|
73
|
-
end
|
74
|
-
end
|
@@ -1,37 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module PgEventstore
|
4
|
-
# @!visibility private
|
5
|
-
class Preloader
|
6
|
-
attr_reader :connection
|
7
|
-
private :connection
|
8
|
-
|
9
|
-
# @param connection [PgEventstore::Connection]
|
10
|
-
def initialize(connection)
|
11
|
-
@connection = connection
|
12
|
-
end
|
13
|
-
|
14
|
-
# @param raw_events [Array<Hash>]
|
15
|
-
# @return [Array<Hash>]
|
16
|
-
def preload_related_objects(raw_events)
|
17
|
-
streams = stream_queries.find_by_ids(raw_events.map { _1['stream_id'] }).to_h { [_1['id'], _1] }
|
18
|
-
types = event_type_queries.find_by_ids(raw_events.map { _1['event_type_id'] }).to_h { [_1['id'], _1] }
|
19
|
-
raw_events.each do |event|
|
20
|
-
event['stream'] = streams[event['stream_id']]
|
21
|
-
event['type'] = types[event['event_type_id']]['type']
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
private
|
26
|
-
|
27
|
-
# @return [PgEventstore::EventTypeQueries]
|
28
|
-
def event_type_queries
|
29
|
-
EventTypeQueries.new(connection)
|
30
|
-
end
|
31
|
-
|
32
|
-
# @return [PgEventstore::StreamQueries]
|
33
|
-
def stream_queries
|
34
|
-
StreamQueries.new(connection)
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
@@ -1,77 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module PgEventstore
|
4
|
-
# @!visibility private
|
5
|
-
class StreamQueries
|
6
|
-
attr_reader :connection
|
7
|
-
private :connection
|
8
|
-
|
9
|
-
# @param connection [PgEventstore::Connection]
|
10
|
-
def initialize(connection)
|
11
|
-
@connection = connection
|
12
|
-
end
|
13
|
-
|
14
|
-
# Finds a stream in the database by the given Stream object
|
15
|
-
# @param stream [PgEventstore::Stream]
|
16
|
-
# @return [PgEventstore::Stream, nil] persisted stream
|
17
|
-
def find_stream(stream)
|
18
|
-
builder =
|
19
|
-
SQLBuilder.new.
|
20
|
-
from('streams').
|
21
|
-
where('streams.context = ? AND streams.stream_name = ? AND streams.stream_id = ?', *stream.to_a).
|
22
|
-
limit(1)
|
23
|
-
pg_result = connection.with do |conn|
|
24
|
-
conn.exec_params(*builder.to_exec_params)
|
25
|
-
end
|
26
|
-
deserialize(pg_result) if pg_result.ntuples == 1
|
27
|
-
end
|
28
|
-
|
29
|
-
# @param ids [Array<Integer>]
|
30
|
-
# @return [Array<Hash>]
|
31
|
-
def find_by_ids(ids)
|
32
|
-
return [] if ids.empty?
|
33
|
-
|
34
|
-
builder = SQLBuilder.new.from('streams').where('id = ANY(?)', ids.uniq.sort)
|
35
|
-
connection.with do |conn|
|
36
|
-
conn.exec_params(*builder.to_exec_params)
|
37
|
-
end.to_a
|
38
|
-
end
|
39
|
-
|
40
|
-
# @param stream [PgEventstore::Stream]
|
41
|
-
# @return [PgEventstore::RawStream] persisted stream
|
42
|
-
def create_stream(stream)
|
43
|
-
create_sql = <<~SQL
|
44
|
-
INSERT INTO streams (context, stream_name, stream_id) VALUES ($1, $2, $3) RETURNING *
|
45
|
-
SQL
|
46
|
-
pg_result = connection.with do |conn|
|
47
|
-
conn.exec_params(create_sql, stream.to_a)
|
48
|
-
end
|
49
|
-
deserialize(pg_result)
|
50
|
-
end
|
51
|
-
|
52
|
-
# @return [PgEventstore::Stream] persisted stream
|
53
|
-
def find_or_create_stream(stream)
|
54
|
-
find_stream(stream) || create_stream(stream)
|
55
|
-
end
|
56
|
-
|
57
|
-
# @param stream [PgEventstore::Stream] persisted stream
|
58
|
-
# @return [PgEventstore::Stream]
|
59
|
-
def update_stream_revision(stream, revision)
|
60
|
-
connection.with do |conn|
|
61
|
-
conn.exec_params(<<~SQL, [revision, stream.id])
|
62
|
-
UPDATE streams SET stream_revision = $1 WHERE id = $2
|
63
|
-
SQL
|
64
|
-
end
|
65
|
-
stream.stream_revision = revision
|
66
|
-
stream
|
67
|
-
end
|
68
|
-
|
69
|
-
private
|
70
|
-
|
71
|
-
# @param pg_result [PG::Result]
|
72
|
-
# @return [PgEventstore::Stream, nil]
|
73
|
-
def deserialize(pg_result)
|
74
|
-
PgEventstore::Stream.new(**pg_result.to_a.first.transform_keys(&:to_sym))
|
75
|
-
end
|
76
|
-
end
|
77
|
-
end
|
File without changes
|
/data/db/migrations/{5_create_subscription_commands.sql → 3_create_subscription_commands.sql}
RENAMED
File without changes
|
File without changes
|