Work with tables in agent functions
This page covers using your agent's database tables from the code of an agent function: searching, inserting and updating rows with tables from "@yellow/bot". You create the tables and their columns on the Database page.
import { tables } from "@yellow/bot";
The examples use an orders table with the columns email, status, order_id and total. In the editor, pick tables and columns from the suggestions instead of typing them:
- Type
"right aftertables.search(,tables.insert(ortables.update(to choose the table. - Type
"@to choose a column for a filter key, a row you insert, or thesetof an update. The column appears as a chip such as orders.email. Behind it the code holds a$bot_table_column_…reference; see What the editor stores. - Type
"aftercolumn:in a list-style filter to choose a column.
See Get suggestions.
tables.search
Finds rows that match a filter.
tables.search(table, filter?, options?) // → rows
| Parameter | Description |
|---|---|
table | The table to search. Required. |
filter | Which rows to return. Leave it out to return every row, up to the limit. |
options.sort | Sort by one column: 1 or "asc" ascending, -1 or "desc" descending |
options.limit | Number of rows to return. Default 10, maximum 100. A larger number returns 100 rows, without an error. |
options.skip | Number of rows to skip, for paging. offset works the same way. |
options.columns | Return only these columns |
Each row comes back keyed by column name, plus id, created_at and updated_at:
created_at: "2026-10-14T12:04:09.157Z", updated_at: "2026-10-14T12:04:09.157Z" }
A JSON column comes back as text. Parse it before you read its fields:
const details = typeof row.details === "string" ? JSON.parse(row.details) : row.details;
Match a value
const rows = await tables.search("orders", {
"orders.email": email,
});
Match several conditions
All conditions in a filter must match. To match any one of them, use $or.
// Open orders for this customer
await tables.search("orders", {
"orders.email": email,
"orders.status": "open",
});
// Orders that are open or on hold
await tables.search("orders", {
$or: [
{ "orders.status": "open" },
{ "orders.status": "on_hold" },
],
});
$or applies to the whole filter. It can't be combined with other conditions next to it, and $or or $and can't be nested. Put every condition inside the $or:
// Every order for this customer, plus any order on hold
await tables.search("orders", {
$or: [
{ "orders.email": email },
{ "orders.status": "on_hold" },
],
});
$and is also accepted, and means the same as listing the conditions side by side.
Other comparisons
| Operator | Matches when | Example |
|---|---|---|
$eq | The value is equal. The same as writing the value directly. | { $eq: "open" } |
$between | The value is in a range, inclusive. Also how you write greater than or less than. | { $between: [100, 500] } |
$contains | The text contains a value | { $contains: "pune" } |
$ncontains | The text doesn't contain a value | { $ncontains: "test" } |
$ci | Add to ignore upper and lower case. Works only on columns set as case-insensitive. | { $eq: "[email protected]", $ci: true } |
const bigOrders = await tables.search("orders", {
"orders.total": { $between: [100, 1000] },
});
// More than 500: use a high upper bound
const over500 = await tables.search("orders", {
"orders.total": { $between: [500.01, 1000000000] },
});
// Customers in Pune, any case, except test accounts
const pune = await tables.search("customers", {
"customers.city": { $contains: "pune", $ci: true },
"customers.email": { $ncontains: "test" },
});
A value in a filter can't be null, undefined or "". To find rows by a value you don't have yet, check the input first: if (!email) { return { found: false }; }.
Sort, limit and choose columns
const latest = await tables.search(
"orders",
{ "orders.email": email },
{
sort: { created_at: -1 },
limit: 5,
columns: ["orders.order_id", "orders.status"],
},
);
You can sort by id, created_at and updated_at by name. Sort by one column only.
Page through many rows
One search returns at most 100 rows, and a function can search 5 times per run, so one run can read at most 500 rows. To read more than 100, page with skip:
- Sort by
id, so pages don't overlap. - Keep
limitat 100 or less. - Move
skipforward by the number of rows the last page returned, and stop when a page comes back empty or short.
const rows = [];
for (let page = 0; page < 5; page++) {
const batch = await tables.search(
"orders",
{ "orders.status": "open" },
{ sort: { id: 1 }, limit: 100, skip: rows.length },
);
rows.push(...batch);
if (batch.length < 100) {
break;
}
}
If limit is above 100, each page still returns 100 rows. Moving skip by your limit would then jump past rows without any error, so always move it by what came back.
List-style filter
search also accepts conditions as a list, with the operations equals (or selector, which is the same), contains, not_contains and range. For range, the value is [low, high]. Both styles return the same rows.
const rows = await tables.search("orders", {
where: [
{ column: "orders.email", operation: "equals", value: email },
{ column: "orders.total", operation: "range", value: [100, 1000] },
],
sort: [{ column: "orders.total", order: "desc" }],
columns: ["orders.order_id", "orders.total"],
limit: 5,
offset: 0,
});
query is accepted in place of where.
tables.insert
Adds one row.
tables.insert(table, data) // → { success, message }
const result = await tables.insert("leads", {
"leads.name": name,
"leads.phone": phone,
});
if (!result.success) {
return { saved: false, reason: result.message };
}
return { saved: true };
A row the database rejects can either stop the function with an error or return { success: false, message }, depending on why it was rejected. Check result.success, and wrap the call in try / catch when the function should carry on.
Pass one object per call. A list is refused with "one row per call". To add several rows, call insert once for each, up to 5 per run.
const saved = [];
for (const item of items.slice(0, 5)) {
const result = await tables.insert("order_items", {
"order_items.order_id": orderId,
"order_items.sku": item.sku,
"order_items.qty": item.qty,
});
saved.push(result.success);
}
A value can't be null: leave the column out instead. "" is accepted only for text columns. id, created_at and updated_at are set for you and can't be written.
tables.update
Changes values in rows that match a filter.
tables.update(table, { set, where }) // → true
await tables.update("orders", {
set: { "orders.status": "cancelled" },
where: [{ column: "orders.order_id", operation: "equals", value: orderId }],
});
update also takes the filter in the same style as search, with the changes as a third argument:
await tables.update(
"orders",
{ "orders.order_id": orderId },
{ $set: { "orders.status": "cancelled" } },
);
- A filter is required. An update without one is refused with "update requires a filter", so a function can never rewrite a whole table.
- An update that would change more than 5 rows is refused.
updatereturnstruewhen it has run, whether or not a row matched. If you need to know that a row exists, search for it first.
When something is wrong
| Mistake | Error |
|---|---|
| A column typed by hand | column must be a Studio reference starting with '$bot_table_column_' — got 'email'. Pick it from the list in Studio to insert the token. |
| A table typed by hand | table must be a Studio reference starting with '$bot_table_' — got 'orders'. Pick it from the list in Studio to insert the token. |
| A column from another table | column '…' belongs to table '…', not '…' |
| An operator that doesn't exist | tables: unknown operator '$gt' on column '…'. Supported: $eq, $between, $contains, $ncontains |
$between with a single value instead of a list | tables: $between on '…' needs [low, high] |
| A list as a value | filter on '…' is an array — use $between for a range, or $or for a set of values |
$or next to other conditions | tables: $or cannot be combined with other conditions — put every condition inside it |
| Sorting by two columns | tables: sort takes ONE column, got 2 (…). For stable paging, sort by a unique column. |
| An option in the filter | tables: 'limit' is an option, not a column — pass it in the third argument, e.g. search(table, filter, { limit: ... }) |
null in an insert or update | '…' has no value — omit the column instead of passing null |
| An update with no filter | update requires a filter — with no conditions it would rewrite every row |
See Handle errors for how to catch them.
Related
- Global variables, for small values shared across the conversation
- Examples: order lookup, saving a lead without duplicates, cancelling an order