Skip to main content

typedb-query-builder💪

Description

It'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 queries
  • easy integration with any changes of typedb-client API. As a matter of fact, these changes should be handled by typedb-query-builder, so that you don't have to worry about your code
  • removing unnecessary issues related with typos that appeared during typing verbose queries

Usage

The main compoenent of this packages is typedb_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_name and relationship_name are the actual names of the concepts in schema, while variable_name is 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:

TypeDBEntityQuery and TypeDBRelationshipQuery have 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 by attribute_name, it's actual value by attribute_value. Optionally you can specify a type of the attribute according to it's value in a schema, so that the TypeDBQueryBuilder will be able to create the right query which will be in line with a given schema. If not provided TypeDBQueryBuilder will assume that the attribute is of type string.

Additionally, TypeDBRelationshipQuery has 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 by role argument. thing specifies what concept should be added to a relationship. It has to be already instantiated object of type TypeDBEntityQuery or TypeDBRelationshipQuery.

Example

from typedb_query_builder.typedb_query_builder import TypeDBQueryBuilder

tqb = 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 entities
pi1.relates('associated_protein', p2)
pi1.has('pi_id', 'PI1')                                     # Add an attribute to a relationship

tqb.compile_query()                                         # Compile query
query = tqb.get_query()                                     # Get query

print(query)

Result

match $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";

Metadata

Release files for typedb-query-builder 0.0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for typedb-query-builder 0.0.4
File Size Uploaded Provenance Actions
typedb_query_builder-0.0.4.tar.gz 8.3 kB Without provenance Details for typedb_query_builder-0.0.4.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for typedb-query-builder 0.0.4
File Interpreter ABI Platform Actions
typedb_query_builder-0.0.4-py3-none-any.whl Python 3 none any Details for typedb_query_builder-0.0.4-py3-none-any.whl

Total release size: 16.0 kB

Release files / typedb_query_builder-0.0.4.tar.gz

Download URL typedb_query_builder-0.0.4.tar.gz
Size 8.3 kB
Tags Source
SHA-256 checksum
How to use checksums
fb290ae199990f59804dd3bab9f4cadfa2303884bc92c681b3761837da7ca077
BLAKE2b-256 checksum
How to use checksums
6a497a08015e5efdebbf9f921411ca1a038d360b084721ba99986f18a0c3d6d3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.5

Release files / typedb_query_builder-0.0.4-py3-none-any.whl

Download URL typedb_query_builder-0.0.4-py3-none-any.whl
Size 7.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d12fa3c953cf717dd99c1c864234b4d80a4f3ba032f0edd39127371514ef0270
BLAKE2b-256 checksum
How to use checksums
24eb7bb0fe7c8254c5309129f777c805611f359e18b2e29b88fe4a6b1bbcf2c1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.5

Security for typedb-query-builder 0.0.4

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page