mgraph-db


Namemgraph-db JSON
Version 1.1.9 PyPI version JSON
download
home_pageNone
SummaryMGraph-DB
upload_time2025-02-25 02:06:11
maintainerNone
docs_urlNone
authorDinis Cruz
requires_python<4.0,>=3.11
licenseApache 2.0
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # MGraph-DB: A Memory-based GraphDB for Python, GenAI, Semantic Web and Serverless

![Current Release](https://img.shields.io/badge/release-v1.1.9-blue)

> **QUICKSTART GUIDE**: For working code examples, check out our test suite at:
> ```
> tests/quickstart/test_README_examples.py
> ```
> This file contains executable versions of all examples shown below, providing a hands-on way to learn the library.

MGraph-DB is a high-performance, type-safe graph database implementation in Python, optimized for in-memory operations with JSON persistence. Its architecture makes it particularly well-suited for:

- **GenAI Applications**
  - Knowledge graph construction and querying
  - Semantic relationship modeling
  - Context management for LLMs
  - Graph-based reasoning systems

- **Semantic Web**
  - RDF data processing
  - Ontology management
  - Linked data applications
  - SPARQL-compatible queries

- **Serverless Deployments**
  - Quick cold starts with memory-first design
  - Efficient JSON serialization
  - Low memory footprint
  - Built-in serverless support via [MGraph_DB_Serverless](https://github.com/owasp-sbot/MGraph-DB-Serverless)

- **Python Ecosystem**
  - Native Python implementation
  - Full type safety and validation
  - Clean, Pythonic API
  - Rich integration capabilities

## Major Features

### Production-Ready Type System
- Complete implementation of the three-layer architecture (Domain, Model, Schema)
- Comprehensive runtime type checking across all layers
- Type-safe property accessors and method decorators
- Robust validation for nested data structures
- Clean class hierarchies with explicit interfaces

### Advanced Graph Operations
- High-performance in-memory graph operations
- Sophisticated query system with chainable operations
- Rich traversal capabilities with type filtering
- Flexible node and edge attribute management
- Comprehensive CRUD operations for graph elements

### Optimized Indexing System
- O(1) lookups for all core operations
- Multi-dimensional indexing (type, attribute, relationship)
- Efficient graph traversal support
- Advanced query optimization
- Index persistence and restoration

### Query System Enhancements
- View-based query results with navigation
- Rich filtering and traversal operations
- Chainable query interface
- Query result caching
- Query operation history tracking

### Export Capabilities
- Support for multiple export formats:
  - GraphML
  - DOT
  - Mermaid
  - RDF/Turtle
  - N-Triples
  - GEXF
  - TGF
  - Cypher
  - CSV
  - JSON

### Visualization Support
- Integration with common visualization libraries
- Custom layout algorithms
- Interactive graph exploration
- Support for large graph visualization
- Multiple visualization format exports

## Quick Start

Here's a simple example showing basic graph operations:

```python
def test_basic_graph_operations():
    from mgraph_db.mgraph.MGraph import MGraph
    
    # Create a new graph
    mgraph = MGraph()
    
    with mgraph.edit() as edit:
        # Create two nodes
        node1 = edit.new_node(node_data={"value": "First Node"})
        node2 = edit.new_node(node_data={"value": "Second Node"})
        
        # Connect nodes with an edge
        edge = edit.new_edge(from_node_id=node1.node_id, 
                            to_node_id=node2.node_id)
        
        # Verify nodes and edge were created
        assert node1.node_id is not None
        assert node2.node_id is not None
        assert edge.edge_id is not None
    
    # Query the graph
    with mgraph.data() as data:
        nodes = data.nodes()
        edges = data.edges()
        
        assert len(nodes) == 2
        assert len(edges) == 1
```

## Use Cases

### GenAI Integration

```python
def test_genai_integration():
    from mgraph_db.mgraph.MGraph import MGraph
    
    mgraph = MGraph()
    with mgraph.edit() as edit:
        # Create a knowledge graph for LLM context
        context = edit.new_node(node_data={"type": "context", 
                                         "value": "user query"})
        entity = edit.new_node(node_data={"type": "entity", 
                                        "value": "named entity"})
        relation = edit.new_edge(from_node_id=context.node_id,
                                to_node_id=entity.node_id,
                                edge_data={"type": "contains"})
        
        # Verify the knowledge graph
        assert context.node_data.value == "user query"
        assert entity.node_data.value == "named entity"
        assert relation.edge_data.type == "contains"
```

### Semantic Web Applications

```python
def test_semantic_web():
    from mgraph_db.mgraph.MGraph import MGraph
    
    mgraph = MGraph()
    with mgraph.edit() as edit:
        # Create RDF-style triples
        subject = edit.new_node(node_data={"uri": "http://example.org/subject"})
        object = edit.new_node(node_data={"uri": "http://example.org/object"})
        predicate = edit.new_edge(from_node_id=subject.node_id,
                                 to_node_id=object.node_id,
                                 edge_data={"predicate": "relates_to"})
        
        # Verify triple structure
        assert subject.node_data.uri == "http://example.org/subject"
        assert object.node_data.uri == "http://example.org/object"
        assert predicate.edge_data.predicate == "relates_to"
```

## Advanced Usage

### Type-Safe Operations

```python
def test_type_safe_operations():
    from mgraph_db.mgraph.MGraph import MGraph
    from mgraph_db.mgraph.schemas.Schema__MGraph__Node import Schema__MGraph__Node
    from mgraph_db.mgraph.schemas.Schema__MGraph__Node__Data import Schema__MGraph__Node__Data
    
    # Custom node data with runtime type checking
    class Custom_Node_Data(Schema__MGraph__Node__Data):
        name: str
        value: int
        priority: float
    
    # Type-safe node definition
    class Custom_Node(Schema__MGraph__Node):
        node_data: Custom_Node_Data
    
    mgraph = MGraph()
    with mgraph.edit() as edit:
        # Create typed node
        node = edit.new_node(node_type=Custom_Node,
                            name="test",
                            value=42,
                            priority=1.5)
        
        # Verify type safety
        assert node.node_data.name == "test"
        assert node.node_data.value == 42
        assert node.node_data.priority == 1.5
        
        # Demonstrate type-safe processing
        result = node.node_data.priority * 2.0
        assert result == 3.0
```

### Using the Index System

```python
def test_index_system():
    from mgraph_db.mgraph.MGraph import MGraph
    
    mgraph = MGraph()
    with mgraph.edit() as edit:
        # Create nodes
        node1 = edit.new_node(node_type=Custom_Node,
                             name="test",
                             value=42,
                             priority=1.5)
        
        # Access index
        with edit.index() as index:
            # Get nodes by type
            nodes = index.get_nodes_by_type(Custom_Node)
            assert len(nodes) == 1
            
            # Get by relationship
            nodes_by_type = index.get_nodes_by_type(Custom_Node)
            assert node1.node_id in nodes_by_type
```

### Graph Export and Visualization

```python
def test_export_and_visualization():
    from mgraph_db.mgraph.MGraph import MGraph
    import os
    
    mgraph = MGraph()
    with mgraph.edit() as edit:
        # Create sample graph
        node1 = edit.new_node()
        node2 = edit.new_node()
        edge = edit.new_edge(from_node_id=node1.node_id,
                            to_node_id=node2.node_id)
    
    # Test various export formats
    with mgraph.export() as export:
        # DOT format
        dot = export.to__dot()
        assert 'digraph' in dot
        
        # Mermaid format
        mermaid = export.to__mermaid()
        assert 'graph TD' in mermaid
        
        # GraphML format
        graphml = export.to__graphml()
        assert 'graphml' in graphml
        
        # RDF/Turtle format
        turtle = export.to__turtle()
        assert '@prefix' in turtle
    
    # Test visualization
    output_file = 'test_graph.png'
    with mgraph.screenshot() as screenshot:
        screenshot.save_to(output_file)
        assert os.path.exists(output_file)
        if os.path.exists(output_file):
            os.remove(output_file)
```

## Installation

```bash
pip install mgraph-db
```

## Development Setup

1. Clone the repository:
```bash
git clone https://github.com/your-org/mgraph.git
```

2. Install dependencies:
```bash
pip install -r requirements.txt
```

3. Run tests:
```bash
python -m pytest tests/
```

## Contributing

1. Fork the repository
2. Create a feature branch (`git checkout -b feature/amazing-feature`)
3. Make your changes
4. Run tests (`python -m pytest tests/`)
5. Submit a Pull Request

## License

This project is licensed under the Apache License 2.0 - see the [LICENSE](LICENSE) file for details.

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "mgraph-db",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4.0,>=3.11",
    "maintainer_email": null,
    "keywords": null,
    "author": "Dinis Cruz",
    "author_email": "dinis.cruz@owasp.org",
    "download_url": "https://files.pythonhosted.org/packages/36/6a/3428d83fe874876fe07d1ed4c5386b1cc8329ed16e459c1135f3ee9cf086/mgraph_db-1.1.9.tar.gz",
    "platform": null,
    "description": "# MGraph-DB: A Memory-based GraphDB for Python, GenAI, Semantic Web and Serverless\n\n![Current Release](https://img.shields.io/badge/release-v1.1.9-blue)\n\n> **QUICKSTART GUIDE**: For working code examples, check out our test suite at:\n> ```\n> tests/quickstart/test_README_examples.py\n> ```\n> This file contains executable versions of all examples shown below, providing a hands-on way to learn the library.\n\nMGraph-DB is a high-performance, type-safe graph database implementation in Python, optimized for in-memory operations with JSON persistence. Its architecture makes it particularly well-suited for:\n\n- **GenAI Applications**\n  - Knowledge graph construction and querying\n  - Semantic relationship modeling\n  - Context management for LLMs\n  - Graph-based reasoning systems\n\n- **Semantic Web**\n  - RDF data processing\n  - Ontology management\n  - Linked data applications\n  - SPARQL-compatible queries\n\n- **Serverless Deployments**\n  - Quick cold starts with memory-first design\n  - Efficient JSON serialization\n  - Low memory footprint\n  - Built-in serverless support via [MGraph_DB_Serverless](https://github.com/owasp-sbot/MGraph-DB-Serverless)\n\n- **Python Ecosystem**\n  - Native Python implementation\n  - Full type safety and validation\n  - Clean, Pythonic API\n  - Rich integration capabilities\n\n## Major Features\n\n### Production-Ready Type System\n- Complete implementation of the three-layer architecture (Domain, Model, Schema)\n- Comprehensive runtime type checking across all layers\n- Type-safe property accessors and method decorators\n- Robust validation for nested data structures\n- Clean class hierarchies with explicit interfaces\n\n### Advanced Graph Operations\n- High-performance in-memory graph operations\n- Sophisticated query system with chainable operations\n- Rich traversal capabilities with type filtering\n- Flexible node and edge attribute management\n- Comprehensive CRUD operations for graph elements\n\n### Optimized Indexing System\n- O(1) lookups for all core operations\n- Multi-dimensional indexing (type, attribute, relationship)\n- Efficient graph traversal support\n- Advanced query optimization\n- Index persistence and restoration\n\n### Query System Enhancements\n- View-based query results with navigation\n- Rich filtering and traversal operations\n- Chainable query interface\n- Query result caching\n- Query operation history tracking\n\n### Export Capabilities\n- Support for multiple export formats:\n  - GraphML\n  - DOT\n  - Mermaid\n  - RDF/Turtle\n  - N-Triples\n  - GEXF\n  - TGF\n  - Cypher\n  - CSV\n  - JSON\n\n### Visualization Support\n- Integration with common visualization libraries\n- Custom layout algorithms\n- Interactive graph exploration\n- Support for large graph visualization\n- Multiple visualization format exports\n\n## Quick Start\n\nHere's a simple example showing basic graph operations:\n\n```python\ndef test_basic_graph_operations():\n    from mgraph_db.mgraph.MGraph import MGraph\n    \n    # Create a new graph\n    mgraph = MGraph()\n    \n    with mgraph.edit() as edit:\n        # Create two nodes\n        node1 = edit.new_node(node_data={\"value\": \"First Node\"})\n        node2 = edit.new_node(node_data={\"value\": \"Second Node\"})\n        \n        # Connect nodes with an edge\n        edge = edit.new_edge(from_node_id=node1.node_id, \n                            to_node_id=node2.node_id)\n        \n        # Verify nodes and edge were created\n        assert node1.node_id is not None\n        assert node2.node_id is not None\n        assert edge.edge_id is not None\n    \n    # Query the graph\n    with mgraph.data() as data:\n        nodes = data.nodes()\n        edges = data.edges()\n        \n        assert len(nodes) == 2\n        assert len(edges) == 1\n```\n\n## Use Cases\n\n### GenAI Integration\n\n```python\ndef test_genai_integration():\n    from mgraph_db.mgraph.MGraph import MGraph\n    \n    mgraph = MGraph()\n    with mgraph.edit() as edit:\n        # Create a knowledge graph for LLM context\n        context = edit.new_node(node_data={\"type\": \"context\", \n                                         \"value\": \"user query\"})\n        entity = edit.new_node(node_data={\"type\": \"entity\", \n                                        \"value\": \"named entity\"})\n        relation = edit.new_edge(from_node_id=context.node_id,\n                                to_node_id=entity.node_id,\n                                edge_data={\"type\": \"contains\"})\n        \n        # Verify the knowledge graph\n        assert context.node_data.value == \"user query\"\n        assert entity.node_data.value == \"named entity\"\n        assert relation.edge_data.type == \"contains\"\n```\n\n### Semantic Web Applications\n\n```python\ndef test_semantic_web():\n    from mgraph_db.mgraph.MGraph import MGraph\n    \n    mgraph = MGraph()\n    with mgraph.edit() as edit:\n        # Create RDF-style triples\n        subject = edit.new_node(node_data={\"uri\": \"http://example.org/subject\"})\n        object = edit.new_node(node_data={\"uri\": \"http://example.org/object\"})\n        predicate = edit.new_edge(from_node_id=subject.node_id,\n                                 to_node_id=object.node_id,\n                                 edge_data={\"predicate\": \"relates_to\"})\n        \n        # Verify triple structure\n        assert subject.node_data.uri == \"http://example.org/subject\"\n        assert object.node_data.uri == \"http://example.org/object\"\n        assert predicate.edge_data.predicate == \"relates_to\"\n```\n\n## Advanced Usage\n\n### Type-Safe Operations\n\n```python\ndef test_type_safe_operations():\n    from mgraph_db.mgraph.MGraph import MGraph\n    from mgraph_db.mgraph.schemas.Schema__MGraph__Node import Schema__MGraph__Node\n    from mgraph_db.mgraph.schemas.Schema__MGraph__Node__Data import Schema__MGraph__Node__Data\n    \n    # Custom node data with runtime type checking\n    class Custom_Node_Data(Schema__MGraph__Node__Data):\n        name: str\n        value: int\n        priority: float\n    \n    # Type-safe node definition\n    class Custom_Node(Schema__MGraph__Node):\n        node_data: Custom_Node_Data\n    \n    mgraph = MGraph()\n    with mgraph.edit() as edit:\n        # Create typed node\n        node = edit.new_node(node_type=Custom_Node,\n                            name=\"test\",\n                            value=42,\n                            priority=1.5)\n        \n        # Verify type safety\n        assert node.node_data.name == \"test\"\n        assert node.node_data.value == 42\n        assert node.node_data.priority == 1.5\n        \n        # Demonstrate type-safe processing\n        result = node.node_data.priority * 2.0\n        assert result == 3.0\n```\n\n### Using the Index System\n\n```python\ndef test_index_system():\n    from mgraph_db.mgraph.MGraph import MGraph\n    \n    mgraph = MGraph()\n    with mgraph.edit() as edit:\n        # Create nodes\n        node1 = edit.new_node(node_type=Custom_Node,\n                             name=\"test\",\n                             value=42,\n                             priority=1.5)\n        \n        # Access index\n        with edit.index() as index:\n            # Get nodes by type\n            nodes = index.get_nodes_by_type(Custom_Node)\n            assert len(nodes) == 1\n            \n            # Get by relationship\n            nodes_by_type = index.get_nodes_by_type(Custom_Node)\n            assert node1.node_id in nodes_by_type\n```\n\n### Graph Export and Visualization\n\n```python\ndef test_export_and_visualization():\n    from mgraph_db.mgraph.MGraph import MGraph\n    import os\n    \n    mgraph = MGraph()\n    with mgraph.edit() as edit:\n        # Create sample graph\n        node1 = edit.new_node()\n        node2 = edit.new_node()\n        edge = edit.new_edge(from_node_id=node1.node_id,\n                            to_node_id=node2.node_id)\n    \n    # Test various export formats\n    with mgraph.export() as export:\n        # DOT format\n        dot = export.to__dot()\n        assert 'digraph' in dot\n        \n        # Mermaid format\n        mermaid = export.to__mermaid()\n        assert 'graph TD' in mermaid\n        \n        # GraphML format\n        graphml = export.to__graphml()\n        assert 'graphml' in graphml\n        \n        # RDF/Turtle format\n        turtle = export.to__turtle()\n        assert '@prefix' in turtle\n    \n    # Test visualization\n    output_file = 'test_graph.png'\n    with mgraph.screenshot() as screenshot:\n        screenshot.save_to(output_file)\n        assert os.path.exists(output_file)\n        if os.path.exists(output_file):\n            os.remove(output_file)\n```\n\n## Installation\n\n```bash\npip install mgraph-db\n```\n\n## Development Setup\n\n1. Clone the repository:\n```bash\ngit clone https://github.com/your-org/mgraph.git\n```\n\n2. Install dependencies:\n```bash\npip install -r requirements.txt\n```\n\n3. Run tests:\n```bash\npython -m pytest tests/\n```\n\n## Contributing\n\n1. Fork the repository\n2. Create a feature branch (`git checkout -b feature/amazing-feature`)\n3. Make your changes\n4. Run tests (`python -m pytest tests/`)\n5. Submit a Pull Request\n\n## License\n\nThis project is licensed under the Apache License 2.0 - see the [LICENSE](LICENSE) file for details.\n",
    "bugtrack_url": null,
    "license": "Apache 2.0",
    "summary": "MGraph-DB",
    "version": "1.1.9",
    "project_urls": {
        "Homepage": "https://github.com/owasp-sbot/MGraph-DB",
        "Repository": "https://github.com/owasp-sbot/MGraph-DB"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "8d7d90b7b6801ed8196a72c1b17021afedf28a4544dd06fa34c7ba0d36a58a95",
                "md5": "16da63c4e08ff80587550bd4de7719d9",
                "sha256": "54ab39ef70261ccb3ec0f0e060dfa941362a8973f891323d4d0e3025a3176189"
            },
            "downloads": -1,
            "filename": "mgraph_db-1.1.9-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "16da63c4e08ff80587550bd4de7719d9",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0,>=3.11",
            "size": 177119,
            "upload_time": "2025-02-25T02:06:09",
            "upload_time_iso_8601": "2025-02-25T02:06:09.858212Z",
            "url": "https://files.pythonhosted.org/packages/8d/7d/90b7b6801ed8196a72c1b17021afedf28a4544dd06fa34c7ba0d36a58a95/mgraph_db-1.1.9-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "366a3428d83fe874876fe07d1ed4c5386b1cc8329ed16e459c1135f3ee9cf086",
                "md5": "00b07a961e8ebdbf1caedaa84c44f47e",
                "sha256": "15088c69a7fb27f66b5467ea74a3634e8508a1c9ba1d5fc8d574b3fbded442bc"
            },
            "downloads": -1,
            "filename": "mgraph_db-1.1.9.tar.gz",
            "has_sig": false,
            "md5_digest": "00b07a961e8ebdbf1caedaa84c44f47e",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0,>=3.11",
            "size": 90636,
            "upload_time": "2025-02-25T02:06:11",
            "upload_time_iso_8601": "2025-02-25T02:06:11.408408Z",
            "url": "https://files.pythonhosted.org/packages/36/6a/3428d83fe874876fe07d1ed4c5386b1cc8329ed16e459c1135f3ee9cf086/mgraph_db-1.1.9.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-02-25 02:06:11",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "owasp-sbot",
    "github_project": "MGraph-DB",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "mgraph-db"
}
        
Elapsed time: 1.03467s