Zum Inhalt springen
Geprüft

App-Daten abfragen und schreiben

Lies Zeilen aus einer Studio-Tabelle und füge eine hinzu, von außerhalb der Runtime, mit denselben zwei Methoden, die eine laufende App selbst aufruft.

dbs_read und dbs_create sind dieselben zwei Aufrufe, die ein javascript- oder api-Programm zur Laufzeit über xu.dbs_read(...) und xu.dbs_create(...) macht, direkt mit einem Konto-API-Schlüssel erreichbar, sodass dein eigenes Backend die Studio-Tabelle eines Projekts lesen und beschreiben kann, ohne die Runtime zu durchlaufen. Die projektinterne Seite derselben Methoden findest du unter JavaScript-Framework, den Rest von data_module unter Entwicklungsplattform.

Eine table_id finden

table_id ist die eigene _id des Tabellenknotens, nie sein Anzeigename. get_progs mit menu_type: "table" listet jede Tabelle eines Projekts als {_id, properties: {menuName}}-Paare auf, der günstigste Weg, einen lesbaren Namen der ID zuzuordnen, die diese beiden Methoden brauchen.

Methoden

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.

Schritt für Schritt

Eine Seite mit Zeilen lesen

from/to ist ein Zeilenbereich (from ist der Offset, to - from ist die Seitengröße), und beide sind optional. Lässt du sie weg, bekommst du die Standardseite der Tabelle. Jeder andere Filter (row_id, query, sortModel, archived) ist ebenfalls optional.

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. Eine Zeile hinzufügen

table_data ist ein einfaches Objekt, geschlüsselt nach den eigenen Feld-IDs der Tabelle, den Spalten-IDs, die Studio beim Erstellen der Tabelle vergeben hat, nicht nach generischen Namen. Lies zuerst mit Schritt 1 eine Zeile zurück, wenn du nicht sicher bist, wie die Felder einer bestimmten Tabelle heißen.

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'])

Die Antwort

dbs_read verpackt das Ergebnis des Tabellen-Plugins und ergänzt die eigene Gesamtanzahl der Tabelle:

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
  }
}

Jede Zeile ist { id, value }: id ist die eigene Dokument-ID der Zeile (was row_id bei einem späteren Aufruf oder dbs_update/dbs_delete erwartet), value sind die Felddaten, geschlüsselt wie table_data gesendet wurde. total_rows zählt diese Seite, total_table_count die ganze Tabelle (nur aktive Zeilen, außer archived: true wurde gesendet), die Zahl, die ein Steuerelement "Seite 1 von N" lesen würde.

dbs_create antwortet mit dem Schreibvorgang, nicht mit der Zeile:

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

Lies die neue Zeile mit dbs_read zurück (row_id auf data.id gesetzt), wenn du ihren gespeicherten value brauchst, dieser Aufruf gibt ihn nicht zurück. Sendest du für table_data ein Array statt eines Objekts, wird jedes Element in einem Aufruf als eigene Zeile eingefügt, id/rev kommen dann pro Zeile zurück statt einmal.

Fehler

Weiter