Develop NodeJS Apps

AttentionThis page documents an earlier version. Go to the latest (v2.1)version.

Installation

Install the nodejs driver using the following command. You can find the source for the driver here.

  1. $ npm install cassandra-driver

Working Example

Pre-requisites

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 quick start guide.
  • installed a recent version of node. If not, you can find install instructions here.

We will be using the async JS utility to work with asynchronous Javascript. Install this by running the following command:

  1. $ npm install --save async

Writing the js code

Create a file yb-cql-helloworld.js and add the following content to it.

  1. const cassandra = require('cassandra-driver');
  2. const async = require('async');
  3. const assert = require('assert');
  4. // Create a YB CQL client.
  5. // DataStax Nodejs 4.0 loadbalancing default is TokenAwarePolicy with child DCAwareRoundRobinPolicy
  6. // Need to provide localDataCenter option below or switch to RoundRobinPolicy
  7. const loadBalancingPolicy = new cassandra.policies.loadBalancing.RoundRobinPolicy ();
  8. const client = new cassandra.Client({ contactPoints: ['127.0.0.1'], policies : { loadBalancing : loadBalancingPolicy }});
  9. async.series([
  10. function connect(next) {
  11. client.connect(next);
  12. },
  13. function createKeyspace(next) {
  14. console.log('Creating keyspace ybdemo');
  15. client.execute('CREATE KEYSPACE IF NOT EXISTS ybdemo;', next);
  16. },
  17. function createTable(next) {
  18. // The create table statement.
  19. const create_table = 'CREATE TABLE IF NOT EXISTS ybdemo.employee (id int PRIMARY KEY, ' +
  20. 'name varchar, ' +
  21. 'age int, ' +
  22. 'language varchar);';
  23. // Create the table.
  24. console.log('Creating table employee');
  25. client.execute(create_table, next);
  26. },
  27. function insert(next) {
  28. // Create a variable with the insert statement.
  29. const insert = "INSERT INTO ybdemo.employee (id, name, age, language) " +
  30. "VALUES (1, 'John', 35, 'NodeJS');";
  31. // Insert a row with the employee data.
  32. console.log('Inserting row with: %s', insert)
  33. client.execute(insert, next);
  34. },
  35. function select(next) {
  36. // Query the row for employee id 1 and print the results to the console.
  37. const select = 'SELECT name, age, language FROM ybdemo.employee WHERE id = 1;';
  38. client.execute(select, function (err, result) {
  39. if (err) return next(err);
  40. var row = result.first();
  41. console.log('Query for id=1 returned: name=%s, age=%d, language=%s',
  42. row.name, row.age, row.language);
  43. next();
  44. });
  45. }
  46. ], function (err) {
  47. if (err) {
  48. console.error('There was an error', err.message, err.stack);
  49. }
  50. console.log('Shutting down');
  51. client.shutdown();
  52. });

Running the application

To run the application, type the following:

  1. $ node yb-cql-helloworld.js

You should see the following output.

  1. Creating keyspace ybdemo
  2. Creating table employee
  3. Inserting row with: INSERT INTO ybdemo.employee (id, name, age, language) VALUES (1, 'John', 35, 'NodeJS');
  4. Query for id=1 returned: name=John, age=35, language=NodeJS
  5. Shutting down

Installation

Install the nodejs driver using the following command.

  1. $ npm install redis

Working Example

Pre-requisites

This tutorial assumes that you have:

  • 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 here.

Writing the js code

Create a file yb-redis-helloworld.js and add the following content to it.

  1. const redis = require("redis")
  2. const client = redis.createClient();
  3. client.on("error", function (err) {
  4. console.log("Error " + err);
  5. });
  6. // Insert the user profile.
  7. const userid = 1
  8. client.hmset(userid, ["name", "John", "age", "35", "language", "NodeJS"], redis.print);
  9. // Query the user profile.
  10. client.hmget(userid, ["name", "age", "language"], redis.print);
  11. // Close the client.
  12. client.quit(function (err, res) {
  13. console.log('Exiting from quit command.');
  14. });

Running the application

To run the application, type the following:

  1. $ node yb-redis-helloworld.js

You should see the following output.

  1. Reply: OK
  2. Reply: John,35,NodeJS
  3. Exiting from quit command.