/// <summary> /// Creates the Cosmos database and defined collections if not exists. /// </summary> /// <returns></returns> public Task CreateIfNotExistsAsync(CreateDbOptions options = null) { if (options != null && !ThroughputValidator.IsValidThroughput(options.Throughput)) { throw new ArgumentException("The provided throughput is not valid. Must be between 400 and 1000000 and in increments of 100."); } return(CreateIfNotExistsInternalAsync(options)); }
private async Task CreateIfNotExistsInternalAsync(CreateDbOptions options = null) { var databaseId = _database.Model.DatabaseId; var databaseThroughput = options != null && options.ThroughputType == ThroughputType.Database ? options.Throughput : (int?)null; var containerThroughput = options != null && options.ThroughputType == ThroughputType.Container ? options.Throughput : (int?)null; await _cosmosClient.CreateDatabaseIfNotExistsAsync(databaseId, databaseThroughput).ConfigureAwait(false); foreach (var collectionModel in _database.Model.CollectionModels) { await _cosmosClient.GetDatabase(databaseId) .DefineContainer(name: collectionModel.CollectionId, partitionKeyPath: "/id") .WithIndexingPolicy() .WithIncludedPaths() .Path("/*") .Path("/_all/*") .Attach() .WithIndexingMode(IndexingMode.Consistent) .Attach() .CreateIfNotExistsAsync(containerThroughput) .ConfigureAwait(false); } }