eos/arango-db-3-connector

为 `triagens/arangodb` 提供工厂和接口封装,以实现更简单、更干净的用法

1.0.0 2019-02-01 17:27 UTC

This package is auto-updated.

Last update: 2024-08-29 04:48:58 UTC


README

triagens/arangodb 提供工厂和接口封装,以实现更简单、更干净的用法。

安装

composer req eos/arango-db-3-connector

用法

使用此库的最简单方法是创建一个实现 ArangoDBInterfaceArangoDB 实例。

<?php

$arangoDB = \Eos\ArangoDBConnector\ArangoDB::create(
    ['tcp://127.0.0.1:8529'],
    'your_database_name',
    'your_database_user',
    'your_database_password',
    [
        \ArangoDBClient\ConnectionOptions::OPTION_WAIT_SYNC => true, // your connection configuration
    ]
);

// create your database
$arangoDB->database()->createDatabase();

// create your first collection
$arangoDB->collections()->createDocumentCollection('example');

// create your first document
$arangoDB->execute('INSERT { name: @name } INTO example', ['name'=>'Test']);

// for custom statements use: $arangoDB->statements()->createStatement([...]);

连接

如果需要,将通过 ConnectionFactoryInterface 的实例创建连接。

ConnectionFactoryInterface 的默认实现是 ConnectionFactory

<?php

$connectionFactory = new \Eos\ArangoDBConnector\Connection\ConnectionFactory(
    ['tcp://127.0.0.1:8529'],
    'your_database_name',
    'your_database_user',
    'your_database_password',
    [
        \ArangoDBClient\ConnectionOptions::OPTION_WAIT_SYNC => true, // your connection configuration
    ]
);

数据库管理

可以使用 DatabaseHandlerInterface 的实例来管理您的数据库。

DatabaseHandlerInterface 的默认实现是 DatabaseHandler

<?php

/** @var \Eos\ArangoDBConnector\Connection\ConnectionFactoryInterface $connectionFactory */

/** @var \Eos\ArangoDBConnector\Database\DatabaseHandlerInterface $databaseHandler */
$databaseHandler = new \Eos\ArangoDBConnector\Database\DatabaseHandler($connectionFactory);

// create your database (configured in the connection)
$databaseHandler->createDatabase();

// if your database user is not allowed to create a new database, overwrite the configured user for this action:
$databaseHandler->createDatabase('admin_user','admin_password');

// remove your database (configured in the connection)
$databaseHandler->removeDatabase();

// if your database user is not allowed to remove a database, overwrite the configured user for this action:
$databaseHandler->removeDatabase('admin_user','admin_password');

集合管理

可以使用 CollectionHandlerInterface 的实例来管理您的集合。

CollectionHandlerInterface 的默认实现是 CollectionHandler

<?php

/** @var \Eos\ArangoDBConnector\Connection\ConnectionFactoryInterface $connectionFactory */

/** @var \Eos\ArangoDBConnector\Collection\CollectionHandlerInterface $collectionHandler */
$collectionHandler = new \Eos\ArangoDBConnector\Collection\CollectionHandler($connectionFactory);

// create a document collection
$collectionHandler->createDocumentCollection('your_collection');

// create an edge collection
$collectionHandler->createEdgeCollection('your_collection');

// create an index for a collection
$collectionHandler->createIndex('your_collection','hash',['your_field']);

// remove a collection
$collectionHandler->removeCollection('your_collection');

语句

可以通过 StatementFactoryInterface 的实例创建您的语句。

StatementFactoryInterface 的默认实现是 StatementFactory

<?php

/** @var \Eos\ArangoDBConnector\Connection\ConnectionFactoryInterface $connectionFactory */

/** @var \Eos\ArangoDBConnector\Statement\StatementFactoryInterface $statementFactory */
$statementFactory = new \Eos\ArangoDBConnector\Statement\StatementFactory($connectionFactory);

// create a generic statement
$statement =$statementFactory->createStatement(['query'=>'FOR document IN your_collection RETURN document']);
$cursor = $statement->execute();

// create query statement
$statement =$statementFactory->createQueryStatement('INSERT { name: @name } INTO your_collection', ['name'=>'Test']);
$statement->execute();