Skip to content

BaseSBModel

BaseSBModel

Bases: BaseModel, ABC

Abstract base model for Supabase tables, integrating with Pydantic.

This class provides a foundation for creating Pydantic models that map to tables in a Supabase database.

Subclasses should define their table structure using Pydantic's field definition syntax. They can override methods such as db_client() to customize the database client used for interactions.

Classes:

Name Description
DoesNotExist

Exception raised when a query returns no results but at least one result was expected.

MultipleObjectsReturned

Exception raised when a query returns multiple results but only one result was expected.

Methods:

Name Description
db_client

Gets the database client class to use for interactions.

delete

Deletes the model instance from the database.

save

Saves the model instance to the database (either inserting or updating).

DoesNotExist

Bases: Exception

Exception raised when a query returns no results but at least one result was expected.

MultipleObjectsReturned

Bases: Exception

Exception raised when a query returns multiple results but only one result was expected.

db_client classmethod

db_client() -> type[BaseClient]

Gets the database client class to use for interactions.

This method can be overridden in subclasses to provide a custom database client implementation. The default implementation returns SupabaseClient.

Returns:

Type Description
BaseClient

The database client class.

Source code in supadantic/models.py
133
134
135
136
137
138
139
140
141
142
143
144
145
@classmethod
def db_client(cls) -> type['BaseClient']:
    """
    Gets the database client class to use for interactions.

    This method can be overridden in subclasses to provide a custom database
    client implementation.  The default implementation returns `SupabaseClient`.

    Returns:
        (BaseClient): The database client class.
    """

    return SupabaseClient

delete

delete() -> None

Deletes the model instance from the database.

This method deletes the record from the database corresponding to the model instance's ID. If the model instance does not have an ID, this method does nothing.

Source code in supadantic/models.py
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
def delete(self: _M) -> None:
    """
    Deletes the model instance from the database.

    This method deletes the record from the database corresponding to the
    model instance's ID. If the model instance does not have an ID, this
    method does nothing.
    """

    if self.id:
        query_builder = QueryBuilder()
        query_builder.set_equal(id=self.id)
        query_builder.set_delete_mode(True)

        db_client = self._get_db_client()
        db_client.execute(query_builder=query_builder)

save

save() -> _M

Saves the model instance to the database (either inserting or updating).

If the model instance has an ID, it is updated in the database. Otherwise, it is inserted as a new record. The model instance is updated with the data returned from the database after the save operation.

Returns:

Type Description
_M

The saved model instance, updated with data from the database (e.g., the assigned ID for a new record).

Source code in supadantic/models.py
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
def save(self: _M) -> _M:
    """
    Saves the model instance to the database (either inserting or updating).

    If the model instance has an ID, it is updated in the database.  Otherwise,
    it is inserted as a new record.  The model instance is updated with the
    data returned from the database after the save operation.

    Returns:
        (_M): The saved model instance, updated with data from the database (e.g.,
                the assigned ID for a new record).
    """

    db_client = self._get_db_client()
    data = self.model_dump(exclude={'id'})

    query_builder = QueryBuilder()

    if self.id:
        query_builder.set_equal(id=self.id)
        query_builder.set_update_data(data)
    else:
        query_builder.set_insert_data(data)

    response_data = db_client.execute(query_builder=query_builder)[0]
    return self.__class__(**response_data)