entity_store_sequel 0.2.2 → 0.4.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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1fbaafda3599b05b00d50669608de59f140546e61066def1dbb355d2eea59ccf
|
4
|
+
data.tar.gz: 565ac6d7d44085cb366f2250147c20c63f4a1b05143cb8f30286f485172c184b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a5ab29eb7d354b40cd35411226e8e96d65ca7df0fda329378a849033fc96a13864558eae931966756fef5ae18733806075dcd69cf723c1f28bcebcca2c606c10
|
7
|
+
data.tar.gz: c8ffb267c8103f4bf4cd03e56099f55e3d6ecec1b93357176de0e1a24018d1c8191763b7ac5578944eeefd9795fce6dfaa024c88c881a6ebbef5f38449dcec04
|
@@ -70,10 +70,13 @@ module EntityStoreSequel
|
|
70
70
|
end
|
71
71
|
|
72
72
|
def clear_entity_events(id, excluded_types)
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
events.where(id:
|
73
|
+
if excluded_types.empty?
|
74
|
+
events.where(_entity_id: id).delete
|
75
|
+
else
|
76
|
+
events.where(_entity_id: id).select(:id, :_type).each do |event|
|
77
|
+
next if excluded_types.include?(event[:_type])
|
78
|
+
events.where(id: event[:id]).delete
|
79
|
+
end
|
77
80
|
end
|
78
81
|
end
|
79
82
|
|
@@ -129,20 +132,42 @@ module EntityStoreSequel
|
|
129
132
|
add_events_with_ids(events_with_id)
|
130
133
|
end
|
131
134
|
|
135
|
+
def upsert_events(items)
|
136
|
+
events_with_id = items.map do |e|
|
137
|
+
[ e._id, e ]
|
138
|
+
end
|
139
|
+
|
140
|
+
filtered_events = []
|
141
|
+
events_with_id.each do |id, event|
|
142
|
+
next if events.where(id: id.to_s).count > 0
|
143
|
+
|
144
|
+
filtered_events << event
|
145
|
+
|
146
|
+
doc = event_doc(id, event)
|
147
|
+
events.insert(doc)
|
148
|
+
end
|
149
|
+
|
150
|
+
filtered_events
|
151
|
+
end
|
152
|
+
|
132
153
|
def add_events_with_ids(event_id_map)
|
133
154
|
event_id_map.each do |id, event|
|
134
|
-
doc =
|
135
|
-
:id => id.to_s,
|
136
|
-
:_type => event.class.name,
|
137
|
-
:_entity_id => event.entity_id.to_s,
|
138
|
-
:entity_version => event.entity_version,
|
139
|
-
:at => event.attributes[:at],
|
140
|
-
:data => PigeonHole.generate(hash_without_keys(event.attributes, :entity_id, :at, :entity_version)),
|
141
|
-
}
|
155
|
+
doc = event_doc(id, event)
|
142
156
|
events.insert(doc)
|
143
157
|
end
|
144
158
|
end
|
145
159
|
|
160
|
+
def event_doc(id, event)
|
161
|
+
{
|
162
|
+
:id => id.to_s,
|
163
|
+
:_type => event.class.name,
|
164
|
+
:_entity_id => event.entity_id.to_s,
|
165
|
+
:entity_version => event.entity_version,
|
166
|
+
:at => event.attributes[:at],
|
167
|
+
:data => PigeonHole.generate(hash_without_keys(event.attributes, :entity_id, :at, :entity_version, :_id)),
|
168
|
+
}
|
169
|
+
end
|
170
|
+
|
146
171
|
# Public: loads the entity from the store, including any available snapshots
|
147
172
|
# then loads the events to complete the state
|
148
173
|
#
|
@@ -259,6 +284,25 @@ module EntityStoreSequel
|
|
259
284
|
|
260
285
|
private
|
261
286
|
|
287
|
+
def get_raw_events(entity_id)
|
288
|
+
rows = events.where(:_entity_id => entity_id).order_by(:entity_version, :id)
|
289
|
+
rows.map do |attrs|
|
290
|
+
if self.class.native_json_hash
|
291
|
+
hash = attrs[:data].to_h
|
292
|
+
else
|
293
|
+
hash = PigeonHole.parse(attrs[:data])
|
294
|
+
end
|
295
|
+
|
296
|
+
hash[:_id] = attrs[:id]
|
297
|
+
hash[:_type] = attrs[:_type]
|
298
|
+
hash[:entity_version] = attrs[:entity_version]
|
299
|
+
hash[:entity_id] = attrs[:_entity_id]
|
300
|
+
hash[:at] = attrs[:at]
|
301
|
+
|
302
|
+
hash
|
303
|
+
end
|
304
|
+
end
|
305
|
+
|
262
306
|
def filter_method_name(index)
|
263
307
|
index == 0 ? :where : :or
|
264
308
|
end
|
@@ -47,6 +47,21 @@ describe PostgresEntityStore do
|
|
47
47
|
end
|
48
48
|
end
|
49
49
|
|
50
|
+
class DummyEntityExcludedEvent
|
51
|
+
include EntityStore::Event
|
52
|
+
|
53
|
+
attr_accessor :marker
|
54
|
+
|
55
|
+
def apply(entity)
|
56
|
+
# No-op
|
57
|
+
end
|
58
|
+
|
59
|
+
def ==(other)
|
60
|
+
# Crude check relying on inspect, ok for tests
|
61
|
+
self.inspect == other.inspect
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
50
65
|
let(:store) do
|
51
66
|
described_class.connection_string = ENV['SQL_CONNECTION'] || 'postgres://localhost/cronofy_test'
|
52
67
|
described_class.init
|
@@ -123,23 +138,42 @@ describe PostgresEntityStore do
|
|
123
138
|
let(:third_event) { DummyEntityNameSet.new(:entity_id => entity_id, :entity_version => 2, :name => random_string) }
|
124
139
|
let(:unrelated_event) { DummyEntityNameSet.new(:entity_id => second_entity_id, :entity_version => 4, :name => random_string) }
|
125
140
|
let(:fourth_event) { DummyEntityNameSet.new(:entity_id => entity_id, :entity_version => 3, :name => random_string) }
|
141
|
+
let(:excluded_event) { DummyEntityExcludedEvent.new(:entity_id => entity_id, :entity_version => 5, :marker => random_string) }
|
126
142
|
|
127
143
|
before do
|
128
|
-
store.add_events([ second_event, unrelated_event, first_event, third_event, fourth_event ])
|
144
|
+
store.add_events([ second_event, unrelated_event, first_event, third_event, fourth_event, excluded_event ])
|
129
145
|
end
|
130
146
|
|
131
|
-
|
147
|
+
context "clearing everything" do
|
148
|
+
subject { store.clear_entity_events(entity_id, []) }
|
132
149
|
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
150
|
+
it "clears the events from the entity" do
|
151
|
+
subject
|
152
|
+
events = store.get_events( [ id: entity_id ])[entity_id]
|
153
|
+
expect(events).to be_empty
|
154
|
+
end
|
155
|
+
|
156
|
+
it "does not delete unrelated the events" do
|
157
|
+
subject
|
158
|
+
events = store.get_events( [ id: second_entity_id ])[second_entity_id]
|
159
|
+
expect(events).to eq [unrelated_event]
|
160
|
+
end
|
137
161
|
end
|
138
162
|
|
139
|
-
|
140
|
-
subject
|
141
|
-
|
142
|
-
|
163
|
+
context "excluding an event type from deletion" do
|
164
|
+
subject { store.clear_entity_events(entity_id, [DummyEntityExcludedEvent.to_s]) }
|
165
|
+
|
166
|
+
it "clears all other events from the entity" do
|
167
|
+
subject
|
168
|
+
events = store.get_events( [ id: entity_id ])[entity_id]
|
169
|
+
expect(events).to eq [excluded_event]
|
170
|
+
end
|
171
|
+
|
172
|
+
it "does not delete unrelated the events" do
|
173
|
+
subject
|
174
|
+
events = store.get_events( [ id: second_entity_id ])[second_entity_id]
|
175
|
+
expect(events).to eq [unrelated_event]
|
176
|
+
end
|
143
177
|
end
|
144
178
|
end
|
145
179
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: entity_store_sequel
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Stephen Binns
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-02-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: sequel
|
@@ -101,7 +101,7 @@ homepage: http://github.com/cronofy/entity_store_sequel
|
|
101
101
|
licenses:
|
102
102
|
- MIT
|
103
103
|
metadata: {}
|
104
|
-
post_install_message:
|
104
|
+
post_install_message:
|
105
105
|
rdoc_options: []
|
106
106
|
require_paths:
|
107
107
|
- lib
|
@@ -116,12 +116,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
116
116
|
- !ruby/object:Gem::Version
|
117
117
|
version: '0'
|
118
118
|
requirements: []
|
119
|
-
|
120
|
-
|
121
|
-
signing_key:
|
119
|
+
rubygems_version: 3.4.21
|
120
|
+
signing_key:
|
122
121
|
specification_version: 4
|
123
122
|
summary: Sequel body for Entity Store
|
124
123
|
test_files:
|
125
|
-
- spec/spec_helper.rb
|
126
|
-
- spec/entity_store_spec.rb
|
127
124
|
- spec/entity_store_sequel/postgres_entity_store_spec.rb
|
125
|
+
- spec/entity_store_spec.rb
|
126
|
+
- spec/spec_helper.rb
|