Skip to content

SQLAlchemy repositories

Repositories are used to interact with the database and perform CRUD operations. All SQLAlchemy repositories are located in the fastauth.contrib.sqlalchemy.repositories module. All repository have a AsyncSession in init method, to run db operations.

User repository

The user repository is used to interact with the User model in the database. To create user repository inherit SQLAlchemyUserRepository class and set the user_model attribute. Also set User and ID type in Generic to get type hinting.

from fastauth.contrib.sqlalchemy import repositories
import uuid


class UserRepository(repositories.SQLAlchemyUserRepository[User, uuid.UUID]):
    user_model = User

RBAC repository

The RBAC repository is used to interact with the Role and Permission models.

from fastauth.contrib.sqlalchemy import repositories
import uuid



    role_model = Role
    permission_model = Permission

OAuth repository

The OAuth repository is used to interact with the OAuth and User models.

from fastauth.contrib.sqlalchemy import repositories
import uuid



    user_model = User
    oauth_model = OAuthAccount

Full example

from fastauth.contrib.sqlalchemy import repositories
import uuid


class UserRepository(repositories.SQLAlchemyUserRepository[User, uuid.UUID]):
    user_model = User


class RBACRepository(repositories.SQLAlchemyRBACRepository[Role, Permission]):
    role_model = Role
    permission_model = Permission


class OAuthRepository(repositories.SQLAlchemyOAuthRepository[User, OAuthAccount]):
    user_model = User
    oauth_model = OAuthAccount