# Databases and tables

> Keep structured records in Lumen's own tables. Define typed columns and a key, let flows and agents add, find and update rows, and read or export them in the console.

Source: https://docs.go-lumen.ai/data/databases-and-tables

**In one sentence**: A table is Lumen's own spreadsheet: typed columns, an optional key that stops duplicates, and rows that flows add, find and update and that people read in the console.

Tables are for the records your automations produce: one row per invoice, per lead, per delivery. They are not Google Sheets, and they are not a copy of a file. If you need to edit an Excel or CSV file that already exists, see [Spreadsheets](https://docs.go-lumen.ai/data/spreadsheets.md) instead.

## Databases and tables

A **database** groups tables, the way a folder groups files. It has a name, an icon and a color. A **table** belongs to exactly one database and appears in it as a tab. In the API and in YAML a table is a [`Sheet`](https://docs.go-lumen.ai/reference/resources/sheet.md) and a database is a [`SheetBase`](https://docs.go-lumen.ai/reference/resources/sheetbase.md).

A table's definition (its title, columns and key) is a workspace resource, like a flow. Its **rows are stored in Postgres**, not in the Kubernetes cluster, so a table that gets thousands of rows a day never slows the rest of Lumen down. Deleting a table deletes its rows. A table made again under the same name starts empty.

| Limit | Default |
| --- | --- |
| Rows per table | 100,000. A full table refuses further adds. |
| Size of one row | 64 KiB of values |
| Columns per table | 100 |
| Key columns | 8 |
| Options per choice or select column | 100 |

Self-hosted installs can change the row limits in the Helm chart.

## Columns and types

Every write is checked against the column's type, so a total column never holds a word. A refused value fails the step with a sentence that names the column, for example `total: "lots" is not an amount`.

| Type | Holds | In the console |
| --- | --- | --- |
| `text` | Free text, up to 16 KiB | **Short text** |
| `number` | A decimal number. `12.5` and `12.50` are the same value. | **Number** |
| `money` | An amount and a three-letter currency. A value without a currency takes the column's `currency`, `ILS` when unset. | **Currency** |
| `date` | A calendar date, `YYYY-MM-DD` | **Date** |
| `boolean` | Yes or no | **Checkbox** |
| `choice` | One of a fixed list | (kept for existing tables) |
| `select` | One option from a list, or with `multiple: true` several. Each option has a color. | **Select**, **Multiple select** |
| `file` | A reference to a file Lumen keeps, never the bytes, and only your workspace's own files. Click the cell to download. | **File** |
| `user` | A member of the workspace | **User** |
| `formula` | Worked out from the other columns of the row when it is written. Read only. | **Formula** |

A column has a `name`, which is its stable id (lowercase letters, digits, `_` or `-`), and a `title`, which is what people see. The title can change; the name cannot, because flows refer to it. A column marked `required` makes a row without a value invalid.

Two rules to know before you design a table:

-   **Removing a column hides it.** Its values stay in the rows and come back if you add a column with the same name and type.
-   **A column's type is fixed.** The only changes are `choice` to `text` or `select`, and a single `select` to `text`. Otherwise add a new column, move the values, and remove the old one.

Related records (`link`, `lookup`, `rollup`) are shown in the console as **Coming soon** and are refused for now.

## The key

A table can name up to eight columns as its **key**. Together they identify a row: no two rows have the same key, and two values count as the same when they are the same value (`12.5` and `12.50`). Key columns are always required, and a formula, a multiple select or a file cannot be one.

A key does two things. It stops duplicates, and it lets a flow add or update by it: with `mode: upsert`, [Add a row to a table](#the-steps) sets the values on the row that has the same key, or adds the row if none does. An invoice that arrives twice updates its row instead of adding a second one.

> **Choose the key before you create the table:**
> 
> The key is set when the table is made and cannot change afterwards, because rows are indexed by it. A table with no key cannot be upserted into.

## Define a database and a table

**YAML:**

A table names its database in `baseRef` and its place among the tabs in `position`. The database must exist.

```yaml title="finance-database.yaml"
apiVersion: lumen.ai/v1alpha1
kind: SheetBase
metadata:
  name: finance
  namespace: tenant-acme
spec:
  displayName: Finance
  icon: bank
  color: blue
---
apiVersion: lumen.ai/v1alpha1
kind: Sheet
metadata:
  name: invoices
  namespace: tenant-acme
spec:
  displayName: Invoices
  baseRef: finance
  position: 0
  columns:
    - {name: supplier, title: Supplier, type: text, required: true}
    - {name: invoice_no, title: Invoice number, type: text, required: true}
    - {name: total, title: Total, type: money, currency: ILS}
    - {name: vat, title: VAT, type: formula, formula: "ROUND({total} * 0.17, 2)"}
    - {name: due, title: Due date, type: date}
    - {name: status, title: Status, type: choice, choices: [open, paid]}
    - {name: scan, title: Scan, type: file}
  key: [invoice_no]
```

Apply it with `lumen apply -f finance-database.yaml`. A new table must name a database.

**Console:**

1.  Open **Databases** in the sidebar. With one database it opens on its first table; with several it lists them as cards.
2.  Choose **New database** (or **Create your first database**), and give it a name, an icon and a color.
3.  In the database, click the round **+** after the tabs (**Add a table**). Type in the first row to start, or paste a table from Excel or Google Sheets.
4.  Add columns with the last header cell (**Add field**): pick a type, name it, and for a select list its options.

Only workspace admins see the buttons that create or change databases, tables and fields. Members can read everything.

A database that still has tables cannot be deleted: move its tables to another database or delete them first. A table that flows still write to cannot be deleted either, and the console names the flows to change first.

## Read and edit a table in the console

**Databases** in the sidebar lists your databases. Open one to see its tables as tabs.

![The Databases page with three database cards: Finance with 3 tables and 24 rows, Operations with 5 tables and 26 rows, and Marketing with 2 tables and 8 rows. Each card shows an icon, a description and when it was last updated. A dashed New database card follows.](https://docs.go-lumen.ai/img/data/databases-and-tables-01-databases.png)

*Databases lists each database with its table and row counts.*

![The Finance database open on its Invoices table. Tabs for Invoices, Suppliers and Monthly expenses sit above a Rows and Forms switch. The toolbar has a search box and icons for filter, sort, fields, views and automation, and on the right add row, delete and export. The grid shows columns Invoice no., Supplier, Issued, Total, Status, VAT included and Invoice file with three invoice rows.](https://docs.go-lumen.ai/img/data/databases-and-tables-02-table.png)

*A table: tabs for the tables, a toolbar, and typed cells. Money is formatted in its own currency, a select shows as a colored dot.*

What the toolbar offers:

| Control | What it does |
| --- | --- |
| **Search rows** | Finds text in the visible columns of the loaded rows. |
| **Filter**, **Sort** | Filter by a column and sort by one column. Both run on the server, so they cover every row. |
| **Fields** | Hide or show columns. |
| **Views** | A view is a saved filter, sort, set of hidden columns and column widths. It is shared with the workspace or personal to you. Admins save shared views; a member's changes are saved as a personal view. |
| **Automation** | What writes to this table: the flows and agents, and a **Source** column that says per row whether a run, an agent or a person made it. |
| **Add row**, **Delete rows** | Admins only. Tick rows to delete several at once. Deleting is permanent. |
| **Export CSV** | Downloads the whole table, not only the loaded rows, as a CSV file that opens in Excel. It applies the current filter, sort and visible columns. |

To edit a cell, click it, type, and press Enter to save and move down. Esc cancels. A refused value keeps the cell open and shows the reason. Pasting a table from Excel or Google Sheets fills several cells at once (over 50 rows it asks first), and Ctrl+Z undoes the changes of your visit.

**Import:** the console has no file import. Paste a table from a spreadsheet into the grid, or let a flow add the rows.

## Work with tables in a flow

Three steps work on a table, and a fourth turns rows into a file. In each, the table is named in `sheet` (and optionally `base`, which makes the step fail if the table is not in that database).

| Step | Console title | What it does | Credits |
| --- | --- | --- | --- |
| `add-row` | **Add a row to a table** | Adds the document's fields as a row. | 1 |
| `find-row` | **Look up a row in a table** | Finds a row and brings its columns into the steps after it. | 1 |
| `update-row` | **Update a row in a table** | Finds a row and changes the columns you choose. | 1 |
| [`make-csv`](https://docs.go-lumen.ai/reference/steps/make-csv.md) | **Make a CSV file** | Turns a list of rows into a CSV file. | 0.5 |

### Add a row

`mapping` says what goes in each column. A value is `"{field}"` alone, which carries the field's own value, so a total stays money and a date stays a date, or text with placeholders, which is read as the column's type. A column you leave out takes the field with the same name. To fill a `file` column, map it to `"{file}"`.

`mode: insert` (the default) always adds a row, and a step that runs again after a lost worker still adds once. `mode: upsert` updates the row with the same key or adds one, and needs a table with a key. The step adds the fields `rowId` and `rowCreated`.

### Find a row

`where` is a list of up to eight tests that must all pass. Each has a `column`, an `op` (`eq` by default, `contains`, `gt`, `gte`, `lt`, `lte`) and a `value` that may hold placeholders. The first match's columns become fields for the steps after it, with `prefix` in front of the names if you want one. The step also adds `found`, `count` and `rowId`.

`all: true` brings every match as the list field `rows`, up to `limit` (50 when unset, 10,000 at most). When more rows matched than the limit allowed, `truncated` is true, so an export is never cut short silently. If a test's value is missing, the step finds nothing and says what it lacked; it never drops the test.

### Update a row

`where` finds the oldest matching row and `set` is a mapping of the columns to change. A value that turns out empty leaves its column as it is. `ifMissing` is `fail` (the default) or `skip`. The step adds `rowId` and `updated`.

### Make a CSV file

`from` names the list field (`rows`, find-row's, when unset). `columns` picks and renames columns in order, each with a `name` and an optional `title`; unset, every key the rows have is written. `filename` may hold placeholders (`export.csv` when unset). `bom` (on by default) starts the file with a UTF-8 byte order mark so Excel reads Hebrew. A cell that starts with `=`, `+`, `-` or `@` is prefixed so a spreadsheet does not run it as a formula. A file holds at most 10,000 rows. Its output is a file, so you can save it, attach it to an email or rename it like any other.

### An example flow

This flow reads an invoice, finds the details with an AI step, and upserts them by invoice number. It writes to the table defined above.

```yaml title="data-invoice-ledger.yaml"
apiVersion: lumen.ai/v1alpha1
kind: Flow
metadata:
  name: data-invoice-ledger
  namespace: tenant-acme
spec:
  displayName: Invoice ledger
  triggers:
    - id: manual
      ref: {name: manual-run}
      next: [text]
  nodes:
    - id: text
      stepRef: {name: read-text}
      next: [details]
    - id: details
      stepRef: {name: chat-agent}
      config:
        instructions: Find these details in the invoice.
        fields: [{name: supplier}, {name: invoice_no}, {name: total, type: money}, {name: due, type: date}]
        files: {mode: ifNoText}
      next: [row]
    - id: row
      stepRef: {name: add-row}
      config:
        base: finance
        sheet: invoices
        mode: upsert
        mapping: {supplier: "{details.supplier}", invoice_no: "{details.invoice_no}", total: "{details.total}", due: "{details.due}", status: open, scan: "{file}"}
```

A second flow reads the open invoices back out as a CSV file and saves it to a folder in Files:

```yaml title="data-open-invoices-export.yaml"
apiVersion: lumen.ai/v1alpha1
kind: Flow
metadata:
  name: data-open-invoices-export
  namespace: tenant-acme
spec:
  displayName: Open invoices as CSV
  triggers:
    - id: manual
      ref: {name: manual-run}
      next: [find]
  nodes:
    - id: find
      stepRef: {name: find-row}
      config:
        sheet: invoices
        where: [{column: status, op: eq, value: open}]
        all: true
        limit: 1000
      next: [csv]
    - id: csv
      stepRef: {name: make-csv}
      config:
        from: rows
        columns:
          - {name: supplier, title: Supplier}
          - {name: invoice_no, title: Invoice number}
          - {name: total, title: Total}
        filename: "open-invoices.csv"
      next: [keep]
    - id: keep
      stepRef: {name: save-file}
      config: {destination: lumen, folder: "Exports/{flow}"}
```

In the console, the same steps are in the step picker as **Add a row to a table**, **Look up a row in a table**, **Update a row in a table** and **Make a CSV file**. The editor proposes columns from the fields a step would receive.

## Use tables from an agent

An **AI agent** step can call [`add-row`](https://docs.go-lumen.ai/reference/steps/add-row.md), [`find-row`](https://docs.go-lumen.ai/reference/steps/find-row.md) and [`update-row`](https://docs.go-lumen.ai/reference/steps/update-row.md) as tools. List them in the agent's `tools`, name the table in its instructions, and the model fills each tool's settings (`sheet`, `where`, `mapping`) as its input. Each successful call costs the step's credits on top of the agent's own. A call the table refuses is shown to the model and not charged.

```yaml title="data-supplier-assistant.yaml"
apiVersion: lumen.ai/v1alpha1
kind: Flow
metadata:
  name: data-supplier-assistant
  namespace: tenant-acme
spec:
  displayName: Supplier assistant
  triggers:
    - id: manual
      ref: {name: manual-run}
      next: [assistant]
  nodes:
    - id: assistant
      stepRef: {name: chat-agent}
      config:
        instructions: >-
          Answer questions about our invoices. Look them up in the table
          invoices. Mark an invoice paid when the person says it was paid.
        tools: [find-row, update-row]
```

## Next steps

[SpreadsheetsRead and update Excel and CSV files, and merge two sheets by a key.](https://docs.go-lumen.ai/data/spreadsheets.md)[FilesWhere files live, and how to keep, save and find them.](https://docs.go-lumen.ai/data/files.md)[Tools and MCPChoose which steps an agent may call.](https://docs.go-lumen.ai/ai/tools-and-mcp.md)[StepsHow a step's settings, inputs and outputs work.](https://docs.go-lumen.ai/build/steps.md)
