Query engine
Bases: object
Query engine for models
Parameters: | dynamo : boto.dynamodb2.DynamoDBConnection, optional namespace : list, optional
default_conflict : {‘update’, ‘overwrite’, ‘raise’}, optional
|
---|
Notes
The engine is used to save, sync, delete, and query DynamoDB. Here is a basic example of saving items:
item1 = MyModel()
engine.save(item1)
item1.foobar = 'baz'
item2 = MyModel()
engine.save([item1, item2], overwrite=True)
You can also use the engine to query tables:
user = engine.query(User).filter(User.id == 'abcdef).first()
# calling engine() is a shortcut for engine.query()
user = engine(User).filter(User.id == 'abcdef).first()
d_users = engine(User).filter(User.school == 'MIT',
User.name.beginswith_('D')).all()
# You can pass in equality constraints as keyword args
user = engine(User).filter(id='abcdef').first()
Scans are like queries, except that they don’t use an index. Scans iterate over the ENTIRE TABLE so they are REALLY SLOW. Scans have access to additional filter conditions such as “contains” and “in”.
# This is suuuuuper slow!
user = engine.scan(User).filter(id='abcdef').first()
# If you're doing an extremely large scan, you should tell it to return
# a generator
all_users = engine.scan(User).gen()
# to filter a field not specified in the model declaration:
prince = engine.scan(User).filter(User.field_('bio').beginswith_(
'Now this is a story all about how')).first()
Get the default_conflict value
Notes
The default_conflict setting configures the default behavior of save(), sync(), and delete(). Below is an explanation of the different values of default_conflict.
default_conflict | method | default |
---|---|---|
‘update’ | ||
save | overwrite=True | |
sync | raise_on_conflict=True | |
delete | raise_on_conflict=False | |
‘overwrite’ | ||
save | overwrite=True | |
sync | raise_on_conflict=False | |
delete | raise_on_conflict=False | |
‘raise’ | ||
save | overwrite=False | |
sync | raise_on_conflict=True | |
delete | raise_on_conflict=True |
Delete items from dynamo
Parameters: | items : list or Model
raise_on_conflict : bool, optional
|
---|---|
Raises: | exc : boto.dynamodb2.exceptions.ConditionalCheckFailedException
|
Notes
Due to the structure of the AWS API, deleting with raise_on_conflict=False is much faster because the requests can be batched.
Delete one or more items from dynamo as specified by primary keys
Parameters: | model : Model pkeys : list, optional
**kwargs : dict
|
---|---|
Returns: | count : int
|
Notes
If the model being deleted has no range key, you may use strings instead of primary key dicts. ex:
>>> class Item(Model):
... id = Field(hash_key=True)
...
>>> items = engine.delete_key(Item, ['abc', 'def', '123', '456'])
Delete one or more items from dynamo as specified by primary keys
Parameters: | model : Model pkeys : list, optional
**kwargs : dict
|
---|---|
Returns: | count : int
|
Notes
If the model being deleted has no range key, you may use strings instead of primary key dicts. ex:
>>> class Item(Model):
... id = Field(hash_key=True)
...
>>> items = engine.delete_key(Item, ['abc', 'def', '123', '456'])
Fetch one or more items from dynamo from the primary keys
Parameters: | model : Model pkeys : list, optional
consistent : bool, optional
**kwargs : dict
|
---|---|
Returns: | items : list or object
|
Notes
If the model being fetched has no range key, you may use strings instead of primary key dicts. ex:
>>> class Item(Model):
... id = Field(hash_key=True)
...
>>> items = engine.get(Item, ['abc', 'def', '123', '456'])
Overwrite model data with freshest from database
Parameters: | items : list or Model
consistent : bool, optional
|
---|
Register one or more models with the engine
Registering is required for schema creation or deletion
Save models to dynamo
Parameters: | items : list or Model overwrite : bool, optional
|
---|---|
Raises: | exc : boto.dynamodb2.exceptions.ConditionalCheckFailedException
|
Notes
Overwrite will replace the entire item with the new one, not just different fields. After calling save(overwrite=True) you are guaranteed that the item in the database is exactly the item you saved.
Due to the structure of the AWS API, saving with overwrite=True is much faster because the requests can be batched.
Sync model changes back to database
This will push any updates to the database, and ensure that all of the synced items have the most up-to-date data.
Parameters: | items : list or Model
raise_on_conflict : bool, optional
consistent : bool, optional
|
---|---|
Raises: | exc : boto.dynamodb2.exceptions.ConditionalCheckFailedException
|