Overview
The database interface is designed to simplify common database operations in a room-based system. Each method in database client corresponds to a particular request sent to the server, handling tasks like table creation, updates, deletions, and searches. The client leveragesJsonResponse
objects and typed parameters to streamline the process.
API Methods
Below is a summary of the primary methods. Each method is asynchronous, so you shouldawait
the call.
1. 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.
2. 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
mode
parameter.- 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.
3. 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.
4. 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.
5. 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.
6. 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.
7. 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.
8. Update
- Description: Updates existing records in a table.
- Parameters:
- table: Name of the table to update.
- where: A SQL
WHERE
clause 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.
9. Delete
- Description: Deletes records from a table that match a specified condition.
- Parameters:
- table: The target table.
- where: A SQL
WHERE
clause for filtering which records to delete.
- Returns: A promise that resolves once the records are deleted.
10. Merge
- Description: Performs an upsert (update/insert) by merging incoming records into an existing table. Records matching the
on
column 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.
11. 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: Either an SQL
WHERE
clause string or an object representing key-value equals conditions. - limit: Maximum number of matching records to return.
- select: An array of column names to be returned.
- Returns: An array of matching records.
12. 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.
13. 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.
14. 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.
15. 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.
16. List Indexes
- Description: Lists the indexes currently defined on a table.
- Parameters:
- table: The name of the table for which to list indexes.
- Returns: A record object containing index information.
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.