Example #1
0
        public async Task CreateDocumentCollectionIfNotExistsAsync <T>()
        {
            var packageCollection = new DocumentCollection();

            packageCollection.Id = typeof(T).Name;
            // I don't use PartitionKey until it required.
            await _client.CreateDocumentCollectionIfNotExistsAsync(
                UriFactory.CreateDatabaseUri(_databaseId),
                packageCollection);
        }
Example #2
0
        /// <summary>
        /// Insert entity to database even though collection for this entity wasn't created yet.
        /// </summary>
        /// <typeparam name="T">Type of entity to insert</typeparam>
        /// <param name="entity">Entity to insert</param>
        public async Task <bool> InsertAsync <T>(T entity, string collectionName) where T : IBaseEntity
        {
            await GetDBClient();

            await _documentClient.CreateDocumentCollectionIfNotExistsAsync(UriFactory.CreateDatabaseUri(cosmosDBName),
                                                                           new DocumentCollection { Id = typeof(T).Name });

            var result = await _documentClient.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri(cosmosDBName, collectionName), entity);

            return(result.StatusCode == System.Net.HttpStatusCode.Created);
        }
Example #3
0
 public async Task EnsureCollectionExists()
 {
     await DocumentClient.CreateDocumentCollectionIfNotExistsAsync(DatabaseUri, new DocumentCollection
     {
         Id = _collectionName
     });
 }
        /// <summary>
        /// Constructor
        /// </summary>
        protected DocumentRepository(
            string collectionName,
            IOptions <AzureCosmosDbSettings> options,
            IDocumentClient documentClient)
        {
            _collection     = collectionName;
            _partitionKey   = options.Value.PartitionKey;
            _database       = options.Value.Database;
            _documentClient = documentClient;
            _collectionUri  = UriFactory.CreateDocumentCollectionUri(_database, _collection);

            DocumentCollection documentCollection = new DocumentCollection
            {
                Id           = collectionName,
                PartitionKey = new PartitionKeyDefinition
                {
                    Paths = new Collection <string> {
                        _partitionKey
                    }
                }
            };
            Uri dbUri = UriFactory.CreateDatabaseUri(_database);

            documentClient
            .CreateDocumentCollectionIfNotExistsAsync(dbUri, documentCollection)
            .GetAwaiter()
            .GetResult();
        }
Example #5
0
 private static async Task CreateCollectionIfNotExistsAsync(IDocumentClient documentClient, string collectionId, IOptions <CosmosDbOptions> settings)
 {
     try
     {
         await documentClient.ReadDocumentCollectionAsync(UriFactory.CreateDocumentCollectionUri(settings.Value.DatabaseId, collectionId));
     }
     catch (DocumentClientException e)
     {
         if (e.StatusCode == System.Net.HttpStatusCode.NotFound)
         {
             await documentClient.CreateDocumentCollectionIfNotExistsAsync(
                 UriFactory.CreateDatabaseUri(settings.Value.DatabaseId),
                 new DocumentCollection
             {
                 Id           = collectionId,
                 PartitionKey = new PartitionKeyDefinition
                 {
                     Paths = new Collection <string>
                     {
                         "/type"
                     }
                 }
             },
                 new RequestOptions
             {
                 OfferThroughput  = settings.Value.OfferThroughput,
                 ConsistencyLevel = settings.Value.ConsistencyLevel
             });
         }
         else
         {
             throw;
         }
     }
 }
Example #6
0
        public async Task GivenDocumentCollectionAlreadyExists_WhenCreatingDocumentCollectionIfNotExists_ThenIndexingPolicyShouldBeUpdated()
        {
            Uri collectionUri = new Uri("http://database/collection");

            IndexingPolicy expectedIndexingPolicy = new IndexingPolicy();

            DocumentCollection documentCollection = new DocumentCollection()
            {
                IndexingPolicy = expectedIndexingPolicy,
            };

            DocumentCollection existingDocumentCollection = new DocumentCollection();

            _documentClient.ReadDocumentCollectionAsync(collectionUri, Arg.Any <RequestOptions>()).Returns(
                new ResourceResponse <DocumentCollection>(existingDocumentCollection));

            // Check to make sure the index policy is requested to be updated.
            _documentClient.ReplaceDocumentCollectionAsync(
                Arg.Is <DocumentCollection>(x => x == existingDocumentCollection && x.IndexingPolicy == expectedIndexingPolicy),
                Arg.Any <RequestOptions>())
            .Returns(new ResourceResponse <DocumentCollection>(existingDocumentCollection));

            DocumentCollection result = await _documentClient.CreateDocumentCollectionIfNotExistsAsync(new Uri("http://database"), collectionUri, documentCollection);

            Assert.NotNull(result);
        }
