Skip to content

Commit e645429

Browse files
authored
Merge pull request #49 from EventStore/snippets
API structure and naming alignment with other clients.
2 parents c044290 + ce25378 commit e645429

23 files changed

+2188
-1455
lines changed

Cargo.toml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,34 @@ webpki = "0.21"
4242
base64 = "0.13"
4343
nom = "6"
4444
thiserror = "1"
45+
async-trait = "0.1"
4546

4647
[build-dependencies]
4748
tonic-build = { version = "0.4", features = ["prost"] }
4849

4950
[[test]]
5051
name = "integration"
5152

53+
[[example]]
54+
name = "appending_events"
55+
crate-type = ["staticlib"]
56+
57+
[[example]]
58+
name = "quickstart"
59+
crate-type = ["staticlib"]
60+
61+
[[example]]
62+
name = "reading_events"
63+
crate-type = ["staticlib"]
64+
65+
[[example]]
66+
name = "server_side_filtering"
67+
crate-type = ["staticlib"]
68+
69+
[[example]]
70+
name = "subscribing_to_stream"
71+
crate-type = ["staticlib"]
72+
5273
[dev-dependencies]
5374
serde = { version = "1.0", features = ["derive"] }
5475
pretty_env_logger = "0.4"

docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ services:
2727
- volumes-provisioner
2828

2929
node1.eventstore: &template
30-
image: eventstore/eventstore:20.6.1-buster-slim
30+
image: eventstore/eventstore:20.10.0-buster-slim
3131
container_name: node1.eventstore
3232
env_file:
3333
- vars.env

examples/appending_events.rs

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
use eventstore::{
2+
AppendToStreamOptions, Client, Credentials, EventData, ExpectedRevision, ReadStreamOptions,
3+
Single, StreamPosition,
4+
};
5+
use futures::TryStreamExt;
6+
use serde::{Deserialize, Serialize};
7+
use std::error::Error;
8+
use uuid::Uuid;
9+
10+
#[derive(Serialize, Deserialize)]
11+
struct TestEvent {
12+
pub id: String,
13+
pub important_data: String,
14+
}
15+
16+
type Result<A> = std::result::Result<A, Box<dyn Error>>;
17+
18+
pub async fn append_to_stream(client: &Client) -> Result<()> {
19+
// region append-to-stream
20+
21+
let data = TestEvent {
22+
id: "1".to_string(),
23+
important_data: "some value".to_string(),
24+
};
25+
26+
let event = EventData::json("some-event", &data)?.id(Uuid::new_v4());
27+
let options = AppendToStreamOptions::default().expected_revision(ExpectedRevision::NoStream);
28+
29+
let _ = client
30+
.append_to_stream("some-stream", &options, event)
31+
.await?;
32+
33+
// endregion append-to-stream
34+
35+
Ok(())
36+
}
37+
38+
pub async fn append_with_same_id(client: &Client) -> Result<()> {
39+
// region append-duplicate-event
40+
41+
let data = TestEvent {
42+
id: "1".to_string(),
43+
important_data: "some value".to_string(),
44+
};
45+
46+
let event = EventData::json("some-event", &data)?.id(Uuid::new_v4());
47+
let options = AppendToStreamOptions::default();
48+
49+
let _ = client
50+
.append_to_stream("same-event-stream", &options, event.clone())
51+
.await?;
52+
53+
let _ = client
54+
.append_to_stream("same-event-stream", &options, event)
55+
.await?;
56+
57+
// endregion append-duplicate-event
58+
59+
Ok(())
60+
}
61+
62+
pub async fn append_with_no_stream(client: &Client) -> Result<()> {
63+
// region append-with-no-stream
64+
65+
let data = TestEvent {
66+
id: "1".to_string(),
67+
important_data: "some value".to_string(),
68+
};
69+
70+
let event = EventData::json("some-event", &data)?.id(Uuid::new_v4());
71+
let options = AppendToStreamOptions::default().expected_revision(ExpectedRevision::NoStream);
72+
73+
let _ = client
74+
.append_to_stream("same-event-stream", &options, event)
75+
.await?;
76+
77+
let data = TestEvent {
78+
id: "2".to_string(),
79+
important_data: "some other value".to_string(),
80+
};
81+
82+
let event = EventData::json("some-event", &data)?.id(Uuid::new_v4());
83+
84+
let _ = client
85+
.append_to_stream("same-event-stream", &options, event)
86+
.await?;
87+
88+
// endregion append-with-no-stream
89+
90+
Ok(())
91+
}
92+
93+
pub async fn append_with_concurrency_check(client: Client) -> Result<()> {
94+
// region append-with-concurrency-check
95+
96+
let options = ReadStreamOptions::default().position(StreamPosition::End);
97+
98+
let last_event = client
99+
.read_stream("concurrency-stream", &options, Single)
100+
.await?
101+
.ok()
102+
.expect("we expect the stream to at least exist.")
103+
.expect("we expect the stream to have at least one event.");
104+
105+
let data = TestEvent {
106+
id: "1".to_string(),
107+
important_data: "clientOne".to_string(),
108+
};
109+
110+
let event = EventData::json("some-event", data)?.id(Uuid::new_v4());
111+
let options = AppendToStreamOptions::default().expected_revision(ExpectedRevision::Exact(
112+
last_event.get_original_event().revision,
113+
));
114+
115+
let _ = client
116+
.append_to_stream("concurrency-stream", &options, event)
117+
.await?;
118+
119+
let data = TestEvent {
120+
id: "2".to_string(),
121+
important_data: "clientTwo".to_string(),
122+
};
123+
124+
let event = EventData::json("some-event", &data)?.id(Uuid::new_v4());
125+
126+
let _ = client
127+
.append_to_stream("concurrency-stream", &options, event)
128+
.await?;
129+
130+
// endregion append-with-concurrency-check
131+
132+
Ok(())
133+
}
134+
135+
pub async fn append_overriding_user_credentials(client: &Client) -> Result<()> {
136+
let data = TestEvent {
137+
id: "1".to_string(),
138+
important_data: "clientOne".to_string(),
139+
};
140+
141+
let event = EventData::json("some-event", data)?.id(Uuid::new_v4());
142+
143+
// region overriding-user-credentials
144+
let options =
145+
AppendToStreamOptions::default().authenticated(Credentials::new("admin", "changeit"));
146+
147+
let _ = client
148+
.append_to_stream("some-stream", &options, event)
149+
.await?;
150+
151+
// endregion overriding-user-credentials
152+
153+
Ok(())
154+
}

