catalog
catalog
¶
Schema introspection and safe query builder for read-only database access.
Discovers PostgreSQL tables, views, and materialized views at runtime, then provides parameterized query building with whitelist-by-construction column/table validation. Shared by the API and DVM services.
The Catalog performs four discovery queries at startup:
information_schema.tablesfor base tables and regular views.pg_catalog.pg_matviewsfor materialized views (not in information_schema).pg_attributefor column metadata of all discovered objects (including materialized views, which are absent frominformation_schema.columns).pg_constraint+pg_attributefor primary keys, pluspg_indexfor materialized-view unique indexes.
See Also
Api: REST API service that uses the Catalog for endpoint generation. Dvm: NIP-90 DVM service that uses the Catalog for query execution.
Classes¶
CatalogError
¶
Bases: Exception
Client-safe error raised by Catalog operations.
Messages are always controlled literals or validated identifiers — never raw database error details.
The :attr:client_message attribute provides the sanitised string
intended for HTTP/Nostr responses, avoiding str(exception) which
static analysers flag as potential stack-trace exposure.
Source code in src/bigbrotr/services/common/catalog.py
ColumnSchema
dataclass
¶
Schema information for a single column.
TableSchema
dataclass
¶
TableSchema(
name: str,
columns: tuple[ColumnSchema, ...],
primary_key: tuple[str, ...],
is_view: bool,
)
Schema information for a table, view, or materialized view.
QueryResult
dataclass
¶
Result of a paginated query.
Catalog
¶
Schema introspector and safe parameterized query builder.
Usage::
catalog = Catalog()
await catalog.discover(brotr)
result = await catalog.query(brotr, "relay_stats", limit=100, offset=0)
Source code in src/bigbrotr/services/common/catalog.py
Attributes¶
Functions¶
discover
async
¶
discover(brotr: Brotr) -> None
Introspect the public schema and populate table metadata.
Queries information_schema for tables/views, pg_matviews for materialized views, pg_attribute for column info, and pg_constraint/pg_index for primary keys and unique indexes.
Source code in src/bigbrotr/services/common/catalog.py
query
async
¶
query(
brotr: Brotr,
table: str,
*,
limit: int,
offset: int,
max_page_size: int = 1000,
filters: dict[str, str] | None = None,
sort: str | None = None,
) -> QueryResult
Execute a safe paginated query against a discovered table.
Parameters:
-
brotr(Brotr) –Database interface.
-
table(str) –Table or view name (must exist in discovered schema).
-
limit(int) –Maximum rows to return.
-
offset(int) –Number of rows to skip.
-
max_page_size(int, default:1000) –Hard ceiling on limit.
-
filters(dict[str, str] | None, default:None) –Column filters as
{column: "op:value"}or{column: "value"}(defaults to=). -
sort(str | None, default:None) –Sort specification as
"column"or"column:desc".
Returns:
-
QueryResult–Paginated query result.
Raises:
-
CatalogError–If the table, column, or operator is invalid.
Source code in src/bigbrotr/services/common/catalog.py
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 | |
get_by_pk
async
¶
get_by_pk(
brotr: Brotr, table: str, pk_values: dict[str, str]
) -> dict[str, Any] | None
Fetch a single row by primary key.
Parameters:
-
brotr(Brotr) –Database interface.
-
table(str) –Table name.
-
pk_values(dict[str, str]) –Primary key column-value pairs.
Returns:
-
dict[str, Any] | None–Row as a dict, or None if not found.
Raises:
-
CatalogError–If the table has no primary key or values are missing.