Example #7
0
        private static async Task <bool> CheckList(IDocumentClient client, string link)
        {
            var CreateDatabase = Environment.GetEnvironmentVariable("CreateDatabase");

            if (CreateDatabase != null && CreateDatabase == "true")
            {
                var databaseDefinition = new Database {
                    Id = DatabaseId
                };
                await client.CreateDatabaseIfNotExistsAsync(databaseDefinition);

                var collectionDefinition = new DocumentCollection {
                    Id = CollectionId
                };
                await client.CreateDocumentCollectionIfNotExistsAsync(UriFactory.CreateDatabaseUri(DatabaseId),
                                                                      collectionDefinition);
            }

            var response = client.CreateDocumentQuery <Item>(UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId), $"select * from c WHERE  c.Url='{link}'").ToList();

            if (response.Count > 0)
            {
                return(true);
            }

            await client.CreateDocumentAsync(
                UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId),
                new Item()
            {
                Url = link
            });

            return(false);
        }
Example #8
0
        private async Task BuildCollection()
        {
            await _documentClient.CreateDatabaseIfNotExistsAsync(new Database { Id = databaseId });

            await _documentClient.CreateDocumentCollectionIfNotExistsAsync(UriFactory.CreateDatabaseUri(databaseId),
                                                                           new DocumentCollection { Id = collectionId });
        }
Example #9
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);
        }
        private static async Task <DocumentCollection> CreateDocumentCollectionIfNotExistsAsync(IDocumentClient documentClient, string databaseName, string collectionName,
                                                                                                string partitionKey, int throughput)
        {
            Uri databaseUri = UriFactory.CreateDatabaseUri(databaseName);

            DocumentCollection documentCollection = new DocumentCollection
            {
                Id = collectionName
            };

            if (!string.IsNullOrEmpty(partitionKey))
            {
                documentCollection.PartitionKey.Paths.Add(partitionKey);
            }

            RequestOptions collectionOptions = null;

            if (throughput != 0)
            {
                collectionOptions = new RequestOptions
                {
                    OfferThroughput = throughput
                };
            }

            return(await documentClient.CreateDocumentCollectionIfNotExistsAsync(databaseUri, documentCollection, collectionOptions));
        }
Example #11
0
 async Task CreateContainerAsync()
 {
     DocumentCollection collection = new DocumentCollection()
     {
         Id = config["DataStore:Containers:Identities:Id"]
     };
     Uri dbUri = UriFactory.CreateDatabaseUri(config["DataStore:Id"]);
     await dbClient.CreateDocumentCollectionIfNotExistsAsync(dbUri, collection);
 }
        private async Task <DocumentCollection> EnsureCollectionIsCreated(IDocumentClient documentClient)
        {
            await documentClient.CreateDatabaseIfNotExistsAsync(new Database { Id = DatabaseId });

            ResourceResponse <DocumentCollection> collectionResponse = await documentClient
                                                                       .CreateDocumentCollectionIfNotExistsAsync(UriFactory.CreateDatabaseUri(DatabaseId), new DocumentCollection { Id = CollectionId });

            return(collectionResponse.Resource);
        }
Example #13
0
        private Task EnsureDocumentCollectionAsync()
        {
            var databaseUri = _configs.DatabaseUri;

            var documentCollection = new DocumentCollection
            {
                Id = _configs.DocumentCollectionId
            };

            return(_client.CreateDocumentCollectionIfNotExistsAsync(databaseUri, documentCollection));
        }
