Define a collection owner

🟡 Intermediate

collection script

This example shows how to link a collection to a value via a collection owner, so that each update automatically pushes the changed document into the value and triggers a callback script.

  • Create a TEXT value to receive the content of an updated collection document.

  • Define an owner.collections with schemaId and filterId to watch specific documents.

  • Write a JS script that reacts when the linked value is updated.

  • Connect the value and the script via a callback.ospp.

git checkout origin/osp-scripts-configuration .
git checkout origin/osp-collections-configuration .
git checkout origin/example-collections-update-value-with-owner .

Steps

1. Define a new value

Create a value.ospp file with a TEXT type:

root/collections/owner/value.ospp
{
    "name": "Device",
    "description": "Last updated ip device",
    "type": "TEXT"
}

2. Define a collection owner to update the value

A collection owner is linked to a collection by specifying :

  • schemaId: the id of the collection.

  • filterId: the id of the filter. If the updated document matches this filter, the value will be updated.

root/collections/owner/owner.collections
{
    "schemaId": "root.collections.device",
    "filterId": "filter_status_connected",
    "type": "ON_COLLECTION"
}

3. Write a JS script to call with the content of the updated value

In this example, we use a script to do something when the value is updated. For that, we need to have an output.scripts:

root/collections/on_update/output.scripts
{
    "scriptId": "root.collections.on_update"
}

This output is linked to this detached.scripts:

root/collections/on_update/detached.scripts
{
    "moduleId": "modules.scripts.scripts-1",
    "sourceFile": "root/collections/on_update/device_updated.js",
    "accessedValues": []
}

Which is linked to this script:

root/collections/on_update/device_updated.js
function main() {
    let device = JSON.parse(trigger.content);
    log.info("Updated device contains : " + JSON.stringify(device) + "");

    //do something
}

main();

4. Define a callback for the value

The last step is to define a callback to link the value and the script output.

root/collections/owner/callback.ospp
{
    "linkedOutputs": [
        {
            "outputId": "root.collections.on_update"
        }
    ]
}