Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,23 @@ match result {

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

#### Using the `IsEventQLTrue` precondition

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:

```rust
let result = client.write_events(
vec![event.clone()],
vec![Precondition::IsEventQLTrue {
query: "FROM e IN events WHERE e.type == 'io.eventsourcingdb.library.book-borrowed' PROJECT INTO COUNT() < 10".to_string(),
}],
).await;
match result {
Ok(written_events) => // ...
Err(err) => // ...
}
```

### Reading Events

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.
Expand Down
6 changes: 6 additions & 0 deletions src/client/precondition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,10 @@ pub enum Precondition {
#[serde(rename = "eventId")]
event_id: String,
},
/// Check if an EventQL query returns true
#[serde(rename = "isEventQlTrue")]
IsEventQLTrue {
/// The EventQL query to check
query: String,
},
}
20 changes: 20 additions & 0 deletions tests/write_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,26 @@ async fn write_events_with_is_subject_on_event_id_condition_on_empty_subject() {
assert!(result.is_err(), "Expected an error, but got: {:?}", result);
}

#[tokio::test]
async fn write_events_with_is_eventql_true_condition() {
let container = Container::start_default().await.unwrap();
let client = container.get_client().await.unwrap();

let event_candidates = vec![
create_test_eventcandidate("/test/42", json!({"value": 1})),
create_test_eventcandidate("/test/42", json!({"value": 1})),
];
let result = client
.write_events(
event_candidates.clone(),
vec![Precondition::IsEventQLTrue {
query: "FROM e IN events PROJECT INTO COUNT() == 0".to_string(),
}],
)
.await;
assert!(result.is_ok(), "Expected an ok, but got: {:?}", result);
}

#[tokio::test]
async fn write_events_with_is_subject_on_event_id_condition_on_non_empty_subject_correct_id() {
let container = Container::start_default().await.unwrap();
Expand Down