internal static async Task CreateDatabaseAndCollectionIfNotExistAsync()

in src/WebJobs.Extensions.CosmosDB/CosmosDBUtility.cs [44:80]


        internal static async Task CreateDatabaseAndCollectionIfNotExistAsync(CosmosClient service, string databaseName, string containerName, string partitionKey, int throughput, bool setTTL = false)
        {
            await service.CreateDatabaseIfNotExistsAsync(databaseName);

            int? desiredThroughput = null;
            if (throughput > 0)
            {
                desiredThroughput = throughput;
            }

            Database database = service.GetDatabase(databaseName);

            try
            {
                await database.GetContainer(containerName).ReadContainerAsync();
            }
            catch (CosmosException cosmosException) when (cosmosException.StatusCode == System.Net.HttpStatusCode.NotFound)
            {
                ContainerProperties containerProperties = new ContainerProperties()
                {
                    Id = containerName
                };

                if (!string.IsNullOrEmpty(partitionKey))
                {
                    containerProperties.PartitionKeyPath = partitionKey;
                }

                if (setTTL)
                {
                    // Enabling TTL on the container without any defined time. TTL is set on the individual items.
                    containerProperties.DefaultTimeToLive = -1;
                }

                await database.CreateContainerAsync(containerProperties, desiredThroughput);
            }
        }