Compose Database-as-a-Service Help and Documentation

Everything you need to know about Compose, Hosted or Enterprise, is here in our help system. Whether you run one database for your businesses' sole application or six different databases to support an entire corporation, we've got the information you need.

Scylla and Node.js

Working with Node.js and Scylla is simple. The cassandra-driver npm package provides all that is needed to connect to a Scylla database deployment.

A simple Node.js connection

In the example code below, we show how to establish a connection and how to use the information from the Scylla Overview.

var cassandra = require('cassandra-driver')
var uuid = require('uuid')

We start with the obligatory imports of the cassandra-driver and uuid packages. Remember to run npm install cassandra-driver uuid beforehand so that these packages are available to the program.

var authProvider = new cassandra.auth.PlainTextAuthProvider('scylla', 'PASSWORD')

client = new cassandra.Client({
    contactPoints: [
        "portal-5966219e9567df0014000e6d322-5.hercules.compose-3.composedb.com:18543",
        "portal-5966219e9567df0014000e6f332-0.hercules.compose-3.composedb.com:18544",
        "portal-5966219e9567df0014000e71345-4.hercules.compose-3.composedb.com:18545"
    ],
    keyspace: 'example_keyspace',
    authProvider: authProvider,
    sslOptions: true
});

The first thing we create is an Authentication provider. This handles the username and password exchange. The parameters are user name - always "scylla" - and password, which can be found on the Scylla overview.

The next thing to be created is the cassandra client. This takes a hash of options.

  • The first of these is contactPoints, an array of hosts and ports. This information can be extracted from the Scylla overviews connection strings:

Host and port in connection strings

The next option is keyspace. Here we are using example_keyspace, a keyspace that we created in the Scylla and cqlsh instructions on Using cqlsh.

Then comes the authProvider option, which we set to the authentication provider we previously created.

Finally, as Compose's Scylla uses SSL, we need to enable SSL in the driver. This is done by setting the ssl option. We can simply set this to true, but do remember that this is another options hash which can pass SSL options and therefore could also be {}. Unlike Python and cqlsh, no certificates need to be installed or used.

That completes the creation of the client. We can now go on to use it. Here's an insert being executed on a table:

client.execute("INSERT INTO names(name_id, first_name, last_name) VALUES(?,?,?)", [uuid.v4(), "Arthur", "Unique"], { prepare: true },
    function(err, result) {
        if (err) { console.error(err); }
        console.log("success")
    });

This makes use of prepared statements for efficiency, but also, notice we didn't tell the client to connect. That's because execute() tries to connect itself if no connection is currently open. You can explicitly call client.connect() if you wish.

This command should execute and return "success". The Node.js runtime won't exit as the Cassandra driver will still be running.

More Information


Still Need Help?

If this article didn't solve things, summon a human and get some help!

Updated about a year ago

Scylla and Node.js


Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.