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:

    1. $ npm install --save async

    Writing the js code

    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. client.connect(next);
    11. },
    12. function createKeyspace(next) {
    13. console.log('Creating keyspace ybdemo');
    14. client.execute('CREATE KEYSPACE IF NOT EXISTS ybdemo;', next);
    15. },
    16. function createTable(next) {
    17. // The create table statement.
    18. 'name varchar, ' +
    19. 'age int, ' +
    20. 'language varchar);';
    21. // Create the table.
    22. console.log('Creating table employee');
    23. client.execute(create_table, next);
    24. },
    25. function insert(next) {
    26. // Create a variable with the insert statement.
    27. const insert = "INSERT INTO ybdemo.employee (id, name, age, language) " +
    28. "VALUES (1, 'John', 35, 'NodeJS');";
    29. // Insert a row with the employee data.
    30. console.log('Inserting row with: %s', insert)
    31. client.execute(insert, next);
    32. },
    33. function select(next) {
    34. const select = 'SELECT name, age, language FROM ybdemo.employee WHERE id = 1;';
    35. client.execute(select, function (err, result) {
    36. if (err) return next(err);
    37. var row = result.first();
    38. row.name, row.age, row.language);
    39. next();
    40. });
    41. }
    42. ], function (err) {
    43. if (err) {
    44. console.error('There was an error', err.message, err.stack);
    45. }
    46. console.log('Shutting down');
    47. client.shutdown();
    48. });

    To run the application, type the following:

    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

    Install the nodejs driver using the following command.

    1. $ 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:

    1. $ node yb-redis-helloworld.js

    You should see the following output.

    1. Reply: OK