Skip to content

Commit b86e1c9

Browse files
add deleteContainer operation
1 parent 9227a7f commit b86e1c9

3 files changed

Lines changed: 70 additions & 59 deletions

File tree

src/FSharp.CosmosDb/Cosmos.fs

Lines changed: 42 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -47,89 +47,72 @@ module Cosmos =
4747
Query = None
4848
Parameters = [] }
4949

50-
let query<'T> query op : ContainerOperation<'T> =
51-
Query
52-
{ defaultQueryOp () with
53-
Query = Some query
54-
Connection = op }
50+
let query<'T> query op : QueryOp<'T> =
51+
{ defaultQueryOp () with
52+
Query = Some query
53+
Connection = op }
5554

5655
let parameters arr op =
57-
match op with
58-
| Query q ->
59-
Query
60-
{ q with
61-
Parameters = q.Parameters @ arr }
62-
| _ -> failwith "Only the Query discriminated union supports parameters"
56+
{ op with QueryOp.Parameters = op.Parameters @ arr }
6357

6458
// --- INSERT --- //
6559

6660
let insertMany<'T> (values: 'T list) op =
67-
Insert { Connection = op; Values = values }
61+
{ InsertOp.Connection = op; Values = values }
6862

6963
let insert<'T> (value: 'T) op =
70-
Insert { Connection = op; Values = [ value ] }
64+
{ InsertOp.Connection = op; Values = [ value ] }
7165

7266
// --- INSERT --- //
7367

7468
let upsertMany<'T> (values: 'T list) op =
75-
Upsert { Connection = op; Values = values }
69+
{ UpsertOp.Connection = op; Values = values }
7670

7771
let upsert<'T> (value: 'T) op =
78-
Upsert { Connection = op; Values = [ value ] }
72+
{ UpsertOp.Connection = op; Values = [ value ] }
7973

8074
// --- UPDATE --- //
8175

8276
let update<'T> id partitionKey (updater: 'T -> 'T) op =
83-
Update
84-
{ Connection = op
85-
Id = id
86-
PartitionKey = partitionKey
87-
Updater = updater }
77+
{ UpdateOp.Connection = op
78+
Id = id
79+
PartitionKey = partitionKey
80+
Updater = updater }
8881

89-
// --- DELETE --- //
82+
// --- DELETE ITEM --- //
9083

91-
let delete<'T> id partitionKey op =
92-
Delete
93-
{ Connection = op
94-
Id = id
95-
PartitionKey = partitionKey }
84+
let deleteItem<'T> id partitionKey op =
85+
{ DeleteItemOp.Connection = op
86+
Id = id
87+
PartitionKey = partitionKey }
88+
89+
// --- DELETE CONTAINER --- //
90+
91+
let deleteContainer<'T> op : DeleteContainerOp<'T> =
92+
{ DeleteContainerOp.Connection = op }
9693

9794
// --- READ --- //
9895

9996
let read id partitionKey op =
100-
Read
101-
{ Connection = op
102-
Id = id
103-
PartitionKey = partitionKey }
97+
{ ReadOp.Connection = op
98+
Id = id
99+
PartitionKey = partitionKey }
104100

105101
// --- REPLACE --- //
106102

107103
let replace<'T> (item: 'T) op =
108-
Replace { Connection = op; Item = item }
104+
{ ReplaceOp.Connection = op; Item = item }
109105

110106
// --- Execute --- //
111107

112108
let private getClient (connInfo: ConnectionOperation) = connInfo.GetClient()
113109

114110
let dispose (connInfo: ConnectionOperation) = (connInfo :> IDisposable).Dispose()
115111

116-
let execAsync<'T> (op: ContainerOperation<'T>) =
117-
match op with
118-
| Query op -> OperationHandling.execQuery getClient op
119-
| Insert op -> OperationHandling.execInsert getClient op
120-
| Update op -> OperationHandling.execUpdate getClient op
121-
| Delete op -> OperationHandling.execDelete getClient op
122-
| Upsert op -> OperationHandling.execUpsert getClient op
123-
| Read op -> OperationHandling.execRead getClient op
124-
| Replace op -> OperationHandling.execReplace getClient op
125-
126-
let execBatchAsync<'T> batchSize (op: ContainerOperation<'T>) =
127-
match op with
128-
| Query op ->
129-
let queryOps = QueryRequestOptions()
130-
queryOps.MaxItemCount <- batchSize
131-
OperationHandling.execQueryBatch getClient op queryOps
132-
| _ -> failwith "Batch return operation only supported with query operations, use `execAsync` instead."
112+
let execBatchAsync<'T> batchSize op =
113+
let queryOps = QueryRequestOptions()
114+
queryOps.MaxItemCount <- batchSize
115+
OperationHandling.execQueryBatch getClient op queryOps
133116

