Appearance
SQL
Each TerraScale database is a SQL database in the SQLite dialect. It lives in a set of regions you choose, at least one of São Paulo (gru), the San Francisco Bay Area (sjc) and Frankfurt (fra). Every region of the set is an equal peer that commits reads and writes where you called; there is no home region. Writes run in fast mode by default (committed locally in about a millisecond and replicated to the other regions) or in strong mode (serializable across regions on two of three regions). Every response carries a TerraScale-Bookmark that gives read-your-writes in any region. See regions and replication and consistency.
Quickstart
You need a database named app and an API key with read and write access to it. The getting started guide creates both.
CLI
Install the CLI, then:
sh
ts auth login
ts sql app "CREATE TABLE customers (id INTEGER PRIMARY KEY, email TEXT UNIQUE NOT NULL, seats INTEGER NOT NULL)"
ts sql app "INSERT INTO customers (email, seats) VALUES ('team@example.com', 12)"
ts sql app "SELECT email, seats FROM customers WHERE seats >= 10"ts sql runs one statement in its own transaction and prints rows as JSON lines. With several projects, put -project <name> before sql.
psql
The API key is the password; the database name in the URL selects the database. With several projects, prefix the database name with the project and a slash ({project}/{database}). A project key reaches its own project; an organization key reaches every project of its organization, and the project in the database name is the one it acts in.
sh
read -rs PGPASSWORD && export PGPASSWORD # paste the API key, then press Enter
psql "postgresql://app@pg.terrascale.tech:5432/app?sslmode=verify-full&sslrootcert=system"sql
SELECT email, seats FROM customers WHERE seats >= 10;Any Postgres driver connects the same way. The Postgres wire also accepts the common Postgres syntax drivers and ORMs send: $1 parameters, ::type casts, SERIAL, ILIKE, RETURNING, ON CONFLICT, NOW() and the pg_catalog and information_schema views. See Postgres clients.
HTTP
Exchange the API key for an access token as in getting started, then send statements to sql/execute. Use your project ID from the dashboard or ts projects.
bash
curl -sS --fail-with-body --header @/dev/fd/3 \
-H 'Content-Type: application/json' \
-d '{"statements":[{"sql":"SELECT email, seats FROM customers WHERE seats >= ?","args":[10]}]}' \
"https://api.terrascale.tech/v1/projects/$PROJECT_ID/databases/app/sql/execute" \
3< <(printf 'Authorization: Bearer %s\n' "$TERRASCALE_ACCESS_TOKEN")json
{"results":[{"columns":[{"name":"email","decltype":"TEXT"},{"name":"seats","decltype":"INTEGER"}],"rows":[["team@example.com",12]],"rows_affected":0,"last_insert_rowid":null}]}The C#, TypeScript, Go, Python and Rust SDKs call the same endpoint; see SDKs.
Query from your application
from terrascale import TerraScale
# Server-side: reads TERRASCALE_API_KEY and TERRASCALE_PROJECT_ID.
with TerraScale() as client:
db = client.database("app")
customers = db.query(
"SELECT email, seats FROM customers WHERE seats >= ? ORDER BY seats DESC LIMIT 25",
[10],
)
for row in customers:
print(row.get_string("email"), row.get_int64("seats"))import { createTerraBaseV1Client } from "@terrascale/sdk";
// Server-side: TERRASCALE_ACCESS_TOKEN is a 60-second token exchanged from
// your dashboard API key. Keep both out of browser bundles.
const client = createTerraBaseV1Client({
baseUrl: "https://api.terrascale.tech",
project: process.env.TERRASCALE_PROJECT_ID,
token: process.env.TERRASCALE_ACCESS_TOKEN,
});
const db = client.database("app");
// One transaction: both statements commit or neither does.
await db.batch([
"CREATE TABLE IF NOT EXISTS customers (id INTEGER PRIMARY KEY, email TEXT UNIQUE NOT NULL, seats INTEGER NOT NULL)",
{ sql: "INSERT INTO customers (email, seats) VALUES (?, ?)", args: ["team@example.com", 12] },
]);
const customers = await db.query(
"SELECT email, seats FROM customers WHERE seats >= ? ORDER BY seats DESC LIMIT 25",
[10],
);import (
"context"
"os"
tb "terrascale.tech/sdk/v1"
"terrascale.tech/sdk/tsauth"
)
// Server-side: the token source exchanges your API key.
project := os.Getenv("TERRASCALE_PROJECT_ID")
tokens, err := tsauth.NewAPIKeySource(tsauth.APIKeyConfig{
Key: os.Getenv("TERRASCALE_API_KEY"), Project: project,
})
if err != nil { panic(err) }
client, err := tb.New(tb.Config{TokenSource: tokens})
if err != nil { panic(err) }
db := client.Database(project, "app")
customers, err := db.Query(context.Background(),
"SELECT email, seats FROM customers WHERE seats >= ? ORDER BY seats DESC LIMIT 25", 10)
if err != nil { panic(err) }using TerraScale.DotNet.TerraBase;
// Server-side: the client exchanges your dashboard API key for
// short-lived access tokens. Keep the key out of client apps.
using var http = new HttpClient(TerraBaseHttp.CreateHandler());
var sql = new TerraBaseSqlClient(http, new TerraBaseSqlClientOptions { ApiKey = apiKey });
TerraBaseSqlDatabase db = sql.Database(projectId, "app");
// One transaction: both statements commit or neither does.
await db.BatchAsync([
new("CREATE TABLE IF NOT EXISTS customers (id INTEGER PRIMARY KEY, email TEXT UNIQUE NOT NULL, seats INTEGER NOT NULL)"),
new("INSERT INTO customers (email, seats) VALUES (?, ?)", ["team@example.com", 12])
], cancellationToken);
var customers = await db.QueryAsync(
"SELECT email, seats FROM customers WHERE seats >= ? ORDER BY seats DESC LIMIT 25",
[10], cancellationToken);
foreach (var row in customers)
Console.WriteLine($"{row.GetString("email")}: {row.GetInt64("seats")}");use terrascale::v1::Client;
// Server-side: the client exchanges and refreshes project tokens.
let project = std::env::var("TERRASCALE_PROJECT_ID")?;
let api_key = std::env::var("TERRASCALE_API_KEY")?;
let client = Client::for_project_api_key(&project, &api_key).build()?;
let db = client.database("app").sql();
let customers = db.query(
"SELECT email, seats FROM customers WHERE seats >= ? ORDER BY seats DESC LIMIT 25",
&[10.into()],
).await?;
for row in customers {
println!("{}: {}", row.get_str("email")?, row.get_i64("seats")?);
}What SQL supports
- Tables, indexes (including partial and expression indexes), views and triggers.
- Foreign keys are always enforced,
ON DELETE CASCADEincluded. - JSON is stored as
TEXT;json_extract,->,->>andjson_eachwork on it. - Values are null, 64-bit integers, doubles, text and blobs. Over HTTP a blob is
{"base64":"…"}; over the Postgres wire it isbytea. Auuidcolumn holds the canonical lowercase hyphenated text and binds in text or binary.
Transactions
- HTTP: each request is one transaction of 1 to 256 statements, all or nothing.
BEGIN STRONG(orBEGIN ISOLATION LEVEL SERIALIZABLE) andCOMMITmay wrap the whole batch to run it in strong mode; any otherBEGIN,COMMITorROLLBACKis refused. - WebSocket:
GET /v1/projects/{project}/databases/{database}/sql/wswith subprotocolterrascale.sql.v2. Send the access token as anAuthorizationheader or, from a browser, as a first{"type":"auth","token":"…"}frame.executeframes run statements as over HTTP;begin,commitandrollbackframes hold a transaction of at most 5 seconds. Abeginframe takesconsistency(fastorstrong) andbookmark, and result frames return the newbookmark. - Postgres wire:
BEGIN,COMMIT,ROLLBACKand savepoints work;BEGIN ISOLATION LEVEL SERIALIZABLEorBEGIN STRONGopens a strong transaction. A transaction lives at most 5 seconds, then rolls back. - Without a transaction, each statement commits on its own.
- Schema changes, and writes to tables with a
UNIQUEconstraint other than the primary key, always run in strong mode. See replication and consistency.
Limits
| Limit | Value |
|---|---|
| Statement | 16 KiB |
| HTTP request body | 1 MiB |
| Result | 10,000 rows and 8 MiB |
| Rows changed per transaction | 1,024 |
| File growth per transaction | 8 MiB |
Refused statements
ATTACH, DETACH, VACUUM, ANALYZE, temporary and virtual tables, and internal table names are refused, and FULL JOIN/FULL OUTER JOIN answers 0A000 (sql_error) with full outer join is not supported; use LEFT JOIN with UNION ALL. PRAGMA is limited to table_info, table_xinfo, table_list, index_list, index_info, index_xinfo, foreign_key_list, foreign_key_check and foreign_keys. sqlite_schema is readable.
Errors
Errors use TerraScale codes and PostgreSQL SQLSTATEs across REST, WebSocket and the Postgres wire. For example, unique_violation carries 23505, foreign_key_violation carries 23503, and a retryable serialization_conflict carries 40001. A refused HTTP statement rolls back the request and returns an error with code, message, statement and sqlstate. Writes with a read-only key answer 403 forbidden_scope over HTTP and 42501 over the Postgres wire.
Reads need the sql:read scope; writes and schema changes also need sql:write. A Read and write key from the dashboard has both.
Live shapes and rules
Live shapes send a table snapshot followed by committed changes. Use React's useQuery or Svelte's createQuery to read data that changes. These framework examples require the next JavaScript SDK release. Select rows with where and columns with columns; rows enter or leave the shape as they start or stop matching. A where clause may use the table's columns, literals, operators, CASE, CAST, COLLATE and the functions abs, coalesce, ifnull, iif, nullif, length, octet_length, lower, upper, trim, ltrim, rtrim, substr, substring, instr, round, ceil, ceiling, floor, sign, typeof, min, max, likely, unlikely, likelihood, like, glob, date, time, datetime, julianday, unixepoch, strftime, json_extract, json_type, json_valid and json_array_length; anything else answers 400 unsafe_where.
jsx
import { useQuery } from "@terrascale/sdk/react";
function Todos({ db, where = "done = 0" }) {
const { data, status, error } = useQuery(db.shape("todos", {
where, columns: ["id", "title"],
}));
if (status === "error") return <p role="alert">{String(error)}</p>;
return <ul aria-busy={status === "loading"}>
{data.map(todo => <li key={String(todo.id)}>{String(todo.title)}</li>)}
</ul>;
}svelte
<script>
import { createQuery } from "@terrascale/sdk/svelte";
let { db, where = "done = 0" } = $props();
const todos = createQuery(db.shape("todos", { where, columns: ["id", "title"] }));
$effect(() => todos.setShape(db.shape("todos", { where, columns: ["id", "title"] })));
</script>
<ul aria-busy={$todos.status === "loading"}>
{#each $todos.data as todo (todo.id)}
<li>{todo.title}</li>
{/each}
</ul>data is the live row array in stable row-key order. Status is loading until the snapshot arrives, live at the current head, reconnecting while resuming a lost connection, or error with a terminal error. A server must_refetch returns to loading while replacing the snapshot. Previous rows remain visible while React switches filters or Svelte's setShape switches the store's source. A shape nobody reads for a day is retired; the next read starts it under a new handle, and a client still holding the old handle gets must_refetch.
Equivalent shapes on one client share one stream. It closes two seconds after the last consumer unmounts; a quick remount reuses it. No application refresh callback is needed. For code outside a framework, use db.shape("todos", { where: "done = 0" }).subscribe(({ rows }) => render(rows)) and call the returned subscription's close() when the view closes.
End-user tokens read live shapes and rows as the database's rules allow: a table's read rule picks the rows and its columns the columns. A filter limits the shape result; authorization comes from the rules, not the filter. Keep privileged API keys on your server. See the JavaScript SDK.
Agent databases
Create databases with a TTL, fork an existing database or a point in time, and issue a key scoped to one database with read or read-write access. The database is deleted on expiry. See agent databases.
Recovery and deletion
Point-in-time recovery restores into a new database within the source's recovery window and needs a free database slot. A rewind restores an earlier state in place: one transaction replaces every table, index, view and trigger, keeping the database's id, name and credentials and discarding later changes; see backups and restore. Database deletion destroys backup data keys, making retained encrypted backups unreadable; physical cleanup is separate. This does not apply to deleting individual SQL rows. Independent forks remain available.
Usage caps
Plans enforce a monthly write cap and SQL read fair use. Reaching the write cap stops new writes; reaching SQL read fair use stops SQL reads, while shape reads stay available. Customer-set hard dollar spending caps are coming. See pricing.