Example #14
0
        private async Task CreateCollection(string database, string collection, RequestOptions requestOptions = null)
        {
            var databaseUri = UriFactory.CreateDatabaseUri(database);

            var documentCollection = new DocumentCollection {
                Id = collection
            };

            documentCollection.PartitionKey.Paths.Add("/Language");

            await _documentClient.CreateDocumentCollectionIfNotExistsAsync(databaseUri, documentCollection, requestOptions);
        }
Example #15
0
        public static async Task CreateCosmosDbAndCollectionIfNotExists(this IDocumentClient documentClient, ILogger logger, string databaseId, string collectionId)
        {
            logger.Info($"Creating Cosmos Db Collection if not exists for database {databaseId} with id {collectionId}");
            var collection = new DocumentCollection {
                Id = collectionId
            };
            var requestOptions = new RequestOptions {
                OfferThroughput = 400
            };
            await documentClient.CreateDocumentCollectionIfNotExistsAsync($"dbs/{databaseId}", collection, requestOptions);

            logger.Info($"Created Cosmos Db Collection for database {databaseId} with id {collectionId}");
        }
        /// <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;
            }
        }
Example #17
0
        /// <summary>
        /// Creates the DocumentCollection if it does not exit otherwise returns the existing one.
        /// </summary>
        /// <param name="dbId">Database id.</param>
        /// <param name="documentCollection">Document collection.</param>
        /// <param name="requestOptions">Request options.</param>
        /// <returns>Created document collection.</returns>
        public async Task <ResourceResponse <DocumentCollection> > GetOrCreateCollectionAsync(
            string dbId, DocumentCollection documentCollection, RequestOptions requestOptions = null)
        {
            Code.ExpectsNotNullOrWhiteSpaceArgument(dbId, nameof(dbId), TaggingUtilities.ReserveTag(0x2381b1d1 /* tag_961hr */));
            Code.ExpectsArgument(documentCollection, nameof(documentCollection), TaggingUtilities.ReserveTag(0x2381b1d2 /* tag_961hs */));

            IDocumentClient client = await GetDocumentClientAsync().ConfigureAwait(false);

            return(await DocumentDbAdapter.ExecuteAndLogAsync(TaggingUtilities.ReserveTag(0x2381b1d3 /* tag_961ht */),
                                                              () => client.CreateDocumentCollectionIfNotExistsAsync(
                                                                  UriFactory.CreateDatabaseUri(dbId),
                                                                  documentCollection,
                                                                  requestOptions)).ConfigureAwait(false));
        }
Example #18
0
        public async Task <DocumentCollection> CreateDocumentCollectionIfNotExistsAsync(
            IDocumentClient client,
            string collectionId)
        {
            Throw.IfNull(client, nameof(client));
            Throw.IfNullOrWhiteSpace(collectionId, nameof(collectionId));

            var uri  = UriFactory.CreateDatabaseUri(_settings.DatabaseId);
            var coll = new DocumentCollection {
                Id = collectionId
            };

            return(await client.CreateDocumentCollectionIfNotExistsAsync(uri, coll));
        }
        private async Task CreateWorkerLeasesCollection()
        {
            var collectionDefinition = new DocumentCollection
            {
                Id = _options.Value.LeasesCollection
            };

            collectionDefinition.PartitionKey.Paths.Add("/leaseType");

            var leaseConstraint = new UniqueKey
            {
                Paths = new Collection <string> {
                    "/leaseType"
                }
            };

            collectionDefinition.UniqueKeyPolicy.UniqueKeys.Add(leaseConstraint);

            await _documentClient.CreateDocumentCollectionIfNotExistsAsync(
                UriFactory.CreateDatabaseUri(_options.Value.Database),
                collectionDefinition,
                new RequestOptions { OfferThroughput = _options.Value.OfferThroughput }).ConfigureAwait(false);
        }
