Skip to main content

An ORM-like wrapper for the rethinkdb asyncio driver

Project description

## Introduction

Resync is a Django-ORM inspired wrapper around the official rethinkdb driver's (currently undocumented) asyncio connection class.

### Usage Examples

```python
import asyncio
import resync
from resync import fields
from resync.models import Model, NestedDocument

from myapp.auth.models import User


class Grommet(NestedDocument):
"""
Nested documents don't have a table associated, but allow you to define a
schema for arbitrary json sub-documents in a model.
"""
weight = fields.FloatField()
enabled = fields.BooleanField(default=False)


class Widget(Model):
"""
Model classes refer to a table in the database. By default, the table
name is the lowercased name of the class.
"""

id = fields.StrField()
grommet = fields.NestedDocumentField(Grommet)
foo = fields.StrField()
owner = fields.ForeignKeyField(User)


async def create_widget(user: User) -> None:
"""
Model objects can be created and manipulated like ordinary python objects,
then saved to the database.
"""
new_widget = Widget(owner=user, foo='bar')
new_widget.grommet = Grommet(weight=15.0)
await new_widget.save()
print(new_widget.id) # '45226082-976e-4226-b2c7-0d2c86c45b73': generated by rethinkdb


async def get_widget_by_id(id: str) -> Widget:
"""
Use the 'objects' class attribute to query the database to retrieve an
instance.
"""
widget = await Widget.objects.get(id=id)
return widget


async def get_all_widgets() -> typings.List[Widget]:
"""
Querysets can be `await`ed to get all the results in a list ...
"""
widgets = await Widget.objects.all()
return widgets


async def send_all_widgets_to_socket(socket: aiohttp.web.WebSocketResponse) -> None:
"""
... or iterated over using `async for`
"""
async for widget in Widget.objects.all():
serializer = MyWidgetSerializer(widget)
socket.send(serializer.data)


async def rename_all_widgets() -> None:
"""
Make simple changes to the database in a single query without extracting
the objects to python code.
"""
await Widget.objects.all().update(foo='baz')


async def get_enabled_widgets_for_user(user: User) -> typings.List[Widget]:
"""
Querying related fields works similarly to django, returning a queryset
that can be manipulated as normal
"""
ret = []
async for widget in user.widget_set.filter(enabled=True):
ret.append(widget)
return ret


def main():
"""
Resync deals transparently with connections, managing a connection pool to
reuse open connections when available.
"""
resync.setup({
'host': 'my.rethinkdb.fqdn',
'db': 'my_database_name',
'user': 'test',
'password': '123456'
}) # These arguments are passed to rethinkdb.connect
# (docs: https://www.rethinkdb.com/api/python/connect/)

loop = asyncio.get_event_loop()
fut = asyncio.ensure_future(get_all_widgets())
loop.run_until_complete(fut)
print(fut.result()) # List of widgets...

teardown = asyncio.ensure_future(resync.teardown())
loop.run_until_complete(teardown) # Closes all connections.
loop.close()
```

### TODO

- Tests
- Docs

### Contributors

- James Keys (author) <@skolsuper>

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

resync-orm-0.2.2.tar.gz (14.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

resync_orm-0.2.2-py2.py3-none-any.whl (16.3 kB view details)

Uploaded Python 2Python 3

File details

Details for the file resync-orm-0.2.2.tar.gz.

File metadata

  • Download URL: resync-orm-0.2.2.tar.gz
  • Upload date:
  • Size: 14.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for resync-orm-0.2.2.tar.gz
Algorithm Hash digest
SHA256 a499e785a688d741cf6f9842de125590dc027b1aa21e09a75b6140dfdca3ecee
MD5 f3d738b06eba8fb7a6c46e823a1cbe9c
BLAKE2b-256 127ec89a4eda0906badb7e0b42509b6268994c898bc9923ad525d071e357b645

See more details on using hashes here.

File details

Details for the file resync_orm-0.2.2-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for resync_orm-0.2.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 4555d82034f156e6ed6947460b02efbb58e3c90ae7f1ac9a3fbe1b35689fb61f
MD5 3f05ebaf1e3f3ac586fe4f6b15e7f532
BLAKE2b-256 5da79f95b67d7272342d8346548ef57b33d224a261c18e2cef36c833c8dc5d77

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page