Skip to content
Verified

Query and write app data

Read rows from a Studio table, and add one, from outside the runtime, with the same two methods a running app itself calls.

dbs_read and dbs_create are the same two calls a javascript or api program makes at runtime through xu.dbs_read(...) and xu.dbs_create(...), reachable directly with an account API key so your own backend can read and write a project's Studio table without going through the runtime at all. See JavaScript Framework for the in-project side of the same methods, and Development platform for the rest of data_module.

Finding a table_id

table_id is the table node's own _id, never its display name. get_progs with menu_type: "table" lists every table in a project as {_id, properties: {menuName}} pairs, the cheapest way to map a name you can read to the id these two methods need.

Methods

POST/cpi/dbs_read
user_access

DBS Read

Reads records from an app table with optional row range, single-row lookup, sorting, filtering, and archived-record selection.

POST/cpi/dbs_create
admin

DBS Create

Creates a new record in the specified app table from the provided key-value table data.

Walkthrough

Read a page of rows

from/to is a row range (from is the offset, to - from is the page size), and both are optional, omit them and you get the table's default page. Every other filter (row_id, query, sortModel, archived) is optional too.

curl -s https://xuda.ai/cpi/dbs_read \
  -H 'content-type: application/json' \
  -d '{"api_key":"YOUR_API_KEY","app_id":"YOUR_APP_ID","table_id":"YOUR_TABLE_ID","from":0,"to":50}'
const res = await fetch('https://xuda.ai/cpi/dbs_read', {
  method: 'POST',
  headers: { 'content-type': 'application/json' },
  body: JSON.stringify({
    api_key: 'YOUR_API_KEY',
    app_id: 'YOUR_APP_ID',
    table_id: 'YOUR_TABLE_ID',
    from: 0,
    to: 50,
  }),
});
const { code, data } = await res.json();
if (code > 0) for (const row of data.rows) console.log(row.id, row.value);
import requests

res = requests.post(
    'https://xuda.ai/cpi/dbs_read',
    json={'api_key': 'YOUR_API_KEY', 'app_id': 'YOUR_APP_ID', 'table_id': 'YOUR_TABLE_ID', 'from': 0, 'to': 50},
)
body = res.json()
if body['code'] > 0:
    for row in body['data']['rows']:
        print(row['id'], row['value'])

2. Add a row

table_data is a plain object keyed by the table's own field ids, the column ids Studio assigned when the table was built, not generic names. Read a row back with step 1 first if you are not sure what a given table's fields are called.

curl -s https://xuda.ai/cpi/dbs_create \
  -H 'content-type: application/json' \
  -d '{
    "api_key": "YOUR_API_KEY",
    "app_id": "YOUR_APP_ID",
    "table_id": "YOUR_TABLE_ID",
    "table_data": { "CNT_Name": "Priya Shah", "CNT_Email": "[email protected]" }
  }'
const res = await fetch('https://xuda.ai/cpi/dbs_create', {
  method: 'POST',
  headers: { 'content-type': 'application/json' },
  body: JSON.stringify({
    api_key: 'YOUR_API_KEY',
    app_id: 'YOUR_APP_ID',
    table_id: 'YOUR_TABLE_ID',
    table_data: { CNT_Name: 'Priya Shah', CNT_Email: '[email protected]' },
  }),
});
const { code, data } = await res.json();
if (code > 0) console.log(data.id, data.rev);
import requests

res = requests.post(
    'https://xuda.ai/cpi/dbs_create',
    json={
        'api_key': 'YOUR_API_KEY',
        'app_id': 'YOUR_APP_ID',
        'table_id': 'YOUR_TABLE_ID',
        'table_data': {'CNT_Name': 'Priya Shah', 'CNT_Email': '[email protected]'},
    },
)
body = res.json()
if body['code'] > 0:
    print(body['data']['id'], body['data']['rev'])

The response

dbs_read wraps the table's plugin result and adds the table's own total count on top:

json
{
  "code": 1,
  "data": {
    "rows": [
      {
        "id": "dbs-392_tbl_a91f0e_7c2b9a11",
        "value": { "CNT_Name": "Priya Shah", "CNT_Email": "[email protected]" }
      }
    ],
    "total_rows": 1,
    "opt": { "...": "the query options this read actually ran, echoed back for reference" },
    "total_table_count": 42
  }
}

Each row is { id, value }: id is the row's own document id (what row_id on a later call, or dbs_update/dbs_delete, expects), value is the field data keyed the same way table_data was sent. total_rows counts this page; total_table_count is the whole table (active rows only, unless archived: true was sent), the number a "Page 1 of N" control would read.

dbs_create answers with the write, not the row:

json
{
  "code": 1,
  "data": {
    "ok": true,
    "id": "dbs-392_tbl_a91f0e_7c2b9a11",
    "rev": "1-9f8e7d6c5b4a3f2e1d0c"
  }
}

Read the new row back with dbs_read (row_id set to data.id) if you need its stored value, this call does not echo it. Sending an array for table_data instead of one object inserts every element as its own row in one call, id/rev come back per row in that case rather than once.

Errors

Next