Skip to main content

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.

Release files for aiocqlengine 0.3.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for aiocqlengine 0.3.2
File Size Uploaded
aiocqlengine-0.3.2.tar.gz 10.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for aiocqlengine 0.3.2
File Interpreter ABI Platform
aiocqlengine-0.3.2-py3-none-any.whl Python 3 none any Details

Total release size: 19.7 kB

Release files / aiocqlengine-0.3.2.tar.gz

Download URL aiocqlengine-0.3.2.tar.gz
Size 10.0 kB
Tags Source
SHA-256 checksum
How to use checksums
c27f754e5786235fa7b4f433e7763b4b4fb3eb87c8e83c8db2ae4880d81e8340
BLAKE2b-256 checksum
How to use checksums
abd05c7837f4a135196fdaab2f0094ae86448a2c03826afd5a780a3c51e76a4b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.5 CPython/3.7.9 Darwin/20.6.0

Release files / aiocqlengine-0.3.2-py3-none-any.whl

Download URL aiocqlengine-0.3.2-py3-none-any.whl
Size 9.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a9677e231e25ee49f0ae09d10581e9f4c3ed3d1433404415d319736cd8613a4a
BLAKE2b-256 checksum
How to use checksums
f66c74c3ca43b6ed645cc846039bda55f386d78d7d9d57a3cc97d42e8fa0c54c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.5 CPython/3.7.9 Darwin/20.6.0

Release history Release notifications | RSS feed

This release

0.3.2 This release

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page