Coder Social home page Coder Social logo

supadantic's Introduction

Supadantic

PyPI - Downloads GitHub License versions pypi

Supadantic is a small Python library that allows you to manage Supabase tables through Pydantic models. It is very convenient to use in projects based on FastAPI, 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

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

supadantic's People

Contributors

makridenko avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

supadantic's Issues

Documentation

  1. Idea is to use sphinx + readthedocs. Need to add docs generation to CI/CD
  2. there is an error in README.md (supabase link)

Related objects support

Need to describe.

class User(BaseDBEntity):
    pass

class UserSettings(BaseDBEntity):
    user = User()

user = User()
user_settings = UserSettings(user=user)

Type `List[str]` not working properly

class User(BaseSBModel):
    some_field = List[str]

user = User(some_field=['foo', 'bar'])
user.save() # Error (supabase SDK returns "['foo', 'bar']")

`QSet` object

QSet object will store supadantic entities.
It should have two methods:

  • delete - delete all objects in QSet
  • update - (deprecate bulk_update) - update all objects in QSet
users = User.get_list()
users.update(data={'name': 'John Doe'})
users.delete()

Add more useful filters

# Old filters
User.objects.filter(eq={'name': 'test'}, neq={'id': 1})

# New filters
User.objects.filter(name='test').exclude(id=1)

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.