Skip to content

Commit 2645c15

Browse files
authored
Merge pull request #58 from utopia-php/feat-metadata
Decouple the DB lib from Appwrite collections
2 parents aa7de03 + 50ef69e commit 2645c15

5 files changed

Lines changed: 34 additions & 34 deletions

File tree

src/Database/Database.php

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class Database
3838
const PERMISSION_WRITE = 'write';
3939

4040
// Collections
41-
const COLLECTIONS = 'collections';
41+
const METADATA = '_metadata';
4242

4343
// Lengths
4444
const LENGTH_KEY = 255;
@@ -73,8 +73,8 @@ class Database
7373
* @var array
7474
*/
7575
protected $collection = [
76-
'$id' => self::COLLECTIONS,
77-
'$collection' => self::COLLECTIONS,
76+
'$id' => self::METADATA,
77+
'$collection' => self::METADATA,
7878
'name' => 'collections',
7979
'attributes' => [
8080
[
@@ -243,7 +243,7 @@ public function create(): bool
243243
['indexesInQueue', self::VAR_STRING, 1000000, false],
244244
]);
245245

246-
$this->createCollection(self::COLLECTIONS, $attributes);
246+
$this->createCollection(self::METADATA, $attributes);
247247

248248
return true;
249249
}
@@ -291,7 +291,7 @@ public function createCollection(string $id, array $attributes = [], array $inde
291291
{
292292
$this->adapter->createCollection($id, $attributes, $indexes);
293293

294-
if($id === self::COLLECTIONS) {
294+
if($id === self::METADATA) {
295295
return new Document($this->collection);
296296
}
297297

@@ -326,7 +326,7 @@ public function createCollection(string $id, array $attributes = [], array $inde
326326
}
327327
}
328328

329-
return $this->createDocument(Database::COLLECTIONS, $collection);
329+
return $this->createDocument(self::METADATA, $collection);
330330
}
331331

332332
/**
@@ -339,7 +339,7 @@ public function createCollection(string $id, array $attributes = [], array $inde
339339
*/
340340
public function getCollection(string $id): Document
341341
{
342-
return $this->getDocument(self::COLLECTIONS, $id);
342+
return $this->getDocument(self::METADATA, $id);
343343
}
344344

345345
/**
@@ -354,7 +354,7 @@ public function listCollections($limit = 25, $offset = 0): array
354354
{
355355
Authorization::disable();
356356

357-
$result = $this->find(self::COLLECTIONS, [], $limit, $offset);
357+
$result = $this->find(self::METADATA, [], $limit, $offset);
358358

359359
Authorization::reset();
360360

@@ -372,7 +372,7 @@ public function deleteCollection(string $id): bool
372372
{
373373
$this->adapter->deleteCollection($id);
374374

375-
return $this->deleteDocument(self::COLLECTIONS, $id);
375+
return $this->deleteDocument(self::METADATA, $id);
376376
}
377377

378378
/**
@@ -434,8 +434,8 @@ public function createAttribute(string $collection, string $id, string $type, in
434434
throw new LimitException('Row width limit reached. Cannot create new attribute.');
435435
}
436436

437-
if($collection->getId() !== self::COLLECTIONS) {
438-
$this->updateDocument(self::COLLECTIONS, $collection->getId(), $collection);
437+
if($collection->getId() !== self::METADATA) {
438+
$this->updateDocument(self::METADATA, $collection->getId(), $collection);
439439
}
440440

441441
switch ($type) {
@@ -513,8 +513,8 @@ public function deleteAttribute(string $collection, string $id): bool
513513

514514
$collection->setAttribute('attributes', $attributes);
515515

516-
if($collection->getId() !== self::COLLECTIONS) {
517-
$this->updateDocument(self::COLLECTIONS, $collection->getId(), $collection);
516+
if($collection->getId() !== self::METADATA) {
517+
$this->updateDocument(self::METADATA, $collection->getId(), $collection);
518518
}
519519

520520
return $this->adapter->deleteAttribute($collection->getId(), $id);
@@ -588,8 +588,8 @@ public function addAttributeInQueue(string $collection, string $id, string $type
588588
throw new LimitException('Row width limit reached. Cannot create new attribute.');
589589
}
590590

591-
if($collection->getId() !== self::COLLECTIONS) {
592-
$this->updateDocument(self::COLLECTIONS, $collection->getId(), $collection);
591+
if($collection->getId() !== self::METADATA) {
592+
$this->updateDocument(self::METADATA, $collection->getId(), $collection);
593593
}
594594

595595
return true;
@@ -617,8 +617,8 @@ public function removeAttributeInQueue(string $collection, string $id): bool
617617

618618
$collection->setAttribute('attributesInQueue', $attributes);
619619

620-
if($collection->getId() !== self::COLLECTIONS) {
621-
$this->updateDocument(self::COLLECTIONS, $collection->getId(), $collection);
620+
if($collection->getId() !== self::METADATA) {
621+
$this->updateDocument(self::METADATA, $collection->getId(), $collection);
622622
}
623623

624624
return true;
@@ -664,8 +664,8 @@ public function createIndex(string $collection, string $id, string $type, array
664664
'orders' => $orders,
665665
]), Document::SET_TYPE_APPEND);
666666

667-
if($collection->getId() !== self::COLLECTIONS) {
668-
$this->updateDocument(self::COLLECTIONS, $collection->getId(), $collection);
667+
if($collection->getId() !== self::METADATA) {
668+
$this->updateDocument(self::METADATA, $collection->getId(), $collection);
669669
}
670670

671671
switch ($type) {
@@ -717,8 +717,8 @@ public function deleteIndex(string $collection, string $id): bool
717717

718718
$collection->setAttribute('indexes', $indexes);
719719

720-
if($collection->getId() !== self::COLLECTIONS) {
721-
$this->updateDocument(self::COLLECTIONS, $collection->getId(), $collection);
720+
if($collection->getId() !== self::METADATA) {
721+
$this->updateDocument(self::METADATA, $collection->getId(), $collection);
722722
}
723723

724724
return $this->adapter->deleteIndex($collection->getId(), $id);
@@ -772,8 +772,8 @@ public function addIndexInQueue(string $collection, string $id, string $type, ar
772772
throw new LimitException('Index limit reached. Cannot create new index.');
773773
}
774774

775-
if($collection->getId() !== self::COLLECTIONS) {
776-
$this->updateDocument(self::COLLECTIONS, $collection->getId(), $collection);
775+
if($collection->getId() !== self::METADATA) {
776+
$this->updateDocument(self::METADATA, $collection->getId(), $collection);
777777
}
778778

779779
return true;
@@ -801,8 +801,8 @@ public function removeIndexInQueue(string $collection, string $id): bool
801801

802802
$collection->setAttribute('indexesInQueue', $indexes);
803803

804-
if($collection->getId() !== self::COLLECTIONS) {
805-
$this->updateDocument(self::COLLECTIONS, $collection->getId(), $collection);
804+
if($collection->getId() !== self::METADATA) {
805+
$this->updateDocument(self::METADATA, $collection->getId(), $collection);
806806
}
807807

808808
return true;
@@ -818,7 +818,7 @@ public function removeIndexInQueue(string $collection, string $id): bool
818818
*/
819819
public function getDocument(string $collection, string $id): Document
820820
{
821-
if($collection === self::COLLECTIONS && $id === self::COLLECTIONS) {
821+
if($collection === self::METADATA && $id === self::METADATA) {
822822
return new Document($this->collection);
823823
}
824824

@@ -835,7 +835,7 @@ public function getDocument(string $collection, string $id): Document
835835
$document = new Document($cache);
836836
$validator = new Authorization(self::PERMISSION_READ);
837837

838-
if (!$validator->isValid($document->getRead()) && $collection->getId() !== self::COLLECTIONS) { // Check if user has read access to this document
838+
if (!$validator->isValid($document->getRead()) && $collection->getId() !== self::METADATA) { // Check if user has read access to this document
839839
return new Document();
840840
}
841841

@@ -852,7 +852,7 @@ public function getDocument(string $collection, string $id): Document
852852

853853
$validator = new Authorization(self::PERMISSION_READ);
854854

855-
if (!$validator->isValid($document->getRead()) && $collection->getId() !== self::COLLECTIONS) { // Check if user has read access to this document
855+
if (!$validator->isValid($document->getRead()) && $collection->getId() !== self::METADATA) { // Check if user has read access to this document
856856
return new Document();
857857
}
858858

src/Database/Validator/Structure.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ public function isValid($document)
196196
return false;
197197
}
198198

199-
if (empty($this->collection->getId()) || Database::COLLECTIONS !== $this->collection->getCollection()) {
199+
if (empty($this->collection->getId()) || Database::METADATA !== $this->collection->getCollection()) {
200200
$this->message = 'Collection "'.$this->collection->getCollection().'" not found';
201201
return false;
202202
}

tests/Database/Base.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -985,7 +985,7 @@ public function testCount()
985985
public function testEncodeDecode()
986986
{
987987
$collection = new Document([
988-
'$collection' => Database::COLLECTIONS,
988+
'$collection' => Database::METADATA,
989989
'$id' => 'users',
990990
'name' => 'Users',
991991
'attributes' => [

tests/Database/Validator/QueriesTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ class QueriesTest extends TestCase
1414
* @var array
1515
*/
1616
protected $collection = [
17-
'$id' => Database::COLLECTIONS,
18-
'$collection' => Database::COLLECTIONS,
17+
'$id' => Database::METADATA,
18+
'$collection' => Database::METADATA,
1919
'name' => 'movies',
2020
'attributes' => [
2121
[

tests/Database/Validator/StructureTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ class StructureTest extends TestCase
1414
* @var array
1515
*/
1616
protected $collection = [
17-
'$id' => Database::COLLECTIONS,
18-
'$collection' => Database::COLLECTIONS,
17+
'$id' => Database::METADATA,
18+
'$collection' => Database::METADATA,
1919
'name' => 'collections',
2020
'attributes' => [
2121
[

0 commit comments

Comments
 (0)