Skip to content
This repository was archived by the owner on Dec 15, 2020. It is now read-only.

Commit cf1861d

Browse files
committed
Add docs for cqlengine
1 parent 5c17ba1 commit cf1861d

1 file changed

Lines changed: 73 additions & 0 deletions

File tree

README.rst

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,79 @@ Usage
7070
7171
Python 3.5+ is required
7272

73+
74+
Using with cqlengine
75+
-----
76+
77+
.. code-block:: python
78+
79+
import asyncio
80+
import uuid
81+
import os
82+
83+
from aiocassandra import aiosession, AioModel
84+
from cassandra.cluster import Cluster
85+
from cassandra.cqlengine import columns, connection, management
86+
87+
cluster = Cluster()
88+
session = cluster.connect()
89+
90+
91+
class User(AioModel):
92+
user_id = columns.UUID(primary_key=True)
93+
username = columns.Text()
94+
95+
96+
async def main():
97+
aiosession(session)
98+
99+
# Set aiosession for cqlengine
100+
session.set_keyspace('example_keyspace')
101+
connection.set_session(session)
102+
103+
# Model.objects.create() and Model.create() in async way:
104+
user_id = uuid.uuid4()
105+
await User.objects.async_create(user_id=user_id, username='user1')
106+
# also can use: await User.async_create(user_id=user_id, username='user1)
107+
108+
# Model.objects.all() and Model.all() in async way:
109+
print(list(await User.async_all()))
110+
print(list(await User.objects.filter(user_id=user_id).async_all()))
111+
112+
# Model.object.update() in async way:
113+
await User.objects(user_id=user_id).async_update(username='updated-user1')
114+
115+
# Model.objects.get() and Model.get() in async way:
116+
user = await User.objects.async_get(user_id=user_id)
117+
assert user.user_id == (await User.async_get(user_id=user_id)).user_id
118+
print(user, user.username)
119+
120+
# obj.save() in async way:
121+
user.username = 'saved-user1'
122+
await user.async_save()
123+
124+
# obj.delete() in async way:
125+
await user.async_delete()
126+
127+
# Didn't break original functions
128+
print('Left users: ', len(User.objects.all()))
129+
130+
131+
def create_keyspace(keyspace):
132+
os.environ['CQLENG_ALLOW_SCHEMA_MANAGEMENT'] = 'true'
133+
connection.register_connection('cqlengine', session=session, default=True)
134+
management.create_keyspace_simple(keyspace, replication_factor=1)
135+
management.sync_table(User, keyspaces=[keyspace])
136+
137+
138+
create_keyspace('example_keyspace')
139+
140+
loop = asyncio.get_event_loop()
141+
loop.run_until_complete(main())
142+
cluster.shutdown()
143+
loop.close()
144+
145+
73146
Thanks
74147
------
75148

0 commit comments

Comments
 (0)