Appearance
Use Drizzle ORM
Drizzle ORM ships a sqlite-proxy driver that runs every query through two callbacks. The JavaScript SDK's @terrascale/sdk/drizzle export supplies those callbacks from a TerraBase database handle, so Drizzle's SQLite dialect runs over the SDK's SQL API. No drizzle-orm dependency is added to the SDK itself.
Install
sh
npm install drizzle-ormThe @terrascale/sdk/drizzle adapter ships in the next SDK release. Until then, install the SDK from the repository: inside a clone's sdk/js directory run npm install && npm pack, then install the tarball it produces:
sh
npm install /path/to/terrascale-sdk-<version>.tgzConnect
Export TERRASCALE_PROJECT_ID, exchange TERRASCALE_API_KEY for a short-lived access token as shown in the JavaScript getting-started guide, and export it as TERRASCALE_ACCESS_TOKEN.
js
import { drizzle } from "drizzle-orm/sqlite-proxy";
import { createTerraBaseClient } from "@terrascale/sdk";
import { drizzleProxy } from "@terrascale/sdk/drizzle";
const { query, batch } = drizzleProxy(createTerraBaseClient({
baseUrl: "https://api.terrascale.tech",
project: process.env.TERRASCALE_PROJECT_ID,
token: process.env.TERRASCALE_ACCESS_TOKEN,
}).database("app"));
const orm = drizzle(query, batch);query sends one statement per call; batch sends every statement of a Drizzle batch in one request that the server applies as a single transaction: all statements commit or none do.
Define a schema and query
js
import { integer, sqliteTable, text } from "drizzle-orm/sqlite-core";
import { eq } from "drizzle-orm";
const users = sqliteTable("users", {
id: integer("id").primaryKey(),
name: text("name").notNull(),
});
await orm.insert(users).values({ id: 1, name: "Ada" });
const all = await orm.select().from(users);
const ada = await orm.select().from(users).where(eq(users.name, "Ada"));For multi-statement writes, prefer orm.batch([...]): it reaches the server as one atomic transaction. orm.transaction(...) over the proxy driver sends its begin, statement and commit calls as separate requests, so it does not give you an atomic transaction.
Migrations
Generate SQL migration files with drizzle-kit generate, then apply them with ts migrate or the SQL API. drizzle-kit push and schema introspection are not supported: they need direct catalog access the SQL API does not provide.
What the adapter does
drizzleProxy(db) returns exactly the two callbacks drizzle-orm/sqlite-proxy expects:
query(sql, params, method): sends one statement withdb.executeand maps the result rows —allandvaluesreturn rows as arrays of column values in column order,getreturns the first row (or no row),runreturns no rows.batch(queries): sends every statement in onedb.batchrequest and maps each result the same way.
Over the Postgres wire
Drizzle's Postgres dialect also works against TerraScale through node-postgres, using the Postgres wire. Write pgTable schemas, serial ids, $1 parameters, returning() and onConflictDoUpdate() as you would for Postgres, and run drizzle-kit migrations against the same connection:
ts
import { drizzle } from "drizzle-orm/node-postgres";
import pg from "pg";
const pool = new pg.Pool({
host: "pg.terrascale.tech",
port: 5432,
database: `${process.env.TERRASCALE_PROJECT_ID}/${process.env.TERRASCALE_DATABASE_NAME}`,
user: "any",
password: process.env.TERRASCALE_API_KEY,
ssl: true,
});
export const db = drizzle(pool);Wrap writes that must not race in db.transaction(fn, { isolationLevel: "serializable" }) to run them in strong mode.