diff --git a/README.md b/README.md index 2a58637..c316616 100644 --- a/README.md +++ b/README.md @@ -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(""); + record = await bucket.beginWrite("sensor-1", { + ts: us("2021-01-01T11:00:01Z"), + labels: { + score: 20, + }, + }); + await record.write(""); + + // 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. diff --git a/examples/QuickStart.js b/examples/QuickStart.js index b62e050..64dd7f9 100644 --- a/examples/QuickStart.js +++ b/examples/QuickStart.js @@ -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(""); + record = await bucket.beginWrite("sensor-1", { + ts: us("2021-01-01T11:00:01Z"), + labels: { + score: 20, + }, + }); + await record.write(""); - // 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()); }