Git Product home page Git Product logo

flex's Introduction

Dataclass aware ORM with pluggable backends (DynamoDB included).

Install

Install virtual environment and dependencies using make install

$ make install
$ source venv/bin/activate
(venv) $ 

Run

Start the compose stack, which runs local versions of dynamodb and api gateway

(venv) $ docker compose up -d 

Usage

Run the example below using:

(venv) $ export DYNAMODB=http://localhost:8009
(venv) $ python example/widgets.py

Examples

from dataclasses import dataclass
from flex import FlexObject


@dataclass
class Inventory(FlexObject):
    quantity: int = 0

    @classmethod
    @property
    def sort_key(cls) -> str:
        return 'quantity'


@dataclass
class Button(Inventory):
    type: str = "square"
    widget: str = None


@dataclass
class Widget(Inventory):
    theme: str = "default"
    _buttons = [Button(name="button zero", quantity=5, id="button0")]

    @property
    def buttons(self) -> []:
        buttons = self.relation(Button, backref='widget')
        return buttons

    @buttons.setter
    def buttons(self, buttons: str) -> None:
        for button in buttons:
            button.widget = self.id

        self._buttons = buttons

    def save(self):
        super(Widget, self).save()

        for button in self._buttons:
            button.save()


# SCENARIO 1:
# Create table, Button, Widget, Add Buton to Widget. Save Widget
""" Create tables """
Button.create_table(skip_exists=True)
Widget.create_table(skip_exists=True)

""" Create a Button """
button1 = Button(name="button one", quantity=5, id="button1")

""" Create a Widget """
wid1 = Widget(name="widget one", quantity=10, id="widget1")

""" Add a button to widget1's existing list of buttons """
wid1.buttons += [button1]
print("WID1", wid1, wid1.buttons)
""" Save widget 1 """
wid1.save()

""" Create another widget """
wid2 = Widget(name="widget two", quantity=15, id="widget2")
wid2.save()

# SCENARIO 2:
# Issue SQL to find Widgets
""" Execute arbitrary SQL on the Widget table """
results = Widget.execute(f"SELECT * FROM \"Widget\" WHERE quantity<? and id=?", [15, 'widget1'])
for result in results:
    print("RESULT:", result, result.buttons)

# Find widgets using template object
""" Find Widget objects using template """
results = Widget.find({'id': 'widget1'}, response=True)
print("FIND:", results.response)

for result in results.all():
    print("RESULT:", result, result.buttons)

# SCENARIO 3:
# Delete specific widget by instance
""" Delete widget instance using instance method"""
print("DELETE widget1", wid1.delete().response)

# SCENARIO 4:
# Delete specific widget by template object
""" Try to find deleted object """
print("FIND2", Widget.find({'id': 'widget1', 'name': 'widget one'}))

# SCENARIO 5:
# Create table, Add Inventory Items
""" Create tables """
Inventory.create_table(skip_exists=True)

""" Create some Inventory objects and save them """
inv = Inventory(name="item", quantity=10, id="item1")
inv.save()
inv = Inventory(name="item", quantity=20, id="item2")
inv.save()

""" Delete widget2 using Class.delete(...) 
print("DELETE widget2", Widget.delete({'id': 'widget2', 'quantity': 15}))

results = Widget.execute(f"DELETE FROM \"Widget\" WHERE quantity=? and name='widget one' and id=?", [10, 'widget1'])
print(results)
"""

flex's People

Stargazers

Darren Govoni avatar

Watchers

Darren Govoni avatar  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.