Overview
TheDatabaseClient serves as a high-level interface for interacting with a relational-like database. It lets you manage room-scoped tables and perform structured queries. Each method in the DatabaseClient corresponds to a particular request sent to the server, handling tasks like table creation, updates, deletions, and searches. The client leverages JsonResponse objects and typed parameters to streamline the process. Use it to:
- List tables and create/drop them (with schema or from data, with create/overwrite/create_if_not_exists modes).
- Modify schemas (add/drop columns) and data (insert, update, delete, merge/upsert).
- Search with text filters, vector similarity, and indexed lookups; create indexes (scalar, vector, full-text) and drop them.
- Optimize storage and (Python) inspect/restore table versions.
API Methods
Below is a summary of the primary methods. Each method is asynchronous, so you shouldawait the call.
List tables
Description Retrieves a list of all table names currently present in the database. Returns:- A promise that resolves to an array of table name strings.
Create table with schema
- Description: Creates a new table with an optional schema and initial data. You can specify how the table should be created through the
modeparameter.- modes:
"create": Creates the table; fails if it already exists."overwrite": Drops the existing table (if any) and creates a new one."create_if_not_exists": Creates the table only if it does not already exist.
- modes:
- Parameters:
- name: The name of the new table.
- schema: An optional record defining column names and their data types.
- data: An optional array of initial records to populate the table.
- mode: The creation mode (default is
"create").
- Returns: A promise that resolves once the table is created.
Create table from data
- Description: Creates a table using only data and an optional mode.
- Parameters:
- name: The table name to create.
- data: An array of records to initialize the table with.
- mode: Table creation mode (default
"create").
- Returns: A promise that resolves once the table is created.
Drop table
- Description: Drops (deletes) a table by name, optionally ignoring if it does not exist.
- Parameters:
- name: The name of the table to drop.
- ignoreMissing: If
true, no error is thrown if the table does not exist.
- Returns: A promise that resolves once the table is dropped.
Add columns
- Description: Adds one or more columns to an existing table, specifying default value expressions.
- Parameters:
- table: Name of the target table.
- newColumns: A record mapping column names to default value expressions (SQL or literal).
- Returns: A promise that resolves once the columns are added.
Drop columns
- Description: Drops (removes) one or more columns from an existing table.
- Parameters:
- table: Name of the target table.
- columns: An array of column names to remove.
- Returns: A promise that resolves once the columns are dropped.
Insert
- Description: Inserts one or more new records into a table.
- Parameters:
- table: The name of the table to insert into.
- records: An array of objects, each containing column-value pairs.
- Returns: A promise that resolves once the records are inserted.
Update
- Description: Updates existing records in a table.
- Parameters:
- table: Name of the table to update.
- where: A SQL
WHEREclause specifying which records to update (e.g."id = 123"). - values: A record of key-value pairs for direct assignment (e.g.
{ age: 30 }). - valuesSql: A record of key-value pairs for SQL expressions (e.g.
{ age: "age + 1" }).
- Returns: A promise that resolves once the update is complete.
Delete
- Description: Deletes records from a table that match a specified condition.
- Parameters:
- table: The target table.
- where: A SQL
WHEREclause for filtering which records to delete.
- Returns: A promise that resolves once the records are deleted.
Merge (upsert)
- Description: Performs an upsert (update/insert) by merging incoming records into an existing table. Records matching the
oncolumn are updated; otherwise, new rows are inserted. - Parameters:
- table: The target table.
- on: The column name used to match existing records.
- records: The record(s) to merge/upsert.
- Returns: A promise that resolves once the operation is complete.
Search
- Description: Searches for records in a table. This can be used for plain text search, vector similarity search, or simple SQL filtering.
- Parameters:
- table: The target table name.
- text: An optional search string (if using full-text indexes).
- vector: An optional numeric array for vector-based similarity queries.
- where: SQL
WHEREclause string or an object representing key-value equals conditions. - offset: Optional offset for pagination.
- limit: Maximum number of matching records to return.
- select: An array of column names to be returned.
- Returns: An array of matching records.
Optimize
- Description: Optimizes a table (e.g., compacts its storage or rebuilds indexes if required).
- Parameters:
- table: Name of the table to optimize.
- Returns: A promise that resolves once the operation is complete.
Create vector index
- Description: Creates a vector index on a specified column for vector similarity searches.
- Parameters:
- table: The target table name.
- column: The column that holds vector data.
- Returns: A promise that resolves once the index is created.
Create scalar index
- Description: Creates a scalar index (typical database index) on a specified column.
- Parameters:
- table: The target table name.
- column: The column for which to create the scalar index.
- Returns: A promise that resolves once the scalar index is created.
Create full text search index
- Description: Creates a full-text search index on a text column, useful for text-based queries.
- Parameters:
- table: The table name.
- column: The text column to index.
- Returns: A promise that resolves once the index is created.
Drop index
- Description: Drop an index by name.
- Parameters:
- table: Table name.
- name: Index name to drop.
- Returns:
None. - Availability: Python SDK/server today; JS/TS/.NET may add a helper in a future release.
List indexes
- Description: Lists the indexes currently defined on a table.
- Parameters:
- table: The name of the table for which to list indexes.
- Returns:
- Python:
list[TableIndex](name,columns,type). - JS/TS:
JsonResponse.jsonobject with index metadata.
- Python:
Table versions (Python)
- Description: List historical versions of a table and restore/checkout a specific version.
- Methods:
list_versions(table)→ list of versions (version,timestamp,metadata).restore(table, version)→ restore table to a prior version.checkout(table, version)→ load a version as the current working copy.
- Availability: Python SDK/server today.
Conclusion
The database interface provides a convenient abstraction for managing a database within a room server environment. It covers a broad set of operations—ranging from table creation and schema management to complex operations like vector-based and full-text searches. By handling the request/response flow throughRoomClient, it keeps your database interactions modular and maintainable.