examples/quickstart.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
use eventstore::{
2+
AppendToStreamOptions, Client, Credentials, EventData, ExpectedRevision, ReadResult,
3+
ReadStreamOptions,
4+
};
5+
use futures::TryStreamExt;
6+
use serde::{Deserialize, Serialize};
7+
use std::error::Error;
8+
use uuid::Uuid;
9+
10+
#[derive(Serialize, Deserialize)]
11+
struct TestEvent {
12+
pub id: String,
13+
pub important_data: String,
14+
}
15+
16+
type Result<A> = std::result::Result<A, Box<dyn Error>>;
17+
18+
pub async fn run() -> Result<()> {
19+
// region createClient
20+
let settings = "{connectionString}".parse()?;
21+
let client = Client::create(settings).await?;
22+
// endregion createClient
23+
24+
// region createEvent
25+
let event = TestEvent {
26+
id: Uuid::new_v4().to_string(),
27+
important_data: "I wrote my first event!".to_string(),
28+
};
29+
30+
let event_data = EventData::json("TestEvent", event)?.id(Uuid::new_v4());
31+
// endregion createEvent
32+
33+
let main_event_data = event_data.clone();
34+
35+
// region appendEvents
36+
client
37+
.append_to_stream("some-stream", &Default::default(), event_data)
38+
.await?;
39+
// endregion appendEvents
40+
41+
let event_data = main_event_data.clone();
42+
let options =
43+
AppendToStreamOptions::default().authenticated(Credentials::new("admin", "changeit"));
44+
45+
// region overriding-user-credentials
46+
client
47+
.append_to_stream("some-stream", &options, event_data)
48+
.await?;
49+
// endregion overriding-user-credentials
50+
51+
// region readStream
52+
let result = client
53+
.read_stream("some-stream", &Default::default(), 10)
54+
.await?;
55+
56+
if let ReadResult::Ok(events) = result {
57+
// Doing something productive with the events.
58+
}
59+
// endregion readStream
60+
61+
Ok(())
62+
}

0 commit comments

Comments
 (0)