protected override void OnDatabaseCreating(CouchDatabaseBuilder databaseBuilder)
 {
     databaseBuilder.Document <Rebel>()
     .HasIndex("surnames_index", builder => builder
               .IndexByDescending(r => r.Surname)
               .ThenByDescending(r => r.Name));
 }
Example #2
0
        protected CouchContext(CouchOptions options)
        {
            Check.NotNull(options, nameof(options));

            var optionsBuilder  = new CouchOptionsBuilder(options);
            var databaseBuilder = new CouchDatabaseBuilder();

#pragma warning disable CA2214 // Do not call overridable methods in constructors
            OnConfiguring(optionsBuilder);
            OnDatabaseCreating(databaseBuilder);
#pragma warning restore CA2214 // Do not call overridable methods in constructors

            Client = new CouchClient(options);
            IEnumerable <PropertyInfo> databasePropertyInfos = GetDatabaseProperties();

            foreach (PropertyInfo dbProperty in databasePropertyInfos)
            {
                Type documentType = dbProperty.PropertyType.GetGenericArguments()[0];

                var initDatabasesTask = (Task)InitDatabasesGenericMethod.MakeGenericMethod(documentType)
                                        .Invoke(this, new object[] { dbProperty, options });
                initDatabasesTask.ConfigureAwait(false).GetAwaiter().GetResult();

                var applyDatabaseChangesTask = (Task)ApplyDatabaseChangesGenericMethod.MakeGenericMethod(documentType)
                                               .Invoke(this, new object[] { dbProperty, options, databaseBuilder });
                applyDatabaseChangesTask.ConfigureAwait(false).GetAwaiter().GetResult();
            }
        }
            protected override void OnDatabaseCreating(CouchDatabaseBuilder databaseBuilder)
            {
                databaseBuilder
                .Document <OtherRebel>()
                .ToDatabase("shared-rebels");

                databaseBuilder
                .Document <SimpleRebel>()
                .ToDatabase("shared-rebels");
            }
Example #4
0
        private static void SetupDiscriminators(CouchDatabaseBuilder databaseBuilder)
        {
            // Get all options that share the database with another one
            IEnumerable <KeyValuePair <Type, CouchDocumentBuilder> >?sharedDatabase = databaseBuilder.DocumentBuilders
                                                                                      .Where(opt => opt.Value.Database != null)
                                                                                      .GroupBy(v => v.Value.Database)
                                                                                      .Where(g => g.Count() > 1)
                                                                                      .SelectMany(g => g);

            foreach (KeyValuePair <Type, CouchDocumentBuilder> option in sharedDatabase)
            {
                option.Value.Discriminator = option.Key.Name;
            }
        }
Example #5
0
        private void InitializeDatabases(CouchOptions options, CouchDatabaseBuilder databaseBuilder)
        {
            foreach (PropertyInfo dbProperty in GetDatabaseProperties())
            {
                Type documentType = dbProperty.PropertyType.GetGenericArguments()[0];

                var initDatabasesTask = (Task)InitDatabasesGenericMethod.MakeGenericMethod(documentType)
                                        .Invoke(this, new object[] { dbProperty, options, databaseBuilder });
                initDatabasesTask.ConfigureAwait(false).GetAwaiter().GetResult();

                var applyDatabaseChangesTask = (Task)ApplyDatabaseChangesGenericMethod.MakeGenericMethod(documentType)
                                               .Invoke(this, new object[] { dbProperty, options, databaseBuilder });
                applyDatabaseChangesTask.ConfigureAwait(false).GetAwaiter().GetResult();
            }
        }
Example #6
0
        protected CouchContext(CouchOptions options)
        {
            Check.NotNull(options, nameof(options));

            var optionsBuilder  = new CouchOptionsBuilder(options);
            var databaseBuilder = new CouchDatabaseBuilder();

#pragma warning disable CA2214 // Do not call overridable methods in constructors
            OnConfiguring(optionsBuilder);
            OnDatabaseCreating(databaseBuilder);
#pragma warning restore CA2214 // Do not call overridable methods in constructors

            Client = new CouchClient(options);

            SetupDiscriminators(databaseBuilder);
            InitializeDatabases(options, databaseBuilder);
        }
Example #7
0
 protected virtual void OnDatabaseCreating(CouchDatabaseBuilder databaseBuilder)
 {
 }
Example #8
0
        private async Task ApplyDatabaseChangesAsync <TSource>(PropertyInfo propertyInfo, CouchOptions options, CouchDatabaseBuilder databaseBuilder)
            where TSource : CouchDocument
        {
            Type documentType = typeof(TSource);

            if (!databaseBuilder.DocumentBuilders.ContainsKey(documentType))
            {
                return;
            }

            var database        = (CouchDatabase <TSource>)propertyInfo.GetValue(this);
            var documentBuilder = (CouchDocumentBuilder <TSource>)databaseBuilder.DocumentBuilders[documentType];

            if (!documentBuilder.IndexDefinitions.Any())
            {
                return;
            }

            List <IndexInfo> indexes = await database.GetIndexesAsync().ConfigureAwait(false);

            foreach (IndexSetupDefinition <TSource> indexSetup in documentBuilder.IndexDefinitions)
            {
                await TryCreateOrUpdateIndexAsync(options, indexes, indexSetup, database)
                .ConfigureAwait(false);
            }
        }
Example #9
0
        private async Task InitDatabaseAsync <TSource>(PropertyInfo propertyInfo, CouchOptions options, CouchDatabaseBuilder databaseBuilder)
            where TSource : CouchDocument
        {
            ICouchDatabase <TSource> database;
            Type documentType = typeof(TSource);

            if (databaseBuilder.DocumentBuilders.ContainsKey(documentType))
            {
                var documentBuilder = (CouchDocumentBuilder <TSource>)databaseBuilder.DocumentBuilders[documentType];
                var databaseName    = documentBuilder.Database ?? Client.GetClassName(documentType);
                database = options.CheckDatabaseExists
                    ? await Client.GetOrCreateDatabaseAsync <TSource>(databaseName, documentBuilder.Shards, documentBuilder.Replicas, documentBuilder.Partitioned, documentBuilder.Discriminator).ConfigureAwait(false)
                    : Client.GetDatabase <TSource>(databaseName, documentBuilder.Discriminator);
            }
            else
            {
                database = options.CheckDatabaseExists
                    ? await Client.GetOrCreateDatabaseAsync <TSource>().ConfigureAwait(false)
                    : Client.GetDatabase <TSource>();
            }

            propertyInfo.SetValue(this, database);
        }
 protected override void OnDatabaseCreating(CouchDatabaseBuilder databaseBuilder)
 {
     databaseBuilder.Document <Rebel>()
     .HasIndex("skywalkers", b => b.IndexBy(b => b.Surname));
 }