Time Series: JavaScript Support



JavaScript time series API methods

The JavaScript time series API includes these methods:


timeseries (doc, name)

Choose a time series by the ID of its owner document and by the series name.

Parameter Type Description
doc string
or
document instance
Document ID, e.g. timeseries('users/1-A', 'StockPrice')

e.g. timeseries(this, 'StockPrice')
name string Time Series Name

timeseries.append

You can use two overloads, to append tagged or untagged time series entries.

  • timeseries.append (timestamp, values)
  • timeseries.append (timestamp, values, tag)
Parameter Type Description
timestamp Date Timestamp
values number[] Values
tag string Tag

timeseries.delete (from, to)

Use this method to delete a range of entries from a document.

Parameter Type Description
from (optional) Date Entries will be deleted starting at this timestamp (inclusive).
Default: the minimum date value.
to (optional) Date Entries will be deleted up to this timestamp (inclusive).
Default: the maximum date value.

timeseries.get (from, to)

Use this method to retrieve a range of time series entries.

Parameter Type Description
from (optional) Date Get time series entries starting from this timestamp (inclusive).
Default: The minimum date value.
to (optional) Date Get time series entries ending at this timestamp (inclusive).
Default: The maximum date value.

Return Type:
Values are returned in an array of time series entries, i.e. -

[
	{
		"Timestamp" : ...
		"Tag": ...
		"Values": ...
		"IsRollup": ...
	},
	{
		"Timestamp" : ...
		"Tag": ...
		"Values": ...
		"IsRollup": ...
	}
	...
]

Examples

  • This example shows a script that appends 100 entries to time series "HeartRates" in document "Users/john".
    The script is passed to method session.Advanced.Defer.

    const baseTime = new Date();
    
    // Prepare random values and timestamps to patch
    const values = [];
    const timeStamps = [];
    
    for (let i = 0; i < 100; i++) {
        const randomValue = 65 + Math.round(20 * Math.random());
        values.push(randomValue);
    
        // NOTE: the timestamp passed in the patch request script should be in UTC
        const timeStamp = new Date(baseTime.getTime() + 60_000 * i);
        const utcDate = new Date(timeStamp.getTime() + timeStamp.getTimezoneOffset() * 60_000);
        timeStamps.push(utcDate);
    }
    
    // Define the patch request
    // ========================
    
    const patchRequest = new PatchRequest();
    
    // Provide a JavaScript script, use the 'append' method
    // Note: "args." can be replaced with "$". E.g.: "args.tag" => "$tag"
    patchRequest.script = `
        for(var i = 0; i < args.values.length; i++)
        {
            timeseries(id(this), args.timeseries)
            .append (
                new Date(args.timeStamps[i]),
                args.values[i],
                args.tag);
        }`;
    
    // Provide values for the params used within the script
    patchRequest.values = {
        timeseries: "HeartRates",
        timeStamps: timeStamps,
        values: values,
        tag: "watches/fitbit"
    }
    
    // Define the patch command
    const patchCommand = new PatchCommandData("users/john", null, patchRequest, null)
    
    // Pass the patch command to 'defer'
    session.advanced.defer(patchCommand);
    
    // Call saveChanges for the patch request to execute on the server
    await session.saveChanges();
  • This example shows a script that deletes time series "HeartRates" for documents that match the specified query. The script is passed to the PatchByQueryOperation operation.

    const indexQuery = new IndexQuery();
    
    indexQuery.query = `
        from users as u
        where u.age < 30
        update
        {
            timeseries(u, "HeartRates").delete()
        }`;
    
    const deleteByQueryOp = new PatchByQueryOperation(indexQuery);
    
    // Execute the operation: 
    // Time series "HeartRates" will be deleted for all users with age < 30
    await documentStore.operations.send(deleteByQueryOp);