-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
65 lines (47 loc) · 1.67 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from loguru import logger
from pydantic import BaseModel
from sic_mongodb import (
connect,
init_db,
get_session,
create_db,
insert,
get_all,
get_single,
update_single,
delete_single,
close
)
import asyncio
class SampleModel(BaseModel):
name: str
age: int
async def main():
# Connect to MongoDB
await connect('localhost', 27017)
# Add database and collection
await create_db('test_db_1', collections=["test_collection", "test_collection_1", "test_collection_2"])
# Initialize database
await init_db()
# Get database session
db = await get_session()
# Create data
sample_data = SampleModel(name="John Doe", age=30)
inserted_id = await insert(db['test_db_1'], sample_data, collection_name='test_collection')
logger.info(f"Inserted ID: {inserted_id}")
# Retrieve all data
all_data = await get_all(db['test_db_1'], 'test_collection', 10)
logger.info(f"All Data: {all_data}")
# Retrieve data by field
single_data = await get_single(db['test_db_1'], 'name', 'John Doe', 'test_collection')
logger.info(f"Single Data: {single_data}")
field_to_update = {"age": 3} # Example update operation: increment age by 1
updated_data = await update_single(db['test_db_1'], field='name', value='John Doe', update=field_to_update, collection_name='test_collection')
logger.info(f"Updated Data: {updated_data}")
# Delete data by field
#delete_count = await delete_single(db['test_db_1'], 'name', 'John Doe', 'test_collection')
#logger.info(f"Deleted Count: {delete_count}")
# Close connection
await close()
# Run the main function
asyncio.run(main())