GithubHelp home page GithubHelp logo

angeltam / dynamorm Goto Github PK

View Code? Open in Web Editor NEW

This project forked from nerdwalletoss/dynamorm

0.0 1.0 0.0 594 KB

Python object & relation mapping library for Amazon's DynamoDB service

Home Page: https://nerdwalletoss.github.io/dynamorm/

License: Other

Python 100.00%

dynamorm's Introduction

DynamORM

image

image

Latest PyPI version

Supported Python Versions


This package is a work in progress -- Feedback / Suggestions / Etc welcomed!

Python + DynamoDB โ™ก

DynamORM (pronounced Dynamo-R-M) is a Python object & relation mapping library for Amazon's DynamoDB service.

The project has two goals:

  1. Abstract away the interaction with the underlying DynamoDB libraries. Python access to the DynamoDB service has evolved quickly, from Dynamo v1 in boto to Dynamo v2 in boto and then the new resource model in boto3. By providing a consistent interface that will feel familiar to users of other Python ORMs (SQLAlchemy, Django, Peewee, etc) means that we can always provide best-practices for queries and take advantages of new features without needing to refactor any application logic.
  2. Delegate schema validation and serialization to more focused libraries. Building "ORM" semantics is "easy", doing data validation and serialization is not. We support both Marshmallow and Schematics for building your object schemas. You can take advantage of the full power of these libraries as they are transparently exposed in your code.

Supported Schema Validation Libraries

Example

import datetime

from dynamorm import DynaModel, GlobalIndex, ProjectAll

# In this example we'll use Marshmallow, but you can also use Schematics too!
# You can see that you have to import the schema library yourself, it is not abstracted at all
from marshmallow import fields

# Our objects are defined as DynaModel classes
class Book(DynaModel):
    # Define our DynamoDB properties
    class Table:
        name = 'prod-books'
        hash_key = 'isbn'
        read = 25
        write = 5

    class ByAuthor(GlobalIndex):
        name = 'by-author'
        hash_key = 'author'
        read = 25
        write = 5
        projection = ProjectAll()

    # Define our data schema, each property here will become a property on instances of the Book class
    class Schema:
        isbn = fields.String(validate=validate_isbn)
        title = fields.String()
        author = fields.String()
        publisher = fields.String()

        # NOTE: Marshmallow uses the `missing` keyword during deserialization, which occurs when we save
        # an object to Dynamo and the attr has no value, versus the `default` keyword, which is used when
        # we load a document from Dynamo and the value doesn't exist or is null.
        year = fields.Number(missing=lambda: datetime.datetime.utcnow().year)


# Store new documents directly from dictionaries
Book.put({
    "isbn": "12345678910",
    "title": "Foo",
    "author": "Mr. Bar",
    "publisher": "Publishorama"
})

# Work with the classes as objects.  You can pass attributes from the schema to the constructor
foo = Book(isbn="12345678910", title="Foo", author="Mr. Bar",
           publisher="Publishorama")
foo.save()

# Or assign attributes
foo = Book()
foo.isbn = "12345678910"
foo.title = "Foo"
foo.author = "Mr. Bar"
foo.publisher = "Publishorama"

# In all cases they go through Schema validation, calls to .put or .save can result in ValidationError
foo.save()

# You can then fetch, query and scan your tables.
# Get on the hash key, and/or range key
book = Book.get(isbn="12345678910")

# Update items, with conditions
# Here our condition ensures we don't have a race condition where someone else updates the title first
book.update(title='Corrected Foo', conditions=(title=book.title,))

# Query based on the keys
Book.query(isbn__begins_with="12345")

# Scan based on attributes
Book.scan(author="Mr. Bar")
Book.scan(author__ne="Mr. Bar")

# Query based on indexes
Book.ByAuthor.query(author="Mr. Bar")

Documentation

Full documentation is built from the sources each build and can be found online at:

https://nerdwalletoss.github.io/dynamorm/

The tests/ also contain the most complete documentation on how to actually use the library, so you are encouraged to read through them to really familiarize yourself with some of the more advanced concepts and use cases.

dynamorm's People

Contributors

borgstrom avatar dpnnw avatar akshaynanavati avatar andyinsf avatar robby-bryant avatar sharadh avatar ahollenbach avatar jasonbot avatar ryankirkman avatar

Watchers

James Cloos 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.