Append & Delete Time Series Operations
-
Use
TimeSeriesBatchOperation
to Append and Delete multiple time series entries on a single document.
A list of predefined Append and Delete actions will be executed in this single batch operation. -
To Append and Delete multiple time series entries on multiple documents, see PatchByQueryOperation.
-
For a general Operations overview, see What are Operations.
-
In this page:
Usage
Flow:
-
Prepare the Append and Delete operations:
- Create an instance of
TimeSeriesOperation.AppendOperation
to define an Append action. - Create an instance of
TimeSeriesOperation.DeleteOperation
fo define a Delete action.
- Create an instance of
-
Create an instance of
TimeSeriesOperation
and pass it the the time series name.- Call
TimeSeriesOperation.append
to add the Append operation. - Call
TimeSeriesOperation.delete
to add the Delete operation.
- Call
-
Create a
TimeSeriesBatchOperation
instance and pass it:- The document ID
- The
TimeSeriesOperation
object
- Execute the
TimeSeriesBatchOperation
operation by callingstore.operations.send
Note:
- All the added Append and Delete operations will be executed in a single-node transaction.
- Delete actions are executed before Append actions. As seen in this example.
- Appending entries to a time series that doesn't yet exist yet will create the time series.
- An exception will be thrown if the specified document does Not exist.
Examples
Append multiple entries:
In this example, we append four entries to a time series.
const baseTime = new Date();
// Define the Append operations:
let nextMinute = new Date(baseTime.getTime() + 60_000);
const appendOp1 = new AppendOperation(nextMinute, [79], "watches/fitbit");
nextMinute = new Date(baseTime.getTime() + 60_000 * 2);
const appendOp2 = new AppendOperation(nextMinute, [82], "watches/fitbit");
nextMinute = new Date(baseTime.getTime() + 60_000 * 3);
const appendOp3 = new AppendOperation(nextMinute, [80], "watches/fitbit");
nextMinute = new Date(baseTime.getTime() + 60_000 * 4);
const appendOp4 = new AppendOperation(nextMinute, [78], "watches/fitbit");
// Define the 'TimeSeriesOperation':
const timeSeriesOp = new TimeSeriesOperation("HeartRates");
// Add the Append operations by calling 'append':
timeSeriesOp.append(appendOp1);
timeSeriesOp.append(appendOp2);
timeSeriesOp.append(appendOp3);
timeSeriesOp.append(appendOp4);
// Define 'TimeSeriesBatchOperation':
const timeSeriesBatchOp = new TimeSeriesBatchOperation("users/john", timeSeriesOp);
// Execute the batch operation:
await documentStore.operations.send(timeSeriesBatchOp);
Delete multiple entries:
In this example, we delete a range of two entries from a time series.
const baseTime = new Date();
const from = new Date(baseTime.getTime() + 60_000 * 2);
const to = new Date(baseTime.getTime() + 60_000 * 3);
// Define the Delete operation:
const deleteOp = new DeleteOperation(from, to);
// Define the 'TimeSeriesOperation':
const timeSeriesOp = new TimeSeriesOperation("HeartRates");
// Add the Delete operation by calling 'delete':
timeSeriesOp.delete(deleteOp);
// Define the 'TimeSeriesBatchOperation':
const timeSeriesBatchOp = new TimeSeriesBatchOperation("users/john", timeSeriesOp);
// Execute the batch operation:
await documentStore.operations.send(timeSeriesBatchOp);
Append & delete entries in the same batch:
-
In this example, we append and delete entries in the same batch operation.
-
Note: the Delete actions are executed before all Append actions.
const baseTime = new Date();
// Define some Append operations:
let nextMinute = new Date(baseTime.getTime() + 60_000);
const appendOp1 = new AppendOperation(nextMinute, [79], "watches/fitbit");
nextMinute = new Date(baseTime.getTime() + 60_000 * 2);
const appendOp2 = new AppendOperation(nextMinute, [82], "watches/fitbit");
nextMinute = new Date(baseTime.getTime() + 60_000 * 3);
const appendOp3 = new AppendOperation(nextMinute, [80], "watches/fitbit");
const from = new Date(baseTime.getTime() + 60_000 * 2);
const to = new Date(baseTime.getTime() + 60_000 * 3);
// Define a Delete operation:
const deleteOp = new DeleteOperation(from, to);
// Define the 'TimeSeriesOperation':
const timeSeriesOp = new TimeSeriesOperation("HeartRates");
// Add the Append & Delete operations to the list of actions
// Note: the Delete action will be executed BEFORE all the Append actions
// even though it is added last
timeSeriesOp.append(appendOp1);
timeSeriesOp.append(appendOp2);
timeSeriesOp.append(appendOp3);
timeSeriesOp.delete(deleteOp);
// Define the 'TimeSeriesBatchOperation':
const timeSeriesBatchOp = new TimeSeriesBatchOperation("users/john", timeSeriesOp);
// Execute the batch operation:
await documentStore.operations.send(timeSeriesBatchOp);
// Results:
// All 3 entries that were appended will exist and are not deleted.
// This is because the Delete action occurs first, before all Append actions.
Syntax
TimeSeriesBatchOperation
TimeSeriesBatchOperation(documentId, operation)
Parameter | Type | Description |
---|---|---|
documentId | string |
The ID of the document to which you want to Append/Delete time series data. |
operation | TimeSeriesOperation |
This class defines which Append/Delete actions to perform within the batch operation. |
TimeSeriesOperation
class TimeSeriesOperation {
name;
append;
delete;
}
Property | Type | Description |
---|---|---|
name | string |
The time series name |
append | method |
Pass a AppendOperation object to this method to add it to the list |
delete | method |
Pass a DeleteOperation object to this method to add it to the list |
AppendOperation
class AppendOperation {
timestamp;
values;
tag;
}
Property | Type | Description |
---|---|---|
timestamp | Date |
The time series entry will be appended at this timestamp |
values | number[] |
Entry values |
tag | string |
Entry tag (optional) |
DeleteOperation
class DeleteOperation {
from;
to;
}
Property | Type | Description |
---|---|---|
from | Date |
Entries will be deleted starting at this timestamp (inclusive) Default: the minimum date value |
to | Date |
Entries will be deleted up to this timestamp (inclusive) Default: the maximum date value |