Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trying to use db_engine.configure_database([User]) #392

Open
StandinKP opened this issue Dec 19, 2023 · 1 comment
Open

Trying to use db_engine.configure_database([User]) #392

StandinKP opened this issue Dec 19, 2023 · 1 comment

Comments

@StandinKP
Copy link

Trying to use db_engine.configure_database([User]) but as we need to await it, it cannot be directly run without any async function declared and called. Do you need to create a factory for this?

This is my code:

from config.settings import DB
from logger import logging
from odmantic import AIOEngine
from motor.motor_asyncio import AsyncIOMotorClient
from odmantic.session import AIOSession

from models.user import User

db_client = AsyncIOMotorClient(
    f"mongodb://{quote_plus(DB.user)}:{quote_plus(DB.pass_)}@{DB.host}:{DB.port}"
    if (DB.user and DB.pass_)
    else f"mongodb://{DB.host}:{DB.port}"
)
db_engine = AIOEngine(database=DB.name, client=db_client)

await db_engine.configure_database([User])
@StandinKP
Copy link
Author

Managed to use it with below code:

loop = asyncio.get_event_loop()
if loop and loop.is_running():
    tsk = loop.create_task(db_engine.configure_database([User]))
    tsk.add_done_callback(
        lambda t: print(f"Task done with result={t.result()}  << return val of main()")
    )

But is this the correct approach or any other way is supposed to be used?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant