Skip to main content

FalkorDB client library for kiarina namespace

Project description

kiarina-lib-falkordb

A Python client library for FalkorDB with configuration management and connection pooling.

Features

  • Configuration Management: Use pydantic-settings-manager for flexible configuration
  • Connection Pooling: Automatic connection caching and reuse
  • Retry Support: Built-in retry mechanism for connection failures
  • Sync & Async: Support for both synchronous and asynchronous operations
  • Type Safety: Full type hints and Pydantic validation

Installation

pip install kiarina-lib-falkordb

Quick Start

Basic Usage (Sync)

from kiarina.lib.falkordb import get_falkordb

# Get a FalkorDB client with default settings
db = get_falkordb()

# Select a graph and run a query
graph = db.select_graph("social")
result = graph.query("CREATE (p:Person {name: 'Alice', age: 30}) RETURN p")
print(result.result_set)

Async Usage

from kiarina.lib.falkordb.asyncio import get_falkordb

async def main():
    # Get an async FalkorDB client
    db = get_falkordb()

    # Select a graph and run a query
    graph = db.select_graph("social")
    result = await graph.query("CREATE (p:Person {name: 'Bob', age: 25}) RETURN p")
    print(result.result_set)

Configuration

This library uses pydantic-settings-manager for flexible configuration management.

Environment Variables

Configure the FalkorDB connection using environment variables:

# FalkorDB connection URL
export KIARINA_LIB_FALKORDB_URL="falkor://localhost:6379"

# Enable retry mechanism
export KIARINA_LIB_FALKORDB_USE_RETRY="true"

# Timeout settings
export KIARINA_LIB_FALKORDB_SOCKET_TIMEOUT="6.0"
export KIARINA_LIB_FALKORDB_SOCKET_CONNECT_TIMEOUT="3.0"

# Retry settings
export KIARINA_LIB_FALKORDB_RETRY_ATTEMPTS="3"
export KIARINA_LIB_FALKORDB_RETRY_DELAY="1.0"

Programmatic Configuration

from kiarina.lib.falkordb import settings_manager

# Configure for multiple environments
settings_manager.user_config = {
    "development": {
        "url": "falkor://localhost:6379",
        "use_retry": True,
        "retry_attempts": 3
    },
    "production": {
        "url": "falkor://prod-server:6379",
        "use_retry": True,
        "retry_attempts": 5,
        "socket_timeout": 10.0
    }
}

# Switch to production configuration
settings_manager.active_key = "production"
db = get_falkordb()

Runtime Overrides

from kiarina.lib.falkordb import get_falkordb

# Override settings at runtime
db = get_falkordb(
    url="falkor://custom-server:6379",
    use_retry=True
)

Advanced Usage

Connection Caching

from kiarina.lib.falkordb import get_falkordb

# These will return the same cached connection
db1 = get_falkordb()
db2 = get_falkordb()
assert db1 is db2

# Use different cache keys for separate connections
db3 = get_falkordb(cache_key="secondary")
assert db1 is not db3

Custom Configuration Keys

from kiarina.lib.falkordb import settings_manager, get_falkordb

# Configure multiple named configurations
settings_manager.user_config = {
    "analytics": {
        "url": "falkor://analytics-db:6379",
        "socket_timeout": 30.0
    },
    "cache": {
        "url": "falkor://cache-db:6379",
        "socket_timeout": 5.0
    }
}

# Use specific configurations
analytics_db = get_falkordb("analytics")
cache_db = get_falkordb("cache")

Error Handling and Retries

from kiarina.lib.falkordb import get_falkordb

# Enable automatic retries for connection issues
db = get_falkordb(use_retry=True)

try:
    graph = db.select_graph("mydata")
    result = graph.query("MATCH (n) RETURN count(n)")
except Exception as e:
    print(f"Query failed: {e}")

Configuration Reference

Setting Environment Variable Default Description
url KIARINA_LIB_FALKORDB_URL "falkor://localhost:6379" FalkorDB connection URL
use_retry KIARINA_LIB_FALKORDB_USE_RETRY false Enable automatic retries
socket_timeout KIARINA_LIB_FALKORDB_SOCKET_TIMEOUT 6.0 Socket timeout in seconds
socket_connect_timeout KIARINA_LIB_FALKORDB_SOCKET_CONNECT_TIMEOUT 3.0 Connection timeout in seconds
health_check_interval KIARINA_LIB_FALKORDB_HEALTH_CHECK_INTERVAL 60 Health check interval in seconds
retry_attempts KIARINA_LIB_FALKORDB_RETRY_ATTEMPTS 3 Number of retry attempts
retry_delay KIARINA_LIB_FALKORDB_RETRY_DELAY 1.0 Delay between retries in seconds

URL Formats

FalkorDB URLs support the following formats:

  • falkor://localhost:6379 - Basic connection
  • falkor://username:password@localhost:6379 - With authentication
  • falkors://localhost:6379 - SSL/TLS connection
  • falkors://username:password@localhost:6379 - SSL/TLS with authentication

Development

Prerequisites

  • Python 3.12+
  • Docker (for running FalkorDB in tests)

Setup

# Clone the repository
git clone https://github.com/kiarina/kiarina-python.git
cd kiarina-python

# Setup development environment (installs tools, syncs dependencies, downloads test data)
mise run setup

# Start FalkorDB for testing
docker compose up -d falkordb

Running Tests

# Run format, lint, type checks and tests
mise run package kiarina-lib-falkordb

# Coverage report
mise run package:test kiarina-lib-falkordb --coverage

# Run specific tests
uv run --group test pytest packages/kiarina-lib-falkordb/tests/test_sync.py
uv run --group test pytest packages/kiarina-lib-falkordb/tests/test_async.py

Dependencies

Note on FalkorDB Client

This library uses a fork of the official FalkorDB Python client instead of the upstream version. The fork includes:

  • Redis-py 6.x compatibility: Support for redis-py 6.4.0+ (upstream only supports 5.x)
  • Async client bug fixes: Fixes for issues in the asynchronous client implementation
  • Enhanced stability: Additional improvements for production use

The fork is based on the upstream develop branch and will be synchronized with upstream changes. Once these improvements are merged upstream, this library will migrate back to the official client.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Contributing

This is a personal project, but contributions are welcome! Please feel free to submit issues or pull requests.

Related Projects

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

kiarina_lib_falkordb-1.1.1.tar.gz (6.5 kB view details)

Uploaded Source

Built Distribution

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

kiarina_lib_falkordb-1.1.1-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

Details for the file kiarina_lib_falkordb-1.1.1.tar.gz.

File metadata

  • Download URL: kiarina_lib_falkordb-1.1.1.tar.gz
  • Upload date:
  • Size: 6.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for kiarina_lib_falkordb-1.1.1.tar.gz
Algorithm Hash digest
SHA256 aed5c19c9819549278d89e5778dbde6a0abf668e3f177a825e36f4e1882b5763
MD5 9807e1eece264b3c3d2c44caf62342c9
BLAKE2b-256 5118547bfe8e0a36f6a15da08a85c5f4bd7314749e53ec2d8ced372cb555ac27

See more details on using hashes here.

File details

Details for the file kiarina_lib_falkordb-1.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for kiarina_lib_falkordb-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f995eacc91d7c1068c3975cedfa70ccb65c85c202abfc47eee0bd87780852c01
MD5 2196dd28d4f9a1fafc2f67554bb5cfd4
BLAKE2b-256 c7ae10ae748b70a7c67fa471a1ef291dd982f177eec7519c4d7da6cd5691c280

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