Docs menu

Database

There are two cases: a database OpenX creates for you, and a database you already run.

A database OpenX creates for you

Once your workspace has its private landing zone, Cloud -> Data Stores creates a CosmosDB account in the Azure subscription your workspace is connected to - a real Azure resource you own, not a connection to a database someone else set up. Fill in the account and click Save & Provision: that Save builds it, and the list updates as provisioning completes. Once the account is live you create databases inside it, then containers inside those; a container you add is built when someone presses Deploy. Step by step: CosmosDB Setup.

A database you already run

There is no one-click connector for an existing database (PostgreSQL, SQL Server, Azure Data Explorer, MySQL, MongoDB). Here are the two ways to get its data into your workshop.

Pull it through an API Source

If your database is reachable behind an HTTP endpoint - an internal API, a reporting endpoint, or a small serverless function that runs your query and returns JSON - point an API Source at it. OpenX polls it on a schedule and the rows flow onto your surface like any other feed.

Don't have an endpoint? Stand up a tiny read-only one: a serverless function (Azure Functions, etc.) that runs SELECT ... and returns JSON, then poll it with an API Source. Use a read-only account and keep credentials in your secret store.

Or push from code you run

A Connection is an IoT Hub device, and anything that can send a device message can feed it. So a small program you run - an Azure Function on a timer, a container that polls, a job on your own server - can read rows from your database and send them to a connection. There is no OpenX route you post database rows to; the connection's device endpoint is the way in.

  1. Add a Connection and Deploy it, then open Inspector -> Connection Info and copy Device ConnStr (primary).
  2. In your program, read the rows and send each as a message with an Azure IoT device SDK.

An Azure Data Explorer example (an example to adapt, not run against a workspace):

import { Client as KustoClient, KustoConnectionStringBuilder } from 'azure-kusto-data';
import { Client, Message } from 'azure-iot-device';
import { Mqtt } from 'azure-iot-device-mqtt';

// A managed identity, so the unattended job has no password to keep.
const kusto = new KustoClient(
  KustoConnectionStringBuilder.withAadManagedIdentities(
    'https://your-cluster.region.kusto.windows.net',
  ),
);
const device = Client.fromConnectionString(Deno.env.get('DEVICE_CONNECTION_STRING')!, Mqtt);

const results = await kusto.execute(
  'your-database',
  'YourTable | where timestamp > ago(1h) | summarize value = avg(value) by bin(timestamp, 5m)',
);

for (const row of results.primaryResults[0].toJSON().data) {
  await device.sendEvent(new Message(JSON.stringify(row)));
}

A SQL Server example is the same shape with a different read:

import sql from 'mssql';
import { Client, Message } from 'azure-iot-device';
import { Mqtt } from 'azure-iot-device-mqtt';

const pool = await sql.connect(Deno.env.get('SQL_CONNECTION_STRING')!);
const device = Client.fromConnectionString(Deno.env.get('DEVICE_CONNECTION_STRING')!, Mqtt);

const result = await pool.request().query(
  'SELECT ReadingTime, SensorID, Temperature FROM Telemetry WHERE ReadingTime > DATEADD(hour, -1, GETUTCDATE())',
);

for (const row of result.recordset) {
  await device.sendEvent(new Message(JSON.stringify(row)));
}

Other databases work the same way if they have a driver for your language (pg for PostgreSQL, mysql2 for MySQL, mongodb for MongoDB). Rows arrive as telemetry on the connection, so the connection's Live Stream shows them, and a warm query reads them like any other device data.

Use the connection string with an SDK, which makes and renews its own short-lived pass. A device token copied from Connection Info lasts seven days and is for testing only (testing tokens). Send over HTTPS instead with the connection's HTTP Endpoint if a firewall allows only web traffic: see Live Devices.

Keeping the program safe

  • Use a read-only database account. The program never needs to write to your database.
  • Keep the connection string and the database password in a secret store or environment variables, never in the code or source control.
  • Use a managed identity where the database supports one.
  • Send only what the surface needs, at the rate the data changes. Every message is a message in the hub.
  • These notes are for the program you run. A CosmosDB account created from Cloud -> Data Stores has no credential for you to store.

Where it runs is up to you: an Azure Function for event-driven work, a container for continuous polling, or a server on your own network when the database is not reachable from outside.

Building / testing

Before the integration is ready, shape representative rows with the Simulator so you can build the surface and warm query now.

Then

Route it to a Surface and crystallize a Warm Query.

On fathym.com: OpenX →

On this page