Last active
October 21, 2021 03:11
-
-
Save xxnickles/e3d2a80728bd6aa6b03a88dfd2ba1a6d to your computer and use it in GitHub Desktop.
Azure table example in F#. Why I need to implement members twice?
This file contains 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 "nuget: Azure.Data.Tables" | |
open System | |
open Azure | |
open Azure.Data.Tables // Namespace for Table storage types | |
open System.Linq | |
open System.Collections.Generic | |
type Customer(firstName, lastName, email: string, phone: string) = | |
let timestamp : Nullable<DateTimeOffset> = Nullable(); | |
let etag = ETag() | |
interface ITableEntity with | |
member val ETag = etag with get, set | |
member val PartitionKey = lastName with get, set | |
member val RowKey = firstName with get, set | |
member val Timestamp = timestamp with get, set | |
new() = Customer(null, null, null, null) | |
member val Email = email with get, set | |
member val PhoneNumber = phone with get, set | |
member val ETag = etag with get, set | |
member val PartitionKey = lastName with get, set | |
member val RowKey = firstName with get, set | |
member val Timestamp = timestamp with get, set | |
// Error out | |
//type Customer(firstName, lastName, email: string, phone: string) = | |
// let timestamp : Nullable<DateTimeOffset> = Nullable(); | |
// let etag = ETag() | |
// interface ITableEntity with | |
// member val ETag = etag with get, set | |
// member val PartitionKey = lastName with get, set | |
// member val RowKey = firstName with get, set | |
// member val Timestamp = timestamp with get, set | |
// | |
// new() = Customer(null, null, null, null) | |
// | |
// member val Email = email with get, set | |
// member val PhoneNumber = phone with get, set | |
let storageConnString = | |
"DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;TableEndpoint=http://127.0.0.1:1002/devstoreaccount1;" | |
let tableClient = TableServiceClient(storageConnString) | |
let table = tableClient.GetTableClient("people") | |
// Create the table if it doesn't exist. | |
table.CreateIfNotExists() |> ignore | |
let customer = Customer("Walter", "Harp", "Walter@contoso.com", "425-555-0101") | |
table.AddEntity(customer) |> ignore | |
let customer1 = | |
Customer("Jeff", "Smith", "Jeff@contoso.com", "425-555-0102") | |
let customer2 = | |
Customer("Ben", "Smith", "Ben@contoso.com", "425-555-0103") | |
let entityList = [ customer1; customer2] | |
// Create the batch. | |
let addEntitiesBatch = List<TableTransactionAction>() | |
// Add the entities to be added to the batch. | |
addEntitiesBatch.AddRange(entityList.Select(fun e -> TableTransactionAction(TableTransactionActionType.Add, e))) | |
// Submit the batch. | |
let response = table.SubmitTransactionAsync(addEntitiesBatch).ConfigureAwait(false) | |
let results = table.Query<Customer>("PartitionKey eq 'Smith'") | |
printfn "ALL customers" | |
for customerB in results do | |
let customer = customerB :> ITableEntity | |
printfn $"customer: {customer.RowKey} {customer.PartitionKey}" | |
let rangeResult = table.Query<Customer>("PartitionKey eq 'Smith' and RowKey lt 'M'") | |
printfn "The m customers" | |
for customerB in rangeResult do | |
let customer = customerB :> ITableEntity | |
printfn $"customer: {customer.RowKey} {customer.PartitionKey}" | |
let singleResult = table.GetEntityAsync<Customer>("Smith", "Ben").Result.Value | |
let s = singleResult :> ITableEntity | |
printfn "A customer" | |
printfn $"customer: {s.RowKey} {s.PartitionKey}" | |
try | |
singleResult.PhoneNumber <- "425-555-0103" | |
table.UpdateEntity(singleResult, ETag("etag"), TableUpdateMode.Replace) |> ignore | |
Console.WriteLine("Update succeeeded") | |
with e -> | |
Console.WriteLine("Update failed") | |
try | |
singleResult.PhoneNumber <- "425-555-0104" | |
ignore(table.UpsertEntity(singleResult, TableUpdateMode.Replace)) | |
Console.WriteLine("Update succeeeded") | |
with e -> | |
Console.WriteLine("Update failed") | |
let subsetResults = query{ | |
for customer in table.Query<Customer>() do | |
select customer.Email | |
} | |
printfn "Only email" | |
subsetResults |> Seq.iter (printfn "%s") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment