CSDBDatabase
CSDBDatabase wraps a parsed document and exposes table access, SQL execution, validation, and serialization.
Create Database Object
- TypeScript
new CSDBDatabase(document: CSDBDocument)
Parse Text
Parse CSDB text into a database.
- TypeScript
const db = CSDBDatabase.parse(text, { validate: true });
JSON clients select an existing server-managed database with the request envelope's
database field; they do not submit arbitrary files or server paths.
Start Table Query
Start a fluent query for a table.
- TypeScript
const query = db.table("workers");
JSON commands are complete, stateless plans, so there is no remote query-builder object to create.
Query Table
Query a table with the fluent API, SQL, or a structured JSON plan.
- TypeScript
- SQL
- JSON
const rows = db.table("workers")
.where("email", "=", "ada@example.com")
.select(["id"])
.all();
SELECT id
FROM workers
WHERE email = 'ada@example.com';
{
"database": "payroll",
"command": {
"kind": "select",
"table": "workers",
"columns": ["id"],
"joins": [],
"where": {
"type": "comparison",
"op": "=",
"left": { "type": "identifier", "name": "email" },
"right": { "type": "literal", "value": "ada@example.com" }
},
"orderBy": [],
"output": "objects"
}
}
Visit TableQuery for the full table query API.
Query SQL
Run SQL text through the database API.
- TypeScript
- JSON
const rows = db.sql(
"SELECT id, email FROM workers WHERE email = ?",
["ada@example.com"]
);
{
"database": "payroll",
"command": {
"kind": "sql",
"statement": "SELECT id, email FROM workers WHERE email = ?",
"params": ["ada@example.com"]
}
}
Execute Plan
Execute a query plan directly.
- TypeScript
- JSON
const rows = db.execute({
kind: "select",
table: "workers",
columns: "*",
joins: [],
orderBy: [],
output: "objects"
});
{
"database": "payroll",
"command": {
"kind": "select",
"table": "workers",
"columns": "*",
"joins": [],
"orderBy": [],
"output": "objects"
}
}
Create Table
Create a table from a TableSchema.
- TypeScript
- SQL
- JSON
db.createTable({
name: "tags",
columns: { label: "text" },
required: ["label"],
primary_key: { columns: ["label"] }
});
CREATE TABLE tags (label text primary key);
{
"database": "payroll",
"command": {
"kind": "create-table",
"schema": {
"name": "tags",
"columns": { "label": "text" },
"required": ["label"],
"primary_key": { "columns": ["label"] }
}
}
}
Drop Table
Drop a table by name.
- TypeScript
- SQL
- JSON
db.dropTable("tags");
DROP TABLE tags;
{
"database": "payroll",
"command": { "kind": "drop-table", "table": "tags" }
}
Validate Database
Validate metadata, schema references, rows, constraints, uniqueness, and foreign keys.
- TypeScript
- JSON
db.validate();
{
"database": "payroll",
"command": { "kind": "validate" }
}
Serialize Database
Serialize the document.
- TypeScript
- JSON
const text = db.toString({ machineIndexes: "omit" });
{
"database": "payroll",
"command": {
"kind": "serialize",
"options": { "machineIndexes": "omit" }
}
}