Database
Aerostack provides a managed D1 (SQLite) database, accessible from your server-side SDK. Every project gets its own isolated database with full SQL support, schema migrations, and intelligent connection routing.
Quick start
import { sdk } from '@aerostack/sdk'
// Single row query
const user = await sdk.db.queryOne(
'SELECT * FROM users WHERE id = ?',
[userId]
)
// Multiple rows
const posts = await sdk.db.query(
'SELECT * FROM posts WHERE user_id = ? ORDER BY created_at DESC',
[userId]
)
// Insert
await sdk.db.query(
'INSERT INTO posts (id, user_id, title, body) VALUES (?, ?, ?, ?)',
[newId, userId, title, body]
)Query methods
| Method | Returns | Use when |
|---|---|---|
sdk.db.query(sql, params) | { results: T[] } | SELECT, INSERT, UPDATE, DELETE |
sdk.db.queryOne(sql, params) | T | null | Fetch a single row |
sdk.db.exec(sql) | void | DDL statements (CREATE TABLE, etc.) |
sdk.db.batch(statements) | results[] | Multiple statements in one round-trip |
Batch operations
Run multiple statements atomically:
const results = await sdk.db.batch([
{ sql: 'INSERT INTO orders (id, user_id) VALUES (?, ?)', params: [orderId, userId] },
{ sql: 'UPDATE inventory SET stock = stock - 1 WHERE product_id = ?', params: [productId] },
{ sql: 'INSERT INTO audit_log (action, user_id) VALUES (?, ?)', params: ['order_created', userId] },
])Batch operations run in a transaction — if any statement fails, all are rolled back.
TypeScript types
const user = await sdk.db.queryOne<{
id: string
email: string
name: string
created_at: string
}>('SELECT * FROM users WHERE id = ?', [userId])
// user is typed: { id: string, email: string, name: string, created_at: string } | nullSchema introspection
// List all tables in your database
const tables = await sdk.db.query(
"SELECT name FROM sqlite_master WHERE type='table'"
)
// Describe a table's columns
const columns = await sdk.db.query(
'PRAGMA table_info(?)',
['users']
)Next steps
- Querying — full query API with examples
- Schema & Migrations — managing your database schema
- Batch Operations — transactions and bulk writes