Skip to content

Commit ed49c21

Browse files
committed
Update with usage guide
1 parent 89dfedf commit ed49c21

File tree

1 file changed

+108
-1
lines changed

1 file changed

+108
-1
lines changed

README.md

Lines changed: 108 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,111 @@
11
[![NuGet Badge](https://buildstats.info/nuget/SqlStreamStore.FSharp?includePreReleases=true)](https://www.nuget.org/packages/SqlStreamStore.FSharp/0.0.1-alpha.12)
2+
23
# SqlStreamStore.FSharp
34

4-
A thin F# wrapper around [SQLStreamStore](https://www.nuget.org/packages/SqlStreamStore), and [SqlStreamStore.Postgres](https://www.nuget.org/packages/SqlStreamStore.Postgres).
5+
A thin F# wrapper around [SQLStreamStore](https://www.nuget.org/packages/SqlStreamStore),
6+
and [SqlStreamStore.Postgres](https://www.nuget.org/packages/SqlStreamStore.Postgres).
7+
8+
## What does it do?
9+
10+
- Provides a nice API to interact with SqlStreamStore from F#
11+
- Has a super slim event sourcing abstraction in the form of `StreamEvent` which is a `StreamMessage` wrapper
12+
13+
## Usage
14+
This quick usage guide presumes familiarity with SQLStreamStore. A more i
15+
16+
### Creating a store
17+
18+
Use the `Create` module.
19+
20+
```f#
21+
open SqlStreamStore
22+
open SqlStreamStore.FSharp
23+
open System
24+
25+
// A new in memory store
26+
let inMemStore : IStreamStore = Create.inMemoryStore()
27+
28+
29+
let config : PostgresConfig =
30+
{
31+
host = Environment.GetEnvironmentVariable "HOST"
32+
port = Environment.GetEnvironmentVariable "PORT"
33+
username = Environment.GetEnvironmentVariable "USERNAME"
34+
password = Environment.GetEnvironmentVariable "PASSWORD"
35+
database = Environment.GetEnvironmentVariable "DATABASE"
36+
}
37+
38+
// A new PostgresStore with a custom schema (use None if you want the tables to be created in public)
39+
let postgresStore : PostgresStreamStore= Create.postgresStore config (Some "my-cool-schema")
40+
41+
// Cast to IStreamStore
42+
let store : IStreamStore = postgresStore :> IStreamStore
43+
44+
// Or create schema if this is the first run
45+
store |> Create.schemaIfNotExists // : Async<Result<IStreamStore,exn>>
46+
```
47+
48+
### Appending to a stream
49+
50+
Use the `Connect` module to connect to the stream you want to append to, and then use the `Append` module to append
51+
messages or events.
52+
53+
```f#
54+
open ...
55+
56+
type MyEvent =
57+
| Hi
58+
| Greeting of Greeting
59+
60+
and Greeting = {data: string}
61+
62+
let eventToAppend : NewStreamEvent<MyEvent> =
63+
Greeting {data = "hello there!"}
64+
|> NewStreamEvent.create "me"
65+
66+
// Given an IStreamStore called store
67+
store // IStreamStore
68+
|> Connect.toStream "my-cool-stream" // Stream
69+
|> Append.streamEvents [eventToAppend] // AsyncResult<AppendResult, exn>
70+
```
71+
72+
### Reading and getting some data from a stream
73+
74+
Use the `Read` module to read the stream and then the `Get` module to get some data without having to bind the result of
75+
the read operation.
76+
77+
```f#
78+
open ...
79+
80+
// Using the MyType declared before and an IStreamStore called store
81+
store // IStreamStore
82+
|> Connect.toStream "my-cool-stream" // Stream
83+
|> Read.entire // Async<Result<ReadStreamPage,exn>>
84+
|> Get.messagesData // Async<Result<string,exn>>
85+
```
86+
87+
#### What if I want to read the stream backwards and specify if I want to prefetch the data, and all the cool stuff that already exists in SQLStreamStore?
88+
89+
Well each of the above functions has an alternative version call that has a `'` at the end for the function's name. The
90+
alternative function takes a list of options as a parameters, and in those options you can do what ever you want.
91+
92+
#### Why do this?
93+
94+
Because 90% of the time these options aren't used, and this is an elegant way to hide them without having to use
95+
builder-pattern.
96+
97+
#### Example
98+
99+
```f#
100+
open ...
101+
102+
// Using the MyType declared before and an IStreamStore called store
103+
store // IStreamStore
104+
|> Connect.toStream "my-cool-stream" // Stream
105+
|> Read.entire' [
106+
ReadEntireOption.FromVersionInclusive 50
107+
ReadEntireOption.NoPrefetch
108+
ReadEntireOption.ReadBackwards
109+
] // Async<Result<ReadStreamPage,exn>>
110+
|> Get.messagesData // Async<Result<string,exn>>
111+
```

0 commit comments

Comments
 (0)