9 min to complete
In this lesson, we will explore connecting to a ScyllaDB cluster using the Node.js driver.
When creating applications that communicate with a database such as ScyllaDB, the programming language being used must have support for database connectivity. Since ScyllaDB is compatible with Cassandra, we can use any of the available Cassandra libraries. In Node.js, we use the cassandra-driver. Let’s begin by writing a sample Node.js application.
Creating a Sample Node.js Application
The sample application that we will go over will connect to a ScyllaDB cluster, display the contents of the Mutant Catalog table, insert and delete data, and show the contents of the table after each action.
We will first go through each section of the code that will be used, explain the main concepts, and then you will run the code yourself in the lab below.
In the application, we first need to import the necessary libraries and modules:
The cassandra-driver client library provides the ability to connect to the cluster and interact with it. The async module lets us control the order in which functions are executed.
The code is stored in a file called part1_app.js. We define the client contact points, datacenter, and keyspace to use, and then we connect to the cluster:
To display the data, we will need to create a function that will run a select statement from the ScyllaDB cluster. The function below will use the defined client object to gather the data and write the first and last names to the log:
The next function is used to insert a new Mutant into the Mutant Catalog table, again using the client object:
After the data is added, we use the following delete function to remove it:
Finally, we will close the client connection:
With the coding part done, let’s run the lab.
Conclusion
In this lesson, we explained how to create a sample Node.js application that executes a few basic CQL statements with a ScyllaDB cluster using the nodejs-driver. This is only the basics, and there are more exciting topics that Division 3 wants developers to explore.
In the meantime, please be safe out there and continue to monitor the mutants!