Patch Time Series Entries



Usage

  • Open a session
  • Construct a PatchCommandData instance and pass it the following:
    • The document ID that contains the time series
    • The document change vector (or null)
    • A PatchRequest instance with a JavaScript that appends or removes time series entries
  • Call session.advanced.defer and pass it the PatchCommandData command.
    Note that you can call defer multiple times prior to calling saveChanges.
  • Call session.saveChanges().
    All patch requests added via defer will be sent to the server for execution when saveChanges is called.

Patching examples

Append multiple entries:

In this example, we append 100 time series entries with random heart rate values to a document.

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();

Delete multiple entries:

In this example, we remove a range of 50 time series entries from a document.

// Define the patch request
// ========================

const patchRequest = new PatchRequest();

// Provide a JavaScript script, use the 'delete' method
// Note: "args." can be replaced with "$". E.g.: "args.to" => "$to"
patchRequest.script = `timeseries(this, args.timeseries)
         .delete(
             args.from,
             args.to 
         );`;
       
// NOTE: the 'from' & 'to' params in the patch request script should be in UTC
const utcDate = new Date(baseTime.getTime() + baseTime.getTimezoneOffset() * 60_000);

// Provide values for the params used within the script
patchRequest.values = {
    timeseries: "HeartRates",
    from: utcDate,
    to: new Date(utcDate.getTime() + 60_000 * 49)
}

// 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();

Syntax

A detailed syntax description for PatchCommandData & PatchRequest can be found in the following section:
Session API using defer syntax.