Define a collection

Description

In this tutorial you will learn how to define a collection.

This collection will be used to store devices with the following properties:

  • no: automatically generated number (display how to set auto-increment fields)

  • description: description of the device (String)

  • serial: serial of the device (String, required)

  • ip_address: ip address of the device (String, required)

  • status: state of the device (Connected/Disconnected, required)

Steps

Create a schema.ospp file and define the schema as following:

{
"schema": {
    "type": "object",
    "properties": {
        "no": { "type": "integer" },
        "description": { "type": "string" },
        "serial": { "type": "string" },
        "ip_address": { "type": "string" },
        "status": {
            "type": "string",
            "enum": [
                "Connected",
                "Disconnect"
            ]
        }
    },
"required": ["serial", "ip_address", "status"]
}

Warning

In addition to the schema definition, we define two filters based on the status property. The actual query behind the filters are defined in the schema.collections file.

"filters": [
    {
    "id": "filter_status_connected",
    "name": "Device is connected"
    },
    {
    "id": "filter_status_disconnected",
    "name": "Device is disconnected"
    }
]

root/collections/device/schema.ospp

Your file should look like this:

{
    "schema": {
        "type": "object",
        "properties": {
            "no": { "type": "integer" },
            "description": { "type": "string" },
            "serial": { "type": "string" },
            "ip_address": { "type": "string" },
            "status": {
                "type": "string",
                "enum": [
                    "Connected",
                    "Disconnect"
                ]
            }
        },
        "required": ["serial", "ip_address", "status"]
    },
    "filters": [
        {
            "id": "filter_status_connected",
            "name": "Device is connected"
        },
        {
            "id": "filter_status_disconnected",
            "name": "Device is disconnected"
        }
    ]
}

2. Define the collection based on this schema

Create a schema.collections file next to the previous file. In this file, we define :

  • the collection name

  • indexes to improve requests

  • filters (ids have to match with filters defined in previous step)

  • auto-increment fields for the no field in the schema

root/collections/device/schema.collections

Your file should look like this:

{
    "moduleId": "modules.collections.collections-1",
    "collectionName": "ip_devices",
    "indexes": [
        {
            "name": "no",
            "index": "{'no': 1}"
        },
        {
            "name": "serial",
            "index": "{'serial': 1}"
        },
        {
            "name": "ip_address",
            "index": "{'ip_address': 1}"
        },
        {
            "name": "status",
            "index": "{'status': 1}"
        }
    ],
    "filters": [
        {
            "id": "filter_status_connected",
            "query": "{status: 'Connected'}"
        },
        {
            "id": "filter_status_disconnected",
            "query": "{status: 'Disconnect'}"
        }
    ],
    "autoIncrementFields": ["no"]
}

Warning

Now you can create/update the collection with a script or from a CollectionTable/Form widget. You can follow this example to create a dashboard containing a CollectionTable.