Develop NodeJS Apps
Install the nodejs driver using the following command. You can find the source for the driver here.
This tutorial assumes that you have:
- installed YugabyteDB, created a universe and are able to interact with it using the CQL shell. If not, please follow these steps in the .
- installed a recent version of . If not, you can find install instructions here.
We will be using the JS utility to work with asynchronous Javascript. Install this by running the following command:
$ npm install --save async
Writing the js code
const cassandra = require('cassandra-driver');
const async = require('async');
const assert = require('assert');
// Create a YB CQL client.
// DataStax Nodejs 4.0 loadbalancing default is TokenAwarePolicy with child DCAwareRoundRobinPolicy
// Need to provide localDataCenter option below or switch to RoundRobinPolicy
const loadBalancingPolicy = new cassandra.policies.loadBalancing.RoundRobinPolicy ();
const client = new cassandra.Client({ contactPoints: ['127.0.0.1'], policies : { loadBalancing : loadBalancingPolicy }});
async.series([
client.connect(next);
},
function createKeyspace(next) {
console.log('Creating keyspace ybdemo');
client.execute('CREATE KEYSPACE IF NOT EXISTS ybdemo;', next);
},
function createTable(next) {
// The create table statement.
'name varchar, ' +
'age int, ' +
'language varchar);';
// Create the table.
console.log('Creating table employee');
client.execute(create_table, next);
},
function insert(next) {
// Create a variable with the insert statement.
const insert = "INSERT INTO ybdemo.employee (id, name, age, language) " +
"VALUES (1, 'John', 35, 'NodeJS');";
// Insert a row with the employee data.
console.log('Inserting row with: %s', insert)
client.execute(insert, next);
},
function select(next) {
const select = 'SELECT name, age, language FROM ybdemo.employee WHERE id = 1;';
client.execute(select, function (err, result) {
if (err) return next(err);
var row = result.first();
row.name, row.age, row.language);
next();
});
}
], function (err) {
if (err) {
console.error('There was an error', err.message, err.stack);
}
console.log('Shutting down');
client.shutdown();
});
To run the application, type the following:
You should see the following output.
Creating keyspace ybdemo
Creating table employee
Inserting row with: INSERT INTO ybdemo.employee (id, name, age, language) VALUES (1, 'John', 35, 'NodeJS');
Query for id=1 returned: name=John, age=35, language=NodeJS
Shutting down
Install the nodejs driver using the following command.
$ npm install redis
Pre-requisites
- installed YugabyteDB, created a universe and are able to interact with it using the Redis shell. If not, please follow these steps in the quick start guide.
- installed a recent version of
node
. If not, you can find install instructions .
Create a file yb-redis-helloworld.js
and add the following content to it.
Running the application
To run the application, type the following:
$ node yb-redis-helloworld.js
You should see the following output.
Reply: OK