jsantos / arango-silex
Model para trabalhar facilmente com ArangoDB
Requires
- triagens/arangodb: ^3.1
Requires (Dev)
- phpunit/phpunit: ^5.7
- silex/silex: ^2.0
This package is auto-updated.
Last update: 2025-01-22 15:49:23 UTC
README
Desenvolvi um Model e ServiceProvider que utiliza a Library oficial do ArangoDB para trabalhar com PHP, assim facilitando a manipulação de dados em sua aplicação.
Adicionando Arango-Silex a minha Aplicação
composer require jsantos/arango-silex dev-master
Estrutura de diretorio
Dentro de src existe dois diretorios, ArangoModel e Provider.
- Dentro de Provider tem o Service (ConnectionArangoServiceProvider.php) de conexão com Banco de Dados ArangoDB.
- Em ArangoModel temos o Model (ArangoModel.php), onde possui as funcionalidades que ajudaram a munipular os dados de sua aplicação.
Configurando conexão com ArangoDB
Dentro de sua aplicação, escolha um diretorio de sua preferencia para criar o arquivo de conexão com ArangoDB, após escolher um diretorio, siga os passos a seguir. Exemplo: Criando arquivo de conexão.
Criaremos uma class ConnectionArango que extende ConnectionArangoServiceprovider e iniciaremos algumas configurações do banco ArangoDB. Precisaremos apenas de 3 atributos, $database, $authUser e $authPassword.
Todos:
- protected $database
- protected $endpoint
- protected $authType
- protected $authUser
- protected $authPassword
- protected $connection
- protected $timeout
- protected $reconnect
- protected $create
- protected $updatePolicy
<?php namespace Exemplo\Provider; use JSantos\Provider\ConnectionArangoServiceProvider; class ConnectionArango extends ConnectionArangoServiceProvider { /** * Nome de seu Banco de Dados ArangoDB * @var string */ protected $database = "meu_banco"; /** * Usuario de seu Banco de Dados ArangoDB * @var string */ protected $authUser = "meu_usuario"; /** * Senha de seu Banco de Dados ArangoDB * @var string */ protected $authPassword = "minha_senha"; }
Diretorio/Arquivo: exemplo/Provider/ConnectionArango.php
Registrando Conexão no Silex
Registrando ConnectionArango que acabamos de fazer.
<?php chdir(dirname(__DIR__)); require "vendor/autoload.php"; use Silex\Application; $app = new Application; $app['debug'] = true; //Registrando Service Controller $app->register(new \Silex\Provider\ServiceControllerServiceProvider()); //Registrando Conexao com ArangoDB $app->register(new \Exemplo\Provider\ConnectionArango()); $app->run();
Como usar o ArangoModel
Primeiro devemos registrar o Service Provider do ArangoModel(ArangoModelServiceProvider).
<?php chdir(dirname(__DIR__)); require "vendor/autoload.php"; use Silex\Application; $app = new Application; $app['debug'] = true; //Registrando Service Controller $app->register(new \Silex\Provider\ServiceControllerServiceProvider()); //Registrando Conexao com ArangoDB $app->register(new \Exemplo\Provider\ConnectionArango()); //Registrando Service Provider de ArangoModel $app->register(new \JSantos\Provider\ArangoModelServiceProvider()); $app->run();
Metodos ArangoModel
- createCollection(string $newCollection)
- hasCollection(string $nameCollection)
- deleteCollection(string $nameCollection, array $data = [])
- createDocument(string $nameCollection, array $data)
- lastInsertId()
- hasDocument(string $nameCollection,$idDocument)
- getDocument(string $nameCollection, $idDocument)
- updateDocument(string $nameCollection, $idDocument, array $data)
- removeAttributeDocument(Document|array $document, string $attribute)
- replaceDocument(string $nameCollection, Document $currentDocument, Document $newDocument)
- removeDocument(Document|array $document)
- prepare(string $queryAQL)
- bindCollection(array $bindCollection)
- bindValue(array $bindValue)
- execute()
- query(string $queryAQL)
- searchInDocument(string $nameCollection, array $document)
Criando Collection
<?php $arango = $this->app['arango.model']; $newCollection = "users"; if (!$arango->createCollection($newCollection)) { echo "error"; } echo "success!";
Verifica se já existe Collection
<?php $arango = $this->app['arango.model']; $myCollection = "users"; if ($arango->hasCollection($myCollection)) { echo "exist"; } echo "no exist";
Deletando Collection
<?php $arango = $this->app['arango.model']; $myCollection = "users"; if ($arango->hasCollection($myCollection)) { $arango->deleteCollection($myCollection); }
Criando Documentos em Collection
<?php $arango = $this->app['arango.model']; $collection = "users"; if (!$arango->hasCollection($collection)) { $arango->createCollection($collection); } $data = [ "email" => "jacsonk47@gmail.com", "password" => password_hash("jacson", PASSWORD_BCRYPT, ["cost"=>12]) ]; $idDocument = $arango->createDocument($collection,$data);