public static async Task <DocumentCollection> EnsureDatabaseAndCollection(this IDocumentClient client, string dbId, string collectionId, string[] partitionKeys = null) { var db = client.CreateDatabaseQuery().Where(d => d.Id == dbId).AsEnumerable().FirstOrDefault() ?? await client.CreateDatabaseAsync(new Database() { Id = dbId }); var collection = client.CreateDocumentCollectionQuery(db.SelfLink) .Where(c => c.Id == collectionId).AsEnumerable().FirstOrDefault(); if (collection == null) { var collectionDefinition = new DocumentCollection() { Id = collectionId }; if (partitionKeys != null && partitionKeys.Length > 0) { foreach (var partitionKey in partitionKeys) { collectionDefinition.PartitionKey.Paths.Add(partitionKey); } } collection = await client.CreateDocumentCollectionAsync(db.SelfLink, collectionDefinition); } return(collection); }
private async Task <ResourceResponse <Database> > CreateDatabaseIfNotExists(string databaseName) { ResourceResponse <Database> result; // Check to verify a database with the id=FamilyDB does not exist try { result = await _documentClient.ReadDatabaseAsync(UriFactory.CreateDatabaseUri(databaseName)).ConfigureAwait(false); Logger.Info("Found {0}", databaseName); } catch (DocumentClientException de) { // If the database does not exist, create a new database if (de.StatusCode == HttpStatusCode.NotFound) { result = await _documentClient.CreateDatabaseAsync(new Database { Id = databaseName }).ConfigureAwait(false); Logger.Info("Created {0}", databaseName); } else { throw; } } return(result); }
/// <summary> /// Creates a Database if it does not exist. This functionality is defined in DocumentClient, but not IDocumentClient /// </summary> /// <param name="documentClient">The document client</param> /// <param name="database">The database to create</param> /// <param name="options">The request options</param> /// <returns>The result</returns> public static async Task <ResourceResponse <Database> > CreateDatabaseIfNotExistsAsync(this IDocumentClient documentClient, Database database, RequestOptions options = null) { var databaseUri = UriFactory.CreateDatabaseUri(database.Id); return(await CreateIfNotExists( () => documentClient.ReadDatabaseAsync(databaseUri, options), () => documentClient.CreateDatabaseAsync(database, options))); }
private async Task <Database> GetOrCreateDatabaseAsync() { var database = _client.CreateDatabaseQuery().Where(db => db.Id == _id).AsEnumerable().FirstOrDefault(); return(database != null ? database : await _client.CreateDatabaseAsync(new Database { Id = _id })); }
/// <summary> /// Get or create (when required) database for the given identifier /// </summary> /// <param name="id">The database identifier</param> /// <returns>The instance of DocumentDB database</returns> public Database GetOrCreateDatabase(string id) { var db = _client.CreateDatabaseQuery().AsEnumerable().FirstOrDefault(d => d.Id == id); return(db ?? _client.CreateDatabaseAsync(new Database { Id = id }).Result); }
private async Task <Database> GetOrCreateDatabaseAsync(bool createOnMissing) { var database = _client.CreateDatabaseQuery().Where(db => db.Id == _id).AsEnumerable().FirstOrDefault(); return(database != null ? database : createOnMissing ? await _client.CreateDatabaseAsync(new Database { Id = _id }) : throw new InvalidOperationException($"Database { _id } does not exist")); }
private async Task CreateDatabaseIfNotExistsAsync() { await CreateResourceIfNotExistsAsync ( () => _client.ReadDatabaseAsync(DatabaseUri), () => _client.CreateDatabaseAsync(new Database { Id = DatabaseId }) ); }
/// <summary> /// Gets a reference to a DocumentDB database, or creates if /// it does not already exist /// </summary> /// <param name="Client">The client.</param> /// <param name="databaseId">The database identifier.</param> /// <returns></returns> public static async Task <Database> GetOrCreateDatabaseAsync(IDocumentClient Client, string databaseId) { Database database = Client.CreateDatabaseQuery().Where(db => db.Id == databaseId).ToArray().FirstOrDefault(); if (database == null) { database = await Client.CreateDatabaseAsync(new Database { Id = databaseId }); } return(database); }
private static async Task CreateDatabase(IDocumentClient client) { Console.WriteLine(); Console.WriteLine("#### Creating Database ####"); Database database = client.CreateDatabaseQuery().Where(db => db.Id == DatabaseId).ToArray() .FirstOrDefault() ?? await client.CreateDatabaseAsync(new Database() { Id = DatabaseId }); Console.WriteLine($"Database Id: {database.Id}; Resource Id: {database.ResourceId}"); }
private static async Task CreateDatabase(IDocumentClient client, string databaseId) { Console.WriteLine(">>> Create Database <<<"); Database databaseDefinition = new Database { Id = databaseId }; ResourceResponse <Database> result = await client.CreateDatabaseAsync(databaseDefinition); Database database = result.Resource; PrintDatabase(database); }
private async Task <Database> GetOrCreateDatabaseAsync() { Database database = _client.CreateDatabaseQuery() .Where(db => db.Id == _databaseId).ToArray().FirstOrDefault(); if (database == null) { database = await _client.CreateDatabaseAsync( new Database { Id = _databaseId }); } return(database); }
public static async Task CreateDatabaseIfNotExistsAsync(this IDocumentClient client, Database database) { try { await client.CreateDatabaseAsync(database); } catch (DocumentClientException ex) { if (!(ex.StatusCode == HttpStatusCode.Conflict && ex.Message.Contains("Resource with specified id or name already exists"))) { throw; } } }
private async Task SetupDatabaseAsync() { var databaseId = _configuration.DatabaseId; var database = _client.CreateDatabaseQuery() .Where(db => db.Id == databaseId) .AsEnumerable() .FirstOrDefault(); if (database == null) { database = new Database { Id = databaseId }; await _client.CreateDatabaseAsync(database); } }
private static async Task CreateDatabaseIfNotExistsAsync(this IDocumentClient client, string databaseId) { try { await client.ReadDatabaseAsync(UriFactory.CreateDatabaseUri(databaseId)); } catch (DocumentClientException e) { if (e.StatusCode == System.Net.HttpStatusCode.NotFound) { await client.CreateDatabaseAsync(new Database { Id = databaseId }); } else { throw; } } }
private async Task CreateDatabaseAsync() { try { logger.Info($"Creating DocumentDb database: {databaseId}", () => { }); var database = new Database { Id = databaseId }; await client.CreateDatabaseAsync(database, options); } catch (Exception e) { logger.Error("Error while creating DocumentDb database", () => new { e }); throw; } }
public async Task <bool> EnsureCreatedAsync(string databaseName) { var database = _documentClient.CreateDatabaseQuery() .Where(db => db.Id == databaseName) .ToArray() .FirstOrDefault(); if (database != null) { return(false); } database = await _documentClient.CreateDatabaseAsync( new Database { Id = databaseName }); return(database != null); }
internal async Task CreateDatabaseIfNotExistsAsync() { try { await _client.ReadDatabaseAsync(UriFactory.CreateDatabaseUri(DatabaseId)); } catch (DocumentClientException e) { if (e.StatusCode == System.Net.HttpStatusCode.NotFound) { await _client.CreateDatabaseAsync(new Database { Id = DatabaseId }); } else { throw; } } }
private async Task CreateDatabaseIfNotExistsAsync() { try { await documentClient.ReadDatabaseAsync(UriFactory.CreateDatabaseUri(databaseId)); } catch (DocumentClientException e) { if (e.StatusCode == HttpStatusCode.NotFound) { await documentClient.CreateDatabaseAsync(new Database { Id = databaseId }); } else { throw; } } }
private async Task CreateDatabaseIfNotExistsAsync() { try { await documentClient.ReadDatabaseAsync(UriFactory.CreateDatabaseUri(cosmosDbConnection.DatabaseId)).ConfigureAwait(false); } catch (DocumentClientException e) { if (e.StatusCode == HttpStatusCode.NotFound) { await documentClient.CreateDatabaseAsync(new Database { Id = cosmosDbConnection.DatabaseId }).ConfigureAwait(false); } else { throw; } } }
private async Task CreateDatabaseIfNotExistsAsync() { try { await _documentClient.ReadDatabaseAsync(UriFactory.CreateDatabaseUri(_applicationConfig.Database)); } catch (DocumentClientException e) { if (e.StatusCode == System.Net.HttpStatusCode.NotFound) { await _documentClient.CreateDatabaseAsync(new Database { Id = _applicationConfig.Database }); } else { throw; } } }
private async Task CreateDatabaseIfNotExists() { try { await _documentClient.ReadDatabaseAsync(UriFactory.CreateDatabaseUri(_databaseName)); } catch (DocumentClientException e) { //Database do not exists! Create it if (e.StatusCode == System.Net.HttpStatusCode.NotFound) { await _documentClient.CreateDatabaseAsync(new Database { Id = _databaseName }); } else { throw; } } }
private static async Task <Database> CreateDatabaseIfNotExistsAsync(this IDocumentClient client, Database database) { try { database = (await client.ReadDatabaseAsync(UriFactory.CreateDatabaseUri(database.Id))).Resource; } catch (DocumentClientException ex) { if (ex.StatusCode == HttpStatusCode.NotFound) { database = (await client.CreateDatabaseAsync(database)).Resource; } else { throw; } } return(database); }
public static async Task <ResourceResponse <Database> > CreateTablesDB(IDocumentClient client) { ResourceResponse <Database> response = null; try { response = await client.CreateDatabaseAsync(new Documents.Database { Id = "TablesDB" }); return(response); } catch (DocumentClientException ex) { if (ex.StatusCode != HttpStatusCode.Conflict) { throw; } return(response); } }
public static async Task <Database> GetOrCreateDatabaseAsync(this IDocumentClient client, string databaseId) { try { var response = await client.ReadDatabaseAsync(UriFactory.CreateDatabaseUri(databaseId)); return(response.Resource); } catch (DocumentClientException e) { if (e.StatusCode == System.Net.HttpStatusCode.NotFound) { var response = await client.CreateDatabaseAsync(new Database { Id = databaseId }); return(response.Resource); } else { throw e.InnerException; } } }
public static async Task Initialize(IDocumentDbClientFactory documentDbClientFactory) { DatabaseId = ConfigurationManager.AppSettings.GetRefValue <string>(DatabaseIdConfigName); CollectionId = ConfigurationManager.AppSettings.GetRefValue <string>(CollectionNameConfigName); IDocumentClient client = documentDbClientFactory.GetClient(); try { await client.ReadDatabaseAsync(UriFactory.CreateDatabaseUri(DatabaseId)); } catch (DocumentClientException ex) { if (ex.StatusCode == HttpStatusCode.NotFound) { await client.CreateDatabaseAsync(new Database { Id = DatabaseId }); } } try { await client.ReadDocumentCollectionAsync(UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId)); } catch (DocumentClientException ex) { if (ex.StatusCode == HttpStatusCode.NotFound) { var documentCollection = new DocumentCollection { Id = CollectionId, IndexingPolicy = CollectionIndexingPolicy, }; await client.CreateDocumentCollectionAsync(UriFactory.CreateDatabaseUri(DatabaseId), documentCollection, CollectionRequestOptions); } } }
public static Task <Database> GetOrAddDatabase(string databaseName, IDocumentClient client, CancellationToken token) { return(Policy .Handle <DocumentClientException>(e => { return true; }) .WaitAndRetryForeverAsync(attempt => TimeSpan.FromSeconds(1 << Math.Min(attempt, 3))) .ExecuteAsync(async() => { token.ThrowIfCancellationRequested(); var query = client.CreateDatabaseQuery().Where(db => db.Id == databaseName).AsDocumentQuery(); if (query.HasMoreResults) { var res = await query.ExecuteNextAsync <Database>(); if (res.Any()) { return res.First(); } } return (await client.CreateDatabaseAsync(new Database { Id = databaseName }).ConfigureAwait(false)).Resource; })); }