API Libraries > Node.js

Node.js

This document describes how to use node.js client for Rockset. This client allows you to:

  • securely connect to Rockset service
  • create, manage and administer collections
  • issue queries against collections

The Node.js client library code is present here.

Installation

npm install rockset --save

Connect

Client objects allow you to connect securely to Rockset service. All other API calls require a valid client object.

In order to create a client object, you will need a valid Rockset API key. If you have access to the Rockset Console, then you can use the console to create an API key. If not, please contact the Rockset team at support@rockset.com

var client = require('rockset')("{apiKey}", "{apiServer}");

Use https://api.rs2.usw2.rockset.com for the apiServer.

Usage

Create a collection

Create a collection using the client object as follows:

client.collections.create({
    'name': 'my-first-collection',
    'description': 'my first collection'
}, function (error, response, body) {
    console.log(response);
});

If your document source is S3, follow the steps below otherwise refer to Rock CLI to add documents to the created collection.

Create an Integration

If you have an Amazon S3 bucket that you want to ingest data from, create a Rockset Integration to store credentials required to access the bucket. Create an integration object using the client object as follows:

client.integrations.create({
    'name': 'my-first-integration',
    'description': 'my-first-integration',
    'aws': {
        'aws_access_key_id': '.....',
        'aws_secret_access_key': '.....'
    }
}, function (error, response, body) {
    console.log(response);
});

Create a collection from Amazon S3

Prior to creating a collection using Amazon S3 as source, create a Rockset Integration first as described above.

client.collections.create({
    'name': 'my-first-s3-collection',
    'description': 'my first s3 collection',
    'sources': [{
        'integration_name': 'my-first-integration',
        's3': {
            'bucket': 'bucket-name'
        }
    }]
}, function (error, response, body) {
    console.log(response);
});

Add Documents

Add documents to an existing collection using the client object.

client.documents.add(
    'my-first-collection',
    {
        'data': [{
            "name": "foo",
            "address": "bar"
        }]
    }, function (error, response, body) {
    console.log(response);
});

Query

Make queries to Rockset using the client object.

client.queries.query({
    'sql': {
        'query': 'select * from _events limit 1'
    }
}, function(error, response, body) {
    console.log(response);
});

You can find more examples here.