Skip to main content
The db global reads and writes records in the product’s database. Both methods are asynchronous (they return Promises).

Query

const rows = await db.query(tableName, filters?, options?);
tableName
string
required
The table to read from.
filters
object
Key/value equality filters, e.g. { status: 'active' }.
options
object
{ limit?, offset?, orderBy?, ascending? }.
const recent = await db.query('readings',
  { device: 'pump-1' },
  { limit: 20, orderBy: 'created_at', ascending: false }
);

Insert

const created = await db.insert(tableName, data);
await db.insert('readings', { device: 'pump-1', temperature: 27.4 });

Example — render the latest reading

const rows = await db.query('readings', {}, { limit: 1, orderBy: 'created_at', ascending: false });
if (rows.length) {
  widget.setText('lastReading', `${rows[0].temperature} °C`);
}

Next: location

Read device geolocation.