项目作者: jamil7

项目描述 :
Thin F# wrapper around SQLStreamStore
高级语言: F#
项目地址: git://github.com/jamil7/SQLStreamStore.FSharp.git
创建时间: 2020-10-03T13:09:50Z
项目社区:https://github.com/jamil7/SQLStreamStore.FSharp

开源协议:MIT License

下载


NuGet Badge

SqlStreamStore.FSharp

A thin F# wrapper around SQLStreamStore,
and SqlStreamStore.Postgres.

What does it do?

  • Provides a nice API to interact with SqlStreamStore from F#
  • Wraps all functions that can throw in Async<Result<'a, exn>>

Usage

This quick usage guide presumes familiarity with SQLStreamStore. A more in-depth guide and documentation are coming soon :)

Creating a store

Use the Create module.

  1. open SqlStreamStore
  2. open SqlStreamStore.FSharp
  3. open System
  4. // A new in memory store
  5. let inMemStore : IStreamStore = Create.inMemoryStore()
  6. let config : PostgresConfig =
  7. {
  8. host = Environment.GetEnvironmentVariable "HOST"
  9. port = Environment.GetEnvironmentVariable "PORT"
  10. username = Environment.GetEnvironmentVariable "USERNAME"
  11. password = Environment.GetEnvironmentVariable "PASSWORD"
  12. database = Environment.GetEnvironmentVariable "DATABASE"
  13. }
  14. // A new PostgresStore with a custom schema (use None if you want the tables to be created in public)
  15. let postgresStore : PostgresStreamStore = Create.postgresStore config (Some "my-cool-schema")
  16. // Cast to IStreamStore
  17. let store : IStreamStore = postgresStore :> IStreamStore
  18. // Or create schema if this is the first run
  19. store |> Create.schemaIfNotExists // : Async<Result<IStreamStore,exn>>

Appending to a stream

Use the Connect module to connect to the stream you want to append to, and then use the Append module to append
messages or events.

  1. open ...
  2. type MyEvent =
  3. | Hi
  4. | Greeting of Greeting
  5. and Greeting = {data: string}
  6. let eventToAppend : NewStreamEvent<MyEvent> =
  7. Greeting {data = "hello there!"}
  8. |> NewStreamEvent.create "me"
  9. // Given an IStreamStore called store
  10. store // IStreamStore
  11. |> Connect.toStream "my-cool-stream" // Stream
  12. |> Append.streamEvents [eventToAppend] // AsyncResult<AppendResult, exn>

Reading and getting some data from a stream

Use the Read module to read the stream and then the Get module to get some data without having to bind the result of
the read operation.

  1. open ...
  2. // Using the MyType declared before and an IStreamStore called store
  3. store // IStreamStore
  4. |> Connect.toStream "my-cool-stream" // Stream
  5. |> Read.entire // Async<Result<ReadStreamPage,exn>>
  6. |> Get.messagesData // Async<Result<string,exn>>

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?

Well each of the above functions has an alternative version call that has a ' at the end for the function’s name. The
alternative function takes a list of options as a parameter.

Why do this?

Because 90% of the time these options aren’t used, and this is an elegant way to hide them without having to use
builder-pattern.

Example

  1. open ...
  2. // Using the MyType declared before and an IStreamStore called store
  3. store // IStreamStore
  4. |> Connect.toStream "my-cool-stream" // Stream
  5. |> Read.entire' [
  6. ReadEntireOption.FromVersionInclusive 50
  7. ReadEntireOption.NoPrefetch
  8. ReadEntireOption.ReadBackwards
  9. ] // Async<Result<ReadStreamPage,exn>>
  10. |> Get.messagesData // Async<Result<string,exn>>