Skip to content

Commit beee188

Browse files
committed
feat(preconditions): add support for isEventQLTrue preconditions
Signed-off-by: Raphael Höser <[email protected]>
1 parent 5c55acf commit beee188

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,23 @@ match result {
114114

115115
*Note that according to the CloudEvents standard, event IDs must be of type string.*
116116

117+
#### Using the `IsEventQLTrue` precondition
118+
119+
If you want to write events depending on an EventQL query, use the `IsEventQLTrue` precondition to create a precondition and pass it in a vector as the second argument:
120+
121+
```rust
122+
let result = client.write_events(
123+
vec![event.clone()],
124+
vec![Precondition::IsEventQLTrue {
125+
query: "FROM e IN events WHERE e.type == 'io.eventsourcingdb.library.book-borrowed' PROJECT INTO COUNT() < 10".to_string(),
126+
}],
127+
).await;
128+
match result {
129+
Ok(written_events) => // ...
130+
Err(err) => // ...
131+
}
132+
```
133+
117134
### Reading Events
118135

119136
To read all events of a subject, call the `read_events` function with the subject and an options object. Set the `recursive` option to `false`. This ensures that only events of the given subject are returned, not events of nested subjects.

src/client/precondition.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,10 @@ pub enum Precondition {
1919
#[serde(rename = "eventId")]
2020
event_id: String,
2121
},
22+
/// Check if an EventQL query returns true
23+
#[serde(rename = "isEventQlTrue")]
24+
IsEventQLTrue {
25+
/// The EventQL query to check
26+
query: String,
27+
},
2228
}

tests/write_events.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,26 @@ async fn write_events_with_is_subject_on_event_id_condition_on_empty_subject() {
212212
assert!(result.is_err(), "Expected an error, but got: {:?}", result);
213213
}
214214

215+
#[tokio::test]
216+
async fn write_events_with_is_eventql_true_condition() {
217+
let container = Container::start_default().await.unwrap();
218+
let client = container.get_client().await.unwrap();
219+
220+
let event_candidates = vec![
221+
create_test_eventcandidate("/test/42", json!({"value": 1})),
222+
create_test_eventcandidate("/test/42", json!({"value": 1})),
223+
];
224+
let result = client
225+
.write_events(
226+
event_candidates.clone(),
227+
vec![Precondition::IsEventQLTrue {
228+
query: "FROM e IN events PROJECT INTO COUNT() == 0".to_string(),
229+
}],
230+
)
231+
.await;
232+
assert!(result.is_err(), "Expected an error, but got: {:?}", result);
233+
}
234+
215235
#[tokio::test]
216236
async fn write_events_with_is_subject_on_event_id_condition_on_non_empty_subject_correct_id() {
217237
let container = Container::start_default().await.unwrap();

0 commit comments

Comments
 (0)