Skip to content

Database

Adek edited this page Aug 14, 2021 · 5 revisions

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.


Fetching The Database

~Anjani.db.get_collection()

Get collection from the database.

Parameters:

  • name (str) - Collection name to fetch.

Example

from typing import Any, MutableMapping

from anjani import command, plugin

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

    # It's recomended to load DataBase on on_load
    # this method executed when the plugin is loaded
    async def on_load(self) -> None:
        self.db_collection = self.bot.get_collection("<COLLECTION_NAME>")

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

    async def cmd_fetchdb(self, ctx: command.Context) -> None:
        data = await self.get_data("chat_id", ctx.chat.id)
        self.log.info(data)
        return "Fetched database complete"
Clone this wiki locally