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 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 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 void Can_parse_connection_string() { const string expected = "AccountEndpoint=https://example.documents.azure.com:443/;AccountKey=MyAccountKey;Database=MyDatabase"; var builder = new DocumentDbConnectionStringBuilder(expected); Assert.Equal(expected, builder.ConnectionString, StringComparer.OrdinalIgnoreCase); builder.AccountKey = "MyOtherAccountKey"; Assert.NotEqual(expected, builder.ConnectionString, StringComparer.OrdinalIgnoreCase); }
public void Can_include_non_standard_elements() { const string expected = "AccountEndpoint=https://example.documents.azure.com:443/;AccountKey=MyAccountKey;Database=MyDatabase;DefaultCollection=Foo;SharedCollection=True"; var builder = new DocumentDbConnectionStringBuilder(expected); Assert.Equal(expected, builder.ConnectionString, StringComparer.OrdinalIgnoreCase); builder.AccountKey = "MyOtherAccountKey"; Assert.NotEqual(expected, builder.ConnectionString, StringComparer.OrdinalIgnoreCase); }
public void AccountEndpointAccountKey_TestValues_Appended() { var builder = new DocumentDbConnectionStringBuilder(); builder.AccountEndpoint = "http://TestDocumentDbUrl.net"; builder.AccountKey = "SecretKey"; Assert.AreEqual(65, builder.ConnectionString.Length, TestResources.AdditionalParametersInConnectionString); StringAssert.Contains(builder.ConnectionString, "AccountEndpoint=http://TestDocumentDbUrl.net", TestResources.InvalidAccountEndpointInConnectionString); StringAssert.Contains(builder.ConnectionString, "AccountKey=SecretKey", TestResources.InvalidAccountKeyInConnectionString); }
private static void DefaultDbOptions(string connectionString, DocumentDbOptions o) { var connectionStringBuilder = new DocumentDbConnectionStringBuilder(connectionString); o.AccountKey ??= connectionStringBuilder.AccountKey; o.AccountEndpoint ??= connectionStringBuilder.AccountEndpoint; o.DatabaseId ??= connectionStringBuilder.Database; o.CollectionId ??= connectionStringBuilder.DefaultCollection ?? Constants.Runtime.DefaultCollection; o.SharedCollection = true; // Anything o.PartitionKeyPaths = new[] { "/id" }; }
public void AccountEndpointAccountKey_TestValues_Appended() { var builder = new DocumentDbConnectionStringBuilder(); builder.AccountEndpoint = "http://TestDocumentDbUrl.net"; builder.AccountKey = "SecretKey"; Assert.AreEqual(65, builder.ConnectionString.Length, TestResources.AdditionalParametersInConnectionString); StringAssert.Contains(builder.ConnectionString, "AccountEndpoint=http://TestDocumentDbUrl.net", TestResources.InvalidAccountEndpointInConnectionString); StringAssert.Contains(builder.ConnectionString, "AccountKey=SecretKey", TestResources.InvalidAccountKeyInConnectionString); }
private static void DefaultDbOptions(string connectionString, DocumentDbOptions o) { var builder = new DocumentDbConnectionStringBuilder(connectionString); o.AccountKey ??= builder.AccountKey; o.AccountEndpoint ??= builder.AccountEndpoint; o.DatabaseId ??= builder.Database; o.CollectionId ??= builder.DefaultCollection ?? Constants.Identity.DefaultCollection; o.SharedCollection = true; // User, Role, Tenant, Application, etc. o.PartitionKeyPaths = new[] { "/id" }; }
private static void DefaultDbOptions(string connectionString, DocumentDbOptions o) { var connectionStringBuilder = new DocumentDbConnectionStringBuilder(connectionString); o.AccountKey = connectionStringBuilder.AccountKey; o.AccountEndpoint = connectionStringBuilder.AccountEndpoint; o.CollectionId = connectionStringBuilder.DefaultCollection ?? Constants.Schemas.DefaultCollection; o.DatabaseId = connectionStringBuilder.Database ?? "Default"; o.SharedCollection = true; // SchemaVersionDocument, ApplicationVersionDocument o.PartitionKeyPaths = new[] { "/id" }; }
private static void DefaultDbOptions(string connectionString, DocumentDbOptions o) { var connectionStringBuilder = new DocumentDbConnectionStringBuilder(connectionString); o.AccountKey = connectionStringBuilder.AccountKey; o.AccountEndpoint = connectionStringBuilder.AccountEndpoint; o.CollectionId = connectionStringBuilder.DefaultCollection ?? "BackgroundTasks"; o.DatabaseId = connectionStringBuilder.Database ?? "Default"; o.SharedCollection = true; // Sequence, Document, etc. o.PartitionKeyPaths = new[] { "/id" }; }
public void Can_access_case_insensitive() { var builder = new DocumentDbConnectionStringBuilder { AccountEndpoint = new Uri("https://example.documents.azure.com:443/", UriKind.Absolute), AccountKey = "MyAccountKey", Database = "MyDatabase" }; Assert.NotNull(builder["AccountKey"]); Assert.NotNull(builder["accountkey"]); Assert.NotNull(builder.AccountKey); builder.Build(); }
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 void Can_build_connection_string() { var expected = "AccountEndpoint=https://example.documents.azure.com:443/;AccountKey=MyAccountKey;Database=MyDatabase"; var builder = new DocumentDbConnectionStringBuilder { AccountEndpoint = new Uri("https://example.documents.azure.com:443/", UriKind.Absolute), AccountKey = "MyAccountKey", Database = "MyDatabase" }; Assert.Equal(expected, builder.ConnectionString, StringComparer.OrdinalIgnoreCase); builder.AccountKey = "MyOtherAccountKey"; Assert.NotEqual(expected, builder.ConnectionString, StringComparer.OrdinalIgnoreCase); }
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); }
public override void ConfigureServices(IServiceCollection services) { // WARNING: this currently points to local emulator storage const string connectionString = "AccountEndpoint=https://localhost:8081/;" + "AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==;" + "Database=Tests;"; var builder = new DocumentDbConnectionStringBuilder(connectionString) { DefaultCollection = $"{Guid.NewGuid().ToString("N").ToUpperInvariant()}" }; services.AddBackgroundTasks(o => { }) .AddDocumentDbBackgroundTaskStore(o => { o.AccountKey = builder.AccountKey; o.AccountEndpoint = builder.AccountEndpoint; o.DatabaseId = builder.Database; o.CollectionId = builder.DefaultCollection; o.SharedCollection = true; o.OfferThroughput = 400; }); }
public MigrationRunner(string connectionString, DocumentDbOptions options) { _options = options; _builder = new DocumentDbConnectionStringBuilder(connectionString); _client = _builder.Build(); }
public static IdentityBuilder AddDocumentDbIdentityStore <TKey, TUser, TRole, TTenant>( this IdentityBuilder identityBuilder, string connectionString, ConnectionScope scope = ConnectionScope.ByRequest, Action <DocumentDbOptions> configureDocumentDb = null) where TKey : IEquatable <TKey> where TUser : IdentityUserExtended <TKey> where TRole : IdentityRoleExtended <TKey> where TTenant : IdentityTenant <TKey> { var services = identityBuilder.Services; services.AddSingleton <ITypeRegistry, TypeRegistry>(); var serviceProvider = services.BuildServiceProvider(); var builder = new DocumentDbConnectionStringBuilder(connectionString); void ConfigureAction(DocumentDbOptions o) { configureDocumentDb?.Invoke(o); o.DatabaseId = o.DatabaseId ?? builder.Database; o.CollectionId = o.CollectionId ?? builder.DefaultCollection ?? Constants.Identity.DefaultCollection; } identityBuilder.Services.Configure <DocumentDbOptions>(ConfigureAction); var dialect = new DocumentDbDialect(); identityBuilder.AddSqlStores <DocumentDbConnectionFactory, TKey, TUser, TRole, TTenant>(connectionString, scope, OnCommand <TKey>(), OnConnection); SqlBuilder.Dialect = dialect; SimpleDataDescriptor.TableNameConvention = s => { switch (s) { case nameof(IdentityRoleExtended): return(nameof(IdentityRole)); case nameof(IdentityUserExtended): return(nameof(IdentityUser)); default: return(s); } }; DescriptorColumnMapper.AddTypeMap <TUser>(StringComparer.Ordinal); DescriptorColumnMapper.AddTypeMap <TRole>(StringComparer.Ordinal); DescriptorColumnMapper.AddTypeMap <TTenant>(StringComparer.Ordinal); services.AddMetrics(); services.AddSingleton(dialect); services.AddSingleton <IQueryableProvider <TUser>, DocumentDbQueryableProvider <TUser> >(); services.AddSingleton <IQueryableProvider <TRole>, DocumentDbQueryableProvider <TRole> >(); services.AddSingleton <IQueryableProvider <TTenant>, DocumentDbQueryableProvider <TTenant> >(); var options = new DocumentDbOptions(); ConfigureAction(options); var identityOptions = serviceProvider.GetRequiredService <IOptions <IdentityOptionsExtended> >().Value; MigrateToLatest <TKey>(connectionString, identityOptions, options); return(identityBuilder); }