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.

Actions Status

Installation

$ pip install aiocqlengine

Change log

0.3.0

  • Due to aiocassandra is not maintained, removed the aiocassandra dependency.

0.2.0

  • Create new session wrapper for ResultSet, users need to wrap session by aiosession_for_cqlengine:
    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
    

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()
    

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
    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.3.2.tar.gz (10.0 kB view details)

Uploaded Source

Built Distribution

aiocqlengine-0.3.2-py3-none-any.whl (9.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: aiocqlengine-0.3.2.tar.gz
  • Upload date:
  • Size: 10.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.5 CPython/3.7.9 Darwin/20.6.0

File hashes

Hashes for aiocqlengine-0.3.2.tar.gz
Algorithm Hash digest
SHA256 c27f754e5786235fa7b4f433e7763b4b4fb3eb87c8e83c8db2ae4880d81e8340
MD5 700cef28adfe83b36dfea2082a2c6bd7
BLAKE2b-256 abd05c7837f4a135196fdaab2f0094ae86448a2c03826afd5a780a3c51e76a4b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: aiocqlengine-0.3.2-py3-none-any.whl
  • Upload date:
  • Size: 9.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.5 CPython/3.7.9 Darwin/20.6.0

File hashes

Hashes for aiocqlengine-0.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 a9677e231e25ee49f0ae09d10581e9f4c3ed3d1433404415d319736cd8613a4a
MD5 ffe4b970b8f3a777982b13306583cb30
BLAKE2b-256 f66c74c3ca43b6ed645cc846039bda55f386d78d7d9d57a3cc97d42e8fa0c54c

See more details on using hashes here.

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