tux.database.controllers.guild
¶
Classes:
Name | Description |
---|---|
GuildController | Controller for managing guild records. |
Classes¶
GuildController()
¶
Bases: BaseController[Guild]
Controller for managing guild records.
This controller provides methods for managing guild records in the database. It inherits common CRUD operations from BaseController.
Initialize the GuildController with the guild table.
Methods:
Name | Description |
---|---|
get_guild_by_id | Get a guild by its ID. |
get_or_create_guild | Get an existing guild or create it if it doesn't exist. |
insert_guild_by_id | Insert a new guild. |
delete_guild_by_id | Delete a guild by its ID. |
find_one | Return the first row that matches where or None. |
get_all_guilds | Get all guilds. |
find_many | Return a list of rows matching where (or all rows). |
execute_transaction | Execute callback inside a database session / transaction block. |
safe_get_attr | Return |
connect_or_create_relation | Return a dict with a single key that can be merged into data dicts. |
Source code in tux/database/controllers/guild.py
Functions¶
get_guild_by_id(guild_id: int) -> Guild | None
async
¶
Get a guild by its ID.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
guild_id | int | The ID of the guild to get | required |
Returns:
Type | Description |
---|---|
Guild | None | The guild if found, None otherwise |
Source code in tux/database/controllers/guild.py
get_or_create_guild(guild_id: int) -> Guild
async
¶
Get an existing guild or create it if it doesn't exist.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
guild_id | int | The ID of the guild to get or create | required |
Returns:
Type | Description |
---|---|
Guild | The existing or newly created guild |
Source code in tux/database/controllers/guild.py
async def get_or_create_guild(self, guild_id: int) -> Guild:
"""Get an existing guild or create it if it doesn't exist.
Parameters
----------
guild_id : int
The ID of the guild to get or create
Returns
-------
Guild
The existing or newly created guild
"""
return await self.upsert(
where={"guild_id": guild_id},
create={"guild_id": guild_id},
update={},
)
_execute_query(op: Callable[[AsyncSession], Any], span_desc: str) -> Any
async
¶
Run op inside a managed session & sentry span (if enabled).
Source code in tux/database/controllers/guild.py
Guild
The existing or newly created guild
"""
return await self.upsert(
where={"guild_id": guild_id},
create={"guild_id": guild_id},
update={},
)
async def insert_guild_by_id(self, guild_id: int) -> Guild:
"""Insert a new guild.
Parameters
----------
guild_id : int
The ID of the guild to insert
Returns
-------
insert_guild_by_id(guild_id: int) -> Guild
async
¶
Insert a new guild.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
guild_id | int | The ID of the guild to insert | required |
Returns:
Type | Description |
---|---|
Guild | The created guild |
Source code in tux/database/controllers/guild.py
delete_guild_by_id(guild_id: int) -> None
async
¶
Delete a guild by its ID.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
guild_id | int | The ID of the guild to delete | required |
find_one(*, where: dict[str, Any], include: dict[str, bool] | None = None, **__: Any) -> ModelT | None
async
¶
Return the first row that matches where or None.
Source code in tux/database/controllers/guild.py
get_all_guilds() -> list[Guild]
async
¶
find_many(*, where: dict[str, Any] | None = None, include: dict[str, bool] | None = None, order: dict[str, str] | None = None, take: int | None = None, skip: int | None = None) -> list[ModelT]
async
¶
Return a list of rows matching where (or all rows).
execute_transaction(callback: Callable[[], Any]) -> Any
async
¶
Execute callback inside a database session / transaction block.
safe_get_attr(obj: Any, attr: str, default: Any = None) -> Any
staticmethod
¶
Return getattr(obj, attr, default)
- keeps old helper available.
connect_or_create_relation(id_field: str, model_id: Any, *_: Any, **__: Any) -> dict[str, Any]
staticmethod
¶
Return a dict with a single key that can be merged into data dicts.
The calling code does something like::
data = {"guild": connect_or_create_relation("guild_id", guild_id)}
We map that pattern to a very small helper that collapses to {"guild_id": guild_id}
.