GithubHelp home page GithubHelp logo

supawee's Introduction

SupaWee

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.

Connecting to the database

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!

Example models

# 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()

Features

  • 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.

Installation

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

!! DO NOT USE ON YOUR PRODUCTION DATABASE !!

Requirements

  • psql installed
  • can run python -m pwiz ...

Possible Future Things:

Development Setup

In case you want to contribute!

Setup

git clone https://github.com/yourusername/supawee.git
cd supawee

Python

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

Testing

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).

FAQ

But there is Supabase Python SDK

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

supawee's People

Contributors

petercsiba avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.