def delete_relationships( db: PartitionedDatabase, record_id: RecordId, body: JsonDict ) -> Tuple[JsonDict, int]: if len(body) == 0: return [], 200 response = [] # TODO: batch these database calls with db.transaction() as tx: for record_relationship_id in body: try: db.delete_outgoing_record_relationship_tx( tx, record_id, record_relationship_id ) except RecordRelationshipNotFoundError: record_status = 404 except Exception as e: record_status = 500 else: record_status = 200 response.append({"id": record_relationship_id, "status": record_status}) # The 207 response (multi-status) allows parts of a batch request to # fail/succeed individually. If all succeed or all fail with the same # status, use this status for the overall response. Otherwise, return 207. # See https://tools.ietf.org/html/rfc4918#section-13 unique_status = set(r["status"] for r in response) if len(unique_status) == 1: response_status = unique_status.pop() else: response_status = 207 return response, response_status
def create_linked_properties(db: PartitionedDatabase, concept_id: ModelId, id_: RecordId, body: Dict): with db.transaction() as tx: # map of model relationship id -> relationship # allows us to make sure model relationships exist model_relationship_map = { model_relationship.id: model_relationship for model_relationship in db.get_outgoing_model_relationships_tx( tx=tx, from_model=concept_id, one_to_many=False) } # map of model relationship id -> existing record relationship # so we can delete existing relationships such that this behaves as an UPSERT existing_record_relationship_map = { record_relationship.model_relationship_id: record_relationship for record_relationship in db.get_outgoing_record_relationships_tx( tx=tx, from_record=id_, one_to_many=False) } payload: List[Dict[str, str]] = list(body["data"]) # assure each request is or a unique schemaLinkedProperty if len(payload) != len( set(p["schemaLinkedPropertyId"] for p in payload)): raise BadRequest("duplicate model linked properties") to_create: List[CreateRecordRelationship] = [] for item in payload: model_relationship_id = item["schemaLinkedPropertyId"] if model_relationship_id not in model_relationship_map.keys(): raise BadRequest( f"no model linked property exists for {item['schemaLinkedPropertyId']}" ) model_relationship = model_relationship_map[model_relationship_id] if model_relationship_id in existing_record_relationship_map.keys( ): db.delete_outgoing_record_relationship_tx( tx=tx, record=id_, relationship=existing_record_relationship_map[ model_relationship_id], ) to_create.append( CreateRecordRelationship( from_=id_, to=item["to"], model_relationship=model_relationship)) result = db.create_record_relationship_batch_tx(tx=tx, to_create=to_create) data = [to_linked_property(r) for r, _ in result] return {"data": data}
def delete_relationship( db: PartitionedDatabase, record_id: RecordId, relationship_id: RecordRelationshipId) -> RecordRelationshipId: with db.transaction() as tx: return db.delete_outgoing_record_relationship_tx( tx, record_id, relationship_id)
def delete_linked_property( db: PartitionedDatabase, concept_id: ModelId, id_: RecordId, link_id: RecordRelationshipId, ): with db.transaction() as tx: return db.delete_outgoing_record_relationship_tx(tx, id_, link_id)