def store_schema_properties(table: Table, schema: Schema) -> None: properties: List[SchemaPropertyDict] = [ { "name": prop_name, # Special case: treat uids as int "primitive": prop_type.primitive.name if prop_name != "uid" else "Int", "is_set": prop_type.is_set, } for prop_name, prop_type in schema.get_properties().items() ] # Don't send over these edges denylist_edges = ("in_scope", ) edges: List[SchemaPropertyDict] = [ { "name": edge_name, "primitive": edge_tuple[0].dest.self_type(), # Forward edge goes to this type "is_set": edge_tuple[0].is_to_many(), } for edge_name, edge_tuple in schema.forward_edges.items() if edge_name not in denylist_edges ] type_definition: SchemaDict = {"properties": properties + edges} table.put_item( Item={ "node_type": schema.self_type(), # Dynamodb doesn't like my fancy typedict "type_definition": cast(Dict[str, Any], type_definition), "display_property": schema.get_display_property(), })
def write_to_table_if_not_found(self, table: Table) -> bool: try: table.put_item( Item=self.to_dynamodb_item(), ConditionExpression= "attribute_not_exists(PK) AND attribute_not_exists(SK)", ) except ClientError as client_error: # boto3 exception handling https://imgflip.com/i/5f5zfj if (client_error.response.get("Error", {"Code", "Unknown"}).get( "Code", "Unknown") == "ConditionalCheckFailedException"): return False else: raise client_error return True
def store_schema(table: Table, schema: Schema) -> None: for f_edge, (edge_t, r_edge) in schema.get_edges().items(): if not (f_edge and r_edge): LOGGER.warn(f"missing {f_edge} {r_edge} for {schema.self_type()}") continue table.put_item(Item={ "f_edge": f_edge, "r_edge": r_edge, "relationship": int(edge_t.rel), }) table.put_item( Item={ "f_edge": r_edge, "r_edge": f_edge, "relationship": int(edge_t.rel.reverse()), })
def write_to_table_if_not_found(self, table: Table) -> bool: """ Write operations for this object need to be special cased (to avoid overwritting) Therefore we do not implement `to_dynamodb_item` however basically the body of that method is used here Returns false if a content object with that Id is already present and does not write to table. True is write was successful. """ try: table.put_item( Item={ "PK": self.get_dynamodb_content_key(self.content_id), "SK": self.get_dynamodb_content_type_key(), "ContentType": self.content_type.get_name(), "ContentRef": self.content_ref, "ContentRefType": self.content_ref_type.value, "AdditionalFields": self.additional_fields if self.additional_fields else {self.ADDITIONAL_FIELDS_PLACE_HOLDER}, "SubmissionTimes": [s.isoformat() for s in self.submission_times], "CreatedAt": self.created_at.isoformat(), "UpdatedAt": self.updated_at.isoformat(), }, ConditionExpression= "attribute_not_exists(PK) AND attribute_not_exists(SK)", ) except ClientError as client_error: # boto3 exception handling https://imgflip.com/i/5f5zfj if (client_error.response.get("Error", {"Code", "Unknown"}).get( "Code", "Unknown") == "ConditionalCheckFailedException"): return False else: raise client_error return True
def seed_fake_data_if_needed(pac_table: Table): if pac_table.scan()['Count'] != 0: return print("Seeding data") for _ in range(100): price = random.choice([499, 999, 1999]) status = Status.Available funder = '0x1234' signer = random_eth_address() pac_table.put_item( Item={ 'id': signer, # using signer as the unique id 'price': str(price), 'status': status.name, 'price_status': "{}-{}".format(price, status.name), 'funder': funder, 'signer': signer, 'updated': datetime.now().isoformat() })
def write_to_table_if_not_found(self, table: Table) -> bool: """ Write record to DDB if the PK/SK combination does not exist. Returns: * True when record was written (did not exist) * False when record could not be written (PK/SK combo existed) """ try: table.put_item( Item=self.to_dynamodb_item(), ConditionExpression= "attribute_not_exists(PK) AND attribute_not_exists(SK)", ) except ClientError as client_error: # boto3 exception handling https://imgflip.com/i/5f5zfj if (client_error.response.get("Error", {"Code", "Unknown"}).get( "Code", "Unknown") == "ConditionalCheckFailedException"): return False else: raise client_error return True
def put_item(table: Table, id_val: str, target: str) -> None: """Format and put a DDB entry.""" LOGGER.info('Adding entry for "%s"...', id_val) table.put_item(Item={"id": id_val, "target": target}, ReturnValues="NONE")
def put_item(table: Table, wizard: str, house: str) -> None: try: table.put_item(Item={'username': wizard, 'house': house, 'points': 0}) except Exception as e: print("Something went wrong: ", e)
def write_to_table(self, table: Table): table.put_item(Item=self.to_dynamodb_item())