utils.py 396 B

123456789101112131415
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. from sqlalchemy import select
  4. from sqlalchemy.ext.asyncio import AsyncSession
  5. async def get_user_by_username(db: AsyncSession, username: str, user_model):
  6. stmt = select(user_model).where(user_model.username == username).limit(1)
  7. try:
  8. result = await db.execute(stmt)
  9. finally:
  10. await db.close()
  11. return result.scalar()