Example #20
0
        public static void CreateCollection(IDocumentClient client, string databaseId, string collectionId, string partitionKeyName = "")
        {
            var result = client.CreateDatabaseIfNotExistsAsync(new Database
            {
                Id = databaseId
            }).GetAwaiter().GetResult();

            if (result.StatusCode == System.Net.HttpStatusCode.Accepted ||
                result.StatusCode == System.Net.HttpStatusCode.OK ||
                result.StatusCode == System.Net.HttpStatusCode.Created)
            {
                var collection = new DocumentCollection
                {
                    Id = collectionId
                };

                if (!string.IsNullOrEmpty(partitionKeyName))
                {
                    if (!partitionKeyName.StartsWith("/"))
                    {
                        partitionKeyName = "/" + partitionKeyName;
                    }
                    collection.PartitionKey = new PartitionKeyDefinition
                    {
                        Paths = new Collection <string> {
                            partitionKeyName
                        }
                    };
                }
                var collectionCreationResult = client.CreateDocumentCollectionIfNotExistsAsync(UriFactory.CreateDatabaseUri(databaseId), collection, new RequestOptions
                {
                    OfferThroughput = DEFAULT_RUS
                }).GetAwaiter().GetResult();

                if (collectionCreationResult.StatusCode == System.Net.HttpStatusCode.Accepted ||
                    collectionCreationResult.StatusCode == System.Net.HttpStatusCode.OK ||
                    collectionCreationResult.StatusCode == System.Net.HttpStatusCode.Created)
                {
                    Task.Delay(500).GetAwaiter().GetResult();
                }
                else
                {
                    throw new NotSupportedException($"Collection '{collection}' cannot be created. ({collectionCreationResult.StatusCode})");
                }
            }
            else
            {
                throw new NotSupportedException($"Database '{databaseId}' cannot be created. Wrong Url or Key? ({result.StatusCode})");
            }
        }
Example #21
0
 private static async Task CreateCollectionIfNotExistsAsync(string DatabaseId, string CollectionId, string partitionkey = null)
 {
     await _client.CreateDocumentCollectionIfNotExistsAsync(UriFactory.CreateDatabaseUri(DatabaseId),
                                                            new DocumentCollection
     {
         Id           = CollectionId,
         PartitionKey = new PartitionKeyDefinition
         {
             Paths = new Collection <string> {
                 "/" + partitionkey
             }
         }
     });
 }
        public CosmosDbTodoRepository(Uri accountEndpoint, string accountKey)
        {
            documentClient = new DocumentClient(accountEndpoint, accountKey);

            documentClient.CreateDatabaseIfNotExistsAsync(new Database {
                Id = dbName
            }).Wait();
            var todoList = new DocumentCollection()
            {
                Id = collectionName
            };

            todoList.PartitionKey.Paths.Add(todoListPartitionKey);
            documentClient.CreateDocumentCollectionIfNotExistsAsync(UriFactory.CreateDatabaseUri(dbName), todoList).Wait();
        }
