public async Task <bool> CreateCollectionIfNotExistsAsync(string cosmosDatabase, string cosmosColection, int docDBRUs, CosmosOperation operation)
        {
            bool status = false;

            try
            {
                this.requestOptions = this.GetDocDbOptions(docDBRUs);
                var uri = $"/dbs/{cosmosDatabase}/colls/{cosmosColection}";
                await this.client.ReadDocumentCollectionAsync(uri, this.requestOptions);

                status = true;
            }
            catch (DocumentClientException)
            {
                await this.CreateCollectionAsync(cosmosDatabase, cosmosColection, docDBRUs, operation.ToString());

                status = true;
            }

            return(status);
        }
Example #2
0
        private async Task CreateCollectionAsync(string cosmosDatabase, string cosmosCollection, int docDBRUs, CosmosOperation operation)
        {
            try
            {
                var collection = new DocumentCollection {
                    Id = cosmosCollection
                };

                var index    = Microsoft.Azure.Documents.Index.Range(DataType.String, -1);
                var indexing = new IndexingPolicy(index)
                {
                    IndexingMode = IndexingMode.Consistent
                };
                collection.IndexingPolicy = indexing;

                if (operation == CosmosOperation.Device)
                {
                    collection.PartitionKey.Paths.Add("/deviceId");
                }

                var dbUri = "/dbs/" + cosmosDatabase;
                this.requestOptions = this.GetDocDbOptions(docDBRUs);
                await this.client.CreateDocumentCollectionAsync(dbUri, collection, this.requestOptions);
            }
            catch (DocumentClientException)
            {
                // If the collection is already created it will throw a DocumentClientException and we do not want to handle that
                // So we do not throw this exception
            }
            catch (Exception e)
            {
                throw new ApplicationException("Error while creating DocumentDb collection", e);
            }
        }