DynamoWrapper is a Python library that provides a PyMongo-like interface for Amazon DynamoDB. It simplifies DynamoDB operations, making them more intuitive for developers familiar with MongoDB.
- PyMongo-like API for DynamoDB operations
- Simplified querying with a familiar syntax
- Easy table access and management
- Support for basic CRUD operations
- Index creation and management
- Batch operations support
You can install DynamoWrapper using pip:
pip install dynamo-wrapper
Here's a simple example to get you started:
from dynamo_wrapper import DynamoClient
# Initialize the client
client = DynamoClient(aws_access_key_id, aws_secret_access_key, region)
# Access a table
table = client['my-table']
# Insert a document
table.insert_one({'id': '1', 'name': 'John Doe', 'age': 30})
# Find documents
results = table.find({'age': ('GT', 25)})
# Update a document
table.update_one({'id': ('EQ', '1')}, {'name': 'Jane Doe'})
# Delete a document
table.delete_one({'id': ('EQ', '1')})from dynamo_wrapper import DynamoClient
client = DynamoClient(aws_access_key_id, aws_secret_access_key, region)table = client['table-name']# Insert one document
table.insert_one({'id': '1', 'name': 'John Doe', 'age': 30})
# Insert multiple documents
table.insert_many([
{'id': '2', 'name': 'Jane Doe', 'age': 28},
{'id': '3', 'name': 'Bob Smith', 'age': 35}
])# Find one document
result = table.find_one({'id': ('EQ', '1')})
# Find multiple documents
results = table.find({'age': ('GT', 25)})
# Find with projection
results = table.find({'age': ('GT', 25)}, projection=['name', 'age'])
# Find with limit
results = table.find({'age': ('GT', 25)}, limit=10)# Update one document
table.update_one({'id': ('EQ', '1')}, {'name': 'Jane Doe'})# Delete one document
table.delete_one({'id': ('EQ', '1')})# Count documents
count = table.count({'age': ('GT', 25)})# Create an index
table.create_index('name')DynamoWrapper supports the following comparison operators:
EQ: Equal toNE: Not equal toLT: Less thanLTE: Less than or equal toGT: Greater thanGTE: Greater than or equal toIN: In a list of valuesBETWEEN: Between two values
Example:
table.find({'age': ('GT', 25), 'status': ('EQ', 'active')})DynamoWrapper provides custom exceptions for various error scenarios. Here are some common exceptions:
ItemNotFoundError: Raised when an item is not found in the tableQueryError: Raised when there's an error in query formation or executionUpdateError: Raised when an update operation failsDeleteError: Raised when a delete operation failsInsertError: Raised when an insert operation fails
Example:
from dynamo_wrapper import ItemNotFoundError
try:
result = table.find_one({'id': ('EQ', '999')})
except ItemNotFoundError:
print("Item not found")