Example #1
0
        public async Task <DocumentCollection> InitializeCollection(IDocumentClient documentClient)
        {
            DocumentCollection existingDocumentCollection = await documentClient.TryGetDocumentCollectionAsync(_relativeCollectionUri);

            if (existingDocumentCollection == null)
            {
                _logger.LogDebug("Creating document collection if not exits: {collectionId}", _collectionId);
                var documentCollection = new DocumentCollection
                {
                    Id           = _collectionId,
                    PartitionKey = new PartitionKeyDefinition
                    {
                        Paths =
                        {
                            $"/{KnownDocumentProperties.PartitionKey}",
                        },
                    },
                };

                var requestOptions = new RequestOptions {
                    OfferThroughput = _initialCollectionThroughput
                };

                existingDocumentCollection = await documentClient.CreateDocumentCollectionIfNotExistsAsync(
                    _relativeDatabaseUri, _relativeCollectionUri, documentCollection, requestOptions);
            }

            await _upgradeManager.SetupCollectionAsync(documentClient, existingDocumentCollection);

            return(existingDocumentCollection);
        }
        /// <summary>
        /// Ensures that the necessary database and collection exist with the proper indexing policy and stored procedures
        /// </summary>
        /// <param name="documentClient">The <see cref="DocumentClient"/> instance to use for initialization.</param>
        /// <param name="cosmosDataStoreConfiguration">The data store configuration.</param>
        /// <returns>A task</returns>
        public async Task InitializeDataStore(IDocumentClient documentClient, CosmosDataStoreConfiguration cosmosDataStoreConfiguration)
        {
            EnsureArg.IsNotNull(documentClient, nameof(documentClient));
            EnsureArg.IsNotNull(cosmosDataStoreConfiguration, nameof(cosmosDataStoreConfiguration));

            try
            {
                _logger.LogInformation("Initializing Cosmos DB collection {CollectionUri}", cosmosDataStoreConfiguration.AbsoluteCollectionUri);

                if (cosmosDataStoreConfiguration.AllowDatabaseCreation)
                {
                    _logger.LogDebug("CreateDatabaseIfNotExists {DatabaseId})", cosmosDataStoreConfiguration.DatabaseId);

                    await documentClient.CreateDatabaseIfNotExistsAsync(new Database
                    {
                        Id = cosmosDataStoreConfiguration.DatabaseId,
                    });
                }

                _logger.LogDebug("CreateDocumentCollectionIfNotExists {HostDescription}", cosmosDataStoreConfiguration.AbsoluteCollectionUri);

                DocumentCollection existingDocumentCollection = await documentClient.TryGetDocumentCollectionAsync(cosmosDataStoreConfiguration.RelativeCollectionUri);

                if (existingDocumentCollection == null)
                {
                    var documentCollection = new DocumentCollection
                    {
                        Id           = cosmosDataStoreConfiguration.CollectionId,
                        PartitionKey = new PartitionKeyDefinition
                        {
                            Paths =
                            {
                                $"/{KnownResourceWrapperProperties.PartitionKey}",
                            },
                        },
                    };

                    existingDocumentCollection = await documentClient.CreateDocumentCollectionIfNotExistsAsync(
                        cosmosDataStoreConfiguration.RelativeDatabaseUri, cosmosDataStoreConfiguration.RelativeCollectionUri, documentCollection);
                }

                await _upgradeManager.SetupCollectionAsync(documentClient, existingDocumentCollection);

                _logger.LogInformation("Cosmos DB collection {CollectionUri} successfully initialized", cosmosDataStoreConfiguration.AbsoluteCollectionUri);
            }
            catch (Exception ex)
            {
                _logger.LogCritical(ex, "Cosmos DB collection {CollectionUri} initialization failed", cosmosDataStoreConfiguration.AbsoluteCollectionUri);
                throw;
            }
        }