type_bridge.models.base¶
base
¶
Abstract base class for TypeDB entities and relations.
TypeDBType
¶
Bases: BaseModel, ABC
Abstract base class for TypeDB entities and relations.
This class provides common functionality for both Entity and Relation types, including type name management, abstract/base flags, and attribute ownership.
Subclasses must implement: - get_supertype(): Get parent type in TypeDB hierarchy - to_schema_definition(): Generate TypeQL schema definition - to_insert_query(): Generate TypeQL insert query for instances
manager
classmethod
¶
Create a CRUD manager for this type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
connection
|
Connection
|
Database, Transaction, or TransactionContext |
required |
Returns:
| Type | Description |
|---|---|
TypeDBManager[Self]
|
Manager instance for this type |
Source code in type_bridge/models/base.py
insert
¶
Insert this instance into the database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
connection
|
Connection
|
Database, Transaction, or TransactionContext |
required |
Returns:
| Type | Description |
|---|---|
Self
|
Self for chaining |
Source code in type_bridge/models/base.py
delete
¶
Delete this instance from the database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
connection
|
Connection
|
Database, Transaction, or TransactionContext |
required |
Returns:
| Type | Description |
|---|---|
Self
|
Self for chaining |
Source code in type_bridge/models/base.py
has
classmethod
¶
Find all instances of this class (and its subtypes) that own attr_class.
Behaviour depends on the receiver:
Entity.has(...)/Relation.has(...): cross-type lookup — returns instances across all concrete types of that kind.<ConcreteType>.has(...)/<AbstractBase>.has(...): narrowed lookup — restricted to that type and its TypeDB subtypes viaisapolymorphism.
Returned relation instances always have their role players hydrated
(in addition to attributes). This is implemented by re-fetching each
relation through concrete_class.manager(connection).get(_iid=...)
after the initial wildcard query, so the relation path is N+1 in the
number of returned relations. Entity lookups remain single-query.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
connection
|
Connection
|
Database, Transaction, or TransactionContext. |
required |
attr_class
|
type[Attribute]
|
Attribute type to search for (e.g. |
required |
value
|
Any | None
|
Optional filter — raw value, Attribute instance,
or Expression (e.g. |
None
|
Returns:
| Type | Description |
|---|---|
list[TypeDBType]
|
List of hydrated model instances (may contain mixed concrete types |
list[TypeDBType]
|
when called on the base |
list[TypeDBType]
|
abstract base subclass). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If called directly on :class: |
Source code in type_bridge/models/base.py
__init_subclass__
¶
Called when a TypeDBType subclass is created.
Source code in type_bridge/models/base.py
__pydantic_init_subclass__
classmethod
¶
Called by Pydantic after model class initialization.
Injects FieldDescriptor instances for class-level query access. This runs after Pydantic's setup is complete, so descriptors won't be removed.
Example
Person.age # Returns FieldRef for query building (class-level access) person.age # Returns attribute value (instance-level access)
Source code in type_bridge/models/base.py
model_copy
¶
Override model_copy to ensure raw values are wrapped in Attribute instances.
Pydantic's model_copy bypasses validators even with revalidate_instances='always', so we pre-wrap values in the update dict before copying. Also preserves _iid from original using Pydantic's pydantic_private.
Source code in type_bridge/models/base.py
get_type_name
classmethod
¶
Get the TypeDB type name for this type.
If name is explicitly set in TypeFlags, it is used as-is. Otherwise, the class name is formatted according to the case parameter.
Source code in type_bridge/models/base.py
get_supertype
classmethod
¶
Get the supertype from Python inheritance, skipping base classes.
Base classes (with base=True) are Python-only and don't appear in TypeDB schema. This method skips them when determining the TypeDB supertype.
Returns:
| Type | Description |
|---|---|
str | None
|
Type name of the parent class, or None if direct subclass |
Source code in type_bridge/models/base.py
is_abstract
classmethod
¶
is_base
classmethod
¶
get_owned_attributes
classmethod
¶
Get attributes owned directly by this type (not inherited).
Returns:
| Type | Description |
|---|---|
dict[str, ModelAttrInfo]
|
Dictionary mapping field names to ModelAttrInfo (typ + flags) |
Source code in type_bridge/models/base.py
get_all_attributes
classmethod
¶
Get all attributes including inherited ones.
Traverses the class hierarchy to collect all owned attributes, including those from parent Entity/Relation classes.
Returns:
| Type | Description |
|---|---|
dict[str, ModelAttrInfo]
|
Dictionary mapping field names to ModelAttrInfo (typ + flags) |
Source code in type_bridge/models/base.py
get_polymorphic_attributes
classmethod
¶
Get all attributes including those from registered subtypes.
For polymorphic queries where the base class is used but concrete subtypes may be returned, this method collects attributes from all known subtypes so the query can fetch all possible attributes.
Returns:
| Type | Description |
|---|---|
dict[str, ModelAttrInfo]
|
Dictionary mapping field names to ModelAttrInfo, including |
dict[str, ModelAttrInfo]
|
attributes from all registered subtypes. |
Source code in type_bridge/models/base.py
to_schema_definition
abstractmethod
classmethod
¶
Generate TypeQL schema definition for this type.
Returns:
| Type | Description |
|---|---|
str | None
|
TypeQL schema definition string, or None if this is a base class |
get_match_clause_info
abstractmethod
¶
Get information to build a TypeQL match clause for this instance.
Used by TypeDBManager for delete/update operations. Returns IID-based matching when available, otherwise falls back to type-specific identification (key attributes for entities, role players for relations).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
var_name
|
str
|
Variable name to use in the match clause |
'$x'
|
Returns:
| Type | Description |
|---|---|
MatchClauseInfo
|
MatchClauseInfo with main clause, extra clauses, and variable name |
Raises:
| Type | Description |
|---|---|
ValueError
|
If instance cannot be identified (no IID and no keys/role players) |
Source code in type_bridge/models/base.py
to_ast
abstractmethod
¶
Generate AST InsertClause for this instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
var
|
str
|
Variable name to use |
'$x'
|
Returns:
| Type | Description |
|---|---|
Any
|
InsertClause containing statements |
to_insert_query
¶
Generate TypeQL insert query string for this instance.
This is a convenience method that uses the AST-based generation internally and compiles it to a string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
var
|
str
|
Variable name to use (default: "$e") |
'$e'
|
Returns:
| Type | Description |
|---|---|
str
|
TypeQL insert query string |