TypeDB V2 store¶
Overview¶
The TypeDB V2 store allows you to store nodes using the Abstract store API. It maps canonical Node objects onto a TypeDB schema, providing graph database capabilities with strong typing and complex relationship modeling.
Configuration¶
Configuration of the typedb store happens using a dict containing an url, database and schema_path.
config = {
"uri": "localhost:1729",
"database": "knowledge_base",
"schema_path": "path/to/schema.tql" # Optional
}
Filter Logic and Practical Examples¶
The TypeDB V2 store uses a URL-query style filter syntax for retrieving and deleting nodes. This section explains how filters work with practical examples.
Filter Syntax¶
The basic filter format follows URL query string conventions:
entity=<entity_type>&<attribute>=<value>&<attribute>=<value>&include=relations
Practical Examples¶
Example 1: Single Attribute Filter¶
# Find a person by email
filter = "entity=person&email=john.doe@example.com"
nodes = store.get_nodes(filter)
# Find documents by title
filter = "entity=document&title=Research Paper 2024"
nodes = store.get_nodes(filter)
Example 2: Multiple Attribute Filters¶
# Find a person by email AND department
filter = "entity=person&email=jane@company.com&department=Engineering"
nodes = store.get_nodes(filter)
# Find documents by author AND status
filter = "entity=document&author=Dr. Smith&status=published"
nodes = store.get_nodes(filter)
Example 3: Including Relations¶
# Get person with all their relations loaded
filter = "entity=person&email=john@example.com&include=relations"
nodes = store.get_nodes(filter)
# The returned node will have its relations populated:
# node.relations will contain RelationData objects
for node in nodes:
print(f"Found {len(node.relations)} relations for {node.id}")
for rel in node.relations:
print(f" - {rel['type']} relation with roles: {rel['roles']}")
Example 4: Retrieving All Nodes¶
# Get all nodes in the database (use with caution on large datasets)
nodes = store.get_nodes(None)
# This is equivalent to:
nodes = store.get_nodes(filter=None)
Filter Behavior and Edge Cases¶
Empty vs None Filter¶
# None filter returns ALL nodes
nodes = store.get_nodes(None) # Returns everything
# Empty string filter raises ValueError
nodes = store.get_nodes("") # Raises ValueError: filter cannot be empty
Case Sensitivity¶
# Filters are case-sensitive
filter1 = "entity=person&name=John Doe"
filter2 = "entity=person&name=john doe"
# These will return different results
Special Characters in Values¶
# Values with special characters are handled automatically
filter = "entity=person&email=user+tag@example.com"
# The + character is preserved correctly
Deletion Filters¶
The same filter syntax applies to remove_nodes():
Safe Deletion (Single Entity)¶
# Delete a specific person
filter = "entity=person&email=john@example.com"
deleted_count = store.remove_nodes(filter)
Bulk Deletion (Requires Permission)¶
# Delete all draft documents (requires allow_multiple=True)
filter = "entity=document&status=draft"
deleted_count = store.remove_nodes(filter, allow_multiple=True)
# Attempting bulk deletion without permission raises ValueError
filter = "entity=document" # No attribute filter
deleted_count = store.remove_nodes(filter) # Raises ValueError
Docstring¶
typedb_store
¶
Classes:
-
EagerQueryAnswer–Eagerly evaluates a TypeDB QueryAnswer to prevent 'concurrent transaction close' errors
-
TypeDbDatastore–TypeDB-backed implementation of the AbstractStore knowledge persistence layer.
EagerQueryAnswer
¶
EagerQueryAnswer(answer: QueryAnswer)
Eagerly evaluates a TypeDB QueryAnswer to prevent 'concurrent transaction close' errors when evaluating iterator wrappers outside of the transaction block.
Source code in src/database_builder_libs/stores/typedb/typedb_store.py
38 39 40 41 42 43 44 45 46 | |
TypeDbDatastore
¶
TypeDbDatastore()
flowchart TD
database_builder_libs.stores.typedb.typedb_store.TypeDbDatastore[TypeDbDatastore]
database_builder_libs.models.abstract_store.AbstractStore[AbstractStore]
database_builder_libs.models.abstract_store.AbstractStore --> database_builder_libs.stores.typedb.typedb_store.TypeDbDatastore
click database_builder_libs.stores.typedb.typedb_store.TypeDbDatastore href "" "database_builder_libs.stores.typedb.typedb_store.TypeDbDatastore"
click database_builder_libs.models.abstract_store.AbstractStore href "" "database_builder_libs.models.abstract_store.AbstractStore"
TypeDB-backed implementation of the AbstractStore knowledge persistence layer.
This adapter maps canonical Node objects onto a TypeDB schema.
Mapping rules¶
Node → TypeDB Entity node.entity_type → entity type node.id → key_attribute value node.payload_data → attributes node.relations → relations
Identity semantics¶
A Node is uniquely identified by id.
Storing the same node twice MUST NOT create duplicates. The adapter performs existence checks before insertion.
Filter language¶
All read and delete operations use a URL-query style filter string:
"entity=<type>&<attribute>=<value>&<attribute>=<value>"
Examples entity=person&email=a@b.com entity=document&title=Report.pdf
Behaviour¶
- get_nodes() returns normalized Node objects reconstructed from TypeDB
- store_node() performs upsert semantics
- remove_node() deletes exactly one node (safety enforced)
- remove_nodes() allows bulk deletion when explicitly enabled
Safety guarantees¶
- Refuses full-entity deletion without explicit permission
- Deduplicates overlapping attribute matches
- Ensures relations are not duplicated
- Schema is automatically applied at initialization
Transactions¶
Each operation runs in its own transaction. Writes are committed automatically when successful.
Notes¶
This adapter assumes the schema defines a key attribute for each entity type.
Methods:
-
connect–Establish connection to the backend.
-
get_nodes–Retrieve nodes using a filter query.
-
remove_node–Delete exactly one node and return it.
-
remove_nodes–Delete nodes matching the filter.
-
store_node–Insert a Node into TypeDB if it does not already exist, and ensure its relations exist.
Source code in src/database_builder_libs/stores/typedb/typedb_store.py
126 127 128 129 130 131 132 | |
connect
¶
connect(config: dict | None = None) -> None
Establish connection to the backend.
This method is idempotent. Calling it multiple times must be safe.
Parameters¶
config : Any | None Backend-specific configuration object.
Raises¶
ConnectionError Backend unreachable. RuntimeError Backend misconfigured.
Source code in src/database_builder_libs/models/abstract_store.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | |
get_nodes
¶
Retrieve nodes using a filter query.
Filter syntax¶
URL query string:
entity=<entity_type>&<attr>=<value>&...
Example entity=person&email=john@doe.com
Behaviour¶
- Returns normalized Node objects
- Deduplicates overlapping matches
- Automatically infers key_attribute from schema
Raises¶
TypeError If filter is not a string ValueError If filter missing required entity parameter
Source code in src/database_builder_libs/stores/typedb/typedb_store.py
830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 | |
remove_node
¶
Delete exactly one node and return it.
Raises¶
ValueError No match or multiple matches RuntimeError Deletion inconsistency detected
Source code in src/database_builder_libs/stores/typedb/typedb_store.py
942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 | |
remove_nodes
¶
Delete nodes matching the filter.
Safety rules¶
- Refuses deleting entire entity type unless allow_multiple=True
- Requires at least one attribute filter by default
Returns¶
int Number of deleted nodes
This operation is irreversible.
Source code in src/database_builder_libs/stores/typedb/typedb_store.py
892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 | |
store_node
¶
store_node(node: Node) -> None
Insert a Node into TypeDB if it does not already exist, and ensure its relations exist.
Behaviour¶
- Creates the entity if it does not exist
- Leaves existing entity attributes unchanged (no merge or update of payload_data)
- Inserts missing relations only
- Operation is idempotent
Identity rule¶
A node is considered existing if an entity with (entity_type, key_attribute, id) exists.
Relations are inserted only if not already present.
Source code in src/database_builder_libs/stores/typedb/typedb_store.py
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 | |