Skip to content

Commit

Permalink
update quick start
Browse files Browse the repository at this point in the history
  • Loading branch information
atimin committed Dec 4, 2024
1 parent c80fb66 commit e505fcb
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 37 deletions.
74 changes: 46 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,34 +33,52 @@ application:
```js
const { Client, QuotaType } = require("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());
}
const main = async () => {
// 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 and labels to the 'entry-1' entry
const us = (dateString) => BigInt(Date.parse(dateString) * 1000);
let record = await bucket.beginWrite("sensor-1", {
ts: us("2021-01-01T11:00:00Z"),
labels: {
score: 10,
},
});

await record.write("<Blob data>");
record = await bucket.beginWrite("sensor-1", {
ts: us("2021-01-01T11:00:01Z"),
labels: {
score: 20,
},
});
await record.write("<Blob data>");

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

main().then(() => console.log("done"));
```

For more examples, see the [Guides](https://www.reduct.store/docs/guides) section in the ReductStore documentation.
32 changes: 23 additions & 9 deletions examples/QuickStart.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,34 @@ const main = async () => {
quotaSize: BigInt(1e9),
});

// 3. Write some data with timestamps in the 'sensor-1' entry
// 3. Write some data with timestamps and labels to the 'entry-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");
let record = await bucket.beginWrite("sensor-1", {
ts: us("2021-01-01T11:00:00Z"),
labels: {
score: 10,
},
});

await record.write("<Blob data>");
record = await bucket.beginWrite("sensor-1", {
ts: us("2021-01-01T11:00:01Z"),
labels: {
score: 20,
},
});
await record.write("<Blob data>");

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

0 comments on commit e505fcb

Please sign in to comment.