run · read

tables

storage.run({ action: "tables" })

Every table in a relational database, with its columns, their types, and which one is the key.

relational databasefreeno row counts
Using Python?

Switch any code block to Python: the choice applies to every page. Python SDK guide and resource reference.

await storage.run({ action: "tables" });
// [
//   { name: "users", columns: [
//       { name: "id",    type: "INTEGER", primaryKey: true },
//       { name: "email", type: "TEXT" },
//       { name: "created_at", type: "TEXT" } ] }
// ]
storage.run(action="tables")
# [
#   {"name": "users", "columns": [
#       {"name": "id",    "type": "INTEGER", "primaryKey": True},
#       {"name": "email", "type": "TEXT"},
#       {"name": "created_at", "type": "TEXT"}]}
# ]

No row counts, deliberately

A COUNT is charged by the row on some providers

Counting every table to show how big it is can cost more than the query you were about to run. If you need a count, ask for the one table you care about.

await storage.run({ action: "execute", sql: "SELECT COUNT(*) AS n FROM users" });
storage.run(action="execute", sql="SELECT COUNT(*) AS n FROM users")

Internal tables are left out

The provider's own bookkeeping tables are filtered, so what comes back is what you created. A table you made and cannot see means you are connected to a different database.

Writing a statement from it

const tables = await storage.run({ action: "tables" });

const users = tables.find((t) => t.name === "users");
const key = users.columns.find((c) => c.primaryKey)?.name ?? "rowid";
const cols = users.columns.map((c) => c.name).join(", ");

await storage.run({ action: "execute",
  sql: `SELECT ${cols} FROM users ORDER BY ${key} DESC LIMIT 20` });
tables = storage.run(action="tables")

users = next(t for t in tables if t["name"] == "users")
key = next((c["name"] for c in users["columns"] if c.get("primaryKey")), "rowid")
cols = ", ".join(c["name"] for c in users["columns"])

storage.run(action="execute", sql=f"SELECT {cols} FROM users ORDER BY {key} DESC LIMIT 20")

Knowing the key matters beyond ordering: a table without one cannot be updated row by row with any confidence, since there is nothing that names a single row.