예제 #1
0
def create_concept_instance_relationships(
    db: PartitionedDatabase,
    relationship_id_or_name: Union[ModelRelationshipId, str],
    body: List[JsonDict],
) -> List[RelationshipInstancePair]:

    relationship_to_create_schema = CreateModelRelationship.schema()
    to_create = []

    for row in body:
        if row.get("relationshipToCreate"):
            model_relationship_to_create = relationship_to_create_schema.load(
                row["relationshipToCreate"])
        else:
            model_relationship_to_create = None

        to_create.append(
            CreateRecordRelationship(
                from_=row["from"],
                to=row["to"],
                model_relationship_to_create=model_relationship_to_create,
            ))

    with db.transaction() as tx:
        rels = db.create_legacy_record_relationship_batch_tx(
            tx=tx, to_create=to_create, relation=relationship_id_or_name)
    return [(
        to_legacy_relationship_instance(record_relationship),
        to_legacy_relationship(model_relationship),
    ) for record_relationship, model_relationship in rels]
예제 #2
0
def create_concept_instance_relationship(
    db: PartitionedDatabase,
    relationship_id_or_name: Union[ModelRelationshipId, str],
    body: JsonDict,
) -> Tuple[RelationshipInstancePair, int]:
    relationship_to_create_schema = CreateModelRelationship.schema()
    model_relationship_to_create: Optional[CreateModelRelationship] = None

    if body.get("relationshipToCreate"):
        model_relationship_to_create = relationship_to_create_schema.load(
            row["relationshipToCreate"])

    to_create = CreateRecordRelationship(
        from_=body.get("from"),
        to=body.get("to"),
        model_relationship_to_create=model_relationship_to_create,
    )

    with db.transaction() as tx:
        rels = db.create_legacy_record_relationship_batch_tx(
            tx=tx, to_create=[to_create], relation=relationship_id_or_name)

        return (
            (
                to_legacy_relationship_instance(rels[0][0]),
                to_legacy_relationship(rels[0][1]),
            ),
            200,
        )