juststeveking / graph-connection
A container package to create a standard way to query against graph databases
Fund package maintenance!
JustSteveKing
Requires
- php: ^7.4
- juststeveking/parameterbag: v1.1
Requires (Dev)
- phploc/phploc: ^6.0
- phpstan/phpstan: ^0.12.25
- phpunit/phpunit: ^9.1
- roave/security-advisories: dev-master
- squizlabs/php_codesniffer: ^3.5
- thecodingmachine/phpstan-safe-rule: ^1.0
This package is auto-updated.
Last update: 2023-01-22 02:19:18 UTC
README
This package is still a work in progress, what I have tested works so far but should not be used in a production environment yet
The purpose of this package is to create a simple to use manager class where you can add graph database connection adapters, and forward calls through to the adapters to query against your graph databases.
Installation
Using composer:
$ composer require juststeveking/graph-connection
You are then free to use it as needed within your projects.
Adapters
The first step is to create an adapter that extends the JustSteveKing\Graph\Connection\Adapters\AdapterInterface
and implements the following methods:
public static function getName(): string;
This of course must return a string, but this string is what is used to alias your adapter when using the ConnectionManager
class.
public function query(string $query) : self
This method is where you would build up your call stack of queries to send over as a transaction.
public function send();
This is where you would send the query over to the database server.
Please ensure you use your own method of storing queries in your adapter, as these cannot be enforced by an interface.
I am currently building out a HttpAdapter
to use alongside neo4j and their v4 HTTP API, which will be released as a separate package.
Usage
Using this library is relatively simple.
You can create a new ConnectionManager
by providing it with an Adapter that follows JustSteveKing\Graph\Connection\Adapters\AdapterInterface
:
<?php use JustSteveKing\Graph\Connection\ConnectionManager; use JustSteveKing\Tests\Graph\Connection\Stubs\WorkingAdapter; use JustSteveKing\Tests\Graph\Connection\Stubs\AnotherAdapter; // the create method takes a variadic argument of adapters // and returns an instance of the ConnectionManager with registered adapters $manager = ConnectionManager::create(new WorkingAdapter(), new AnotherAdapter());
You may also add another connection once instantiated:
<?php use JustSteveKing\Graph\Connection\ConnectionManager; use JustSteveKing\Tests\Graph\Connection\Stubs\WorkingAdapter; use JustSteveKing\Tests\Graph\Connection\Stubs\AnotherAdapter; // the create method takes a variadic argument of adapters // and returns an instance of the ConnectionManager with registered adapters $manager = ConnectionManager::create(new WorkingAdapter()); $manager->addAdapter(new AnotherAdapter());
Once you have created your connection manager, you can then work with the adapters in the following ways:
The use method to select an adapter to query against
<?php use JustSteveKing\Graph\Connection\ConnectionManager; use JustSteveKing\Tests\Graph\Connection\Stubs\WorkingAdapter; use JustSteveKing\Tests\Graph\Connection\Stubs\AnotherAdapter; $manager = ConnectionManager::create(new WorkingAdapter(), new AnotherAdapter()); $response = $manager->use('adapter-alias')->query('graph query')->send();
The using method to select an adapter to query against
<?php use JustSteveKing\Graph\Connection\ConnectionManager; use JustSteveKing\Tests\Graph\Connection\Stubs\WorkingAdapter; use JustSteveKing\Tests\Graph\Connection\Stubs\AnotherAdapter; $manager = ConnectionManager::create(new WorkingAdapter(), new AnotherAdapter()); $response = $manager->using('adapter-alias')->query('graph query')->send();
The getAdapter method to select an adapter to query against
<?php use JustSteveKing\Graph\Connection\ConnectionManager; use JustSteveKing\Tests\Graph\Connection\Stubs\WorkingAdapter; use JustSteveKing\Tests\Graph\Connection\Stubs\AnotherAdapter; $manager = ConnectionManager::create(new WorkingAdapter(), new AnotherAdapter()); $response = $manager->getAdapter('adapter-alias')->query('graph query')->send();
Writing queries for neo4j
I am currently in the process of publishing a Cypher query builder to use alongside this library, but you are also welcome to use any other library or method to write cypher queries yourself.
Tests
There is a composer script available to run the tests:
$ composer run test
However, if you are unable to run this please use the following command:
$ ./vendor/bin/phpunit --testdox
Security
If you discover any security related issues, please email juststevemcd@gmail.com instead of using the issue tracker.