@@ -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
7093let 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+
220282let 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
242313let execRead ( getClient : ConnectionOperation -> CosmosClient ) ( op : ReadOp < 'T >) =
243314 let connInfo = op.Connection
0 commit comments