CSDBDatabase
CSDBDatabase wraps a parsed document and exposes table access, SQL execution, validation, and serialization.
Create Database Object
- TypeScript
- Python
new CSDBDatabase(document: CSDBDocument)
CSDBDatabase(document: CSDBDocument)
Parse Text
Parse CSDB text into a database.
- TypeScript
- Python
const db = CSDBDatabase.parse(text, { validate: true });
db = CSDBDatabase.parse(text, validate=True)
Start Table Query
Start a fluent query for a table.
- TypeScript
- Python
const query = db.table("workers");
query = db.table("workers")
Query Table
Query a table with the fluent API or SQL.
- TypeScript
- Python
- SQL
const rows = db.table("workers")
.where("email", "=", "ada@example.com")
.select(["id"])
.all();
rows = (
db.table("workers")
.where("email", "=", "ada@example.com")
.select(["id"])
.all()
)
SELECT id
FROM workers
WHERE email = 'ada@example.com';
Visit TableQuery for the full table query API.
Query SQL
Run SQL text through the database API.
- TypeScript
- Python
const rows = db.sql(
"SELECT id, email FROM workers WHERE email = ?",
["ada@example.com"]
);
rows = db.sql(
"SELECT id, email FROM workers WHERE email = ?",
["ada@example.com"],
)
Execute Plan
Execute a query plan directly.
- TypeScript
- Python
const rows = db.execute({
kind: "select",
table: "workers",
columns: "*",
joins: [],
orderBy: [],
output: "objects"
});
rows = db.execute({
"kind": "select",
"table": "workers",
"columns": "*",
"joins": [],
"order_by": [],
"output": "objects",
})
Create Table
Create a table from a TableSchema.
- TypeScript
- Python
- SQL
db.createTable({
name: "tags",
columns: { label: "text" },
required: ["label"],
primary_key: { columns: ["label"] }
});
db.create_table({
"name": "tags",
"columns": {"label": "text"},
"required": ["label"],
"primary_key": {"columns": ["label"]},
})
CREATE TABLE tags (label text primary key);
Drop Table
Drop a table by name.
- TypeScript
- Python
- SQL
db.dropTable("tags");
db.drop_table("tags")
DROP TABLE tags;
Validate Database
Validate metadata, schema references, rows, constraints, uniqueness, and foreign keys.
- TypeScript
- Python
db.validate();
db.validate()
Serialize Database
Serialize the document.
- TypeScript
- Python
const text = db.toString({ machineIndexes: "omit" });
text = db.to_string(machine_indexes="omit")