@rockset/client
Official Rockset Javascript/Typescript Client
Requirements
Node 10+
Recommended: use Typescript for type checking.
Installation
npm i @rockset/client
Note: you can also install using yarn
.
Complete Reference
A complete reference of this library (powered by TypeDoc) can be found here.
Usage Examples
Import (ES6 Syntax)
Note: you can also import using CommonJS syntax.
import rockset from '@rockset/client';
Configure Client
const rocksetClient = rockset(process.env.ROCKSET_APIKEY);
Note: most sans-TypeScript projects currently require the following
(we're currently working on better interoperability for Node / CommonJS / ES6):
const rocksetClient = rockset.default(process.env.ROCKSET_APIKEY);
Loading Your Data
Note: we recommend using the Rockset Console for setting up integrations, with which
you can create collections that load data from external data sources.
Create an empty collection
rocksetClient.collections
.createCollection('commons' , {
name: 'MyFirstCollection' ,
description:
'I can write data to this collection' ,
})
.then(console.log)
.catch(console.error);
Create an empty collection with field mappings
import { InputField, OutputField } from '@rockset/client/dist/codegen/api';
rocksetClient.collections
.createCollection('commons', {
name: 'Users',
description: "I'm the map!",
retention_secs: 10000 ,
field_mappings: [
{
name: 'drop_all_fields' ,
is_drop_all_fields: true ,
},
{
name: 'cast_age_to_int',
input_fields: [
{
field_name: 'age' ,
if_missing: InputField.IfMissingEnum.SKIP,
is_drop: false ,
param:
'input_age' ,
},
{
field_name: 'first_name',
if_missing: InputField.IfMissingEnum.PASS,
is_drop: true,
},
{
field_name: 'last_name',
if_missing: InputField.IfMissingEnum.PASS,
is_drop: true,
},
],
output_field: {
field_name:
'user_description' ,
value: {
sql:
"CONCAT(:first_name, ' ', :last_name, ' is ', CAST(:input_age as int), ' years old.')",
} ,
on_error: OutputField.OnErrorEnum.FAIL,
},
},
],
})
.then(console.log)
.catch(console.error);
Add documents into your collection with the Write API
rocksetClient.documents
.addDocuments(
'commons' ,
'Users' ,
{
data: [
{ first_name: 'Brian', last_name: 'Morris', age: '14' },
{ first_name: 'Justin', last_name: 'Smith', age: '78' },
{ first_name: 'Scott', last_name: 'Wong', age: '42' },
],
}
)
.then(console.log)
.catch(console.error);
Query your data
Run a SQL query
rocksetClient.queries
.query({
sql: {
query: 'SELECT * FROM commons.Users u WHERE u.age > :minimum_age',
parameters: [
{
name: 'minimum_age' ,
type:
'int' ,
value: '20' ,
},
],
default_row_limit: 150 ,
generate_warnings: true,
profiling_enabled: true,
},
})
.then(console.log)
.catch(console.error);
let page = await rockset.queries.query({
sql: {
query: 'SELECT * FROM my_collection LIMIT 100',
paginate: true,
initial_paginate_response_doc_count: 20,
},
});
const qid = page.query_id ?? '';
while (page.pagination?.next_cursor) {
console.log(page.results);
page = await rockset.queries.getQueryPagination(
qid,
page.pagination?.next_cursor ?? '',
10,
0
);
}
console.log(page.results);
Run queries using Query Lambdas
rocksetClient.queryLambdas
.createQueryLambda('commons' , {
name: 'MyFirstQueryLambda' ,
description: 'A Query Lambda' ,
sql: {
query: 'SELECT * FROM commons.Users u WHERE u.age > :minimum_age',
default_parameters: [{ name: 'minimum_age', type: 'int', value: '40' }],
},
})
.then(console.log)
.catch(console.error);
rocksetClient.queryLambdas
.executeQueryLambdaByTag('commons', 'MyFirstQueryLambda', 'latest', {
parameters: [{ name: 'minimum_age', type: 'int', value: '20' }],
})
.then(console.log)
.catch(console.error);
rocksetClient.queryLambdas
.updateQueryLambda('commons', 'MyFirstQueryLambda', {
description: 'query for exact age',
sql: {
query: 'SELECT * FROM commons.Users u WHERE u.age = :minimum_age',
default_parameters: [{ name: 'minimum_age', type: 'int', value: '42' }],
},
})
.then((response) => {
console.log(response);
rocksetClient.queryLambdas
.executeQueryLambda(
'commons',
'MyFirstQueryLambda',
response.data.version ,
{
parameters: [{ name: 'minimum_age', type: 'int', value: '10' }],
default_row_limit: 150,
generate_warnings: true,
}
)
.then(console.log)
.catch(console.error);
rocksetClient.queryLambdas
.createQueryLambdaTag('commons', 'MyFirstQueryLambda', {
tag_name: 'dev' ,
version:
response.data.version ,
})
.then(console.log)
.catch(console.error);
})
.catch(console.error);
Manage your collections
Create an alias for your collection
rocksetClient.aliases
.createAlias('commons', {
name: 'UsersAlias' ,
description: 'An alias for users collection' ,
collections: [
'commons.Users',
] ,
})
.then(console.log)
.catch(console.error);
Query an alias, as you would any collection
rocksetClient.queries
.query({
sql: {
query: 'SELECT * FROM commons.UsersAlias',
},
})
.then(console.log)
.catch(console.error);
Point an alias to a new collection to avoid downtime
rocksetClient.collections
.createCollection('commons', {
name: 'UsersV2',
description: 'Updated Users collection',
})
.then(console.log)
.catch(console.error);
rocksetClient.aliases
.updateAlias('commons', 'UsersAlias', {
collections: ['commons.UsersV2'],
})
.then(console.log)
.catch(console.error);
Testing
Unit tests are available in the tests folder.
Set ROCKSET_APIKEY and ROCKSET_HOST endpoint in the environment variables. To run tests:
yarn test
This runs unit tests and integration tests. You can alternately run individual tests or test files using Jest:
jest src/tests/mockclient.test.ts
Advanced
Custom Fetch Function
By default, the client is a thin wrapper that sends REST calls to Rockset using node-fetch
. Some applications may require more complex behavior. If additional functionality is required, the client can be configured to generate the parameters for a REST call, and pass them to a custom fetch function to be handled accordingly.
Here is an example that shows how to support cancelling API calls using a custom fetch function with Axios. To supply a custom fetch function, we pass it in as the last parameter to rockset
.
import axios from 'axios';
import rockset from '@rockset/client';
const customFetchAxios = async (
url: string,
{ headers, method, body: data, queryParams: params, cancelToken }: any
) => {
const res = await axios.request({
url,
headers,
method,
data,
params,
cancelToken,
});
return res.data;
};
const basePath = 'https://api.rs2.usw2.rockset.com';
const rocksetClient = rockset(
process.env.ROCKSET_APIKEY,
basePath,
customFetchAxios
);
const cancelSource = axios.CancelToken.source();
rockset.queries
.query(
{ sql: { query: 'SELECT count(*) FROM _events' } },
{ cancelToken: cancelSource.token }
)
.then(console.log)
.catch(console.error);
cancelSource.cancel();
How to contribute
Please feel free to submit a pull request for modifications that can benefit other users in the community. It is best to have a unit test associated with each pull request.
Support
File any issues with this client through GitHub.
License
This client is licensed under the Apache 2.0 License.