Skip to content

Commit d68df8e

Browse files
Added schema features
feat: Index management feat: user management feat: ArangoSearch view management
1 parent 86cbfbc commit d68df8e

15 files changed

+955
-53
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,8 @@ Manages administrative functions
3737

3838
### SchemaClient
3939
Manages schema related tasks like creating databases, collections, indexes, views and graphs
40+
41+
## Related packages
42+
[AQL query builder](https://github.com/LaravelFreelancerNL/fluentaql)
43+
44+
[ArangoDB Laravel Driver](https://github.com/LaravelFreelancerNL/laravel-arangodb)

src/Connector.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,12 @@ protected function decodeResponse(ResponseInterface $response): array
125125

126126
return $decodedResponse;
127127
}
128+
129+
/**
130+
* @return string
131+
*/
132+
public function getUser(): string
133+
{
134+
return (string) $this->config['AuthUser'];
135+
}
128136
}

src/Schema/ManagesCollections.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ public function getCollections(bool $excludeSystemCollections = false): array
3535
);
3636
}
3737

38-
3938
/**
4039
* Check for collection existence in current DB.
4140
*
@@ -53,14 +52,14 @@ public function hasCollection(string $collection): bool
5352
/**
5453
* @see https://www.arangodb.com/docs/stable/http/collection-getting.html#return-information-about-a-collection
5554
*
56-
* @param string $collection
55+
* @param string $name
5756
* @return array<mixed>
5857
* @throws ArangoException
5958
* @throws GuzzleException
6059
*/
61-
public function getCollection(string $collection): array
60+
public function getCollection(string $name): array
6261
{
63-
$uri = '/_api/collection/' . $collection;
62+
$uri = '/_api/collection/' . $name;
6463
return (array) $this->connector->request('get', $uri);
6564
}
6665

src/Schema/ManagesDatabases.php

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,24 @@ public function getCurrentDatabase(): array
2424
}
2525

2626
/**
27+
* @param bool $full
2728
* @return array<mixed>
2829
*
29-
* @throws GuzzleException|ArangoException
30+
* @throws ArangoException
31+
* @throws GuzzleException
3032
*/
31-
public function listDatabases(): array
33+
public function getDatabases(bool $full = false): array
3234
{
33-
return (array) $this->connector->request('get', '/_api/database');
35+
$user = $this->connector->getUser();
36+
37+
$uri = '/_api/user/' . $user . '/database';
38+
$options = [
39+
'query' => [
40+
'full' => $full
41+
]
42+
];
43+
44+
return (array) $this->connector->request('get', $uri, $options);
3445
}
3546

3647
/**
@@ -41,18 +52,9 @@ public function listDatabases(): array
4152
*/
4253
public function hasDatabase(string $database): bool
4354
{
44-
$databaseList = $this->listDatabases();
45-
return in_array($database, $databaseList);
46-
}
55+
$databaseList = $this->getDatabases();
4756

48-
/**
49-
* @return array<mixed>
50-
*
51-
* @throws GuzzleException|ArangoException
52-
*/
53-
public function listMyDatabases(): array
54-
{
55-
return (array) $this->connector->request('get', '/_api/database/user');
57+
return isset($databaseList[$database]);
5658
}
5759

5860
/**

src/Schema/ManagesIndexes.php

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
3+
namespace ArangoClient\Schema;
4+
5+
use ArangoClient\Connector;
6+
use ArangoClient\Exceptions\ArangoException;
7+
use GuzzleHttp\Exception\GuzzleException;
8+
9+
/*
10+
* @see https://www.arangodb.com/docs/stable/http/indexes.html
11+
*/
12+
trait ManagesIndexes
13+
{
14+
protected Connector $connector;
15+
16+
/**
17+
* @see https://www.arangodb.com/docs/stable/http/indexes-working-with.html#read-all-indexes-of-a-collection
18+
*
19+
* @param string $collection
20+
* @return array<mixed>
21+
* @throws ArangoException
22+
* @throws GuzzleException
23+
*/
24+
public function getIndexes(string $collection): array
25+
{
26+
$options = [
27+
'query' => [
28+
'collection' => $collection
29+
]
30+
];
31+
$results = (array) $this->connector->request('get', '/_api/index', $options);
32+
return (array) $results['indexes'];
33+
}
34+
35+
/**
36+
* @see https://www.arangodb.com/docs/stable/http/indexes-working-with.html#read-index
37+
*
38+
* @param string $id
39+
* @return array<mixed>
40+
* @throws ArangoException
41+
* @throws GuzzleException
42+
*/
43+
public function getIndex(string $id): array
44+
{
45+
$uri = '/_api/index/' . $id;
46+
return (array) $this->connector->request('get', $uri);
47+
}
48+
49+
/**
50+
* @see https://www.arangodb.com/docs/stable/http/indexes-working-with.html#read-index
51+
*
52+
* @param string $collection
53+
* @param string $name
54+
* @return array<mixed>|bool
55+
* @throws ArangoException
56+
* @throws GuzzleException
57+
*/
58+
public function getIndexByName(string $collection, string $name)
59+
{
60+
$indexes = $this->getIndexes($collection);
61+
$searchResult = array_search($name, array_column($indexes, 'name'));
62+
if (is_integer($searchResult)) {
63+
return (array) $indexes[$searchResult];
64+
}
65+
return (bool) $searchResult;
66+
}
67+
68+
/**
69+
* @see https://www.arangodb.com/docs/stable/http/indexes-working-with.html#create-index
70+
*
71+
* @param string $collection
72+
* @param array<mixed> $index
73+
* @return bool
74+
* @throws ArangoException
75+
* @throws GuzzleException
76+
*/
77+
public function createIndex(string $collection, array $index): bool
78+
{
79+
$indexType = 'persistent';
80+
81+
if (isset($index['type'])) {
82+
$indexType = (string) $index['type'];
83+
}
84+
$uri = '/_api/index#' . $indexType;
85+
86+
$index = json_encode((object) $index);
87+
$options = ['body' => $index];
88+
$options['query']['collection'] = $collection;
89+
90+
return (bool) $this->connector->request('post', $uri, $options);
91+
}
92+
93+
/**
94+
* @see https://www.arangodb.com/docs/stable/http/indexes-working-with.html#delete-index
95+
*
96+
* @param string $id
97+
* @return bool
98+
* @throws ArangoException
99+
* @throws GuzzleException
100+
*/
101+
public function deleteIndex(string $id): bool
102+
{
103+
$uri = '/_api/index/' . $id;
104+
105+
return (bool) $this->connector->request('delete', $uri);
106+
}
107+
}

