Database

~Anjani uses our own implementation of MongoDB to fit our need of asynchronous methods and annotation.
It's almost the same like Motor but some methods were built different and removed (deprecated).


Fetching The Database

~Anjani.db.get_collection()

Get collection from the database.

Parameters:

  • name (str) - Collection name to fetch.

Example

from typing import Any, Optional, MutableMapping

from anjani import command, plugin

class ExampleDB(plugin.Plugin):
    name = "Example DB"

    # recommended to initialized database inside on_load listener
    async def on_load(self) -> None:
        self.db = self.bot.db.get_collection("<COLLECTION_NAME>")

    async def get_data(self, key: str, value: Any) -> Optional[MutableMapping[str, Any]]:
        return await self.db.find_one({key: value})

    async def cmd_fetchdb(self, ctx: command.Context) -> str:
        data = await self.get_data("chat_id", ctx.chat.id)
        if not data:
            return "No data found"

        self.log.info(data)
        return "Fetched database complete"