Merge pull request #3282 from appwrite/refactor-database

refactor `database.php`
This commit is contained in:
Torsten Dittmann 2022-05-31 15:33:14 +02:00 committed by GitHub
commit 957ba08817
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,6 +1,7 @@
<?php
use Utopia\App;
use Appwrite\Event\Delete;
use Appwrite\Extend\Exception;
use Utopia\Audit\Audit;
use Utopia\Validator\Boolean;
@ -25,6 +26,7 @@ use Utopia\Database\Exception\Authorization as AuthorizationException;
use Utopia\Database\Exception\Duplicate as DuplicateException;
use Utopia\Database\Exception\Limit as LimitException;
use Utopia\Database\Exception\Structure as StructureException;
use Utopia\Locale\Locale;
use Appwrite\Auth\Auth;
use Appwrite\Network\Validator\Email;
use Appwrite\Network\Validator\IP;
@ -38,17 +40,11 @@ use Appwrite\Event\Audit as EventAudit;
use Appwrite\Event\Database as EventDatabase;
use Appwrite\Event\Event;
use Appwrite\Stats\Stats;
use MaxMind\Db\Reader;
/**
* Create attribute of varying type
*
* @param string $collectionId
* @param Utopia\Database\Document $attribute
* @param Appwrite\Utopia\Response $response
* @param Utopia\Database\Database $dbForProject
* @param Appwrite\Event\Database $database
* @param Appwrite\Event\Audit $audits
* @param Appwrite\Stats\Stats $usage
*
* @return Document Newly created attribute document
*/
@ -162,12 +158,7 @@ App::post('/v1/database/collections')
->inject('audits')
->inject('usage')
->inject('events')
->action(function ($collectionId, $name, $permission, $read, $write, $response, $dbForProject, $audits, $usage, $events) {
/** @var Appwrite\Utopia\Response $response */
/** @var Utopia\Database\Database $dbForProject*/
/** @var Appwrite\Event\Audit $audits */
/** @var Appwrite\Stats\Stats $usage */
/** @var Appwrite\Event\Event $events */
->action(function (string $collectionId, string $name, ?string $permission, ?array $read, ?array $write, Response $response, Database $dbForProject, EventAudit $audits, Stats $usage, Event $events) {
$collectionId = $collectionId == 'unique()' ? $dbForProject->getId() : $collectionId;
@ -224,9 +215,7 @@ App::get('/v1/database/collections')
->inject('response')
->inject('dbForProject')
->inject('usage')
->action(function ($search, $limit, $offset, $cursor, $cursorDirection, $orderType, $response, $dbForProject, $usage) {
/** @var Appwrite\Utopia\Response $response */
/** @var Utopia\Database\Database $dbForProject */
->action(function (string $search, int $limit, int $offset, string $cursor, string $cursorDirection, string $orderType, Response $response, Database $dbForProject, Stats $usage) {
if (!empty($cursor)) {
$cursorCollection = $dbForProject->getDocument('collections', $cursor);
@ -265,9 +254,7 @@ App::get('/v1/database/collections/:collectionId')
->inject('response')
->inject('dbForProject')
->inject('usage')
->action(function ($collectionId, $response, $dbForProject, $usage) {
/** @var Appwrite\Utopia\Response $response */
/** @var Utopia\Database\Database $dbForProject */
->action(function (string $collectionId, Response $response, Database $dbForProject, Stats $usage) {
$collection = $dbForProject->getDocument('collections', $collectionId);
@ -293,11 +280,7 @@ App::get('/v1/database/usage')
->param('range', '30d', new WhiteList(['24h', '7d', '30d', '90d'], true), 'Date range.', true)
->inject('response')
->inject('dbForProject')
->action(function ($range, $response, $dbForProject) {
/** @var Appwrite\Utopia\Response $response */
/** @var Utopia\Database\Database $dbForConsole */
/** @var Utopia\Database\Database $dbForProject */
/** @var Utopia\Registry\Registry $register */
->action(function (string $range, Response $response, Database $dbForProject) {
$usage = [];
if (App::getEnv('_APP_USAGE_STATS', 'enabled') == 'enabled') {
@ -404,10 +387,7 @@ App::get('/v1/database/:collectionId/usage')
->param('collectionId', '', new UID(), 'Collection ID.')
->inject('response')
->inject('dbForProject')
->action(function ($range, $collectionId, $response, $dbForProject) {
/** @var Appwrite\Utopia\Response $response */
/** @var Utopia\Database\Database $dbForProject */
/** @var Utopia\Registry\Registry $register */
->action(function (string $range, string $collectionId, Response $response, Database $dbForProject) {
$collectionDocument = $dbForProject->getDocument('collections', $collectionId);
$collection = $dbForProject->getCollection('collection_' . $collectionDocument->getInternalId());
@ -514,12 +494,7 @@ App::get('/v1/database/collections/:collectionId/logs')
->inject('dbForProject')
->inject('locale')
->inject('geodb')
->action(function ($collectionId, $limit, $offset, $response, $dbForProject, $locale, $geodb) {
/** @var Appwrite\Utopia\Response $response */
/** @var Utopia\Database\Document $project */
/** @var Utopia\Database\Database $dbForProject */
/** @var Utopia\Locale\Locale $locale */
/** @var MaxMind\Db\Reader $geodb */
->action(function (string $collectionId, int $limit, int $offset, Response $response, Database $dbForProject, Locale $locale, Reader $geodb) {
$collectionDocument = $dbForProject->getDocument('collections', $collectionId);
$collection = $dbForProject->getCollection('collection_' . $collectionDocument->getInternalId());
@ -606,12 +581,7 @@ App::put('/v1/database/collections/:collectionId')
->inject('audits')
->inject('usage')
->inject('events')
->action(function ($collectionId, $name, $permission, $read, $write, $enabled, $response, $dbForProject, $audits, $usage, $events) {
/** @var Appwrite\Utopia\Response $response */
/** @var Utopia\Database\Database $dbForProject */
/** @var Appwrite\Event\Audit $audits */
/** @var Appwrite\Stats\Stats $usage */
/** @var Appwrite\Event\Event $events */
->action(function (string $collectionId, string $name, string $permission, ?array $read, ?array $write, bool $enabled, Response $response, Database $dbForProject, EventAudit $audits, Stats $usage, Event $events) {
$collection = $dbForProject->getDocument('collections', $collectionId);
@ -670,13 +640,7 @@ App::delete('/v1/database/collections/:collectionId')
->inject('audits')
->inject('deletes')
->inject('usage')
->action(function ($collectionId, $response, $dbForProject, $events, $audits, $deletes, $usage) {
/** @var Appwrite\Utopia\Response $response */
/** @var Utopia\Database\Database $dbForProject */
/** @var Appwrite\Event\Event $events */
/** @var Appwrite\Event\Audit $audits */
/** @var Appwrite\Event\Delete $deletes */
/** @var Appwrite\Stats\Stats $usage */
->action(function (string $collectionId, Response $response, Database $dbForProject, Event $events, EventAudit $audits, Delete $deletes, Stats $usage) {
$collection = $dbForProject->getDocument('collections', $collectionId);
@ -734,13 +698,7 @@ App::post('/v1/database/collections/:collectionId/attributes/string')
->inject('audits')
->inject('usage')
->inject('events')
->action(function ($collectionId, $key, $size, $required, $default, $array, $response, $dbForProject, $database, $audits, $usage, $events) {
/** @var Appwrite\Utopia\Response $response */
/** @var Utopia\Database\Database $dbForProject*/
/** @var Appwrite\Event\Database $database */
/** @var Appwrite\Event\Audit $audits */
/** @var Appwrite\Stats\Stats $usage */
/** @var Appwrite\Event\Event $events */
->action(function (string $collectionId, string $key, ?int $size, ?bool $required, ?string $default, bool $array, Response $response, Database $dbForProject, EventDatabase $database, EventAudit $audits, Stats $usage, Event $events) {
// Ensure attribute default is within required size
$validator = new Text($size);
@ -783,12 +741,7 @@ App::post('/v1/database/collections/:collectionId/attributes/email')
->inject('audits')
->inject('usage')
->inject('events')
->action(function ($collectionId, $key, $required, $default, $array, $response, $dbForProject, $database, $audits, $usage, $events) {
/** @var Appwrite\Utopia\Response $response */
/** @var Utopia\Database\Database $dbForProject*/
/** @var Appwrite\Event\Database $database */
/** @var Appwrite\Event\Audit $audits */
/** @var Appwrite\Stats\Stats $usage */
->action(function (string $collectionId, string $key, ?bool $required, ?string $default, bool $array, Response $response, Database $dbForProject, EventDatabase $database, Event $audits, Stats $usage, Event $events) {
$attribute = createAttribute($collectionId, new Document([
'key' => $key,
@ -827,13 +780,7 @@ App::post('/v1/database/collections/:collectionId/attributes/enum')
->inject('audits')
->inject('usage')
->inject('events')
->action(function ($collectionId, $key, $elements, $required, $default, $array, $response, $dbForProject, $database, $audits, $usage, $events) {
/** @var Appwrite\Utopia\Response $response */
/** @var Utopia\Database\Database $dbForProject*/
/** @var Appwrite\Event\Database $database */
/** @var Appwrite\Event\Audit $audits */
/** @var Appwrite\Stats\Stats $usage */
/** @var Appwrite\Event\Event $events */
->action(function (string $collectionId, string $key, array $elements, ?bool $required, ?string $default, bool $array, Response $response, Database $dbForProject, EventDatabase $database, EventAudit $audits, Stats $usage, Event $events) {
// use length of longest string as attribute size
$size = 0;
@ -887,12 +834,7 @@ App::post('/v1/database/collections/:collectionId/attributes/ip')
->inject('audits')
->inject('usage')
->inject('events')
->action(function ($collectionId, $key, $required, $default, $array, $response, $dbForProject, $database, $audits, $usage, $events) {
/** @var Appwrite\Utopia\Response $response */
/** @var Utopia\Database\Database $dbForProject*/
/** @var Appwrite\Event\Database $database */
/** @var Appwrite\Event\Audit $audits */
/** @var Appwrite\Stats\Stats $usage */
->action(function (string $collectionId, string $key, ?bool $required, ?string $default, bool $array, Response $response, Database $dbForProject, EventDatabase $database, EventAudit $audits, Stats $usage, Event $events) {
$attribute = createAttribute($collectionId, new Document([
'key' => $key,
@ -930,12 +872,7 @@ App::post('/v1/database/collections/:collectionId/attributes/url')
->inject('audits')
->inject('usage')
->inject('events')
->action(function ($collectionId, $key, $required, $default, $array, $response, $dbForProject, $database, $audits, $usage, $events) {
/** @var Appwrite\Utopia\Response $response */
/** @var Appwrite\Event\Database $database */
/** @var Appwrite\Event\Audit $audits */
/** @var Appwrite\Stats\Stats $usage */
/** @var Appwrite\Event\Event $events */
->action(function (string $collectionId, string $key, ?bool $required, ?string $default, bool $array, Response $response, Database $dbForProject, EventDatabase $database, EventAudit $audits, Stats $usage, Event $events) {
$attribute = createAttribute($collectionId, new Document([
'key' => $key,
@ -975,13 +912,7 @@ App::post('/v1/database/collections/:collectionId/attributes/integer')
->inject('audits')
->inject('usage')
->inject('events')
->action(function ($collectionId, $key, $required, $min, $max, $default, $array, $response, $dbForProject, $database, $audits, $usage, $events) {
/** @var Appwrite\Utopia\Response $response */
/** @var Utopia\Database\Database $dbForProject*/
/** @var Appwrite\Event\Database $database */
/** @var Appwrite\Event\Audit $audits */
/** @var Appwrite\Stats\Stats $usage */
/** @var Appwrite\Event\Event $events */
->action(function (string $collectionId, string $key, ?bool $required, ?int $min, ?int $max, ?int $default, bool $array, Response $response, Database $dbForProject, EventDatabase $database, EventAudit $audits, Stats $usage, Event $events) {
// Ensure attribute default is within range
$min = (is_null($min)) ? PHP_INT_MIN : \intval($min);
@ -1048,13 +979,7 @@ App::post('/v1/database/collections/:collectionId/attributes/float')
->inject('audits')
->inject('events')
->inject('usage')
->action(function ($collectionId, $key, $required, $min, $max, $default, $array, $response, $dbForProject, $database, $audits, $events, $usage) {
/** @var Appwrite\Utopia\Response $response */
/** @var Utopia\Database\Database $dbForProject*/
/** @var Appwrite\Event\Database $database */
/** @var Appwrite\Event\Audit $audits */
/** @var Appwrite\Stats\Stats $usage */
/** @var Appwrite\Event\Event $events */
->action(function (string $collectionId, string $key, ?bool $required, ?float $min, ?float $max, ?float $default, bool $array, Response $response, Database $dbForProject, EventDatabase $database, EventAudit $audits, Event $events, Stats $usage) {
// Ensure attribute default is within range
$min = (is_null($min)) ? -PHP_FLOAT_MAX : \floatval($min);
@ -1122,13 +1047,7 @@ App::post('/v1/database/collections/:collectionId/attributes/boolean')
->inject('audits')
->inject('usage')
->inject('events')
->action(function ($collectionId, $key, $required, $default, $array, $response, $dbForProject, $database, $audits, $usage, $events) {
/** @var Appwrite\Utopia\Response $response */
/** @var Utopia\Database\Database $dbForProject*/
/** @var Appwrite\Event\Database $database */
/** @var Appwrite\Event\Audit $audits */
/** @var Appwrite\Stats\Stats $usage */
/** @var Appwrite\Event\Event $events */
->action(function (string $collectionId, string $key, ?bool $required, ?bool $default, bool $array, Response $response, Database $dbForProject, EventDatabase $database, EventAudit $audits, Stats $usage, Event $events) {
$attribute = createAttribute($collectionId, new Document([
'key' => $key,
@ -1157,9 +1076,7 @@ App::get('/v1/database/collections/:collectionId/attributes')
->inject('response')
->inject('dbForProject')
->inject('usage')
->action(function ($collectionId, $response, $dbForProject, $usage) {
/** @var Appwrite\Utopia\Response $response */
/** @var Utopia\Database\Database $dbForProject */
->action(function (string $collectionId, Response $response, Database $dbForProject, Stats $usage) {
$collection = $dbForProject->getDocument('collections', $collectionId);
@ -1201,9 +1118,7 @@ App::get('/v1/database/collections/:collectionId/attributes/:key')
->inject('response')
->inject('dbForProject')
->inject('usage')
->action(function ($collectionId, $key, $response, $dbForProject, $usage) {
/** @var Appwrite\Utopia\Response $response */
/** @var Utopia\Database\Database $dbForProject */
->action(function (string $collectionId, string $key, Response $response, Database $dbForProject, Stats $usage) {
$collection = $dbForProject->getDocument('collections', $collectionId);
@ -1259,13 +1174,7 @@ App::delete('/v1/database/collections/:collectionId/attributes/:key')
->inject('events')
->inject('audits')
->inject('usage')
->action(function ($collectionId, $key, $response, $dbForProject, $database, $events, $audits, $usage) {
/** @var Appwrite\Utopia\Response $response */
/** @var Utopia\Database\Database $dbForProject */
/** @var Appwrite\Event\Database $database */
/** @var Appwrite\Event\Event $events */
/** @var Appwrite\Event\Audit $audits */
/** @var Appwrite\Stats\Stats $usage */
->action(function (string $collectionId, string $key, Response $response, Database $dbForProject, EventDatabase $database, Event $events, EventAudit $audits, Stats $usage) {
$collection = $dbForProject->getDocument('collections', $collectionId);
@ -1351,13 +1260,7 @@ App::post('/v1/database/collections/:collectionId/indexes')
->inject('audits')
->inject('usage')
->inject('events')
->action(function ($collectionId, $key, $type, $attributes, $orders, $response, $dbForProject, $database, $audits, $usage, $events) {
/** @var Appwrite\Utopia\Response $response */
/** @var Utopia\Database\Database $dbForProject */
/** @var Appwrite\Event\Database $database */
/** @var Appwrite\Event\Audit $audits */
/** @var Appwrite\Stats\Stats $usage */
/** @var Appwrite\Event\Event $events */
->action(function (string $collectionId, string $key, string $type, array $attributes, array $orders, Response $response, Database $dbForProject, EventDatabase $database, EventAudit $audits, Stats $usage, Event $events) {
$collection = $dbForProject->getDocument('collections', $collectionId);
@ -1457,9 +1360,7 @@ App::get('/v1/database/collections/:collectionId/indexes')
->inject('response')
->inject('dbForProject')
->inject('usage')
->action(function ($collectionId, $response, $dbForProject, $usage) {
/** @var Appwrite\Utopia\Response $response */
/** @var Utopia\Database\Database $dbForProject */
->action(function (string $collectionId, Response $response, Database $dbForProject, Stats $usage) {
$collection = $dbForProject->getDocument('collections', $collectionId);
@ -1493,9 +1394,7 @@ App::get('/v1/database/collections/:collectionId/indexes/:key')
->inject('response')
->inject('dbForProject')
->inject('usage')
->action(function ($collectionId, $key, $response, $dbForProject, $usage) {
/** @var Appwrite\Utopia\Response $response */
/** @var Utopia\Database\Database $dbForProject */
->action(function (string $collectionId, string $key, Response $response, Database $dbForProject, Stats $usage) {
$collection = $dbForProject->getDocument('collections', $collectionId);
@ -1540,13 +1439,7 @@ App::delete('/v1/database/collections/:collectionId/indexes/:key')
->inject('events')
->inject('audits')
->inject('usage')
->action(function ($collectionId, $key, $response, $dbForProject, $database, $events, $audits, $usage) {
/** @var Appwrite\Utopia\Response $response */
/** @var Utopia\Database\Database $dbForProject */
/** @var Appwrite\Event\Database $database */
/** @var Appwrite\Event\Event $events */
/** @var Appwrite\Event\Audit $audits */
/** @var Appwrite\Stats\Stats $usage */
->action(function (string $collectionId, string $key, Response $response, Database $dbForProject, EventDatabase $database, Event $events, EventAudit $audits, Stats $usage) {
$collection = $dbForProject->getDocument('collections', $collectionId);
@ -1614,14 +1507,7 @@ App::post('/v1/database/collections/:collectionId/documents')
->inject('usage')
->inject('events')
->inject('mode')
->action(function ($documentId, $collectionId, $data, $read, $write, $response, $dbForProject, $user, $audits, $usage, $events, $mode) {
/** @var Appwrite\Utopia\Response $response */
/** @var Utopia\Database\Database $dbForProject */
/** @var Utopia\Database\Document $user */
/** @var Appwrite\Event\Audit $audits */
/** @var Appwrite\Stats\Stats $usage */
/** @var Appwrite\Event\Event $events */
/** @var string $mode */
->action(function (string $documentId, string $collectionId, string|array $data, ?array $read, ?array $write, Response $response, Database $dbForProject, Document $user, EventAudit $audits, Stats $usage, Event $events, string $mode) {
$data = (\is_string($data)) ? \json_decode($data, true) : $data; // Cast to JSON array
@ -1735,11 +1621,7 @@ App::get('/v1/database/collections/:collectionId/documents')
->inject('dbForProject')
->inject('usage')
->inject('mode')
->action(function ($collectionId, $queries, $limit, $offset, $cursor, $cursorDirection, $orderAttributes, $orderTypes, $response, $dbForProject, $usage, $mode) {
/** @var Appwrite\Utopia\Response $response */
/** @var Utopia\Database\Database $dbForProject */
/** @var Appwrite\Stats\Stats $usage */
/** @var string $mode */
->action(function (string $collectionId, array $queries, int $limit, int $offset, string $cursor, string $cursorDirection, array $orderAttributes, array $orderTypes, Response $response, Database $dbForProject, Stats $usage, string $mode) {
/**
* Skip Authorization to get the collection. Needed in case of empty permissions for document level permissions.
@ -1839,10 +1721,7 @@ App::get('/v1/database/collections/:collectionId/documents/:documentId')
->inject('dbForProject')
->inject('usage')
->inject('mode')
->action(function ($collectionId, $documentId, $response, $dbForProject, $usage, $mode) {
/** @var Appwrite\Utopia\Response $response */
/** @var Utopia\Database\Database $dbForProject */
/** @var string $mode */
->action(function (string $collectionId, string $documentId, Response $response, Database $dbForProject, Stats $usage, string $mode) {
/**
* Skip Authorization to get the collection. Needed in case of empty permissions for document level permissions.
@ -1906,12 +1785,7 @@ App::get('/v1/database/collections/:collectionId/documents/:documentId/logs')
->inject('dbForProject')
->inject('locale')
->inject('geodb')
->action(function ($collectionId, $documentId, $limit, $offset, $response, $dbForProject, $locale, $geodb) {
/** @var Appwrite\Utopia\Response $response */
/** @var Utopia\Database\Document $project */
/** @var Utopia\Database\Database $dbForProject */
/** @var Utopia\Locale\Locale $locale */
/** @var MaxMind\Db\Reader $geodb */
->action(function (string $collectionId, string $documentId, int $limit, int $offset, Response $response, Database $dbForProject, Locale $locale, Reader $geodb) {
$collection = $dbForProject->getDocument('collections', $collectionId);
@ -2002,13 +1876,7 @@ App::patch('/v1/database/collections/:collectionId/documents/:documentId')
->inject('usage')
->inject('events')
->inject('mode')
->action(function ($collectionId, $documentId, $data, $read, $write, $response, $dbForProject, $audits, $usage, $events, $mode) {
/** @var Appwrite\Utopia\Response $response */
/** @var Utopia\Database\Database $dbForProject */
/** @var Appwrite\Event\Audit $audits */
/** @var Appwrite\Stats\Stats $usage */
/** @var Appwrite\Event\Event $events */
/** @var string $mode */
->action(function (string $collectionId, string $documentId, string|array $data, ?array $read, ?array $write, Response $response, Database $dbForProject, EventAudit $audits, Stats $usage, Event $events, string $mode) {
/**
* Skip Authorization to get the collection. Needed in case of empty permissions for document level permissions.
@ -2136,14 +2004,7 @@ App::delete('/v1/database/collections/:collectionId/documents/:documentId')
->inject('deletes')
->inject('usage')
->inject('mode')
->action(function ($collectionId, $documentId, $response, $dbForProject, $events, $audits, $deletes, $usage, $mode) {
/** @var Appwrite\Utopia\Response $response */
/** @var Utopia\Database\Database $dbForProject */
/** @var Appwrite\Event\Event $events */
/** @var Appwrite\Event\Audit $audits */
/** @var Appwrite\Event\Delete $deletes */
/** @var Appwrite\Stats\Stats $usage */
/** @var string $mode */
->action(function (string $collectionId, string $documentId, Response $response, Database $dbForProject, Event $events, EventAudit $audits, Delete $deletes, Stats $usage, string $mode) {
/**
* Skip Authorization to get the collection. Needed in case of empty permissions for document level permissions.