# Databases

The 9 tools for databases. Before an agent first makes one, it reads guide topic databases.

## create_database

Make a database in a space: Airtable-style tables of records with typed fields (text, numbers, choices, dates, people, attachments, links between tables). People see each table as a grid, add and edit records in it, and open a record's fields in a side panel. Give its tables with their fields and records; link fields can point at any table given; a record given as { values, page } also gets its page of notes (Markdown). Read guide topic "databases" first; check it with render_database.

Parameters:
- `space_id` (string, required)
- `title` (string, required)
- `tables` (object[], required)

## read_database

A database as it is now, people's edits included: each table with its id, fields (id, name, type, choices, format, the table a link points at) and how many records. list_records gives the records.

Parameters:
- `item_id` (string, required)

## list_records

Records of one table, each with its id (what update_records, delete_records and comment take) and its values by field name as people read them (a person by name, links by the names of the records); pages: true adds each record's page (its notes, Markdown). Filter, search (fields and pages), sort and page through them; total says how many match.

Parameters:
- `item_id` (string, required)
- `table` (string, required): Its name or id.
- `filters` (object[]): All must hold. gt/lt compare numbers, and dates as YYYY-MM-DD.
- `search` (string): Words in any field.
- `sort` (object[])
- `fields` (string[]): Only these fields (default all).
- `pages` (boolean): Include each record's page (Markdown). Without it, has_page says which have one.
- `limit` (integer): Default 100.
- `offset` (integer)

## add_records

Add records to a table (up to 1000 a call), at the end or after a record. Returns their ids. A select value that is not a choice yet is added as one.

Parameters:
- `item_id` (string, required)
- `table` (string, required): Its name or id.
- `records` (object[], required)
- `after` (string): A record id: the new records go after it (default: at the end).

## update_records

Change records of a table by id: only the fields given change; null or "" clears one. page replaces the record's page (Markdown; "" clears it).

Parameters:
- `item_id` (string, required)
- `table` (string, required)
- `records` (object[], required)

## delete_records

Delete records of a table by id; links to them from other tables are taken off.

Parameters:
- `item_id` (string, required)
- `table` (string, required)
- `ids` (string[], required)

## update_database

Change a database's shape. Any of: title; add_tables (with fields and records); rename_tables; remove_tables (with their records; links into them become text); add_fields; change_fields (by table and field name or id: a new name, type — values are converted through their text, what does not read is cleared — choices, format, link); remove_fields; move_fields (to a 0-based column; 0 makes it the first field, which names records). Applied in that order, all or none.

Parameters:
- `item_id` (string, required)
- `title` (string)
- `add_tables` (object[])
- `rename_tables` (object[])
- `remove_tables` (string[])
- `add_fields` (object[])
- `change_fields` (object[])
- `remove_fields` (object[])
- `move_fields` (object[])

## import_records

Fill a table from a range of one of the space's spreadsheets: the range's first row names the fields, each row under it is a record. A header that matches no field adds one (number when every value is a number, else text). Values are read as the sheet shows them; cleaned like add_records.

Parameters:
- `item_id` (string, required)
- `table` (string, required): The table to fill (name or id).
- `sheet` (string, required): A spreadsheet in this space, by its item id.
- `range` (string, required): "Tab!A1:F200" (no tab = the first): a header row and rows under it.

## render_database

See a database as people do: a screenshot of one table's grid (the first by default), with a record's side panel open when record is given, and errors the page hit.

Parameters:
- `item_id` (string, required)
- `table` (string): Its name or id; default the first.
- `record` (string): A record id: shows its side panel.
