Last active
March 10, 2021 09:24
-
-
Save thanakijwanavit/520294305d03cd41983fb2a5b9e482d3 to your computer and use it in GitHub Desktop.
pynamo saving override
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Order2(Model): | |
class Meta: | |
table_name = ORDER_TABLE | |
region = 'ap-southeast-1' | |
dax_read_endpoints = ['longtermcluster.vuu7lr.clustercfg.dax.apse1.cache.amazonaws.com:8111'] | |
dax_write_endpoints = ['longtermcluster.vuu7lr.clustercfg.dax.apse1.cache.amazonaws.com:8111'] | |
## keys | |
ownerId=UnicodeAttribute() | |
basketId=UnicodeAttribute() | |
## hash key | |
orderId =UnicodeAttribute(hash_key = True) | |
data = SchemaAttribute(schemaUrl=ORDERURL,path='/',null=True) | |
# timestamps | |
creationTime= NumberAttribute() | |
lastEdited=NumberAttribute() | |
#### indexes##### | |
ownerIdIndex=OwnerIdIndex() | |
basketIdIndex=BasketIdIndex() | |
##### default functions #### | |
def __repr__(self): | |
return yaml.dump(self.to_dict()) | |
#### saving #### | |
def pullOutKeys(self): | |
'''update the keys with data''' | |
self.orderId = self.data['orderId'] | |
self.ownerId = self.data['ownerId'] | |
self.basketId = self.data['basketId'] | |
def recordTime(self): | |
'''record last edited and creation time''' | |
self.lastEdited = datetime.now().timestamp() # record last edited | |
if not self.creationTime: # record creation time | |
self.creationTime = datetime.now().timestamp() | |
def save(self): | |
self.recordTime() | |
self.pullOutKeys() | |
try: | |
super().save() | |
return next(self.query(self.orderId)) | |
except ValidationError as e: | |
raise ValidationError(f'failed validation \n {e}') | |
except Exception as e: | |
raise Exception(f'error saving id {self.orderId} {e}') | |
##### other functions ### | |
def to_dict(self): | |
return self.data | |
@classmethod | |
def from_dict(cls, inputDict:dict): | |
return cls(data = inputDict) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment