Skip to content

Instantly share code, notes, and snippets.

@thanakijwanavit
Created July 8, 2021 06:43
Show Gist options
  • Save thanakijwanavit/c46d27869a4751d9137c13249170a07f to your computer and use it in GitHub Desktop.
Save thanakijwanavit/c46d27869a4751d9137c13249170a07f to your computer and use it in GitHub Desktop.
for query the order2 table
class OwnerIdIndex(GlobalSecondaryIndex):
class Meta:
index_name = 'ownerId'
read_capacity_units=1
write_capacity_units=1
projection = AllProjection()
ownerId=UnicodeAttribute(hash_key=True)
creationTime=NumberAttribute(range_key=True)
class BasketIdIndex(GlobalSecondaryIndex):
class Meta:
index_name = 'basketId'
read_capacity_units=1
write_capacity_units=1
projection = AllProjection()
basketId=UnicodeAttribute(hash_key=True)
creationTime=NumberAttribute(range_key=True)
class BranchIdIndex(GlobalSecondaryIndex):
class Meta:
index_name = 'branchId'
read_capacity_units=1
write_capacity_units=1
projection = AllProjection()
branchId=UnicodeAttribute(hash_key=True)
creationTime=NumberAttribute(range_key=True)
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()
branchId=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()
branchIdIndex=BranchIdIndex()
##### 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']
self.branchId = self.data.get('branchId') or ''
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