Giter Club home page Giter Club logo

falcano's Introduction

Falcano

A Pythonic interface for Amazon's DynamoDB that supports Python 3 and single-table design based on PynamoDB.

Installation

pip install falcano

Basic Usage

Basic usage is nearly identical to PynamoDB. Meta must inherit from Model.Meta and Type must be defined for every model.

Create a model that describes a model in your table.

from falcano.model import Model
from falcano.attributes import UnicodeAttribute

class User(Model):
    '''
    A DynamoDB User
    '''
    class Meta(Model.Meta):
        table_name = 'dynamodb-user'
        billing_mode = 'PAY_PER_REQUEST'
    email = UnicodeAttribute(null=True)
    first_name = UnicodeAttribute(range_key=True)
    last_name = UnicodeAttribute(hash_key=True)
    Type = UnicodeAttribute(default='user')

Create the table if needed:

User.create_table(billing_mode='PAY_PER_REQUEST')

Create a new user:

user = User('John', 'Denver')
user.email = '[email protected]'
user.save()

Now, search your table for all users with a last name of 'Denver' and whose first name begins with 'J':

for user in User.query('Denver', User.first_name.startswith('J')):
    print(user.first_name)

Examples of ways to query your table with filter conditions:

for user in User.query('Denver', User.email.eq('[email protected]')):
    print(user.first_name)
for user in User.query('Denver', User.email=='[email protected]'):
    print(user.first_name)

Retrieve an existing user:

try:
    user = User.get('John', 'Denver')
    print(user)
except User.DoesNotExist:
    print('User does not exist')

Advanced Usage

Indexes? No problem:

from falcano.model import Model
from falcano.indexes import GlobalSecondaryIndex, AllProjection
from falcano.attributes import NumberAttribute, UnicodeAttribute

class ViewIndex(GlobalSecondaryIndex):
    class Meta:
        billing_mode = 'PAY_PER_REQUEST'
        projection = AllProjection()
    view = NumberAttribute(default=0, hash_key=True)

class TestModel(Model):
    class Meta(Model.Meta):
        table_name = 'TestModel'
    forum = UnicodeAttribute(hash_key=True)
    thread = UnicodeAttribute(range_key=True)
    view = NumberAttribute(default=0)
    Type = UnicodeAttribute(default='test')
    view_index = ViewIndex()

Now query the index for all items with 0 views:

for item in TestModel.view_index.query(0):
    print(f'Item queried from index: {item}')

It's simple!

Want to use DynamoDB local? Add a host name attribute and specify your local server.

from falcano.models import Model
from falcano.attributes import UnicodeAttribute

class User(Model):
    '''
    A DynamoDB User
    '''
    class Meta(Model.Meta):
        table_name = 'dynamodb-user'
        host = 'http://localhost:8000'
    email = UnicodeAttribute(null=True)
    first_name = UnicodeAttribute(range_key=True)
    last_name = UnicodeAttribute(hash_key=True)
    Type = UnicodeAttribute(default='user')

Single-Table Design Usage

Want to follow single-table design with an index and rename the Type attribute? No problem:

from falcano.model import Model
from falcano.indexes import GlobalSecondaryIndex, AllProjection
from falcano.attributes import NumberAttribute, UnicodeAttribute

class TypeIndex(GlobalSecondaryIndex):
    class Meta:
        index_name = 'Type'
        billing_mode = 'PAY_PER_REQUEST'
        projection = AllProjection()
    Kind = UnicodeAttribute(default=0, hash_key=True)

class BaseModel(Model):
    class Meta(Model.Meta):
        table_name = 'single_table'
        # use the Kind attribute in place of Type for deserialization
        model_type = 'Kind'
    PK = UnicodeAttribute(hash_key=True)
    SK = UnicodeAttribute(range_key=True)
    TypeIndex = TypeIndex()

class User(BaseModel):
    email = UnicodeAttribute(null=True)
    Kind = UnicodeAttribute(default='user')

class Team(BaseModel):
    owner = UnicodeAttribute(null=True)
    Kind = UnicodeAttribute(default='team')

Features

  • Python >= 3.8 support
  • Use of Table boto3 resource
    • DynamoDB API conditions objects
    • Auto-Typing
  • An ORM-like interface with query and scan filters
  • Compatible with DynamoDB Local
  • Support for Unicode, Binary, JSON, Number, Set, and UTC Datetime attributes
  • Support for Global and Local Secondary Indexes
  • Automatic pagination for bulk operations(?)
  • Complex queries
  • Multiple models per table and deserialization into objects

Features not yet implemented

  • Provides iterators for working with queries, scans, that are automatically - paginated
  • Iterators for working with Query and Scan operations
  • Supports the entire DynamoDB API
  • Full table backup/restore
  • Batch operations with automatic pagination

falcano's People

Contributors

blaisethompson avatar dirk-sandberg avatar erictwalker18 avatar jjf130 avatar ktruckenmiller avatar mneil avatar saucoincw3m avatar sukritsangvong avatar

Stargazers

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

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

falcano's Issues

BooleanAttribute Missing

Hi,

Came across the fork whilst trying to use PynamoDB for Single-table design.
I notice this project seems to be missing the BooleanAttribute attribute. Is there a reason for this?

Also, I find I am only able to define a model with both Kind and Type set as attributes of my model - it seems if I just use Kind, there is a code error which is looking for Type and if I just use type, then exceptions raise when there is no Kind.

Thanks

UnicodeSetAttribute sorts my set

I haven't confirmed with a test, however, from the code https://github.com/3mcloud/falcano/blob/master/falcano/attributes.py#L411 it seems as though my sets are being ordered. This would break certain scenarios.

  • I have a queue
  • the queue consists of UUIDs to identify the items in the queue
  • I'm managing the queue via a set in dynamo
  • I create the queue and save it

UUIDs can't be sorted and I really need the order to exist. But, my set is rearranged and the stored data in dynamo is not what I expected. I would process my queue out of order.

MapAttribute serialisation issues

Looking to get some help - I am trying to use this fork to implement a single table design in my python-based API.

Overall, works fine but when I store a Map Attribute field, the result in Dynamo seems like it's been serialised multiple times and looks incorrect (although valid in dynamo)

Comparison:

Created via falcano

new = Item(hash_key=f'testItem#{id}', range_key="testRange#3749189f-3d4c-4937-b31a-924bc84b366c")
 new.data = dict(hello="message", nested={"hello": "message"}, enabled=False, number=0)

Results in

"data": {
    "M": {
      "number": {
        "M": {
          "N": {
            "N": "0"
          }
        }
      },
      "hello": {
        "M": {
          "S": {
            "S": "message"
          }
        }
      },
      "nested": {
        "M": {
          "M": {
            "M": {
              "hello": {
                "M": {
                  "S": {
                    "S": "message"
                  }
                }
              }
            }
          }
        }
      },
      "enabled": {
        "M": {
          "BOOL": {
            "BOOL": false
          }
        }
      }
    }
  },

Creating the attribute manually via the console as Map attribute, this is what it looks like and what I'd expect the python code to do too.

"console": {
    "M": {
      "string": {
        "S": "sdasda"
      },
      "number": {
        "N": "0"
      },
      "map": {
        "M": {
          "boolean": {
            "BOOL": false
          }
        }
      }
    }
  }

What am I doing wrong?

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.