Commits

Max Noel  committed a5ec48a

* Added consistent_read flag to get and query.

  • Participants
  • Parent commits 823da69

Comments (0)

Files changed (2)

File dynamodb_mapper/model.py

         return instance
 
     @classmethod
-    def get(cls, hash_key_value, range_key_value=None):
+    def get(cls, hash_key_value, range_key_value=None, consistent_read=False):
         """Retrieve a single object from DynamoDB according to its primary key.
 
         Note that this is not a query method -- it will only return the object
         matching the exact primary key provided. Meaning that if the table is
         using a composite primary key, you need to specify both the hash and
         range key values.
+
+        :param hash_key_value: The value of the requested item's hash_key.
+
+        :param range_key_value: The value of the requested item's range_key,
+            if the table has a composite key.
+
+        :param consistent_read: If False (default), an eventually consistent
+            read is performed. Set to True for strongly consistent reads.
         """
         table = ConnectionBorg().get_table(cls.__table__)
         return cls.from_dict(
-            table.get_item(hash_key=hash_key_value, range_key=range_key_value))
+            table.get_item(
+                hash_key=hash_key_value,
+                range_key=range_key_value,
+                consistent_read=consistent_read))
 
     @classmethod
     def get_batch(cls, keys):
           - If the primary keys are composite (hash + range), keys must
             be a list of (hash_key, range_key) values
             (e.g. [("user1", 1), ("user1", 2), ("user1", 3)]).
+
+        get_batch *always* performs eventually consistent reads.
         """
         borg = ConnectionBorg()
         table = borg.get_table(cls.__table__)
         ]
 
     @classmethod
-    def query(cls, hash_key_value, range_key_condition=None):
+    def query(cls, hash_key_value, range_key_condition=None, consistent_read=False):
         """Query DynamoDB for items matching the requested key criteria.
 
         You need to supply an exact hash key value, and optionally, conditions
             boto.dynamodb.condition -- one of EQ(x), LE(x), LT(x), GE(x),
             GT(x), BEGINS_WITH(x), BETWEEN(x, y).
 
+        :param consistent_read: If False (default), an eventually consistent
+            read is performed. Set to True for strongly consistent reads.
+
         :rtype: generator
         """
         table = ConnectionBorg().get_table(cls.__table__)
         return (
             cls.from_dict(d)
-            for d in table.query(hash_key_value, range_key_condition)
+            for d in table.query(
+                hash_key_value,
+                range_key_condition,
+                consistent_read=consistent_read)
         )
 
     @classmethod

File dynamodb_mapper/tests/test_model.py

         m_table = m_get_table.return_value
 
         DoomEpisode.get(1)
-        m_table.get_item.assert_called_once_with(hash_key=1, range_key=None)
+        m_table.get_item.assert_called_once_with(hash_key=1, range_key=None, consistent_read=False)
 
     @mock.patch("dynamodb_mapper.model.ConnectionBorg.get_table")
     def test_get_by_composite_key(self, m_get_table):
 
         DoomMap.get(1, "Knee-deep in the dead")
         m_table.get_item.assert_called_once_with(
-            hash_key=1, range_key="Knee-deep in the dead")
+            hash_key=1, range_key="Knee-deep in the dead", consistent_read=False)
 
     @mock.patch("dynamodb_mapper.model.Item")
     @mock.patch("dynamodb_mapper.model.boto")