schema-management
π§± defineLocalSchema(...)β
Programmatically creates or refreshes a local SQLite table used by Supastash. It provides full control over your schema, including column definitions, constraints, and indexes β ensuring consistent structure across devices.
π§© What It Doesβ
-
Creates the table exactly as defined (columns, types, modifiers).
-
Automatically adds missing system columns required for sync and soft deletes:
{
created_at: "TEXT NOT NULL",
updated_at: "TEXT NOT NULL",
synced_at: "TEXT DEFAULT NULL",
deleted_at: "TEXT DEFAULT NULL",
} -
Validates that all columns in
__indicesexist before creating indexes. -
Automatically creates indexes for the following columns (if present):
synced_atdeleted_atcreated_atupdated_at
-
Creates any additional indexes listed under
__indices. -
Optionally drops the existing table and clears Supastash sync state when
deletePreviousSchema = true.
π« What It Doesnβt Doβ
- Does not infer default values, unique constraints, or foreign keys from Supabase metadata.
- Does not enforce foreign keys β by design β since related records may sync out of order.
- Does not create composite or partial indexes automatically (you can define these manually under
__constraints).
π‘ When To Useβ
Use defineLocalSchema(...) when you need:
- Strict, predictable table shapes (types, defaults, constraints).
- Local indexes for faster sync and lookups.
- Control over structure without relying on runtime inference.
If you donβt need any of these, useSupastashData(...) can auto-create tables β but without modifiers or indexes.
π§ͺ Exampleβ
import { defineLocalSchema } from "supastash";
await defineLocalSchema(
"users",
{
id: "TEXT PRIMARY KEY",
full_name: "TEXT NOT NULL",
email: "TEXT UNIQUE NOT NULL",
user_id: "TEXT NOT NULL",
// additional indexes beyond default sync columns
__indices: ["email", "user_id"],
// optional composite / partial constraints
// __constraints: "CREATE INDEX IF NOT EXISTS idx_users_shop_synced ON users(shop_id, synced_at)"
},
true // β οΈ drops and recreates table + clears local sync state
);
βοΈ Parametersβ
| Name | Type | Required | Description |
|---|---|---|---|
tableName | string | β | The name of the local SQLite table. |
schema | LocalSchemaDefinition | β | The column definitions and optional index/constraint metadata. |
deletePreviousSchema | boolean (default: false) | β | Drop the table and Supastash sync state before recreating. |
β οΈ Avoid using
deletePreviousSchema(...,true)in production.
It permanently deletes local data for that table.
π¦ Required Columnsβ
You must define an id column (TEXT PRIMARY KEY).
Other system columns are auto-added if missing:
{
created_at: "TEXT NOT NULL",
updated_at: "TEXT NOT NULL",
synced_at: "TEXT DEFAULT NULL",
deleted_at: "TEXT DEFAULT NULL",
}
π Indexing Behaviorβ
β Auto-created indexes (if column exists)β
synced_atβ speeds up pending-push scans.deleted_atβ optimizes soft-delete lookups.updated_atβ used for incremental sync (changed-since queries).created_atβ used for sort or history lists.
βοΈ Custom indexesβ
Specify them explicitly:
__indices: ["email", "user_id"];
Which generates:
CREATE INDEX IF NOT EXISTS idx_users_email ON users(email);
CREATE INDEX IF NOT EXISTS idx_users_user_id ON users(user_id);
All __indices must exist in the schema or defineLocalSchema(...) throws.
π§Ό Resetting a Tableβ
To force a rebuild of a table and its local sync state:
await defineLocalSchema("users", schema, true);
π Relatedβ
- β
useSupastashData(...)β auto-creates tables from Supabase metadata. - β Does not apply constraints, defaults, or indexes.
- βοΈ
defineLocalSchema(...)β use when structure and performance matter.