public static IEnumerable <IReadOnlyDictionary <string, object> > ReadDocuments(string connectionString, string collectionName, string query = "SELECT * FROM c") { var connectionSettings = DocumentDbConnectionStringBuilder.Parse(connectionString); using (var client = CreateClient(connectionSettings)) { var database = client .CreateDatabaseQuery() .Where(d => d.Id == connectionSettings.Database) .AsEnumerable() .FirstOrDefault(); Assert.IsNotNull(database, "Document database does not exist."); var collection = client .CreateDocumentCollectionQuery(database.CollectionsLink) .Where(c => c.Id == collectionName) .AsEnumerable() .FirstOrDefault(); Assert.IsNotNull(collection, "Document collection does not exist."); return(client .CreateDocumentQuery <Dictionary <string, object> >(collection.DocumentsLink, query, new FeedOptions { EnableCrossPartitionQuery = true }) .ToArray()); } }
public static int CountDocuments(string connectionString, string collectionName) { var connectionSettings = DocumentDbConnectionStringBuilder.Parse(connectionString); using (var client = CreateClient(connectionSettings)) { var database = client .CreateDatabaseQuery() .Where(d => d.Id == connectionSettings.Database) .AsEnumerable() .FirstOrDefault(); Assert.IsNotNull(database, "Document database does not exist."); var collection = client .CreateDocumentCollectionQuery(database.CollectionsLink) .Where(c => c.Id == collectionName) .AsEnumerable() .FirstOrDefault(); Assert.IsNotNull(collection, "Document collection does not exist."); return(client .CreateDocumentQuery(collection.DocumentsLink) .AsEnumerable() // No visitor for Count() ??? .Count()); } }
public void Parse_ConnectionStringWithDatabase_Parsed() { var builder = DocumentDbConnectionStringBuilder.Parse("AccountEndpoint=http://Test2DocumentDbUrl.net;AccountKey=SuperSecret;Database=TestDb"); Assert.AreEqual("http://Test2DocumentDbUrl.net", builder.AccountEndpoint, TestResources.InvalidAccountEndpointParsedFromConnectionString); Assert.AreEqual("SuperSecret", builder.AccountKey, TestResources.InvalidAccountKeyParsedFromConnectionString); Assert.AreEqual("TestDb", builder.Database, TestResources.InvalidDatabaseNameParsedFromConnectionString); }
public void Parse_ConnectionStringWithUnknownArguments_Parsed() { var builder = DocumentDbConnectionStringBuilder.Parse("AccountEndpoint=http://Test2DocumentDbUrl.net;AccountKey=SuperSecret;SomeRandomArgument=Value"); Assert.AreEqual("http://Test2DocumentDbUrl.net", builder.AccountEndpoint, TestResources.InvalidAccountEndpointParsedFromConnectionString); Assert.AreEqual("SuperSecret", builder.AccountKey, TestResources.InvalidAccountKeyParsedFromConnectionString); Assert.IsNull(builder.Database, TestResources.InvalidDatabaseNameParsedFromConnectionString); }
public void Parse_ValidConnectionStringWithSpecialCharacters_Parsed() { var builder = DocumentDbConnectionStringBuilder.Parse("AccountEndpoint=http://TestDocumentDbUrl.net;AccountKey=\"Super;S=ecret\""); Assert.AreEqual("http://TestDocumentDbUrl.net", builder.AccountEndpoint, TestResources.InvalidAccountEndpointParsedFromConnectionString); Assert.AreEqual("Super;S=ecret", builder.AccountKey, TestResources.InvalidAccountKeyParsedFromConnectionString); Assert.IsNull(builder.Database, TestResources.InvalidDatabaseNameParsedFromConnectionString); }
public static async Task CreateSampleCollectionAsync(string connectionString, string collectionName, IEnumerable <object> documents) { var connectionSettings = DocumentDbConnectionStringBuilder.Parse(connectionString); using (var client = CreateClient(connectionSettings)) { var database = await client.CreateDatabaseAsync(new Database { Id = connectionSettings.Database }); var collection = await client.CreateDocumentCollectionAsync(database.Resource.SelfLink, new DocumentCollection { Id = collectionName }); foreach (var document in documents) { await client.CreateDocumentAsync(collection.Resource.SelfLink, document); } } }
public static async Task DeleteDatabaseAsync(string connectionString) { var connectionSettings = DocumentDbConnectionStringBuilder.Parse(connectionString); using (var client = CreateClient(connectionSettings)) { var database = client .CreateDatabaseQuery() .Where(d => d.Id == connectionSettings.Database) .AsEnumerable() .FirstOrDefault(); if (database != null) { await client.DeleteDatabaseAsync(database.SelfLink); } } }
private static IDocumentDbConnectionSettings ParseConnectionString(string connectionString) { var connectionSettings = DocumentDbConnectionStringBuilder.Parse(connectionString); if (String.IsNullOrEmpty(connectionSettings.AccountEndpoint)) { throw Errors.AccountEndpointMissing(); } if (String.IsNullOrEmpty(connectionSettings.AccountKey)) { throw Errors.AccountKeyMissing(); } if (String.IsNullOrEmpty(connectionSettings.Database)) { throw Errors.DatabaseNameMissing(); } return(connectionSettings); }