API Reference¶
Utility Methods¶
Connection¶
Connection borg
-
class
dynamongo.connection.
Connection
(access_key_id=None, secret_access_key=None, region=None, table_prefix=None)[source]¶ Borg that handles access to DynamoDB.
You should never make any explicit/direct
boto3.dynamodb
calls by yourself except for table maintenance operationsBefore making any calls, aws credentials must be set by either:
calling
set_config()
, orsetting environment variables
AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY
AWS_REGION_NAME
AWS_TABLE_PREFIX
Model¶
-
class
dynamongo.model.
Model
(**kwargs)[source]¶ Base model class with which to define custom models.
Example usage:
from dynamongo import Model from dynamongo import IntField, StringField, EmailField class User(Model): __table__ = 'users' __hash_key__ = 'email' # fields email = EmailField(required=True) name = StringField(required=True) year_of_birth = IntField(max_value=2018, min_value=1900)
Each custom model can declare the following class meta data variables:
__table__ (required)
The name of table to be associated with this model. This is usually prefixed with the table prefix as set in
Connection
. i.e, in dynamodb, the table name will appear as<table_prefix><table_name>
__hash_key__ (required)
The name of the field to be used as the Hash key for the table. NOTE: A field for the hash key MUST be declared and it must be of primitive type
str|numeric
__range_key__ (optional)
The name of the field to be used as the Range key for the table. NOTE: This is Optional. However, if declared, a corresponding field MUST be declared and it must be of primitive type
str|numeric
__read_units__ (optional)
The number of read units to provision for this table (default
8
)__write_units__ (optional)
The number of write units to provision for this table (default
8
)See Amazon’s developer guide for more information about provisioned throughput Capacity for Reads and Writes
-
classmethod
keys_in
(values)[source]¶ Convenient method to generate
CompoundKeyCondition
This is useful when working with a model that has a composite primary key i.e, both
hash_key
andrange_key
Example usage:
import datetime from dynamongo import Model from dynamongo import EmailField, UUIDField, DateTimeField class Contacts(Model): __table__ = 'user-contacts' __hash_key__ = 'user_id' __range_key__ = 'email' # fields user_id = UUIDField(required=True) email = EmailField(required=True) created_at = DateTimeField(default=datetime.datetime.now) # select multiple contacts for different users when you have a # list of (user_id, email) tuples keys = [('user_id_1', 'john@gmail.com'), ('user_id_2', 'doe@gmail.com')] contacts = Contacts.get_many( Contacts.keys_in(keys) )
-
classmethod
create_table
()[source]¶ Create a table that’ll be used to store instances of cls in AWS dynamoDB.
This operation should be called before any table read or write operation is undertaken
-
classmethod
get_one
(strategy)[source]¶ Retrieve a single item from DynamoDB according to strategy.
Returns: Instance of cls
- The fetched item
-
classmethod
get_many
(strategy, descending=False, limit=None)[source]¶ Retrieve a multiple items from DynamoDB according to strategy.
Performs either a BatchGet, Query, or Scan depending on strategy
Parameters: - strategy – See Getting multiple items
- descending (bool) – Sort order. Items are sorted by the hash key. Items with the same hash key value are sorted by range key
- limit (int) – The maximum number of items to get (not necessarily the number of items returned)
Returns: list of
cls
-
classmethod
delete_one
(strategy)[source]¶ Deletes a single item in a table. You can perform a conditional delete operation that deletes the item if it exists, or if it has an expected attribute value.
Returns: The deleted item
-
classmethod
delete_many
(strategy)[source]¶ Deletes multiple items in a table.
Returns: BatchResult
-
classmethod
save_one
(item, overwrite=True)[source]¶ Creates a new item, or replaces an old item with a new item. If an item that has the same primary key as the new item already exists in the specified table, the new item completely replaces the existing item
overwrite
specifies under what circumstances should we overwrite an existing item.If
overwrite = True
, an existing item with the same primary key is replaced by the new item unconditionally. This is the default behaviour.If
overwrite = False
, aConditionalCheckFailedException
is raised if there is an existing item with the same primary keyIf
overwrite
is a conditional expression, an existing item with the same primary key is replaced by the new item if and only if the condition is met. otherwiseConditionalCheckFailedException
is raised.Parameters: - item – the item to save. either a
dict
orcls
- overwrite – This can be a
bool
or a condition. it defaults toTrue
Raises: Returns: cls
- item – the item to save. either a
-
classmethod
save_many
(items, overwrite=True)[source]¶ Creates or replaces multiple items. If an item that has the same primary key as the new item already exists in the specified table, the new item completely replaces the existing item
overwrite
specifies under what circumstances should we overwrite an existing item.If
overwrite = True
, an existing item with the same primary key is replaced by the new item unconditionally. This is the default behaviour.If
overwrite = False
and there is an existing item with the same primary key, the item is added onBatchResult.fail
listIf
overwrite
is a conditional expression and an existing item with the same primary key does not meet the condition specified, then the item is added onBatchResult.fail
list.Parameters: - items – a list of items to save. each item can be either a
dict
orcls
- overwrite –
bool
or a condition. it defaults toTrue
Returns: - items – a list of items to save. each item can be either a
-
classmethod
Fields¶
Field classes for various types of data.
-
class
dynamongo.fields.
Field
[source]¶ Basic field from which other fields should extend. It applies no formatting by default, and should only be used in cases where data does not need to be serialized or deserialized.
Supported primitive conditions are
==
,!=
,<
,<=
,>
, and>=
-
in_
(value)[source]¶ Creates a condition where the attribute is in the value,
Parameters: value (list) – The list of values that the attribute is in.
-
contains
(value)[source]¶ Creates a condition where the attribute contains the value.
Parameters: value – The value the attribute contains.
-
begins_with
(value)[source]¶ Creates a condition where the attribute begins with the value.
Parameters: value – The value that the attribute begins with.
-
between
(low, high)[source]¶ Creates a condition where the attribute is greater than or equal to the low value and less than or equal to the high value.
Parameters: - low – The value that the attribute is greater than or equal to.
- high – The value that the attribute is less than or equal to.
-
set_if_not_exists
(value)[source]¶ Set field to the given value if it does not exist otherwise do nothing
-
default
¶ Get the default value
-
-
class
dynamongo.fields.
StringField
(regex=None, max_length=None, min_length=None, **kwargs)[source]¶ A Unicode string field.
-
class
dynamongo.fields.
EmailField
(regex=None, max_length=None, min_length=None, **kwargs)[source]¶ A field that validates input as an E-Mail-Address
-
class
dynamongo.fields.
URLField
(fqdn=True, verify_exists=False, **kwargs)[source]¶ A field that validates the input as a URL.
-
class
dynamongo.fields.
IPAddressField
(regex=None, max_length=None, min_length=None, **kwargs)[source]¶ A field that stores a valid IPv4 or IPv6 address.
-
class
dynamongo.fields.
DateTimeField
(formats=None, serialized_format=None, parser=None, tzd='allow', convert_tz=False, drop_tzinfo=False, **kwargs)[source]¶ A field that holds a combined date and time value.
-
class
dynamongo.fields.
DateField
(formats=None, **kwargs)[source]¶ A field that stores and validates date values.
-
class
dynamongo.fields.
TimedeltaField
(precision='seconds', **kwargs)[source]¶ A field that stores and validates timedelta value
-
class
dynamongo.fields.
ListField
(field, default=[], **kwargs)[source]¶ A field for storing a list of items, all of which must conform to the type specified by the
field
parameter.Note: This field cannot be set to
None
Exceptions¶
-
exception
dynamongo.exceptions.
ValidationError
(*args, **kwargs)[source]¶ Exception raised when invalid data is encountered.
-
exception
dynamongo.exceptions.
ConditionalCheckFailedException
[source]¶ Raised when saving a Model instance would overwrite something in the database and we’ve forbidden that
Conditional Expressions¶
Note
These classes should never be instantiated directly by the user
-
class
dynamongo.conditions.
AndCondition
(left, right)[source]¶ Initialized by ANDing two expressions i.e, BaseCondition & BaseCondition
Update Expressions¶
Note
These classes should never be instantiated directly by the user
-
class
dynamongo.updates.
Update
(field, value=None)[source]¶ Base abstract class for update expressions
-
value
¶ validated value
-
-
class
dynamongo.updates.
SetUpdate
(field, value, if_not_exists=False)[source]¶ Update to set an attribute to the given value