> ## Documentation Index
> Fetch the complete documentation index at: https://docs.hyperwisor.com/llms.txt
> Use this file to discover all available pages before exploring further.

# db

> Query and insert product database records from a script.

The `db` global reads and writes records in the product's database. Both methods
are asynchronous (they return Promises).

## Query

```js theme={null}
const rows = await db.query(tableName, filters?, options?);
```

<ParamField path="tableName" type="string" required>
  The table to read from.
</ParamField>

<ParamField path="filters" type="object">
  Key/value equality filters, e.g. `{ status: 'active' }`.
</ParamField>

<ParamField path="options" type="object">
  `{ limit?, offset?, orderBy?, ascending? }`.
</ParamField>

```js theme={null}
const recent = await db.query('readings',
  { device: 'pump-1' },
  { limit: 20, orderBy: 'created_at', ascending: false }
);
```

## Insert

```js theme={null}
const created = await db.insert(tableName, data);
```

```js theme={null}
await db.insert('readings', { device: 'pump-1', temperature: 27.4 });
```

## Example — render the latest reading

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

<Card title="Next: location" icon="location-dot" href="/script/location-api">
  Read device geolocation.
</Card>
