Skip to content

Commit 4f87a13

Browse files
committed
handing the sample create DB and container
Also, added a createDatabase operation
1 parent 6214110 commit 4f87a13

4 files changed

Lines changed: 77 additions & 6 deletions

File tree

samples/FSharp.CosmosDb.Samples/Program.fs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,6 @@ let deleteFamily conn id pk =
5353

5454
[<EntryPoint>]
5555
let main argv =
56-
ServicePointManager.ServerCertificateValidationCallback <-
57-
fun sender certs chain errors ->
58-
printfn "%A" sender
59-
true
60-
6156
let environmentName =
6257
System.Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")
6358

@@ -91,6 +86,19 @@ let main argv =
9186

9287
printfn "Getting ready to do some Cosmos operations"
9388

89+
do!
90+
conn
91+
|> Cosmos.createDatabaseIfNotExists
92+
|> Cosmos.execAsync
93+
|> AsyncSeq.iter (fun _ -> ())
94+
95+
do!
96+
conn
97+
|> Cosmos.createContainerIfNotExists<Family>
98+
|> Cosmos.execAsync
99+
|> AsyncSeq.iter (fun _ -> ())
100+
101+
94102
let families =
95103
[| { Id = "Powell.1"
96104
LastName = "Powell"
@@ -146,7 +154,6 @@ let main argv =
146154

147155
do!
148156
conn
149-
|> Cosmos.container "Family"
150157
|> Cosmos.deleteContainerIfExists
151158
|> Cosmos.execAsync
152159
|> Async.Ignore

src/FSharp.CosmosDb/Cosmos.fs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ module Cosmos =
5858
let databaseExists<'T> op =
5959
{ CheckIfDatabaseExistsOp.Connection = op }
6060

61+
// --- CREATE DATABASE --- //
62+
let createDatabase op = { CreateDatabaseOp.Connection = op }
63+
64+
let createDatabaseIfNotExists op =
65+
{ CreateDatabaseIfNotExistsOp.Connection = op }
66+
6167
// --- INSERT --- //
6268

6369
let insertMany<'T> (values: 'T list) op =
@@ -282,3 +288,9 @@ type Cosmos =
282288

283289
static member execAsync op =
284290
OperationHandling.execCreateContainerIfNotExists Cosmos.getClient op
291+
292+
static member execAsync op =
293+
OperationHandling.execCreateDatabase Cosmos.getClient op
294+
295+
static member execAsync op =
296+
OperationHandling.execCreateDatabaseIfNotExists Cosmos.getClient op

src/FSharp.CosmosDb/OperationHandling.fs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,3 +425,51 @@ let execCreateContainerIfNotExists
425425
| None ->
426426
failwith
427427
"Unable to create the container. Ensure the name is valid and there is a partition key defined on the type"
428+
429+
let execCreateDatabase (getClient: ConnectionOperation -> CosmosClient) (op: CreateDatabaseOp) =
430+
let connInfo = op.Connection
431+
let client = getClient connInfo
432+
433+
let result =
434+
maybe {
435+
let! databaseId = connInfo.DatabaseId
436+
437+
return
438+
client.CreateDatabaseAsync databaseId
439+
|> Async.AwaitTask
440+
}
441+
442+
match result with
443+
| Some result ->
444+
[ async {
445+
let! res = result
446+
return res.Resource
447+
} ]
448+
|> AsyncSeq.ofSeqAsync
449+
| None ->
450+
failwith
451+
"Unable to create the database. Ensure the database does not already exist and that your connection info is valid"
452+
453+
let execCreateDatabaseIfNotExists (getClient: ConnectionOperation -> CosmosClient) (op: CreateDatabaseIfNotExistsOp) =
454+
let connInfo = op.Connection
455+
let client = getClient connInfo
456+
457+
let result =
458+
maybe {
459+
let! databaseId = connInfo.DatabaseId
460+
461+
return
462+
client.CreateDatabaseIfNotExistsAsync databaseId
463+
|> Async.AwaitTask
464+
}
465+
466+
match result with
467+
| Some result ->
468+
[ async {
469+
let! res = result
470+
return res.Resource
471+
} ]
472+
|> AsyncSeq.ofSeqAsync
473+
| None ->
474+
failwith
475+
"Unable to create the database. Ensure the database does not already exist and that your connection info is valid"

src/FSharp.CosmosDb/Types.fs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ type GetContainerPropertiesOp = { Connection: ConnectionOperation }
109109

110110
type CheckIfContainerExistsOp = { Connection: ConnectionOperation }
111111

112+
type CreateDatabaseOp = { Connection: ConnectionOperation }
113+
114+
type CreateDatabaseIfNotExistsOp = { Connection: ConnectionOperation }
115+
112116
type CreateContainerOp<'T> = { Connection: ConnectionOperation }
113117

114118
type CreateContainerIfNotExistsOp<'T> = { Connection: ConnectionOperation }

0 commit comments

Comments
 (0)