public static Task CreateCollectionAsync(this ICouchbaseCollectionManager manager, CollectionSpec spec, Action <CreateCollectionOptions> configureOptions)
        {
            var options = new CreateCollectionOptions();

            configureOptions(options);

            return(manager.CreateCollectionAsync(spec, options));
        }
Exemplo n.º 2
0
        private async Task CreateCollectionsAsync()
        {
            _logger.LogInformation("Creating collections...");
            var tables = (await _sqlConnection.QueryAsync(@"
                select TABLE_SCHEMA, TABLE_NAME
                from INFORMATION_SCHEMA.TABLES
                WHERE TABLE_TYPE = 'BASE TABLE'")).ToList();

            foreach (var table in tables)
            {
                string collectionName = GetCollectionName(table.TABLE_SCHEMA, table.TABLE_NAME);
                string scopeName      = GetScopeName(table.TABLE_SCHEMA);

                _logger.LogInformation($"Creating collection `{collectionName}`...");

                await CreateScopeIfNecessaryAsync(scopeName);

                if (await CollectionExistsAsync(collectionName, scopeName))
                {
                    _logger.LogInformation("already exists.");
                    continue;
                }

                try
                {
                    var spec = new CollectionSpec(scopeName, collectionName);
                    await _collManager.CreateCollectionAsync(spec);
                }
                catch
                {
                    _logger.LogError($"Unable to create collection `{collectionName}` in scope `{scopeName}`");
                    throw;
                }
                _logger.LogInformation("Done");
            }
            _logger.LogInformation("Collection creation complete.");
        }
        public async Task ExecuteAsync()
        {
            Console.WriteLine("scopeAdmin");
            {
                // tag::scopeAdmin[]
                ICluster clusterAdmin = await Cluster.ConnectAsync(
                    "couchbase://localhost", "Administrator", "password");

                IUserManager users = clusterAdmin.Users;

                var user = new User("scopeAdmin")
                {
                    Password    = "******",
                    DisplayName = "Manage Scopes [travel-sample:*]",
                    Roles       = new List <Role>()
                    {
                        new Role("scope_admin", "travel-sample"),
                        new Role("data_reader", "travel-sample")
                    }
                };

                await users.UpsertUserAsync(user);

                // end::scopeAdmin[]
            }

            ICluster cluster = await Cluster.ConnectAsync("couchbase://localhost", "scopeAdmin", "password");

            IBucket bucket = await cluster.BucketAsync("travel-sample");

            // tag::create-collection-manager[]
            ICouchbaseCollectionManager collectionMgr = bucket.Collections;
            // end::create-collection-manager[]
            {
                Console.WriteLine("create-scope");
                // tag::create-scope[]
                try {
                    await collectionMgr.CreateScopeAsync("example-scope");
                }
                catch (ScopeExistsException) {
                    Console.WriteLine("The scope already exists");
                }
                // end::create-scope[]
            }
            {
                Console.WriteLine("create-collection");
                // tag::create-collection[]
                var spec = new CollectionSpec("example-scope", "example-collection");

                try {
                    await collectionMgr.CreateCollectionAsync(spec);
                }
                catch (CollectionExistsException) {
                    Console.WriteLine("Collection already exists");
                }
                catch (ScopeNotFoundException) {
                    Console.WriteLine("The specified parent scope doesn't exist");
                }
                // end::create-collection[]

                Console.WriteLine("listing-scope-collection");
                // tag::listing-scope-collection[]
                var scopes = await collectionMgr.GetAllScopesAsync();

                foreach (ScopeSpec scopeSpec in scopes)
                {
                    Console.WriteLine($"Scope: {scopeSpec.Name}");

                    foreach (CollectionSpec collectionSpec in scopeSpec.Collections)
                    {
                        Console.WriteLine($" - {collectionSpec.Name}");
                    }
                }
                // end::listing-scope-collection[]

                Console.WriteLine("drop-collection");
                // tag::drop-collection[]
                try {
                    await collectionMgr.DropCollectionAsync(spec);
                }
                catch (CollectionNotFoundException) {
                    Console.WriteLine("The specified collection doesn't exist");
                }
                catch (ScopeNotFoundException) {
                    Console.WriteLine("The specified parent scope doesn't exist");
                }
                // end::drop-collection[]
            }
            {
                Console.WriteLine("drop-scope");
                // tag::drop-scope[]
                try {
                    await collectionMgr.DropScopeAsync("example-scope");
                }
                catch (ScopeNotFoundException) {
                    Console.WriteLine("The specified scope doesn't exist");
                }
                // end::drop-scope[]
            }
        }
 public static Task CreateCollectionAsync(this ICouchbaseCollectionManager manager, CollectionSpec spec)
 {
     return(manager.CreateCollectionAsync(spec, CreateCollectionOptions.Default));
 }