public async Task <bool> EnsureCreatedAsync <TEntity>(
            string databaseId,
            string collectionId,
            int collectionThroughput,
            IndexingPolicy indexingPolicy           = null,
            ThroughputBehaviour onDatabaseBehaviour = ThroughputBehaviour.UseDatabaseThroughput,
            UniqueKeyPolicy uniqueKeyPolicy         = null) where TEntity : class
        {
            var collectionResource = await _cosmonautClient.GetCollectionAsync(databaseId, collectionId);

            var databaseHasOffer = (await _cosmonautClient.GetOfferV2ForDatabaseAsync(databaseId)) != null;

            if (collectionResource != null)
            {
                return(true);
            }

            var newCollection = new DocumentCollection
            {
                Id              = collectionId,
                IndexingPolicy  = indexingPolicy ?? CosmosConstants.DefaultIndexingPolicy,
                UniqueKeyPolicy = uniqueKeyPolicy ?? CosmosConstants.DefaultUniqueKeyPolicy
            };

            SetPartitionKeyDefinitionForCollection(typeof(TEntity), newCollection);

            var finalCollectionThroughput = databaseHasOffer ? onDatabaseBehaviour == ThroughputBehaviour.DedicateCollectionThroughput ? (int?)collectionThroughput : null : collectionThroughput;

            newCollection = await _cosmonautClient.CreateCollectionAsync(databaseId, newCollection, new RequestOptions
            {
                OfferThroughput = finalCollectionThroughput
            });

            return(newCollection != null);
        }
        public async Task <bool> EnsureCreatedAsync <TEntity>(
            string databaseId,
            string collectionId,
            int collectionThroughput,
            IndexingPolicy indexingPolicy = null) where TEntity : class
        {
            var collectionResource = await _cosmonautClient.GetCollectionAsync(databaseId, collectionId);

            if (collectionResource != null)
            {
                return(true);
            }

            var newCollection = new DocumentCollection
            {
                Id             = collectionId,
                IndexingPolicy = indexingPolicy ?? CosmosConstants.DefaultIndexingPolicy
            };

            SetPartitionKeyDefinitionForCollection(typeof(TEntity), newCollection);

            newCollection = await _cosmonautClient.CreateCollectionAsync(databaseId, newCollection, new RequestOptions
            {
                OfferThroughput = collectionThroughput
            });

            return(newCollection != null);
        }
        public async Task <bool> EnsureCreatedAsync <TEntity>(
            string databaseId,
            string collectionId,
            int collectionThroughput,
            IndexingPolicy indexingPolicy = null) where TEntity : class
        {
            var isSharedCollection = typeof(TEntity).UsesSharedCollection();

            var collectionResource = await _cosmonautClient.GetCollectionAsync(databaseId, collectionId);

            if (collectionResource != null)
            {
                return(true);
            }

            var newCollection = new DocumentCollection
            {
                Id = collectionId
            };

            SetPartitionKeyIfCollectionIsNotShared(typeof(TEntity), isSharedCollection, newCollection);
            SetPartitionKeyAsIdIfCollectionIsShared(isSharedCollection, newCollection);

            if (indexingPolicy != null)
            {
                newCollection.IndexingPolicy = indexingPolicy;
            }

            newCollection = await _cosmonautClient.CreateCollectionAsync(databaseId, newCollection, new RequestOptions
            {
                OfferThroughput = collectionThroughput
            });

            return(newCollection != null);
        }
Beispiel #4
0
        public async Task WhenCosmosStoreInitialised_ThenDatabaseAndCollectionIsCreated()
        {
            _serviceProvider.GetService <ICosmosStore <Cat> >();

            var database = await _cosmonautClient.GetDatabaseAsync(_databaseId);

            var collection = await _cosmonautClient.GetCollectionAsync(_databaseId, _collectionName);

            database.Should().NotBeNull();
            database.Id.Should().Be(_databaseId);
            collection.Should().NotBeNull();
            collection.Id.Should().Be(_collectionName);
        }