Created
December 17, 2014 11:13
-
-
Save jamessdixon/bffa8b1c2c3dc806dc41 to your computer and use it in GitHub Desktop.
DocumentDB walk-through using F#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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"; | |
grade=5;pets=[{givenName="Fluffy"}]}]; | |
address={state = "WA"; county = "King"; city = "Seattle"}; | |
isRegistered = true} | |
client.CreateDocumentAsync(documentCollection'.Resource.DocumentsLink, andersenFamily) | |
let wakefieldFamily = {id="WakefieldFamily"; lastName="Wakefield"; | |
parents=[{firstName="Robin"};{firstName="Ben"}]; | |
children=[{firstName="Jesse";gender="female"; | |
grade=8;pets=[{givenName="Goofy"};{givenName="Shadow"}]}]; | |
address={state = "NY"; county = "Manhattan"; city = "NY"}; | |
isRegistered = false} | |
client.CreateDocumentAsync(documentCollection'.Resource.DocumentsLink, wakefieldFamily) | |
let queryString = "SELECT * FROM Families f WHERE f.id = \"AndersenFamily\"" | |
//Note - this does not work and I don't know why | |
let families = client.CreateDocumentQuery(documentCollection'.Resource.DocumentsLink,queryString) | |
families |> Seq.map(fun 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 |
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
then
let fam = query.ExecuteNextAsync<family>().Result
printfn "%+A" fam
readResults query
else
()
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Re: why the cast to family doesn't work, I think it is because the family record doesn't supply a def for etag etc, which comes back from Azure DocDb.