Database¶
utils ¶
Database utilities for DuckDB table creation, schema export, and nested data extraction.
create_duckdb_connection ¶
create_duckdb_connection(*, database: str = ':memory:', read_only: bool = False) -> duckdb.DuckDBPyConnection
Creates and returns a DuckDB connection with configurable settings.
This provides a centralized way to create DuckDB connections that can be extended with additional configuration options as needed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
database
|
str
|
Database path or ":memory:" for in-memory database (default: ":memory:") |
':memory:'
|
read_only
|
bool
|
Whether to open the database in read-only mode (default: False) |
False
|
Returns:
| Type | Description |
|---|---|
DuckDBPyConnection
|
DuckDB connection object |
Example
con = create_duckdb_connection() # In-memory database con = create_duckdb_connection(database="mydata.db") # File-based database con = create_duckdb_connection(database="mydata.db", read_only=True) # Read-only
Source code in t3api_utils/db/utils.py
flatten_and_extract ¶
flatten_and_extract(*, data: List[Dict[str, Any]], extracted_tables: Dict[str, Dict[Any, Dict[str, Any]]]) -> List[Dict[str, Any]]
Flattens nested records and extracts referenced dicts and lists of dicts into separate tables.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
List[Dict[str, Any]]
|
A list of top-level dictionaries containing nested dicts and/or lists. |
required |
extracted_tables
|
Dict[str, Dict[Any, Dict[str, Any]]]
|
A dictionary to collect deduplicated records by table name. |
required |
Returns:
| Type | Description |
|---|---|
List[Dict[str, Any]]
|
A list of flattened dictionaries with references to nested entities via foreign keys. |
Source code in t3api_utils/db/utils.py
export_duckdb_schema ¶
Exports a human-readable representation of the DuckDB schema, including inferred foreign key-like relationships.
Useful for creating AI-generated queries based on the schema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
con
|
DuckDBPyConnection
|
An active DuckDB connection. |
required |
Returns:
| Type | Description |
|---|---|
str
|
A string representation of the schema and inferred relationships. |
Source code in t3api_utils/db/utils.py
export_duckdb_constraints ¶
Retrieves all schema constraints (including primary and foreign keys).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
con
|
DuckDBPyConnection
|
An active DuckDB connection. |
required |
Returns:
| Type | Description |
|---|---|
List[Tuple[Any, ...]]
|
A list of constraint tuples. |
Source code in t3api_utils/db/utils.py
create_table_from_data ¶
create_table_from_data(*, con: DuckDBPyConnection, data_dict: Union[Dict[Any, Dict[str, Any]], List[Dict[str, Any]]], name: str | None = None) -> None
Creates a DuckDB table from the provided data using PyArrow. Automatically drops and recreates the table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
con
|
DuckDBPyConnection
|
An active DuckDB connection. |
required |
data_dict
|
Union[Dict[Any, Dict[str, Any]], List[Dict[str, Any]]]
|
The table data, either as a list or deduplicated dict of rows. |
required |
name
|
str | None
|
Optional name for the table. If not provided, will be inferred from data_model. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the table name cannot be inferred or data is empty. |