Skip to content

Commit

Permalink
add some more md files; change docstrings in py files (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
makridenko committed Jun 9, 2024
1 parent 8947f08 commit c56826d
Show file tree
Hide file tree
Showing 17 changed files with 294 additions and 94 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ __pycache__/
.env
.pytest_cache/
dist/
site/
1 change: 0 additions & 1 deletion docs/base-client.md

This file was deleted.

1 change: 1 addition & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
## v0.0.5
1 change: 1 addition & 0 deletions docs/clients/base/base.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
::: supadantic.clients.base.BaseClient
1 change: 1 addition & 0 deletions docs/clients/base/singletone_meta.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
::: supadantic.clients.base.SingletoneMeta
1 change: 1 addition & 0 deletions docs/clients/cache.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
::: supadantic.clients.cache.CacheClient
1 change: 1 addition & 0 deletions docs/clients/supabase.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
::: supadantic.clients.supabase.SupabaseClient
73 changes: 72 additions & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
@@ -1 +1,72 @@
Hello there!
# Supadantic

[![PyPI - Downloads](https://img.shields.io/pypi/dm/supadantic)](https://github.com/makridenko/supadantic)
[![GitHub License](https://img.shields.io/github/license/makridenko/supadantic)](https://github.com/makridenko/supadantic)
[![versions](https://img.shields.io/pypi/pyversions/supadantic.svg)](https://github.com/makridenko/supadantic)
[![pypi](https://img.shields.io/pypi/v/supadantic.svg)](https://pypi.python.org/pypi/supadantic)

Supadantic is a small Python library that allows you to manage [Supabase](https://supabase.com) tables through [Pydantic](https://github.com/pydantic/pydantic) models. It is very convenient to use in projects based on [FastAPI](https://github.com/tiangolo/fastapi), [Flask](https://github.com/pallets/flask), and so on.


## Important information
Supadantic may not have backward compatibility until version `0.1.0`. This is because the library is still experimental and is being used in several of my other projects, where requirements are still being finalized.


## Installation

Install using `pip install -U supadantic`.

Also, you need to add `SUPABASE_URL` and `SUPABASE_KEY` to your env variables.


## A Simple example

```python
from supadantic.models import BaseSBModel


class User(BaseSBModel):
# id field already defined in BaseSBModel class
name: str = 'John Doe'
is_active: bool = True

# By default table name is class name in snake_case
# If you want to change it - you should implement _get_table_name method
@classmethod
def _get_table_name(cls) -> str:
return 'db_user'

# Save user
active_user = User(name='John Snow')
active_user.save()

non_active_user = User(is_active=False)
non_active_user.save()

# Get all users
users = User.objects.all()

# Count users
users.count()

# Get first user
users.first()

# Get last user
users.last()

# Filter users
active_users = User.objects.filter(is_active=True)
# Or
active_users = User.objects.exclude(is_active=False)

# Update all active users
active_users.update(is_active=False)

# Delete all non active users
User.objects.exclude(is_active=True).delete()

# Get one user and delete
user = User.objects.get(name='John Doe')
user.delete()
```
1 change: 1 addition & 0 deletions docs/models/base_sb_model.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
::: supadantic.models.BaseSBModel
1 change: 1 addition & 0 deletions docs/models/model_metaclass.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
::: supadantic.models.ModelMetaclass
1 change: 1 addition & 0 deletions docs/q_set.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
::: supadantic.q_set.QSet
66 changes: 62 additions & 4 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,69 @@ site_description: Supadantic is a small Python library that allows you to manage
copyright: Maintained by <a href="https://github.com/makridenko/">Alexey Makridenko</a>

nav:
- Home: index.md
- Source:
- BaseClient: base-client.md
- Home:
- Overview: index.md
- Changelog: changelog.md
- API Documentation:
- Clients:
- Base:
- SingletoneMeta: clients/base/singletone_meta.md
- BaseClient: clients/base/base.md
- CacheClient: clients/cache.md
- SupabaseClient: clients/supabase.md
- Models:
- BaseSBModel:
- ModelMetaclass: models/model_metaclass.md
- BaseSBModel: models/base_sb_model.md
- QSet: q_set.md

plugins:
- search
- mkdocstrings
- mkdocstrings:
handlers:
python:
options:
docstring_style: google
show_doctring_examples: true
merge_init_into_class: true
show_source: false
docstring_options:
ignore_init_summary: true
separate_signature: true
show_root_heading: true
show_root_full_path: false
show_signature_annotations: true
show_symbol_type_heading: true
show_symbol_type_toc: true
signature_crossrefs: true
summary: true

theme:
name: 'material'
feature:
tabs: true
icon:
repo: fontawesome/brands/github
features:
- announce.dismiss
- content.action.edit
- content.action.view
- content.code.annotate
- content.code.copy
- content.tooltips
- navigation.footer
- navigation.indexes
- navigation.sections
- navigation.tabs
- navigation.tabs.sticky
- navigation.top
- search.highlight
- search.suggest
- toc.follow
palette:
scheme: slate
primary: black
accent: lime

extra:
social:
Expand All @@ -32,3 +81,12 @@ markdown_extensions:
permalink: true
- pymdownx.arithmatex:
generic: true
- admonition
- pymdownx.keys
- pymdownx.magiclink
- pymdownx.superfences:
custom_fences:
- name: mermaid
class: mermaid
- pymdownx.tasklist:
custom_checkbox: true
39 changes: 25 additions & 14 deletions supadantic/clients/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ def insert(self, data: Dict[str, Any]) -> Dict[str, Any]:
"""
Insert a new record into the table.
:param data: The data to insert.
Args:
data (Dict[str, Any]): The data to insert.
:return: The inserted record.
Returns:
(Dict[str, Any]): The inserted record.
"""
raise NotImplementedError

Expand All @@ -43,10 +45,12 @@ def update(self, *, id: int, data: Dict[str, Any]) -> Dict[str, Any]:
"""
Update a record in the table.
:param id: The ID of the record to update.
:param data: The data to update.
Args:
id (int): The ID of the record to update.
data (Dict[str, Any]): The data to update.
:return: The updated record.
Returns:
(Dict[str, Any]): The updated record.
"""
raise NotImplementedError

Expand All @@ -55,10 +59,12 @@ def select(self, *, eq: Dict[str, Any] | None = None, neq: Dict[str, Any] | None
"""
Select records from the table.
:param eq: The equality filter.
:param neq: The non-equality filter.
Args:
eq (Dict[str, Any] | None): The equality filter.
neq (Dict[str, Any] | None): The non-equality filter.
:return: The selected records.
Returns:
(List[Dict[str, Any]]): The selected records.
"""
raise NotImplementedError

Expand All @@ -67,7 +73,8 @@ def delete(self, *, id: int) -> None:
"""
Delete a record from the table.
:param id: The ID of the record to delete.
Args:
id (int): The ID of the record to delete.
"""
raise NotImplementedError

Expand All @@ -76,10 +83,12 @@ def bulk_update(self, *, ids: Iterable[int], data: Dict[str, Any]) -> List[Dict[
"""
Bulk update records in the table.
:param ids: The IDs of the records to update.
:param data: The data to update.
Args:
ids (Iterable[int]): The IDs of the records to update.
data (Dict[str, Any]): The data to update.
:return: List of updated records.
Returns:
(List[Dict[str, Any]]): List of updated records.
"""
raise NotImplementedError

Expand All @@ -88,8 +97,10 @@ def bulk_delete(self, *, ids: Iterable[int]) -> List[Dict[str, Any]]:
"""
Bulk delete records from the table.
:param ids: The IDs of the records to delete.
Args:
ids (Iterable[int]): The IDs of the records to delete.
:return: List of deleted records.
Returns:
(List[Dict[str, Any]]): List of deleted records.
"""
raise NotImplementedError
45 changes: 30 additions & 15 deletions supadantic/clients/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ def _get_return_data(self, data: Dict[str, Any]) -> Dict[str, Any]:
Get the return data for a record.
Supabase returns iterables as strings, so we need to convert them back.
:param data: The record data.
:return: The return data.
Args:
data (Dict[str, Any]): The record data.
Returns:
(Dict[str, Any]): The return data.
"""
result_data = copy(data)

Expand All @@ -34,8 +37,11 @@ def insert(self, data: Dict[str, Any]) -> Dict[str, Any]:
"""
Insert a new record into the table.
:param data: The data to insert.
:return: The inserted record.
Args:
data (Dict[str, Any]): The data to insert.
Returns:
(Dict[str, Any]): The inserted record
"""

# Get the next ID
Expand All @@ -53,10 +59,11 @@ def update(self, *, id: int, data: Dict[str, Any]) -> Dict[str, Any]:
"""
Update a record in the table.
:param id: The ID of the record to update.
:param data: The data to update.
Args:
id (int): The ID of the record to update.
:return: The updated record.
Returns:
(Dict[str, Any]): The updated record.
"""
self._cache[id].update(data)
return self._get_return_data(self._cache[id])
Expand All @@ -65,10 +72,12 @@ def select(self, *, eq: Dict[str, Any] | None = None, neq: Dict[str, Any] | None
"""
Select records from the table.
:param eq: The equality filter.
:param neq: The non-equality filter.
Args:
eq (Dict[str, Any] | None): The equality filter.
neq (Dict[str, Any] | None): The non-equality filter.
:return: The selected records.
Returns:
(List[Dict[str, Any]]): The selected records.
"""

def _filter(obj: Dict[str, Any]) -> bool:
Expand All @@ -92,7 +101,8 @@ def delete(self, *, id: int) -> None:
"""
Delete a record from the table.
:param id: The ID of the record to delete.
Args:
id (int): The ID of the record to delete.
"""

del self._cache[id]
Expand All @@ -101,9 +111,12 @@ def bulk_update(self, *, ids: Iterable[int], data: Dict[str, Any]) -> List[Dict[
"""
Bulk update records in the table.
:param ids: The IDs of the records to update.
Args:
ids (Iterable[int]): The IDs of the record to update.
data (Data[str, Any]): The updated data.
:param data: List of updated data.
Returns:
(List[str, Any]): The updated records.
"""

result = []
Expand All @@ -117,9 +130,11 @@ def bulk_delete(self, *, ids: Iterable[int]) -> List[Dict[str, Any]]:
"""
Bulk delete records in the table.
:param ids: The IDs of the records to delete.
Args:
ids (Iterable[int]): The IDs of the records to delete.
:return: List of deleted records.
Returns:
(List[Dict[str, Any]]): The deleted records.
"""

result = []
Expand Down
Loading

0 comments on commit c56826d

Please sign in to comment.