Skip to main content

Async wrapper for cqlengine of cassandra python driver.

Project description

aiocqlengine

Async wrapper for cqlengine of cassandra python driver.

This project is built on cassandra-python-driver and aiocassandra.

Actions Status

Installation

$ pip install aiocqlengine

Change log

0.1.1

  • Add AioBatchQuery:
    batch_query = AioBatchQuery()
    for i in range(100):
        Model.batch(batch_query).create(id=uuid.uuid4())
    await batch_query.async_execute()
    

0.2.0

  • Patch the aiosession for ResultSet, now need to import session by:
    from aiocqlengine.session import aiosession_for_cqlengine
    
  • Add new method of AioModel for paging:
    async for results in AioModel.async_iterate(fetch_size=100):
        # Do something with results
        pass
    

Example usage

import asyncio
import uuid
import os

from aiocqlengine.models import AioModel
from aiocqlengine.query import AioBatchQuery
from aiocqlengine.session import aiosession_for_cqlengine
from cassandra.cluster import Cluster
from cassandra.cqlengine import columns, connection, management


class User(AioModel):
    user_id = columns.UUID(primary_key=True)
    username = columns.Text()


async def run_aiocqlengine_example():
    # Model.objects.create() and Model.create() in async way:
    user_id = uuid.uuid4()
    await User.objects.async_create(user_id=user_id, username='user1')
    await User.async_create(user_id=uuid.uuid4(), username='user2')

    # Model.objects.all() and Model.all() in async way:
    print(list(await User.async_all()))
    print(list(await User.objects.filter(user_id=user_id).async_all()))

    # Model.object.update() in async way:
    await User.objects(user_id=user_id).async_update(username='updated-user1')

    # Model.objects.get() and Model.get() in async way:
    user = await User.objects.async_get(user_id=user_id)
    await User.async_get(user_id=user_id)
    print(user, user.username)

    # Model.save() in async way:
    user.username = 'saved-user1'
    await user.async_save()

    # Model.delete() in async way:
    await user.async_delete()

    # Batch Query in async way:
    batch_query = AioBatchQuery()
    User.batch(batch_query).create(user_id=uuid.uuid4(), username="user-1")
    User.batch(batch_query).create(user_id=uuid.uuid4(), username="user-2")
    User.batch(batch_query).create(user_id=uuid.uuid4(), username="user-3")
    await batch_query.async_execute()

    # Async iterator
    async for users in User.async_iterate(fetch_size=100):
        pass

    # The original cqlengine functions were still there
    print(len(User.objects.all()))


def create_session():
    cluster = Cluster()
    session = cluster.connect()

    # Create keyspace, if already have keyspace your can skip this
    os.environ['CQLENG_ALLOW_SCHEMA_MANAGEMENT'] = 'true'
    connection.register_connection('cqlengine', session=session, default=True)
    management.create_keyspace_simple('example', replication_factor=1)
    management.sync_table(User, keyspaces=['example'])

    # Wrap cqlengine connection with aiosession
    aiosession_for_cqlengine(session)
    session.set_keyspace('example')
    connection.set_session(session)
    return session


def main():
    # Setup connection for cqlengine
    session = create_session()

    # Run the example function in asyncio loop
    loop = asyncio.get_event_loop()
    loop.run_until_complete(run_aiocqlengine_example())

    # Shutdown the connection and loop
    session.cluster.shutdown()
    loop.close()


if __name__ == '__main__':
    main()

License

This project is under MIT license.

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

aiocqlengine-0.2.0.tar.gz (10.0 kB view details)

Uploaded Source

Built Distribution

aiocqlengine-0.2.0-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

File details

Details for the file aiocqlengine-0.2.0.tar.gz.

File metadata

  • Download URL: aiocqlengine-0.2.0.tar.gz
  • Upload date:
  • Size: 10.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.2 CPython/3.7.5 Darwin/19.4.0

File hashes

Hashes for aiocqlengine-0.2.0.tar.gz
Algorithm Hash digest
SHA256 236899c1f6d659d99f34e3418ee117c37ea00f53043709587532de3365341fd0
MD5 d11a06ab2bacb3b50f5d3c93311da822
BLAKE2b-256 ad5bc8ae3b0dee2c88c45d6ca763690b6936c1a068fc9f95249af1cda6733643

See more details on using hashes here.

Provenance

File details

Details for the file aiocqlengine-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: aiocqlengine-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 9.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.2 CPython/3.7.5 Darwin/19.4.0

File hashes

Hashes for aiocqlengine-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 861ffd771bbc059cc593966c2399efa733ca575156eb537650b97443da198b32
MD5 f51156151d964ad2ebdbfd14967e1c1a
BLAKE2b-256 462b6ad2dae52bce57963ca324fd44f6afa808fa37f00bf5486abbcb33e4ce48

See more details on using hashes here.

Provenance

Supported by

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