Schema Management
π§± defineLocalSchema(...)
β
Manually defines the schema for a local SQLite table used by Supastash.
This is helpful for:
- Explicitly controlling column types and constraints.
- Ensuring default values and modifiers (e.g.,
NOT NULL
,DEFAULT CURRENT_TIMESTAMP
) are present. - Pre-defining tables before
useSupastashData(...)
is called. - Will not replace previously created tables unless it's called with the
deletePreviousSchema
argument.
β οΈ Important Notesβ
-
useSupastashData(...)
automatically creates local tables based on the remote schema fetched via theget_table_schema
RPC. But there are a few things to keep in mind:- It does not include default values or constraints.
- It does not include modifiers like
UNIQUE
,DEFAULT
, orPRIMARY KEY
. - It only ensures the structure exists enough to read/write data.
If your local table needs strict constraints, timestamps, or default values β use defineLocalSchema(...)
manually instead.
π§ͺ Exampleβ
import { defineLocalSchema } from "supastash";
await defineLocalSchema(
"users",
{
id: "TEXT PRIMARY KEY",
full_name: "TEXT NOT NULL",
email: "TEXT UNIQUE NOT NULL",
created_by: "TEXT",
},
true
); // Pass `true` if you want to force re-creation
π§ Parametersβ
Name | Type | Required | Description |
---|---|---|---|
tableName | string | β | The name of the table to create. |
schema | Record<string, ColumnDefinition> | β | The column definitions (see below). |
deletePreviousSchema | boolean (default: false ) | β | If true , drops any existing table and Supastash sync state for that table. |
β οΈ Avoid using
deletePreviousSchema = true
in production. This option wipes all local data for the table β only use it when resetting development state or during controlled migrations.
π§± Required Columnsβ
Regardless of what you pass in schema
, Supastash will automatically include these columns:
{
created_at: "TEXT NOT NULL",
updated_at: "TEXT NOT NULL",
synced_at: "TEXT DEFAULT NULL",
deleted_at: "TEXT DEFAULT NULL",
}
Your schema must include an id
column (UUID format as TEXT
). If omitted, the function will throw an error.
π‘ Column Type Supportβ
Columns are defined using string templates based on SQL types and modifiers. Examples:
{
id: "TEXT PRIMARY KEY",
quantity: "INTEGER NOT NULL",
price: "REAL",
active: "INTEGER DEFAULT 1",
created_at: "TEXT DEFAULT CURRENT_TIMESTAMP"
}
You can combine multiple modifiers like so:
"name": "TEXT NOT NULL UNIQUE"
π§Ό Resetting a Tableβ
To reset the schema (e.g., in development):
await defineLocalSchema("users", schema, true);
This drops the table, clears sync metadata, and re-creates the table with the new schema.
π Relatedβ
- β
useSupastashData(...)
: Automatically creates missing tables, but with no constraints or default values. - β Does not apply default constraints like
NOT NULL
,UNIQUE
, orDEFAULT
β usedefineLocalSchema(...)
for those.
π getSupastashDb()
β
π Descriptionβ
Returns the active SQLite database used by Supastash for local reads, writes, and sync tracking.
The connection is created only once and reused β based on the adapter you set in your config.
π§ How It Worksβ
-
Retrieves your Supastash config using
getSupastashConfig()
. -
Checks for presence of both:
sqliteClient
: The raw SQLite client you've passed during setup.sqliteClientType
: A string that tells Supastash which adapter to use (expo
,rn-nitro
,rn-storage
).
-
If the database instance hasn't been initialized yet:
- Calls that adapterβs
openDatabaseAsync(...)
method with the configured DB name and client.
- Calls that adapterβs
-
Caches the result in a
db
variable so future calls donβt re-open the database.
β Supported Client Typesβ
Client Type | Adapter Used | Compatible With |
---|---|---|
"expo" | SQLiteAdapterExpo | expo-sqlite |
"rn-storage" | SQLiteAdapterStorage | react-native-sqlite-storage |
"rn-nitro" | SQLiteAdapterNitro | react-native-quick-sqlite |
π§ API Methodsβ
Method | Description | Returns |
---|---|---|
runAsync(sql, params?) | Executes a single statement (e.g., INSERT , UPDATE , DELETE ) | Promise<void> |
getAllAsync(sql, params?) | Fetches all rows from a SELECT query | Promise<any[]> |
getFirstAsync(sql, params?) | Fetches first row only (or null if none) from a SELECT query | Promise<any | null> |
execAsync(statements) | Executes multiple SQL statements separated by ; (used in schema creation) | Promise<void> |
Follows the same call patterns as expo-sqlite
, making it familiar and easy to use.
π¦ Example Usageβ
This function is used internally by other Supastash functions (e.g., defineLocalSchema
, query runners, sync processors) but can also be called manually if you need raw access to the database:
const db = await getSupastashDb();
const users = await db.getAllAsync("SELECT * FROM users");
β οΈ Notesβ
- If
configureSupastash(...)
hasn't been called before this, the config will be empty and this function will throw. - This method assumes the client and clientType are correctly configured; if not, an error will be thrown.
- This is designed to be adapter-agnostic, so it supports multiple SQLite engines while exposing a unified interface.
π getAllTables(): Promise<string[] | null>
β
Returns a list of all user-defined tables in the local SQLite database, excluding internal tables used by Supastash.
β Exampleβ
const tables = await getAllTables();
console.log(tables);
// ["users", "orders", "transactions"]
π Use Casesβ
- Listing all tables available for syncing or inspection.
- Dynamic tooling (e.g., admin panels, migration utilities).
- Debugging which user tables are present in the local DB.
π Returnβ
string[]
β array of table names (excluding system/internal).null
β if no user tables are found.