134117
// --- Access Cosmos APIs directly --- //
135118

@@ -233,3 +216,14 @@ module Cosmos =
233216
processor.Build()
234217
| None ->
235218
failwith "Unable to connect the change feed. Ensure the container and lease container info is all set"
219+
220+
type Cosmos =
221+
static member private getClient (connInfo: ConnectionOperation) = connInfo.GetClient()
222+
static member execAsync (op: QueryOp<'T>) = OperationHandling.execQuery Cosmos.getClient op
223+
static member execAsync op = OperationHandling.execInsert Cosmos.getClient op
224+
static member execAsync op = OperationHandling.execUpdate Cosmos.getClient op
225+
static member execAsync op = OperationHandling.execDeleteItem Cosmos.getClient op
226+
static member execAsync op = OperationHandling.execDeleteContainer Cosmos.getClient op
227+
static member execAsync op = OperationHandling.execUpsert Cosmos.getClient op
228+
static member execAsync op = OperationHandling.execRead Cosmos.getClient op
229+
static member execAsync op = OperationHandling.execReplace Cosmos.getClient op

src/FSharp.CosmosDb/OperationHandling.fs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ let execUpdate (getClient: ConnectionOperation -> CosmosClient) (op: UpdateOp<'T
189189

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

192-
let execDelete (getClient: ConnectionOperation -> CosmosClient) (op: DeleteOp<'T>) =
192+
let execDeleteItem (getClient: ConnectionOperation -> CosmosClient) (op: DeleteItemOp<'T>) =
193193
let connInfo = op.Connection
194194
let client = getClient connInfo
195195

@@ -216,7 +216,29 @@ let execDelete (getClient: ConnectionOperation -> CosmosClient) (op: DeleteOp<'T
216216
|> AsyncSeq.ofSeqAsync
217217

218218
| None -> failwith "Unable to read from the container to get the item for updating"
219+
220+
let execDeleteContainer (getClient: ConnectionOperation -> CosmosClient) (op: DeleteContainerOp<'T>) =
221+
let connInfo = op.Connection
222+
let client = getClient connInfo
223+
224+
let result =
225+
maybe {
226+
let! databaseId = connInfo.DatabaseId
227+
let! containerName = connInfo.ContainerName
228+
229+
let db = client.GetDatabase databaseId
230+
231+
let container = db.GetContainer containerName
232+
233+
return
234+
container.DeleteContainerAsync ()
235+
|> Async.AwaitTask
236+
}
219237

238+
match result with
239+
| Some result -> result
240+
| None -> failwith "Unable to delete container"
241+
220242
let execRead (getClient: ConnectionOperation -> CosmosClient) (op: ReadOp<'T>) =
221243
let connInfo = op.Connection
222244
let client = getClient connInfo

src/FSharp.CosmosDb/Types.fs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace FSharp.CosmosDb
22

3+
open FSharp.CosmosDb
34
open Microsoft.Azure.Cosmos
45
open System.Threading
56
open System.Threading.Tasks
@@ -100,10 +101,13 @@ type UpdateOp<'T> =
100101
PartitionKey: string
101102
Updater: 'T -> 'T }
102103

103-
type DeleteOp<'T> =
104+
type DeleteItemOp<'T> =
104105
{ Connection: ConnectionOperation
105106
Id: string
106107
PartitionKey: string }
108+
109+
type DeleteContainerOp<'T> =
110+
{ Connection: ConnectionOperation }
107111

108112
type ReadOp<'T> =
109113
{ Connection: ConnectionOperation
@@ -114,15 +118,6 @@ type ReplaceOp<'T> =
114118
{ Connection: ConnectionOperation
115119
Item: 'T }
116120

117-
type ContainerOperation<'T> =
118-
| Query of QueryOp<'T>
119-
| Insert of InsertOp<'T>
120-
| Update of UpdateOp<'T>
121-
| Delete of DeleteOp<'T>
122-
| Upsert of UpsertOp<'T>
123-
| Read of ReadOp<'T>
124-
| Replace of ReplaceOp<'T>
125-
126121
type ChangeFeedOptions<'T> =
127122
{ Connection: ConnectionOperation
128123
Processor: string

0 commit comments

Comments
 (0)