Package: typedb-query-builder
typedb-query-builder💪DescriptionIt's a simple and lightweight python library for creating TypeDB queries in an Object-Oriented manner, which abstracts string preprocessing from the user.The main advantages of this solutions are:removing boilerplate repetitive code and string preprocessing with very readable API for building TypeDB querieseasy integration with any changes oftypedb-clientAPI. As a matter of fact, these changes should be handled bytypedb-query-builder, so that you don't have to worry about your coderemoving unnecessary issues related with typos that appeared during typing verbose queriesUsageThe main compoenent of this packages istypedb_query_builder.typedb_query_builder.TypeDBQueryBuilder.It provides you the following methods:match_entity(entity_name: str, variable_name: str)- used for adding and an entity in match queries.match_relationship(relationship_name: str, variable_name: str))- used for adding and a relationship in match queries.insert_entity(entity_name: str, variable_name: str)- used for adding and entity in insert queries.insert_relationship(relationship_name: str, variable_name: str))- used for adding a relationship in match queries.entity_nameandrelationship_nameare the actual names of the concepts in schema, whilevariable_nameis just a variable that you want to assign to it - in the same way as you would assign a concept to a variable in a TypeDB query (note that within one query, one TypeDBQueryBuilder instance, the same variable name cannot be used for several concepts).Each of these functions return an object of one of the following types:TypeDBEntityQuery,TypeDBRelationshipQuery. You can use these object to assign certain information to them, specifically:TypeDBEntityQueryandTypeDBRelationshipQueryhave methods:has(attribute_name: str, attribute_value: str, attribute_type: str = 'string')- which assign an attribute to an Entity or Relationship. The attribute type is provided byattribute_name, it's actual value byattribute_value. Optionally you can specify a type of the attribute according to it's value in a schema, so that theTypeDBQueryBuilderwill be able to create the right query which will be in line with a given schema. If not providedTypeDBQueryBuilderwill assume that the attribute is of typestring.Additionally,TypeDBRelationshipQueryhas method for adding related objects:relates(role: str, thing: Union[TypeDBEntityQuery, "TypeDBRelationshipQuery"])- which allows you to add a Thing (Entity/Relathinship) to a given relation under a certain role provided byroleargument.thingspecifies what concept should be added to a relationship. It has to be already instantiated object of typeTypeDBEntityQueryorTypeDBRelationshipQuery.Examplefromtypedb_query_builder.typedb_query_builderimportTypeDBQueryBuildertqb=TypeDBQueryBuilder()p1=tqb.match_entity('protein','p1')# Add an entity of type 'protein' to a match statement with a variable 'p1'.p1.has('protein_name','ACE')# Assign an attribute of type 'protein_name' with value 'ACE'p1.has('protein_id','Q1')p2=tqb.match_entity('protein','p2')# Add a second entity of type 'protein' to a match statement with a variable 'p2'.p2.has('protein_name','ACE2')# Assign an attribute of type 'protein_name' with value 'ACE2'p2.has('protein_id','Q2')p2.has('external-id',1,'double')# This entity has an attribute 'external-id' which is of type double.pi1=tqb.insert_relationship('protein_interaction','pi')# Add a relationship of type 'protein_interaction' to insert query.pi1.relates('associated_protein',p1)# Add related entitiespi1.relates('associated_protein',p2)pi1.has('pi_id','PI1')# Add an attribute to a relationshiptqb.compile_query()# Compile queryquery=tqb.get_query()# Get queryprint(query)Resultmatch $p1 isa protein, has protein_name "ACE", has protein_id "Q1"; $p2 isa protein, has protein_name "ACE2", has protein_id "Q2", has external-id 1; insert $pi (associated_protein: $p1, associated_protein: $p2) isa protein_interaction, has pi_id "PI1";