Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 26 additions & 26 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
# If you prefer the allow list template instead of the deny list, see community template:
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
#
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib
# Composer dependencies
/vendor/
composer.lock

# Test binary, built with `go test -c`
*.test
# IDE files
/.idea/
/.vscode/
*.swp
*.swo

# Code coverage profiles and other test artifacts
*.out
coverage.*
*.coverprofile
profile.cov
# OS files
.DS_Store
Thumbs.db

# Dependency directories (remove the comment below to include it)
# vendor/
# Test coverage
/coverage/
clover.xml
coverage.xml
*.coverage

# Go workspace file
go.work
go.work.sum

# env file
# Environment files
.env
.env.local
.env.*.local

# Cache files
*.cache
/cache/

# Editor/IDE
# .idea/
# .vscode/
# Log files
*.log
/logs/
126 changes: 125 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,126 @@
# GraphRedis
A Graph Database with Redis!

A Graph Database with Redis!

GraphRedis is a PHP library that provides graph database functionality using Redis as the storage backend. It supports nodes, edges, and basic graph operations with a simple and intuitive API.

## Installation

Install GraphRedis using Composer:

```bash
composer require linkerlin/graph-redis
```

## Requirements

- PHP 7.4 or higher
- Redis extension for PHP
- Redis server

## Usage

### Basic Example

```php
<?php

require_once 'vendor/autoload.php';

use GraphRedis\GraphRedis;

// Create a new GraphRedis instance
$graph = new GraphRedis();

// Connect to Redis (optional, uses default localhost:6379)
$graph->connect('127.0.0.1', 6379);

// Add nodes
$graph->addNode('user1', ['name' => 'John Doe', 'age' => 30]);
$graph->addNode('user2', ['name' => 'Jane Smith', 'age' => 25]);

// Add edge between nodes
$graph->addEdge('user1', 'user2', 'FRIENDS_WITH', ['since' => '2020-01-01']);

// Get node data
$user1 = $graph->getNode('user1');
print_r($user1);

// Get outgoing edges
$edges = $graph->getOutgoingEdges('user1');
print_r($edges);

// Close connection
$graph->close();
```

### Advanced Usage

#### Custom Redis Instance

```php
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);

$graph = new GraphRedis($redis, 'my_graph:');
```

#### Working with Relationships

```php
// Add different types of relationships
$graph->addEdge('user1', 'company1', 'WORKS_AT', ['position' => 'Developer']);
$graph->addEdge('user2', 'company1', 'WORKS_AT', ['position' => 'Manager']);

// Get incoming relationships
$incoming = $graph->getIncomingEdges('company1');
```

## API Reference

### GraphRedis Class

#### Constructor

```php
new GraphRedis($redis = null, $keyPrefix = 'graph:')
```

- `$redis`: Optional Redis instance. If not provided, a new Redis instance will be created.
- `$keyPrefix`: Key prefix for Redis keys (default: 'graph:')

#### Methods

##### Connection

- `connect($host = '127.0.0.1', $port = 6379, $timeout = 0.0)`: Connect to Redis server
- `close()`: Close Redis connection

##### Node Operations

- `addNode($nodeId, array $properties = [])`: Add a node to the graph
- `getNode($nodeId)`: Get node properties
- `removeNode($nodeId)`: Remove a node and all its edges

##### Edge Operations

- `addEdge($fromNodeId, $toNodeId, $relationship = 'CONNECTED_TO', array $properties = [])`: Add an edge between nodes
- `getOutgoingEdges($nodeId)`: Get outgoing edges for a node
- `getIncomingEdges($nodeId)`: Get incoming edges for a node

## Testing

Run tests using PHPUnit:

```bash
composer install
vendor/bin/phpunit
```

## License

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

## Contributing

Contributions are welcome! Please feel free to submit a Pull Request.
32 changes: 32 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "linkerlin/graph-redis",
"description": "A Graph Database with Redis!",
"type": "library",
"license": "Apache-2.0",
"keywords": ["graph", "database", "redis", "graph-database"],
"authors": [
{
"name": "linkerlin",
"email": "[email protected]"
}
],
"require": {
"php": ">=7.4",
"ext-redis": "*"
},
"require-dev": {
"phpunit/phpunit": "^9.0"
},
"autoload": {
"psr-4": {
"GraphRedis\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"GraphRedis\\Tests\\": "tests/"
}
},
"minimum-stability": "stable",
"prefer-stable": true
}
18 changes: 18 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
verbose="true">
<testsuites>
<testsuite name="GraphRedis Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>

<coverage>
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>
</phpunit>
Loading