Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make a usable version of getEventByCorrelationId #19

Merged
merged 1 commit into from
Mar 27, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions paket.dependencies
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ group Marten
nuget TaskBuilder.fs
nuget FSharp.Core >= 4.3.4 < 4.5.4
nuget Marten.FSharp
github baronfel/Newtonsoft.Json.FSharp.Idiomatic src/Newtonsoft.Json.FSharp.Idiomatic/Newtonsoft.Json.FSharp.Idiomatic.fs

group InMemory
source https://api.nuget.org/v3/index.json
Expand Down
4 changes: 3 additions & 1 deletion paket.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3475,7 +3475,9 @@ NUGET
FSharp.Core (>= 4.1.17) - restriction: || (&& (>= net45) (< net46) (< netstandard1.6)) (&& (< net45) (>= netstandard1.6)) (&& (>= net46) (< netstandard1.6)) (>= net47)
NETStandard.Library (>= 1.6.1) - restriction: && (< net45) (>= netstandard1.6)
System.ValueTuple (>= 4.4) - restriction: || (&& (>= net45) (< net46) (< netstandard1.6)) (&& (< net45) (>= netstandard1.6)) (&& (>= net46) (< netstandard1.6)) (>= net47)

GITHUB
remote: baronfel/Newtonsoft.Json.FSharp.Idiomatic
src/Newtonsoft.Json.FSharp.Idiomatic/Newtonsoft.Json.FSharp.Idiomatic.fs (abe8185653e06893623dd3a39b353afc226fa703)
GROUP TableStorage
STORAGE: NONE
NUGET
Expand Down
42 changes: 21 additions & 21 deletions src/CosmoStore.Marten/CosmoStore.Marten.fsproj
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Authors>Roman Provazník;Kunjan Dalal</Authors>
</PropertyGroup>

<ItemGroup>
<Content Include="paket.references" />
<Content Include="RELEASE_NOTES.md" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\CosmoStore\CosmoStore.fsproj" />
</ItemGroup>

<ItemGroup>
<Compile Include="MartenConf.fs" />
<Compile Include="EventStore.fs" />
</ItemGroup>
<Import Project="..\..\.paket\Paket.Restore.targets" />
</Project>
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Authors>Roman Provazník;Kunjan Dalal</Authors>
</PropertyGroup>
<ItemGroup>
<Compile Include="..\..\paket-files\marten\baronfel\Newtonsoft.Json.FSharp.Idiomatic\src\Newtonsoft.Json.FSharp.Idiomatic\Newtonsoft.Json.FSharp.Idiomatic.fs">
<Paket>True</Paket>
<Link>paket-files/Newtonsoft.Json.FSharp.Idiomatic.fs</Link>
</Compile>
<Content Include="paket.references" />
<Content Include="RELEASE_NOTES.md" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\CosmoStore\CosmoStore.fsproj" />
</ItemGroup>
<ItemGroup>
<Compile Include="MartenConf.fs" />
<Compile Include="EventStore.fs" />
</ItemGroup>
<Import Project="..\..\.paket\Paket.Restore.targets" />
</Project>
50 changes: 40 additions & 10 deletions src/CosmoStore.Marten/EventStore.fs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
namespace CosmoStore.Marten
open Npgsql
open Newtonsoft.Json
open Newtonsoft.Json.FSharp.Idiomatic

module EventStore =
open System
Expand Down Expand Up @@ -130,16 +132,28 @@ module EventStore =
let! events = getEvents store streamId filter
return events.Head
}
open Microsoft.FSharp.Quotations.Patterns

let rec private propertyName quotation =
match quotation with
| PropertyGet (_,propertyInfo,_) -> propertyInfo.Name
| Lambda (_,expr) -> propertyName expr
| _ -> ""

// Get a type safe name in case this changes somehow
let private correlationIdPropName = propertyName <@ fun (x : EventRead) -> x.CorrelationId @>

let private getEventsByCorrelationId (store: IDocumentStore) corrId =
let private getEventsByCorrelationIdQuery = sprintf "where data->>'%s' = ?" correlationIdPropName

let private getEventsByCorrelationId (store: IDocumentStore) (corrId : Guid) =
task {
use session = store.LightweightSession()

let! res =
session
|> Session.sqlTask<EventRead> getEventsByCorrelationIdQuery [| box (corrId.ToString()) |]

// let res = session |> Session.query<EventRead> |> Queryable.filter <@ fun x -> Option.toNullable(x.CorrelationId) = Nullable(corrId) @> |> Seq.toList
//TODO: Option is type is not supported by store. So, it is better to convert option type to Nullable and then put a guard on that.
//TODO: remove this not optimized filter once things converted to nullable. Then above code can be used. Until then don't use in production
let res = session |> Session.query<EventRead> |> Seq.filter (fun x -> x.CorrelationId = Some corrId) |> Seq.toList
return (res)
return res |> Seq.toList
}

let private getStreams (store: IDocumentStore) streamsRead = task {
Expand All @@ -154,7 +168,7 @@ module EventStore =
|> Queryable.toListTask

return (res |> Seq.toList)
}
}

let private getStream (store: IDocumentStore) streamId = task {
use session = store.LightweightSession()
Expand All @@ -170,11 +184,27 @@ module EventStore =

let userConnStr(conf) = createConnString (conf.Host) (conf.Username) (conf.Password) (conf.Database)

let converters: JsonConverter [] = [|
OptionConverter()
SingleCaseDuConverter()
MultiCaseDuConverter()
|]

let martenSerializer =
// Need to tell marten to use enums as strings or we have to teach npgsql about our enums when using parameters
let s = Services.JsonNetSerializer(EnumStorage = EnumStorage.AsString)

for c in converters do
s.Customize(fun s -> s.Converters.Add(c))
s

let getEventStore (conf: Configuration) =
let store =
userConnStr conf
|> string
|> DocumentStore.For
Marten.DocumentStore.For(
fun ds ->
ds.Connection(userConnStr conf |> string) |> ignore
ds.Serializer(martenSerializer) |> ignore
)

let eventAppended = Event<EventRead>()

Expand Down
1 change: 1 addition & 0 deletions src/CosmoStore.Marten/paket.references
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ group Marten
FSharp.Core
TaskBuilder.fs
Marten.FSharp
File: Newtonsoft.Json.FSharp.Idiomatic.fs