private static RequestOptions GetRequestOptions( string partitionKey = null, int?throughput = null, int?sharedThroughput = null, CosmosConsistencyLevel consistencyLevel = CosmosConsistencyLevel.Strong) { var requestOptions = new RequestOptions(); if (throughput != null) { requestOptions.OfferThroughput = throughput; } if (sharedThroughput != null) { requestOptions.SharedOfferThroughput = sharedThroughput; } if (partitionKey != null) { requestOptions.PartitionKey = new PartitionKey(partitionKey); } requestOptions.ConsistencyLevel = GetConsistencyLevel(consistencyLevel); return(requestOptions); }
public async Task <bool> CreateCollectionIfNotExistsAsync( string databaseId, string collectionId, string partitionKey, CosmosConsistencyLevel cosmosConsistencyLevel = CosmosConsistencyLevel.Strong, CosmosIndexingMode cosmosIndexingMode = CosmosIndexingMode.Consistent) { var database = _client.GetDatabase(databaseId); var indexingMode = GetIndexingMode(cosmosIndexingMode); var properties = new ContainerProperties(collectionId, partitionKey) { //PartitionKeyDefinitionVersion = PartitionKeyDefinitionVersion.V2, PartitionKeyPath = partitionKey, IndexingPolicy = new IndexingPolicy { Automatic = true, IndexingMode = indexingMode } }; var requestOptions = new ItemRequestOptions { ConsistencyLevel = GetConsistencyLevel(cosmosConsistencyLevel) }; var response = await database.CreateContainerIfNotExistsAsync( properties, requestOptions : requestOptions, cancellationToken : default(CancellationToken)); return(IsResponseValid(response.StatusCode)); }
private static ConsistencyLevel GetConsistencyLevel(CosmosConsistencyLevel cosmosConsistencyLevel) { return(cosmosConsistencyLevel switch { CosmosConsistencyLevel.Strong => ConsistencyLevel.Strong, CosmosConsistencyLevel.BoundedStaleness => ConsistencyLevel.BoundedStaleness, CosmosConsistencyLevel.Session => ConsistencyLevel.Session, CosmosConsistencyLevel.Eventual => ConsistencyLevel.Eventual, CosmosConsistencyLevel.ConsistentPrefix => ConsistencyLevel.ConsistentPrefix, _ => throw new NotSupportedException($"{nameof(cosmosConsistencyLevel)} value({cosmosConsistencyLevel}) is not supported"), });
public async Task <bool> CreateDatabaseIfNotExistsAsync( string databaseId, int?sharedThroughput, CosmosConsistencyLevel cosmosConsistencyLevel = CosmosConsistencyLevel.Strong) { var response = await _client.CreateDatabaseIfNotExistsAsync( databaseId, sharedThroughput, requestOptions : null, default(CancellationToken)); return(IsResponseValid(response.StatusCode)); }
public async Task <bool> CreateDatabaseIfNotExistsAsync( string databaseId, int?sharedThroughput, CosmosConsistencyLevel cosmosConsistencyLevel = CosmosConsistencyLevel.Strong) { var options = GetRequestOptions(partitionKey: null, throughput: null, sharedThroughput, cosmosConsistencyLevel); var database = new Database { Id = databaseId }; var response = await _client.CreateDatabaseIfNotExistsAsync(database, options); return(IsResponseValid(response.StatusCode)); }
public async Task <bool> CreateCollectionIfNotExistsAsync( string databaseId, string collectionId, string partitionKey, CosmosConsistencyLevel cosmosConsistencyLevel = CosmosConsistencyLevel.Strong, CosmosIndexingMode cosmosIndexingMode = CosmosIndexingMode.Consistent) { var uri = UriFactory.CreateDatabaseUri(databaseId); var requestOptions = GetRequestOptions(partitionKey, throughput: null, sharedThroughput: null, cosmosConsistencyLevel); var indexingMode = GetIndexingMode(cosmosIndexingMode); var documentCollection = GetDocumentCollection(collectionId, partitionKey, indexingMode); var response = await _client.CreateDocumentCollectionIfNotExistsAsync(uri, documentCollection, requestOptions); return(IsResponseValid(response.StatusCode)); }