Skip to content

Commit 77675d3

Browse files
add deleteContainerIfExists
also adds these operations: - containerExists - getContainerProperties - databaseExists
1 parent cc85b84 commit 77675d3

4 files changed

Lines changed: 111 additions & 0 deletions

File tree

samples/FSharp.CosmosDb.Samples/Program.fs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,13 @@ let main argv =
138138
|> Cosmos.deleteContainer
139139
|> Cosmos.execAsync
140140
|> Async.Ignore
141+
142+
do!
143+
conn
144+
|> Cosmos.container "Family"
145+
|> Cosmos.deleteContainerIfExists
146+
|> Cosmos.execAsync
147+
|> Async.Ignore
141148

142149
return 0 // return an integer exit code
143150
}

src/FSharp.CosmosDb/Cosmos.fs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ module Cosmos =
5454

5555
let parameters arr op =
5656
{ op with QueryOp.Parameters = op.Parameters @ arr }
57+
58+
// --- DATABASE EXISTS --- //
59+
let databaseExists<'T> op =
60+
{ CheckIfDatabaseExistsOp.Connection = op }
5761

5862
// --- INSERT --- //
5963

@@ -85,12 +89,25 @@ module Cosmos =
8589
{ DeleteItemOp.Connection = op
8690
Id = id
8791
PartitionKey = partitionKey }
92+
93+
// --- GET CONTAINER PROPERTIES --- //
94+
let getContainerProperties op =
95+
{ GetContainerPropertiesOp.Connection = op }
96+
97+
// --- CONTAINER EXISTS --- //
98+
let containerExists op =
99+
{ CheckIfContainerExistsOp.Connection = op }
88100

89101
// --- DELETE CONTAINER --- //
90102

91103
let deleteContainer<'T> op : DeleteContainerOp<'T> =
92104
{ DeleteContainerOp.Connection = op }
93105

106+
// --- DELETE CONTAINER IF EXISTS --- //
107+
108+
let deleteContainerIfExists op : DeleteContainerIfExistsOp =
109+
{ DeleteContainerIfExistsOp.Connection = op }
110+
94111
// --- READ --- //
95112

