Skip to content

Instantly share code, notes, and snippets.

Created December 17, 2014 11:13
Show Gist options
  • Save jamessdixon/bffa8b1c2c3dc806dc41 to your computer and use it in GitHub Desktop.
Save jamessdixon/bffa8b1c2c3dc806dc41 to your computer and use it in GitHub Desktop.
DocumentDB walk-through using F#
#r "../packages/Microsoft.Azure.Documents.Client.0.9.1-preview/lib/net40/Microsoft.Azure.Documents.Client.dll"
#r "../packages/Newtonsoft.Json.4.5.11/lib/net40/Newtonsoft.Json.dll"
open System
open Microsoft.Azure.Documents
open Microsoft.Azure.Documents.Client
open Microsoft.Azure.Documents.Linq
let endpointUrl = "yourEndpoint"
let authKey = "yourAuthKey"
let client = new DocumentClient(new Uri(endpointUrl), authKey)
let database = new Database()
database.Id <- "FamilyRegistry"
let response = client.CreateDatabaseAsync(database).Result
let documentCollection = new DocumentCollection()
documentCollection.Id <- "FamilyCollection"
let documentCollection' = client.CreateDocumentCollectionAsync(response.Resource.CollectionsLink,documentCollection).Result
type Parent = {firstName:string}
type Pet = {givenName:string}
type Child = {firstName:string; gender:string; grade: int; pets:Pet list}
type Address = {state:string; county:string; city:string}
type family = {id:string; lastName:string; parents: Parent list; children: Child list; address: Address; isRegistered:bool}
let andersenFamily = {id="AndersenFamily"; lastName="Andersen";
parents=[{firstName="Thomas"};{firstName="Mary Kay"}];
children=[{firstName="Henriette Thaulow";gender="female";
address={state = "WA"; county = "King"; city = "Seattle"};
isRegistered = true}
client.CreateDocumentAsync(documentCollection'.Resource.DocumentsLink, andersenFamily)
let wakefieldFamily = {id="WakefieldFamily"; lastName="Wakefield";
address={state = "NY"; county = "Manhattan"; city = "NY"};
isRegistered = false}
client.CreateDocumentAsync(documentCollection'.Resource.DocumentsLink, wakefieldFamily)
let queryString = "SELECT * FROM Families f WHERE = \"AndersenFamily\""
//Note - this does not work and I don't know why
let families = client.CreateDocumentQuery(documentCollection'.Resource.DocumentsLink,queryString)
families |> f -> f :?> family)
|> Seq.iter(fun f -> printfn "read %A from SQL" f.lastName)
//let database = client.CreateDatabaseQuery().Where(fun db -> db.Id = "FamilyRegistry" ).ToArray().FirstOrDefault()
//printfn "%s" database.SelfLink
let database' = client.CreateDatabaseQuery() |> Seq.filter(fun db -> db.Id = "FamilyRegistry")
|> Seq.head
printfn "%s" database.SelfLink
Copy link

You probably figured it out already, but for others that come this way here is a 'back of an envelope' recursive function that prints out the values.

  let familiesQuery = families'.AsDocumentQuery()

  let rec readResults (query:IDocumentQuery<_>) =
    if query.HasMoreResults
      let fam = query.ExecuteNextAsync<family>().Result
      printfn "%+A" fam
      readResults query

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment