Skip to content

database

This module is concerned with database related operations.

create_tables

create_tables() -> None

populates database with all tables defined in models.py

Source code in api/core/database.py
14
15
16
def create_tables() -> None:
    """populates database with all tables defined in models.py"""
    SQLModel.metadata.create_all(engine)

get_session

get_session() -> Generator[Session, None, None]

Yields a temporary session to the database.

Yields:

Type Description
Session

A temporary session to interact with the database.

Source code in api/core/database.py
19
20
21
22
23
24
25
26
27
28
29
def get_session() -> Generator[Session, None, None]:
    """
    Yields a temporary session to the database.

    Yields
    ------
    Session
        A temporary session to interact with the database.
    """
    with Session(engine) as session:
        yield session

add_user

add_user(user_create: models.UserCreate, session: Session) -> models.User

Adds a user to the database.

Parameters:

Name Type Description Default
user_create UserCreate

The data required to create a new user.

required
session Session

The session to interact with the database.

required

Returns:

Type Description
User

The user added to the database.

Source code in api/core/database.py
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
def add_user(user_create: models.UserCreate, session: Session) -> models.User:
    """
    Adds a user to the database.

    Parameters
    ----------
    user_create : models.UserCreate
        The data required to create a new user.
    session : Session
        The session to interact with the database.

    Returns
    -------
    models.User
        The user added to the database.
    """
    user: models.User = models.User.from_orm(user_create)
    session.add(user)
    session.commit()
    session.refresh(user)
    return user

get_all_users

get_all_users(session: Session) -> list[models.User]

Retrieve all users from the database.

Parameters:

Name Type Description Default
session Session

The session to interact with the database.

required

Returns:

Type Description
list[User]

A list of all users in the database.

Source code in api/core/database.py
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
def get_all_users(session: Session) -> list[models.User]:
    """
    Retrieve all users from the database.

    Parameters
    ----------
    session : Session
        The session to interact with the database.

    Returns
    -------
    list[models.User]
        A list of all users in the database.
    """

    return session.exec(select(models.User)).all()

get_user

get_user(email: str, session: Session) -> models.User | None

Retrieve one user from the database based on email match.

Parameters:

Name Type Description Default
email str

The email address of the user to retrieve.

required
session Session

The session to interact with the database.

required

Returns:

Type Description
User | None

The user with the specified email if found, otherwise None.

Source code in api/core/database.py
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
def get_user(email: str, session: Session) -> models.User | None:
    """
    Retrieve one user from the database based on email match.

    Parameters
    ----------
    email : str
        The email address of the user to retrieve.
    session : Session
        The session to interact with the database.

    Returns
    -------
    models.User | None
        The user with the specified email if found, otherwise None.
    """

    return session.exec(select(models.User).where(models.User.email == email)).first()