96113
let read id partitionKey op =
@@ -220,10 +237,14 @@ module Cosmos =
220237
type Cosmos =
221238
static member private getClient (connInfo: ConnectionOperation) = connInfo.GetClient()
222239
static member execAsync (op: QueryOp<'T>) = OperationHandling.execQuery Cosmos.getClient op
240+
static member execAsync op = OperationHandling.execCheckIfDatabaseExists Cosmos.getClient op
223241
static member execAsync op = OperationHandling.execInsert Cosmos.getClient op
224242
static member execAsync op = OperationHandling.execUpdate Cosmos.getClient op
225243
static member execAsync op = OperationHandling.execDeleteItem Cosmos.getClient op
244+
static member execAsync op = OperationHandling.execGetContainerProperties Cosmos.getClient op
245+
static member execAsync op = OperationHandling.execCheckIfContainerExists Cosmos.getClient op
226246
static member execAsync op = OperationHandling.execDeleteContainer Cosmos.getClient op
247+
static member execAsync op = OperationHandling.execDeleteContainerIfExists Cosmos.getClient op
227248
static member execAsync op = OperationHandling.execUpsert Cosmos.getClient op
228249
static member execAsync op = OperationHandling.execRead Cosmos.getClient op
229250
static member execAsync op = OperationHandling.execReplace Cosmos.getClient op

src/FSharp.CosmosDb/OperationHandling.fs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,29 @@ let execQueryBatch (getClient: ConnectionOperation -> CosmosClient) (op: QueryOp
6666
| Some result -> result |> AsyncSeq.ofAsyncFeedIterator
6767
| None ->
6868
failwith "Unable to construct a query as some values are missing across the database, container name and query"
69+
70+
let execCheckIfDatabaseExists (getClient: ConnectionOperation -> CosmosClient) (op: CheckIfDatabaseExistsOp) =
71+
let connInfo = op.Connection
72+
let client = getClient connInfo
73+
74+
use iterator = client.GetDatabaseQueryIterator<DatabaseProperties>()
75+
76+
match connInfo.DatabaseId with
77+
| Some databaseId ->
78+
iterator
79+
|> AsyncSeq.unfold (fun t ->
80+
if iterator.HasMoreResults then
81+
Some (iterator.ReadNextAsync(), iterator)
82+
else
83+
None)
84+
|> AsyncSeq.collect (fun i ->
85+
asyncSeq {
86+
let! c = i |> Async.AwaitTask
87+
for x in c do
88+
yield x
89+
})
90+
|> AsyncSeq.exists (fun i -> i.Id = databaseId)
91+
| None -> failwith "failed to check if database exists"
6992

7093
let execInsert (getClient: ConnectionOperation -> CosmosClient) (op: InsertOp<'T>) =
7194
let connInfo = op.Connection
@@ -217,6 +240,45 @@ let execDeleteItem (getClient: ConnectionOperation -> CosmosClient) (op: DeleteI
217240

218241
| None -> failwith "Unable to read from the container to get the item for updating"
219242

243+
let execGetContainerProperties (getClient: ConnectionOperation -> CosmosClient) (op: GetContainerPropertiesOp) =
244+
let connInfo = op.Connection
245+
let client = getClient connInfo
246+
247+
let containerName =
248+
match connInfo.ContainerName with
249+
| None -> failwith "ContainerName is not provided"
250+
| Some containerName -> containerName
251+
252+
use iterator =
253+
match connInfo.DatabaseId with
254+
| None -> failwith "DatabaseId is not provided"
255+
| Some databaseId ->
256+
client
257+
.GetDatabase(databaseId)
258+
.GetContainerQueryIterator<ContainerProperties>()
259+
260+
iterator
261+
|> AsyncSeq.unfold (fun t ->
262+
if iterator.HasMoreResults then
263+
Some (iterator.ReadNextAsync(), iterator)
264+
else
265+
None)
266+
|> AsyncSeq.collect (fun i ->
267+
asyncSeq {
268+
let! c = i |> Async.AwaitTask
269+
for x in c do
270+
yield x
271+
})
272+
|> AsyncSeq.tryFind (fun i -> i.Id = containerName)
273+
274+
let execCheckIfContainerExists (getClient: ConnectionOperation -> CosmosClient) (op: CheckIfContainerExistsOp) =
275+
async {
276+
let! containerProperties = execGetContainerProperties getClient { Connection= op.Connection }
277+
278+
return containerProperties
279+
|> Option.isSome
280+
}
281+
220282
let execDeleteContainer (getClient: ConnectionOperation -> CosmosClient) (op: DeleteContainerOp<'T>) =
221283
let connInfo = op.Connection
222284
let client = getClient connInfo
@@ -238,6 +300,15 @@ let execDeleteContainer (getClient: ConnectionOperation -> CosmosClient) (op: De
238300
match result with
239301
| Some result -> result
240302
| None -> failwith "Unable to delete container"
303+
304+
let execDeleteContainerIfExists (getClient: ConnectionOperation -> CosmosClient) (op: DeleteContainerIfExistsOp) =
305+
async {
306+
let! databaseExists = execCheckIfDatabaseExists getClient { Connection= op.Connection }
307+
let! containerExists = execCheckIfContainerExists getClient { Connection= op.Connection }
308+
if databaseExists && containerExists then
309+
do! execDeleteContainer getClient { Connection= op.Connection }
310+
|> Async.Ignore
311+
}
241312

242313
let execRead (getClient: ConnectionOperation -> CosmosClient) (op: ReadOp<'T>) =
243314
let connInfo = op.Connection

src/FSharp.CosmosDb/Types.fs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ type QueryOp<'T> =
8787
Query: string option
8888
Parameters: (string * obj) list }
8989

90+
type CheckIfDatabaseExistsOp =
91+
{ Connection: ConnectionOperation }
92+
9093
type InsertOp<'T> =
9194
{ Connection: ConnectionOperation
9295
Values: 'T list }
@@ -106,9 +109,18 @@ type DeleteItemOp<'T> =
106109
Id: string
107110
PartitionKey: string }
108111

112+
type GetContainerPropertiesOp =
113+
{ Connection: ConnectionOperation }
114+
115+
type CheckIfContainerExistsOp =
116+
{ Connection: ConnectionOperation }
117+
109118
type DeleteContainerOp<'T> =
110119
{ Connection: ConnectionOperation }
111120

121+
type DeleteContainerIfExistsOp =
122+
{ Connection: ConnectionOperation }
123+
112124
type ReadOp<'T> =
113125
{ Connection: ConnectionOperation
114126
Id: string

0 commit comments

Comments
 (0)