GithubHelp home page GithubHelp logo

sazpaimon / bloop Goto Github PK

View Code? Open in Web Editor NEW

This project forked from numberoverzero/bloop

0.0 2.0 0.0 1.82 MB

Object Mapper for DynamoDB

Home Page: http://bloop.readthedocs.io

License: MIT License

Makefile 0.05% Python 99.95%

bloop's Introduction

https://readthedocs.org/projects/bloop/badge?style=flat-square https://img.shields.io/travis/numberoverzero/bloop/master.svg?style=flat-square https://img.shields.io/codecov/c/github/numberoverzero/bloop/master.svg?style=flat-square https://img.shields.io/gitter/room/numberoverzero/bloop.svg?style=flat-square

Bloop is an object mapper for DynamoDB and DynamoDBStreams. py35+

pip install bloop

Usage

First, we need to import all the things:

>>> from bloop import (
...     BaseModel, Column, String, UUID,
...     GlobalSecondaryIndex, Engine
... )

Next we'll define the account model (with streaming enabled), and create the backing table:

>>> class Account(BaseModel):
...     class Meta:
...         stream = {
...             "include": {"old", "new"}
...         }
...     id = Column(UUID, hash_key=True)
...     name = Column(String)
...     email = Column(String)
...    by_email = GlobalSecondaryIndex(projection='keys', hash_key='email')
...
>>> engine = Engine()
>>> engine.bind(Account)

Let's make a few users and persist them:

>>> import uuid
>>> admin = Account(id=uuid.uuid4(), email="[email protected]")
>>> admin.name = "Admin McAdminFace"
>>> support = Account(name="this-is-fine.jpg", email="[email protected]")
>>> support.id = uuid.uuid4()
>>> engine.save(admin, support)

And find them again:

>>> q = engine.query(
...     Account.by_email,
...     key=Account.email=="[email protected]"
... )
>>> q.first()
Account(email='[email protected]',
        id=UUID('d30e343f-f067-4fe5-bc5e-0b00cdeaf2ba'))
>>> s = engine.scan(
...     Account,
...     filter=Account.name.begins_with("Admin")
... )
>>> s.one()
Account(email='[email protected]',
        id=UUID('08da44ac-5ff6-4f70-8a3f-b75cadb4dd79'),
        name='Admin McAdminFace')

Let's find them in the stream:

>>> stream = engine.stream(Account, "trim_horizon")
>>> next(stream)
{'key': None,
 'meta': {'created_at': datetime.datetime(...),
  'event': {'id': 'cbb9a9b45eb0a98889b7da85913a5c65',
   'type': 'insert',
   'version': '1.1'},
  'sequence_number': '100000000000588052489'},
 'new': Account(
            email='[email protected]',
            id=UUID('d30e343f-...-0b00cdeaf2ba'),
            name='this-is-fine.jpg'),
 'old': None}
>>> next(stream)
{'key': None,
 'meta': {'created_at': datetime.datetime(...),
  'event': {'id': 'cbdfac5671ea38b99017c4b43a8808ce',
   'type': 'insert',
   'version': '1.1'},
  'sequence_number': '200000000000588052506'},
 'new': Account(
            email='[email protected]',
            id=UUID('08da44ac-...-b75cadb4dd79'),
            name='Admin McAdminFace'),
 'old': None}
>>> next(stream)
>>> next(stream)
>>>

What's Next

Check out the User Guide or Public API Reference to create your own nested types, overlapping models, set up cross-region replication in less than 20 lines, and more!

bloop's People

Contributors

numberoverzero avatar xuru avatar

Watchers

 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.