Skip to main content

A modern, Pythonic ORM for TypeDB with an Attribute-based API

Project description

TypeBridge

A modern, Pythonic ORM for TypeDB with an Attribute-based API that aligns with TypeDB's type system.

Features

  • True TypeDB Semantics: Attributes are independent types that entities and relations own
  • Flag System: Clean API for @key, @unique, and @card annotations
  • Flexible Cardinality: Express any cardinality constraint with Card(min, max)
  • Pydantic Integration: Built on Pydantic v2 for automatic validation, serialization, and type safety
  • Type-Safe: Full Python type hints and IDE autocomplete support
  • Declarative Models: Define entities and relations using Python classes
  • Automatic Schema Generation: Generate TypeQL schemas from your Python models
  • Data Validation: Automatic type checking and coercion via Pydantic
  • JSON Support: Seamless JSON serialization/deserialization
  • CRUD Operations: Simple managers for entity and relation operations
  • Query Builder: Pythonic interface for building TypeQL queries

Installation

# Clone the repository
git clone https://github.com/yourusername/type_bridge.git
cd type_bridge

# Install with uv
uv sync

# Or with pip
pip install -e .

Quick Start

1. Define Attribute Types

from type_bridge import String, Integer

class Name(String):
    pass

class Age(Integer):
    pass

2. Define Entities

from type_bridge import Entity, EntityFlags, Flag, Key, Card

class Person(Entity):
    flags = EntityFlags(type_name="person")  # Optional, defaults to lowercase class name

    # Use Flag() for key/unique markers and Card for cardinality
    name: Name = Flag(Key)                   # @key (implies @card(1..1))
    age: Age | None                          # @card(0..1) - optional field
    email: Email                             # @card(1..1) - default cardinality
    tags: list[Tag] = Flag(Card(min=2))      # @card(2..) - two or more

3. Create Instances

# Create entity instances with attribute values
alice = Person(
    name=Name("Alice"),
    age=Age(30),
    email=Email("alice@example.com")
)

# Pydantic handles validation and type coercion automatically
print(alice.name.value)  # "Alice"

4. Work with Data

from type_bridge import Database, SchemaManager

# Connect to database
db = Database(address="localhost:1729", database="mydb")
db.connect()
db.create_database()

# Define schema
schema_manager = SchemaManager(db)
schema_manager.register(Person, Company, Employment)
schema_manager.sync_schema()

# Insert entities
alice = Person.manager(db).insert(
    name="Alice",
    age=30,
    email="alice@example.com"
)

# Insert relations
employment = Employment.manager(db).insert(
    role_players={"employee": alice, "employer": techcorp},
    attributes={"position": "Engineer", "salary": 100000}
)

5. Cardinality Constraints

from type_bridge import Card, Flag

class Person(Entity):
    flags = EntityFlags(type_name="person")

    # Cardinality options:
    name: Name                              # @card(1..1) - exactly one (default)
    age: Age | None                         # @card(0..1) - zero or one
    tags: list[Tag] = Flag(Card(min=2))     # @card(2..) - two or more (unbounded)
    skills: list[Skill] = Flag(Card(max=5)) # @card(0..5) - zero to five
    jobs: list[Job] = Flag(Card(1, 3))      # @card(1..3) - one to three

6. Define Relations

from type_bridge import Relation, RelationFlags, Role

class Employment(Relation):
    flags = RelationFlags(type_name="employment")

    # Define roles with type-safe Role[T] syntax
    employee: Role[Person] = Role("employee", Person)
    employer: Role[Company] = Role("employer", Company)

    # Relations can own attributes
    position: Position                   # @card(1..1)
    salary: Salary | None                # @card(0..1)

7. Using Python Inheritance

class Animal(Entity):
    flags = EntityFlags(abstract=True)  # Abstract entity
    name: Name

class Dog(Animal):  # Automatically: dog sub animal in TypeDB
    breed: Breed

Documentation

See ATTRIBUTE_API.md for complete documentation.

Pydantic Integration

TypeBridge is built on Pydantic v2, giving you powerful features:

class Person(Entity):
    flags = EntityFlags(type_name="person")
    name: Name = Flag(Key)
    age: Age

# Automatic validation and type coercion
alice = Person(name=Name("Alice"), age=Age(30))

# JSON serialization
json_data = alice.model_dump_json()

# JSON deserialization
bob = Person.model_validate_json('{"name": "Bob", "age": 25}')

# Model copying
alice_copy = alice.model_copy(update={"age": Age(31)})

Running Examples

uv run python examples/basic_usage.py
uv run python examples/pydantic_features.py

Running Tests

uv run pytest tests/ -v

Requirements

  • Python 3.13+
  • TypeDB 2.x or 3.x
  • typedb-driver==3.5.5

License

MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

type_bridge-0.1.6.tar.gz (66.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

type_bridge-0.1.6-py3-none-any.whl (39.3 kB view details)

Uploaded Python 3

File details

Details for the file type_bridge-0.1.6.tar.gz.

File metadata

  • Download URL: type_bridge-0.1.6.tar.gz
  • Upload date:
  • Size: 66.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for type_bridge-0.1.6.tar.gz
Algorithm Hash digest
SHA256 73d7539d6058af3c5628ee3747744797cb38b8a9b37fd65e3a00ceb48974d3b3
MD5 9f98581b1e35b05a73936cad586e71ba
BLAKE2b-256 6a2a3fa9cca0831fa141bf3d45fb7dcdb2f83608d8dc50dbdc344c5a81adcff9

See more details on using hashes here.

File details

Details for the file type_bridge-0.1.6-py3-none-any.whl.

File metadata

  • Download URL: type_bridge-0.1.6-py3-none-any.whl
  • Upload date:
  • Size: 39.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for type_bridge-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 dacd2e7da5f9f9f46b696161339dbf3406eda1ed86c8e29610a45dd111cd40be
MD5 a79147a72dcf52395e648a1b298a331f
BLAKE2b-256 efec3df1b7a0696350405f720d84b0f1dec5d94d032dfe98c35636cbc1d387d6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page