Skip to main content
Version: 1.10.x

Quick Start With JavaScript

This quick start guide will walk you through the process of installing and using the ReductStore JavaScript Client SDK to read and write data to a ReductStore instance.

Installing the JavaScript SDK​

To install the SDK, you will only need npm or yarn. You can install the SDK by using the following command:

npm install reduct-js

Running ReductStore​

If you don't already have a ReductStore instance running, you can easily spin up one as a Docker container. To do this, run the following command:

docker run -p 8383:8383 -e RS_API_TOKEN="my-token" reduct/store:latest

This will start a ReductStore instance listening on port 8383 on your local machine. The RS_API_TOKEN environment variable is used to authenticate requests to the ReductStore instance. You can set it to any value you like, but you will need to use the same value when creating a Client object in your code.

If Docker is not an option, you can also download the ReductStore binaries. Check the Download Page.

Hello World Example​

Now when you have the SDK installed and a ReductStore instance running, you can start using the SDK to interact with the ReductStore database. Here is an example of using the SDK to perform basic operations on a ReductStore instance:

import { Client, QuotaType } from "reduct-js";

// 1. Create a ReductStore client
const client = new Client("http://127.0.0.1:8383", {
apiToken: "my-token",
});

// 2. Get or create a bucket with 1Gb quota
const bucket = await client.getOrCreateBucket("my-bucket", {
quotaType: QuotaType.FIFO,
quotaSize: BigInt(1e9),
});

// 3. Write some data with timestamps in the 'sensor-1' entry
const us = (dateString) => BigInt(Date.parse(dateString) * 1000);
let record = await bucket.beginWrite("sensor-1", us("2021-01-01T00:00:00Z"));
await record.write("Record #1");
record = await bucket.beginWrite("sensor-1", us("2021-01-01T00:00:01Z"));
await record.write("Record #2");

// 4. Query the data by time range
for await (const record of bucket.query(
"sensor-1",
us("2021-01-01T00:00:00Z"),
us("2021-01-01T00:00:02Z"),
)) {
console.log(`Record timestamp: ${record.timestamp}`);
console.log(`Record size: ${record.size}`);
console.log(await record.readAsString());
}

Let's break down what this example is doing.

Creating a Client​

Before you can interact with a ReductStore instance, you must create a Client object that represents a connection to the ReductStore instance.

To create the client, you can use the Client class from the reduct-js package. Pass the URL of the ReductStore instance you want to connect to as an argument to the Client constructor.

const client = new Client("http://127.0.0.1:8383", {
apiToken: "my-token",
});

Creating a Bucket​

ReductStore organizes data into buckets, each of which has its own quota and settings. It's a necessary step to create a bucket before writing data to it. You can read more about buckets in the Buckets Guide, but for now, let's just create one.

To create a bucket, you should use the getOrCreateBucket method on a Client instance. Pass the name of the bucket you want to create as an argument, along with settings. If the bucket already exists, the getOrCreateBucket method will return it.

const bucket = await client.getOrCreateBucket("my-bucket", {
quotaType: QuotaType.FIFO,
quotaSize: BigInt(1e9),
});

In this example we create a bucket with a FIFO quota of 1GB. This means that the oldest data will be deleted when the bucket reaches 1GB.

Data Ingestion​

Time series data is stored in entries within a bucket. An entry is a collection of records with unique timestamps. It must have a unique name within the bucket and usually represents a data source, such as a vibration sensor or a CV camera.

To write a timestamped record to an entry in a bucket, you should use the beginWrite method on a Bucket object. Pass the name of the entry you want to write to as an argument, along with the data and timestamp you want to write. The beginWrite method will create the sensor-1 entry if it doesn't exist and return a WritableRecord object. The WritableRecord object can be used to send the data to the database.

const us = (dateString) => BigInt(Date.parse(dateString) * 1000);
let record = await bucket.beginWrite("sensor-1", us("2021-01-01T00:00:00Z"));
await record.write("Record #1");
record = await bucket.beginWrite("sensor-1", us("2021-01-01T00:00:01Z"));
await record.write("Record #2");

Pay attention that the time is a UNIX time in microseconds. This the reason why we use BigInt to represent it.

This is the simplest case of writing data using the Python SDK. You can also write data in chunks and annotate records with labels. You can find more information and examples in the Data Ingestion Guide.

Data Querying​

Usually, we don't read a particular record by its timestamp, but query records in a time range.

To iterate over all records in a given time range, you should use the query method on a bucket instance. Pass the name of the entry to iterate over, and start and stop arguments to specify the time interval. The query method returns an async iterator over the records in the entry:

for await (const record of bucket.query(
"sensor-1",
us("2021-01-01T00:00:00Z"),
us("2021-01-01T00:00:02Z"),
)) {
console.log(`Record timestamp: ${record.timestamp}`);
console.log(`Record size: ${record.size}`);
console.log(await record.readAsString());
}

The query method has many parameters for filtering and returning sample records. For more information and examples, see the Data Querying Guide.

Next Steps​

As you can see to get started with the Client SDK is very easy. However,it doesn't cover all the features of the SDK and the database. Check our Guides to learn more about the ReductStore features and how to use them.