If you’re a JavaScript dev who’s transitioning to use Node.js, you’ll want to take advantage of async functions.

That’s because asynchronous API calls don’t block your code and return promises. In this post, I’ll show you how to create async functions in Node.js that call the Couchbase SDK.

Quick Recap: Setting up a Node.js Project with Couchbase

This post continues my introductory series on using Node.js with Couchbase.

An existing Couchbase database (with travel-sample Bucket enabled) and Node.js environment is assumed for this tutorial, but for more details see last week’s post on getting started with the Node.js SDK for Couchbase.

To catch you up, first install the Couchbase Node.js SDK using the npm command, along with the save option to store the dependency in your package.json configuration file. Here’s what that looks like:

Now you’re ready to dive into the next step.

Connect to Couchbase with an Async Function

Connecting to Couchbase using the JavaScript async function requires you to import the library in addition to three more components:

    • Server name/address
    • Username/password
    • The Bucket to connect to

Wrap it all with a basic async function and create the cluster object.

Once you provide the main connection info, then select the Bucket and any specific Scope or Collection (I used the default Collection throughout this example). The resulting Collection object is used for subsequent database calls.

Getting a JSON Document

Now that you have an async function set up, let’s learn how you can get a JSON document from Couchbase.

In order to complete a basic key-value operation, you need to know an existing document ID. For this example, let’s use the ID for Chalets Marmotte Mountain Adventure in France: hotel_5336.

The basic get syntax – shown in previous blog post too – is:

To make it async, use the await keyword inside an async function. We’ll call this function afterwards with our hotel ID. It’s also a good habit to start capturing and printing any errors.

Call your new function at the end of the script. Remember, the entire script should be held within the main() function that you call:

Then test your async function by running and seeing the output for that particular document:

Upserting a JSON Document

Next, let’s cover how you can upsert a JSON document using your async function.

Using your original script, you can add the ability to create a new document and then request it back – to show the full roundtrip. To save or add a document into the database, use the upsert function and pass it a JSON object.

Remember, you can keep the connection and get function and create a new function for the upsert.

First, create the newHotel function. As you can see below, we set it up to take the key/ID and a JSON object.

Next, create the JSON document we will send to the Couchbase database.

We’ll call the new hotel entry hotel_3 when calling the new function:

If you put all this code before the getHotel call, you can adjust the document ID for the get function to check that the new hotel was saved.

Here is the output of creating the document and then getting the same one back:

Full Code Sample

Here’s all of the example code put together from today’s post:

Conclusion

Congratulations on doing a full upsert and get cycle in Node.js! Now you’re ready to build a more complicated application – which I’ll cover in future blog posts.

For a more in-depth developer guide, see the Couchbase Node.js SDK documentation here.

Catch up with the rest of the Node.js + Couchbase how-to series:

 

Roll up your sleeves and try it out for yourself:
Try out Couchbase Cloud today

 

Author

Posted by Tyler Mitchell

Works as Senior Product Marketing Manager at Couchbase, helping bring knowledge about products into the public limelight while also supporting our field teams with valuable content. His personal passion is all things geospatial, having worked in GIS for half his career. Now AI and Vector Search is top of mind.

Leave a reply