Skip to main content

Couchbase

A distributed NoSQL document-oriented database that provides consistent, low-latency data access to improve application performance.

TagsNoSQLDatabaseDocument-oriented
Network Connection needs

This integration needs network access to the server where the service is running.

See the Network access page for details about how to achieve that.

Credential configuration

Fill in the appropiate values for your Couchbase connection: connectionString, username and password.

In the extra options field, you can provide any of the parameters found here.

Here is an example of a filled credential configuration form in YepCode:

Couchbase Snippets available in editor

note

The title is the triggering text for YepCode to autocomplete the script.

Integration

New integration from credential
const couchbaseCluster = await yepcode.integration.couchbase('credential-slug')
New integration from plain authentication data
const couchbase = require("couchbase");

const couchbaseCluster = await couchbase.connect("connection-string", {
username: "username"",
password: "password"
// You should use a yepcode env variable to don't store plain password
// See: https://docs.yepcode.io/docs/processes/team-variables
});

Create a Bucket

Create a bucket
await couchbaseCluster.cluster
.buckets()
.createBucket({
name: "bucket_name",
flushEnabled: true,
ramQuotaMB: 100,
})
.then(() => console.log("Bucket Created!"))
.catch(console.error);

Get a Bucket

Get a bucket
const bucket = couchbaseCluster.bucket("bucket_name");

Create a scope

Create a scope
await bucket
.collections()
.createScope("scope_name")
.then(() => console.log("Scope Created!"))
.catch(console.error);

Create a Collection

Create a collection
await bucket
.collections()
.createCollection({
name: "collection_name",
scopeName: "scope_name",
})
.then(() => console.log("Collection Created!"))
.catch(console.error);

Get a Collection

Get a collection
const collection = bucket.scope("scope_name").collection("collection_name")

Insert a Document

Insert a document
const sampleItem = {
foo: "bar"
}

await collection.upsert(item_id", sampleItem)

Get a Document

Get a document

let result = await collection.get("item_id")
console.log('Result: ', result)

Query a Collection

Query a collection
// If your collection does not have any index,
// you'll need to create a primary index to be able to query it
await collection.queryIndexes().createPrimaryIndex({
ignoreIfExists: true,
});

const queryResult =
await bucket
.scope("scope_name")
.query('SELECT name FROM `collection_name` WHERE some_item_key=$1 LIMIT 10', {
parameters: ['param1'],
})

console.log('Query Results:')
queryResult.rows.forEach((row) => console.log(row))

Query a Collection (Stream)

Query a collection (Stream)
// If your collection does not have any index,
// you'll need to create a primary index to be able to query it
await collection.queryIndexes().createPrimaryIndex({
ignoreIfExists: true,
});

const queryStream = bucket.scope("scope_name")
.query('SELECT name FROM `collection_name` WHERE some_item_key=$1 LIMIT 10', {
parameters: ['param1'],
})

queryStream.on("row", console.log);
queryStream.on("end", () => console.log("Query stream end"));

Delete a Document

Delete a document
await collection.remove("item_id")

Delete a Scope

Delete a scope
await bucket
.collections()
.dropScope("scope_name")
.then(() => console.log("Scope Dropped!"))
.catch(console.error);

Delete a Bucket

Delete a bucket
await couchbaseCluster
.buckets()
.dropBucket("bucket_name")
.then(() => console.log("Bucket Dropped!"))
.catch(console.error);