Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added a tryLoad operator to make loading a document in raven more idi…

…omatic returning an option type rather than nulls
  • Loading branch information...
commit 1bfaf17e462f55ec785bd2eef2b6eccb7e922684 1 parent 02d20bd
@colinbull colinbull authored
View
8 Raven.Client.Lightweight.FSharp/Raven.fs
@@ -17,6 +17,11 @@ open Newtonsoft.Json
[<AutoOpen>]
module Operators =
+ let private toOption<'a> (a : 'a) =
+ match box a with
+ | null -> None
+ | _ -> Some(a)
+
let query (f : (IRavenQueryable<'a> -> #IQueryable<'b>)) (a : IDocumentSession) =
f(a.Query<'a>()).AsEnumerable()
@@ -65,6 +70,9 @@ open Newtonsoft.Json
let attachmentBody = attachment.Data.Invoke().ReadData()
(attachment.Metadata.JsonDeserialization<'a>(), attachment.Etag, attachmentBody)
+ let tryLoad<'a> (id : seq<string>) (a : IDocumentSession) =
+ a.Load<'a>(id) |> Seq.map2 (fun id result -> (id, toOption result)) id
+
let load<'a> (id : seq<string>) (a : IDocumentSession) =
a.Load(id).Cast<'a>()
View
19 Raven.Tests.FSharp/RavenTests.fs
@@ -200,7 +200,24 @@ type ``Given a Initailised Document store execute using computation expression``
return expected, actual
} |> run session
Assert.Equal(exp,act)
-
+
+ [<Fact>]
+ let ``Should be able to save and retrieve an entity with tryLoad``() =
+ use ds = test.NewDocumentStore()
+ use session = ds.OpenSession()
+ let cust, act =
+ raven {
+ let! customer = store (Customer.Create("test", new DateTime(2012, 1, 1)))
+ do! saveChanges
+ let! actual = (tryLoad<Customer> ["customers/test";"customers/doesntexist"])
+ return customer, actual
+ } |> run session
+ let exp =
+ [
+ ("customers/test", Option.Some(cust));
+ ("customers/doesntexist", None)
+ ]
+ Assert.True((exp = (act |> Seq.toList)))
[<Fact>]
let ``Should be able to query for filtered all entites``() =

0 comments on commit 1bfaf17

Please sign in to comment.
Something went wrong with that request. Please try again.