Skip to content

Configure application at runtime to connect to an on-prem or cloud database using the Node.js DataStax Driver

License

Notifications You must be signed in to change notification settings

DataStax-Examples/switch-connection-nodejs

Repository files navigation

Switch Connections between Apache Cassandra™ and Astra databases

This application shows how to use the Node.js DataStax Driver to connect to an on-prem Cassandra database or a Astra database in the cloud at runtime using environment variables.

Contributor(s): Dave Bechberger

Objectives

  • Shows the differences between a Cassandra connection configuration and an Astra connection configuration in a single app.
  • Provide a demonstration of how to configure the database connection at runtime.
  • See the documentation for more details about the Astra connection configuration for the Node.js Driver

Project Layout

  • app.js - The main application file which contains all the logic to switch between the configurations

How this Sample Works

This sample uses environment variables to specify the configuration parameters and whether to use a Cassandra configuration or an Astra configuration. All the logic to switch between the configurations occurs in the getClientConfiguration function.

  • If you specify the USEASTRA environment variable:
    • The environment variables are checked to see that DBUSERNAME, DBPASSWORD, SECURECONNECTBUNDLEPATH, and KEYSPACE exist
      • If they exist then the Apollo connection is created
      • If they do not exist then an error is thrown
  • If you so not specify the USEASTRA environment variable:
    • The environment variables are checked to see that CONTACTPOINTS and DATACENTER exist
      • If they exist then the standard connection is created
      • If they do not exist then an error is thrown
      • If DBUSERNAME and DBPASSWORD exist then credentials are added to the configuration
      • If KEYSPACE exists then it is added to the configuration

While these criteria added to the configuration are commonly used configuration parameters you are able to specify any additional ones in the code.

Setup and Running

Prerequisites

  • Node 4 and above
  • A Cassandra cluster or an Astra database to connect to with the appropriate connection information

Additionally while this example uses environment variables to control which configuration is selected this could also be done via the use of configuration files or command line parameters.

For clarity this sample does not contain any of the normal error handling process you would want to wrap around connecting to a cluster to handle likely errors that would occur.

Running

To connect to an Apollo database you first need to download the secure connect bundle following the instructions found here.

Once you have this information you can run this to connect to Apollo by using the command below, with the appropriate configuration added:

USEASTRA=true DBUSERNAME=XXX DBPASSWORD=XXX KEYSPACE=XXX SECURECONNECTBUNDLEPATH="/valid/path/to/secureconnectbundle.zip" node app.js

If you would like to connect to a Cassandra cluster use the command below, with the appropriate configuration added:

CONTACTPOINTS=XX.XX.XX.XX DATACENTER=XXXX node app.js

Once you run this against either option you will get output that specifies the number of hosts and their IP addresses printed to the console:

Connected to cluster with 3 host(s) ["XX.XX.XX.136:9042","XX.XX.XX.137:9042","XX.XX.XX.138:9042"]

About

Configure application at runtime to connect to an on-prem or cloud database using the Node.js DataStax Driver

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published