Example #23
0
        public async Task Run(ILogger logger)
        {
            var database = new Database
            {
                Id = DocumentSettings.DatabaseName
            };

            var documentCollection = new DocumentCollection
            {
                Id           = DocumentSettings.AccountProviderLegalEntitiesCollectionName,
                PartitionKey = new PartitionKeyDefinition
                {
                    Paths = new Collection <string>
                    {
                        "/ukprn"
                    }
                },
                UniqueKeyPolicy = new UniqueKeyPolicy
                {
                    UniqueKeys = new Collection <UniqueKey>
                    {
                        new UniqueKey
                        {
                            Paths = new Collection <string> {
                                "/accountProviderLegalEntityId"
                            }
                        },
                        new UniqueKey
                        {
                            Paths = new Collection <string> {
                                "/accountLegalEntityId"
                            }
                        }
                    }
                }
            };

            var requestOptions = new RequestOptions
            {
                OfferThroughput = 1000
            };

            //todo: logging from eas
            await _documentClient.CreateDatabaseIfNotExistsAsync(database);

            await _documentClient.CreateDocumentCollectionIfNotExistsAsync(UriFactory.CreateDatabaseUri(database.Id), documentCollection, requestOptions);
        }
        public async Task Run()
        {
            var database = new Database
            {
                Id = DocumentSettings.DatabaseName
            };

            var documentCollection = new DocumentCollection
            {
                Id           = DocumentSettings.AccountUsersCollectionName,
                PartitionKey = new PartitionKeyDefinition
                {
                    Paths = new Collection <string>
                    {
                        "/accountId"
                    }
                },
                UniqueKeyPolicy = new UniqueKeyPolicy
                {
                    UniqueKeys = new Collection <UniqueKey>
                    {
                        new UniqueKey
                        {
                            Paths = new Collection <string> {
                                "/userRef"
                            }
                        }
                    }
                }
            };

            _logger.LogInformation("Creating ReadStore database and collection if they don't exist");

            var createDatabaseResponse = await _documentClient.CreateDatabaseIfNotExistsAsync(database);

            _logger.LogInformation($"Database {(createDatabaseResponse.StatusCode == HttpStatusCode.Created ? "created" : "already existed")}");

            var requestOptions = new RequestOptions {
                OfferThroughput = 1000
            };
            var createDocumentCollectionResponse = await _documentClient.CreateDocumentCollectionIfNotExistsAsync(UriFactory.CreateDatabaseUri(database.Id), documentCollection, requestOptions);

            _logger.LogInformation($"Document collection {(createDocumentCollectionResponse.StatusCode == HttpStatusCode.Created ? "created" : "already existed")}");
        }
 public ProductsController(IDocumentClient client)
 {
     _cosmosClient = client;
     try
     {
         _cosmosClient.CreateDatabaseIfNotExistsAsync(new Database {
             Id = Constants.CosmosDBName
         }).Wait();
         _cosmosClient.CreateDocumentCollectionIfNotExistsAsync(UriFactory.CreateDatabaseUri(Constants.CosmosDBName), new DocumentCollection {
             Id = CosmosCollectionName
         }).Wait();
         _collectionUri = UriFactory.CreateDocumentCollectionUri(Constants.CosmosDBName, CosmosCollectionName);
     }
     catch (Exception ex)
     {
         Console.WriteLine($"Error building ProductsController: {ex.ToString()}");
         throw;
     }
 }
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public async void Configure(IApplicationBuilder app, IHostingEnvironment env, IDocumentClient client)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseMvc();

            // Init documentdb database
            Database shopifychallenge = await client.CreateDatabaseIfNotExistsAsync(new Database { Id = Configuration["DbConfig:DataBaseName"] });

            await client.CreateDocumentCollectionIfNotExistsAsync(shopifychallenge.SelfLink, new DocumentCollection { Id = Configuration["DbConfig:CollectionName"] });
        }
Example #27
0
        private async Task CreateLockClaimsCollection(IDocumentClient documentClient,
                                                      CosmosDataStoreOptions cosmosDataStoreOptions)
        {
            var collectionDefinition = new DocumentCollection
            {
                Id           = cosmosDataStoreOptions.DistributedLocksCollection,
                PartitionKey = new PartitionKeyDefinition
                {
                    Paths = new Collection <string> {
                        "/id"
                    }
                }
            };

            await documentClient.CreateDocumentCollectionIfNotExistsAsync(
                UriFactory.CreateDatabaseUri(cosmosDataStoreOptions.Database),
                collectionDefinition,
                new RequestOptions { OfferThroughput = cosmosDataStoreOptions.OfferThroughput }).ConfigureAwait(false);
        }
Example #28
0
        public async Task <HGV> method(string id)
        {
            try
            {
                var PrimaryKey = Constants.PrimaryKey;
                var url        = Constants.Uri;
                _client = new DocumentClient(new Uri(url), PrimaryKey);
                //Inserting to cosmos
                var record = new HGV
                {
                    HGVName = "TestHGV",
                    Country = "United States",
                };
                var ans = await _client.CreateDatabaseIfNotExistsAsync(new Database { Id = DatabaseId });

                var collectionDefinition = new DocumentCollection {
                    Id = CollectionId
                };
                _client.CreateDocumentCollectionIfNotExistsAsync(UriFactory.CreateDatabaseUri(DatabaseId), collectionDefinition).Wait();

                var document = await _client.CreateDocumentAsync(
                    UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId)
                    , record);

                HGV hgvrecord = _client.CreateDocumentQuery <HGV>(
                    UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId))
                                .Where(v => v.HGVName == "TestHGV")
                                .AsEnumerable()
                                .FirstOrDefault();

                //Getting data from cosmos based on id
                HGV document1 = _client.ReadDocumentAsync <HGV>(UriFactory.CreateDocumentUri(DatabaseId, CollectionId, id)).Result;
                return(document1);
            }
            catch (Exception e)
            {
                throw e;
            }
        }