SupaWee is a Peewee model generation primarily to be used with Supabase (and other PostgresSQL). It intends to be what sqlacodegen is for SQLAlchemy.
Born out of laziness of learning the mainstream SQLAlchemy, and shivers of using plain SQL. I liked PeeWee as I am familiar with Django and seems lightweight to be deployed into AWS Lambdas or other edge functions. So wrote these two helper scripts to interface Python with PostgreSQL in a minimalistic way.
supawee
uses peewee.DatabaseProxy
as the model.meta.database
.
This is so we can defer initialization of peewee.PostgresqlDatabase
which is just too heavy.
You can connect to your PostgreSQL Server when you ready with login url:
from supawee.client import connect_to_postgres
with connect_to_postgres("postgresql://postgres:postgres@localhost:54322/postgres"):
do_your_stuff()
using login url instead of the 5 parameters means you only have to deal with one environment variable!
# your/models.py
class BaseUsers(BaseModel):
email = CharField(null=True)
id = UUIDField(null=True)
which you then can subclass to have relevant functionality right on the Model:
# your/auth/related/objects/folder/user.py
from supawee.generated.models import BaseUsers
class Users(BaseUsers):
class Meta:
db_table = "users"
@staticmethod
def exists_by_email(email_raw: str) -> bool:
email = email_raw.lower()
return Users.select().where(Users.email == email).exists()
- Auto-generate PeeWee models from your local database (handles basic circular deps)
- Minimalistic so it can be easily packaged to Lambdas and Edge functions.
- Easy setup^TM and integration with Supabase and PostgreSQL.
BEWARE: This performs one terrible hack including dropping local temp table public.users
to generate auth.users
.
Install SupaWee using pip:
pip install git+https://github.com/petercsiba/supawee.git
And run it:
# model_file, description='filepath to models.py which will have all base models'
# '--host', default='localhost'
# '--port', default='54322'
# '--username', default='postgres'
# '--database', default='postgres'
# '--password', default='postgres'
supawee example/models.py
psql
installed- can run
python -m pwiz ...
(pwiz
is a part ofpeewee
package)
- Understand if async and connection pooling work well with this
- Supabase has connection pooling: https://supabase.com/docs/guides/database/connecting-to-postgres#connection-pooler
- There is peewee async for some reason: https://peewee-async.readthedocs.io/en/latest/
- Look into FastAPI template to maybe support that too https://github.com/AndyPythonCode/FastAPI-crud-with-peewee/tree/main/backend
In case you want to contribute!
git clone https://github.com/yourusername/supawee.git
cd supawee
Setup Python your preferred way. For your emotional stability please use a virtualenv.
The package creator used pyenv virtualenv 3.9.16 supawee
pip install -r requirements/common.txt -r requirements/local.txt
TODO I promise!
Currently, best bet is to test manually with:
# lazy way
python -m supawee.generate_models
# proper way
pip install -e .
supawee example/models.py
NOTE: You would need a local PostgreSQL Server running (e.g. supabase start
).
Yes you right, but I dislike using plain strings for column and table names:
# Snapshot of Supabase Python SDK (released mid 2022):
for i in range(iterator):
value = {'vendor_id': vendor_id, 'product_name': fake.ecommerce_name(),
'inventory_count': fake.random_int(1, 100), 'price': fake.random_int(45, 100)}
main_list.append(value)
data = supabase.table('Product').insert(main_list).execute()
https://supabase.com/blog/loading-data-supabase-python#inserting-data-into-supabase