Provisioning Cluster Resources
Provisioning cluster resources is managed at the collection or bucket level, depending upon the service affected. Common use cases are outlined here, less common use cases are covered in the API docs.
The primary means for managing clusters is through the Couchbase Web UI which provides an easy to use interface for adding, removing, monitoring and modifying buckets. In some instances you may wish to have a programmatic interface. For example, if you wish to manage a cluster from a setup script, or if you are setting up buckets in test scaffolding.
The Node.js SDK also comes with some convenience functionality for common Couchbase management requests.
Management operations in the SDK may be performed through several interfaces depending on the object:
-
BucketManager —
Cluster.buckets()
-
UserManager —
Cluster.users()
-
QueryIndexManager —
Cluster.queryIndexes()
-
AnalyticsIndexManager —
Cluster.analyticsIndexes()
-
SearchIndexManager —
Cluster.searchIndexes()
-
CollectionManager —
Bucket.collections()
-
ViewIndexManager —
Bucket.viewIndexes()
.
When using a Couchbase version earlier than 6.5, you must create a valid Bucket connection using cluster.bucket(name) before you can use cluster level managers.
|
Creating and Removing Buckets
The BucketManager
interface may be used to create and delete buckets from the Couchbase cluster.
It is instantiated through the Cluster.buckets()
method.
const bucketMgr = cluster.buckets()
The CreateBucketSettings
and BucketSettings
structs are used for creating and updating buckets, BucketSettings
is also used for exposing information about existing buckets.
Note that any property that is not explicitly set when building the BucketSettings will use the default value.
In the case of the update, this is not necessarily the currently configured value, so you should be careful to set all properties to their correct expected values when updating an existing bucket configuration.
|
Here is the list of parameters available:
Name |
Description |
Can be updated |
|
The name of the bucket, required for creation. |
false |
|
Enables flushing to be performed on this bucket (see the Flushing Buckets section below). |
true |
|
Whether or not to replicate indexes. |
false |
|
How much memory should each node use for the bucket, required for creation. |
true |
|
The number of replicas to use for the bucket. |
true |
|
The type of the bucket, required for creation. |
false |
|
The type of the eviction to use for the bucket, defaults to |
true (note: changing will cause the bucket to restart causing temporary inaccessibility) |
|
The default maximum time-to-live to apply to documents in the bucket. (note: This option is only available for Couchbase and Ephemeral buckets in Couchbase Enterprise Edition.) |
true |
|
The compression mode to apply to documents in the bucket. (note: This option is only available for Couchbase and Ephemeral buckets in Couchbase Enterprise Edition.) |
true |
The following example creates a "hello" bucket:
await bucketMgr.createBucket({
name: 'hello',
flushEnabled: false,
replicaIndex: false,
ramQuotaMB: 200,
numReplicas: 1,
bucketType: couchbase.BucketType.Couchbase,
})
We can now get this bucket and update it to enable flush:
var settings = await bucketMgr.getBucket('hello')
settings.flushEnabled = true
await bucketMgr.updateBucket(settings)
Once you no longer need to use the bucket, you can remove it:
await bucketMgr.dropBucket('hello')
Flushing Buckets
When a bucket is flushed, all content is removed. Because this operation is potentially dangerous it is disabled by default for each bucket. Bucket flushing may be useful in test environments where it becomes a simpler alternative to removing and creating a test bucket. You may enable bucket flushing on a per-bucket basis using the Couchbase Web Console or when creating a bucket.
You can flush a bucket in the SDK by using the flush
method:
await bucketMgr.flushBucket('hello')
The flush
operation may fail if the bucket does not have flush enabled, in that case it will return an error.
Collection Management
The CollectionManager
interface may be used to create and delete scopes from the Couchbase cluster.
It is instantiated through the Bucket.collections()
method.
const bucket = cluster.bucket('travel-sample')
const collectionMgr = bucket.collections()
You can create a scope:
try {
await collectionMgr.createScope('example-scope')
} catch (e) {
if (e instanceof couchbase.ScopeExistsError) {
console.log('The scope already exists')
} else {
throw e
}
}
You can then create a collection within that scope:
try {
var collectionSpec = new couchbase.CollectionSpec({
name: 'example-collection',
scopeName: 'example-scope',
})
await collectionMgr.createCollection(collectionSpec)
} catch (e) {
if (e instanceof couchbase.CollectionExistsError) {
console.log('The collection already exists')
} else if (e instanceof couchbase.ScopeNotFoundError) {
console.log('The scope does not exist')
} else {
throw e
}
}
Finally, you can drop unneeded collections and scopes:
try {
await collectionMgr.dropCollection('example-collection', 'example-scope')
} catch (e) {
if (e instanceof couchbase.CollectionNotFoundError) {
console.log('The collection does not exist')
} else if (e instanceof couchbase.ScopeNotFoundError) {
console.log('The scope does not exist')
} else {
throw e
}
}
try {
await collectionMgr.dropScope('example-scope')
} catch (e) {
if (e instanceof couchbase.ScopeNotFoundError) {
console.log('The scope does not exist')
} else {
throw e
}
}
Note that the most minimal permissions to create and drop a Scope or Collection is Manage Scopes along with Data Reader
You can create users with the appropriate RBAC programmatically:
const userMgr = clusterAdm.users()
const bucketAdm = clusterAdm.bucket('travel-sample')
await userMgr.upsertUser({
username: 'scope_admin',
password: 'password',
displayName: 'JS Manage Scopes [travel-sample:*]',
roles: [
{ name: 'scope_admin', bucket: 'travel-sample' },
{ name: 'data_reader', bucket: 'travel-sample' },
],
})
Index Management
In general, you will rarely need to work with Index Managers from the SDK. For those occasions when you do, please see the relevant API docs:
-
QueryIndexManager —
Cluster.queryIndexes()
; -
AnalyticsIndexManager —
Cluster.analyticsIndexes()
; -
SearchIndexManager —
Cluster.searchIndexes()
; -
ViewIndexManager —
Bucket.viewIndexes()
.