type_bridge.crud.typedb_manager¶
typedb_manager
¶
Unified TypeDB Manager using AST and Strategies.
This module provides TypeDBManager, a unified CRUD manager that replaces the separate EntityManager and RelationManager with a single generic implementation using the Strategy pattern.
TypeDBManager
¶
Unified CRUD manager for TypeDB entities and relations.
Source code in type_bridge/crud/typedb_manager.py
add_hook
¶
remove_hook
¶
insert
¶
Insert a new instance and populate _iid.
For entities, uses a single roundtrip (insert + fetch combined). For relations, uses two roundtrips (insert, then fetch) because TypeDB 3.x relation inserts don't bind the variable.
Source code in type_bridge/crud/typedb_manager.py
get
¶
Get instances matching filters.
Source code in type_bridge/crud/typedb_manager.py
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 | |
update
¶
Update an instance in the database.
Uses the Strategy pattern to identify the instance, then updates all non-key attributes to match the current state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
instance
|
T
|
Instance with updated values |
required |
Returns:
| Type | Description |
|---|---|
T
|
The updated instance |
Source code in type_bridge/crud/typedb_manager.py
780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 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 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 | |
delete
¶
Delete an instance and return it.
Source code in type_bridge/crud/typedb_manager.py
all
¶
insert_many
¶
Insert multiple instances in a single query (batched).
For entities, combines all inserts into a single query for efficiency. For relations, falls back to individual inserts (due to match clause complexity).
Source code in type_bridge/crud/typedb_manager.py
put
¶
Insert or update an instance (idempotent) and populate _iid.
Uses TypeQL's PUT clause for idempotent insertion. For entities, uses a single roundtrip. For relations, uses two.
Source code in type_bridge/crud/typedb_manager.py
delete_many
¶
Delete multiple instances.
Optimized for batch deletion: instances with IIDs are deleted in a single query using disjunctive matching (OR pattern), reducing N roundtrips to 1.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
instances
|
list[T]
|
List of instances to delete |
required |
strict
|
bool
|
If True, raise EntityNotFoundError if any entity doesn't exist. In strict mode, checks all entities first and raises before any deletion. |
False
|
Returns:
| Type | Description |
|---|---|
list[T]
|
List of actually-deleted entities (excludes those that didn't exist) |
Source code in type_bridge/crud/typedb_manager.py
1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 | |
put_many
¶
Put multiple instances (idempotent insert/update).
Attempts batch operation first for efficiency. If a key constraint violation occurs (some entities exist with different data), falls back to individual operations which are idempotent.
For entities, uses batch PUT when possible (N→1 roundtrips). For relations, uses individual operations (match clause complexity).
Source code in type_bridge/crud/typedb_manager.py
update_many
¶
get_by_iid
¶
Fetch an instance by its internal ID with polymorphic type resolution.
Source code in type_bridge/crud/typedb_manager.py
1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 | |
filter
¶
Create a chainable query with filters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*expressions
|
Any
|
Expression objects (Person.age.gt(Age(30)), etc.) |
()
|
**filters
|
Any
|
Attribute filters (exact match) - age=30, name="Alice" |
{}
|
Returns:
| Type | Description |
|---|---|
TypeDBQuery[T]
|
TypeDBQuery for chaining |
Raises:
| Type | Description |
|---|---|
ValueError
|
If expressions reference attribute types not owned by the model |
Source code in type_bridge/crud/typedb_manager.py
count
¶
Count all instances of this type, optionally filtering.
Source code in type_bridge/crud/typedb_manager.py
group_by
¶
Group results by field values and compute aggregations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*fields
|
Any
|
Field descriptors to group by (e.g., Person.department) |
()
|
Returns:
| Type | Description |
|---|---|
GroupByQuery[T]
|
GroupByQuery for chained aggregations |
Example
Group by department, compute average age per department¶
result = manager.group_by(Person.department).aggregate(Person.age.avg())
Returns: {¶
"Engineering": {"avg_age": 35.5},¶
"Sales":¶
}¶
Source code in type_bridge/crud/typedb_manager.py
TypeDBQuery
¶
Chainable query builder for TypeDBManager.
Source code in type_bridge/crud/typedb_manager.py
filter
¶
Add additional filters to the query.
Validates that expression attribute types are owned by the model class.
Source code in type_bridge/crud/typedb_manager.py
order_by
¶
Order results by fields. Prefix with '-' for descending.
Source code in type_bridge/crud/typedb_manager.py
limit
¶
offset
¶
execute
¶
Execute the query and return results.
Source code in type_bridge/crud/typedb_manager.py
1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 | |
all
¶
first
¶
Get the first matching instance.
Source code in type_bridge/crud/typedb_manager.py
count
¶
Count matching instances.
Note: For relation queries with role player filters, TypeDB 3.x's reduce count may not work as expected. In those cases, we fall back to fetching IIDs and counting unique results.
Source code in type_bridge/crud/typedb_manager.py
delete
¶
Delete all matching instances and return count.
exists
¶
update_with
¶
Update instances by applying a function to each.
Uses atomic transaction semantics: if the function fails on any entity, no updates are persisted (all or nothing).
Note: _iid is preserved during attribute modification by the wrap validator in TypeDBType base class.
Source code in type_bridge/crud/typedb_manager.py
aggregate
¶
Execute aggregation queries.
Performs database-side aggregations for efficiency.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*aggregates
|
Any
|
AggregateExpr objects (Person.age.avg(), Person.score.sum(), etc.) |
()
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary mapping aggregate keys to results |
Examples:
Single aggregation¶
result = manager.filter().aggregate(Person.age.avg()) avg_age = result['avg_age']
Multiple aggregations¶
result = manager.filter(Person.city.eq(City("NYC"))).aggregate( Person.age.avg(), Person.score.sum(), Person.salary.max() )
Source code in type_bridge/crud/typedb_manager.py
1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 | |
group_by
¶
Group results by field values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*fields
|
Any
|
FieldRef objects or field descriptors to group by (e.g., Person.department) |
()
|
Returns:
| Type | Description |
|---|---|
GroupByQuery[T]
|
GroupByQuery for chained aggregations |
Example
result = manager.group_by(Person.department).aggregate(Person.age.avg())
Source code in type_bridge/crud/typedb_manager.py
GroupByQuery
¶
Query for grouped aggregations.
Allows grouping entities by field values and computing aggregations per group.
Initialize grouped query.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
manager
|
TypeDBManager[T]
|
TypeDBManager instance |
required |
filters
|
dict[str, Any]
|
Dict-based filters |
required |
expressions
|
list[Any]
|
Expression-based filters |
required |
group_fields
|
tuple[Any, ...]
|
Fields to group by (FieldRef or field descriptors) |
required |
Source code in type_bridge/crud/typedb_manager.py
aggregate
¶
Execute grouped aggregation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*aggregates
|
Any
|
AggregateExpr objects |
()
|
Returns:
| Type | Description |
|---|---|
dict[Any, dict[str, Any]]
|
Dictionary mapping group values to aggregation results |
Example
Group by department, compute average age per department¶
result = manager.group_by(Person.department).aggregate(Person.age.avg())
Returns: {¶
"Engineering": {"avg_age": 35.5},¶
"Sales":¶
}¶
Source code in type_bridge/crud/typedb_manager.py
2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 | |