src/Schema/ManagesUsers.php

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
<?php
2+
3+
namespace ArangoClient\Schema;
4+
5+
use ArangoClient\Connector;
6+
use ArangoClient\Exceptions\ArangoException;
7+
use GuzzleHttp\Exception\GuzzleException;
8+
9+
/*
10+
* @see https://www.arangodb.com/docs/stable/http/views.html
11+
*/
12+
trait ManagesUsers
13+
{
14+
protected Connector $connector;
15+
16+
/**
17+
* @param string $username
18+
* @return array<mixed>
19+
* @throws ArangoException
20+
* @throws GuzzleException
21+
*/
22+
public function getUser(string $username): array
23+
{
24+
$uri = '/_api/user/' . $username;
25+
26+
return (array) $this->connector->request('get', $uri);
27+
}
28+
29+
/**
30+
* @return array<mixed>
31+
* @throws ArangoException
32+
* @throws GuzzleException
33+
*/
34+
public function getUsers(): array
35+
{
36+
return (array) $this->connector->request('get', '/_api/user');
37+
}
38+
39+
/**
40+
* @param string $username
41+
* @return bool
42+
* @throws ArangoException
43+
* @throws GuzzleException
44+
*/
45+
public function hasUser(string $username): bool
46+
{
47+
$users = $this->getUsers();
48+
49+
return array_search($username, array_column($users, 'user'), true) !== false;
50+
}
51+
52+
/**
53+
* @param array<mixed> $user
54+
* @return array<mixed>
55+
* @throws ArangoException
56+
* @throws GuzzleException
57+
*/
58+
public function createUser(array $user): array
59+
{
60+
$body = json_encode((object) $user);
61+
62+
return (array) $this->connector->request('post', '/_api/user', ['body' => $body]);
63+
}
64+
65+
/**
66+
* @param string $username
67+
* @param array<mixed> $properties
68+
* @return array<mixed>
69+
* @throws ArangoException
70+
* @throws GuzzleException
71+
*/
72+
public function updateUser(string $username, array $properties): array
73+
{
74+
$uri = '/_api/user/' . $username;
75+
76+
$properties = json_encode((object) $properties);
77+
$options = ['body' => $properties];
78+
79+
return (array) $this->connector->request('patch', $uri, $options);
80+
}
81+
82+
/**
83+
* @param string $username
84+
* @param array<mixed> $user
85+
* @return array<mixed>
86+
* @throws ArangoException
87+
* @throws GuzzleException
88+
*/
89+
public function replaceUser(string $username, array $user): array
90+
{
91+
$uri = '/_api/user/' . $username;
92+
93+
$user = json_encode((object) $user);
94+
$options = ['body' => $user];
95+
96+
return (array) $this->connector->request('put', $uri, $options);
97+
}
98+
99+
/**
100+
* @param string $username
101+
* @return bool
102+
* @throws ArangoException
103+
* @throws GuzzleException
104+
*/
105+
public function deleteUser(string $username): bool
106+
{
107+
$uri = '/_api/user/' . $username;
108+
109+
return (bool) $this->connector->request('delete', $uri);
110+
}
111+
112+
/**
113+
* @param string $username
114+
* @param string $database
115+
* @return string
116+
* @throws ArangoException
117+
* @throws GuzzleException
118+
*/
119+
public function getDatabaseAccessLevel(string $username, string $database): string
120+
{
121+
$uri = '/_api/user/' . $username . '/database/' . $database;
122+
123+
return (string) $this->connector->request('get', $uri);
124+
}
125+
126+
/**
127+
* @param string $username
128+
* @param string $database
129+
* @param string $grant
130+
* @return array<mixed>
131+
* @throws ArangoException
132+
* @throws GuzzleException
133+
*/
134+
public function setDatabaseAccessLevel(string $username, string $database, string $grant): array
135+
{
136+
$uri = '/_api/user/' . $username . '/database/' . $database;
137+
138+
$grant = json_encode((object) ['grant' => $grant]);
139+
$options = ['body' => $grant];
140+
141+
return (array) $this->connector->request('put', $uri, $options);
142+
}
143+
144+
/**
145+
* @param string $username
146+
* @param string $database
147+
* @return bool
148+
* @throws ArangoException
149+
* @throws GuzzleException
150+
*/
151+
public function clearDatabaseAccessLevel(string $username, string $database)
152+
{
153+
$uri = '/_api/user/' . $username . '/database/' . $database;
154+
155+
$result = (array) $this->connector->request('delete', $uri);
156+
157+
return ((int) $result['code'] === 202);
158+
}
159+
}

0 commit comments

Comments
 (0)