mos. provides a built-in, SQLite-based solution for storing persistent data. You can also define data sources that automatically update database table contents.
Each file in the ./db_table directory defines a database table with optional static data or a data source.
Basic knowledge of relational databases and SQLite is recommended to make full use of database tables in mos.
Quick guide
A database table is defined in a JSON file in the ./db_table directory.
./db_table/
└── users.json
The following example users.json defines a users table with an integer id column and a text name column:
{
"table": {
"name": "users",
"fields": [
{
"name": "id",
"type": "INTEGER"
},
{
"name": "name",
"type": "TEXT"
}
],
"primaryKey": [
"id"
],
"queries": {
"byId": {
"where": "id = ?"
}
}
}
}
mos. may drop and recreate a database table when its definition file changes. This can cause data loss if the table does not use static data or a data source.
Database table files
./db_table directory
Database table files are stored in the ./db_table directory.
./db_table/
├── locations.json
└── users.json
Database table definition
A database table definition describes the table name, its columns, and optional queries.
{
"table": {
"name": "users",
"fields": [
{
"name": "id",
"type": "INTEGER"
},
{
"name": "name",
"type": "TEXT"
}
],
"primaryKey": [
"id"
],
"queries": {
"byId": {
"where": "id = ?"
}
}
}
}
The example above defines the users table with the columns id and name. The following properties are available:
name: The table name. It is used to reference the table from layers that display data or from other queries.
fields: A list of columns in the table. Each field has a name and a type.
name: The name of the column.
type: The column type. Supported values are TEXT, INTEGER, REAL, and DATETIME.
primaryKey: A list of columns used as the table's primary key. If omitted or empty, no primary key is defined.
queries: Named queries that can be referenced elsewhere in the configuration.
Queries
The queries object defines named queries that can be used elsewhere in the configuration, for example, to retrieve data for a grid layer.
Each entry in queries uses the query name as its key. You can define a query using predefined properties or provide a custom SQL statement.
Using predefined query properties
Use predefined properties to filter, sort, and limit results while mos. builds the SQL query.
For example, the following query admins returns up to five users whose isAdmin field is 1, sorted by name in ascending order:
{
"table": {
"name": "users",
"fields": [
{
"name": "name",
"type": "TEXT"
},
{
"name": "isAdmin",
"type": "INTEGER"
}
],
"queries": {
"admins": {
"where": "isAdmin = 1",
"orderBy": "name ASC",
"limit": 5
}
}
}
}
Using custom SQL queries
Use the custom property to define a SQL query directly.
The following example returns up to five administrators, sorted by name:
{
"queries": {
"admins": {
"custom": "SELECT * FROM users WHERE isAdmin = 1 ORDER BY name ASC LIMIT 5;"
}
}
}
Query parameters
Queries can use the ? placeholder syntax to accept parameters when executed.
The following example defines a query with two parameters:
{
"queries": {
"admins": {
"custom": "SELECT * FROM users WHERE isAdmin = ? ORDER BY name ASC LIMIT ?;"
}
}
}
Pass the parameter values through queryParams when referencing the query elsewhere in the configuration. Values are bound in the order in which they appear in the array.
{
"table": "users",
"query": "admins",
"queryParams": [
"1",
"10"
]
}
You can also reference a parameter more than once in a query by using numbered placeholders such as ?1 and ?2.
Custom functions
mos. provides custom SQL functions that can be used in queries.
REGEXPT
The REGEXPT function checks whether a text value matches a regular expression.
The following example checks whether the name field contains the string mos:
{
"queries": {
"mosAdmins": {
"custom": "SELECT * FROM users WHERE REGEXPT(name, '.*mos.*');"
}
}
}
DISTANCE
The DISTANCE function calculates the distance between the device's current position and coordinates stored in a table.
The following example defines a locations table with latitude and longitude fields and uses DISTANCE to calculate a distance for each result:
{
"table": {
"name": "locations",
"fields": [
{
"name": "id",
"type": "INTEGER"
},
{
"name": "name",
"type": "TEXT"
},
{
"name": "latitude",
"type": "REAL"
},
{
"name": "longitude",
"type": "REAL"
}
],
"primaryKey": [
"id"
],
"queries": {
"withDistance": {
"custom": "SELECT *, DISTANCE(latitude, longitude) AS distance FROM locations;"
}
}
}
}
The DISTANCE function requires a device that supports location tracking and the user's permission to access the device's location. Otherwise, the function returns 0.
F19COMPARE
The F19COMPARE function compares two UTF-8 strings without distinguishing between uppercase and lowercase letters. It focuses on matching equivalent letters, so different letters may match in edge cases.
For example:
{
"queries": {
"mosAdmins": {
"custom": "SELECT * FROM users WHERE F19COMPARE(name, 'pière');"
}
}
}
F19SEARCH
The F19SEARCH function searches UTF-8 text without distinguishing between uppercase and lowercase letters. It focuses on matching equivalent letters, but different letters may match in edge cases.
The first parameter specifies the search term, and the second parameter specifies the text to search.
For example:
{
"queries": {
"mosAdmins": {
"custom": "SELECT * FROM users WHERE F19SEARCH('ièr', name);"
}
}
}
Static database table data
Use the data property to define static data for a database table. After creating the table, mos. inserts the specified entries into it.
The following example inserts two entries into the users table:
{
"table": {
"name": "users",
"fields": [
{
"name": "id",
"type": "INTEGER"
},
{
"name": "name",
"type": "TEXT"
}
],
"primaryKey": [
"id"
],
"queries": {
"byId": {
"where": "id = ?"
}
}
},
"data": [
{
"id": 1,
"name": "Jane"
},
{
"id": 2,
"name": "John"
}
]
}
Database table source
A database table can use a source to update its contents automatically by periodically sending a request to a URL.
The source object defines the request and how the table is updated.
The following example defines a source for the users table. It replaces the table contents with data from the specified URL every hour:
{
"table": {
"name": "users",
"fields": [
{
"name": "id",
"type": "INTEGER"
},
{
"name": "name",
"type": "TEXT"
}
],
"primaryKey": [
"id"
],
"queries": {
"byId": {
"where": "id = ?"
}
}
},
"source": {
"type": "replace",
"request": {
"url": "https://example.mobilitysuite.com",
"method": "GET"
},
"dataQuery": "users",
"update": {
"type": "polling",
"durations": {
"standard": 3600
},
"standardDuration": "standard"
}
}
}
The source object supports the following properties:
type: Defines how source data is applied to the table. Supported values are insertOrUpdate and replace.
request: Defines the request to make.
url: The URL used for the request.
method: The HTTP method used for the request.
dataQuery: An optional string that specifies where the data to insert is located in the response JSON. It is required if the response is not a JSON array.
update: Defines how the table is updated.
type: The update type. Currently, the only available value is polling.
durations: A map of possible polling intervals. You can switch between these durations using the setTablePollingState action.
standardDuration: The key of the default duration in the durations map.
Troubleshooting
If a database table does not contain the expected data or a query does not return the expected results, check the following:
- Is the table definition file stored in the
./db_table directory?
- Are the table name, fields, and primary key defined correctly?
- Does the query reference the correct table and columns?
- If the query uses parameters, are the
queryParams specified correctly?
- If the table uses a data source, are the URL, HTTP method, and
dataQuery configured correctly?
- If the table data is not updated, is the polling configuration defined correctly?
- If data is lost after changing the table definition, is the data provided again by static data or a data source?