1
0
Fork 0
mirror of synced 2024-06-02 10:54:44 +12:00

Create attributes and indexes using database worker

This commit is contained in:
kodumbeats 2021-06-18 12:13:37 -04:00
parent 4a4165a304
commit afd347ca6f
3 changed files with 130 additions and 11 deletions

View file

@ -245,8 +245,9 @@ App::post('/v1/database/collections/:collectionId/attributes')
->param('filters', [], new ArrayList(new Whitelist(['encrypt', 'json'])), 'Array of filters.', true)
->inject('response')
->inject('dbForExternal')
->inject('database')
->inject('audits')
->action(function ($collectionId, $id, $type, $size, $required, $signed, $array, $filters, $response, $dbForExternal, $audits) {
->action(function ($collectionId, $id, $type, $size, $required, $signed, $array, $filters, $response, $dbForExternal, $database, $audits) {
/** @var Appwrite\Utopia\Response $response */
/** @var Utopia\Database\Database $dbForExternal*/
/** @var Appwrite\Event\Event $audits */
@ -257,9 +258,9 @@ App::post('/v1/database/collections/:collectionId/attributes')
throw new Exception('Collection not found', 404);
}
$success = $dbForExternal->createAttribute($collectionId, $id, $type, $size, $required, $signed, $array, $filters);
$success = $dbForExternal->addAttributeInQueue($collectionId, $id, $type, $size, $required, $signed, $array, $filters);
// Database->createAttribute() does not return a document
// Database->addAttributeInQueue() does not return a document
// So we need to create one for the response
$attribute = new Document([
'$collection' => $collectionId,
@ -272,6 +273,11 @@ App::post('/v1/database/collections/:collectionId/attributes')
'filters' => $filters
]);
$database
->setParam('type', CREATE_TYPE_ATTRIBUTE)
->setParam('document', $attribute)
;
$audits
->setParam('event', 'database.attributes.create')
->setParam('resource', 'database/attributes/'.$attribute->getId())
@ -445,8 +451,9 @@ App::post('/v1/database/collections/:collectionId/indexes')
->param('orders', [], new ArrayList(new Text(256)), 'Array of index orders.', true)
->inject('response')
->inject('dbForExternal')
->inject('database')
->inject('audits')
->action(function ($collectionId, $id, $type, $attributes, $lengths, $orders, $response, $dbForExternal, $audits) {
->action(function ($collectionId, $id, $type, $attributes, $lengths, $orders, $response, $dbForExternal, $database, $audits) {
/** @var Appwrite\Utopia\Response $response */
/** @var Utopia\Database\Database $dbForExternal */
/** @var Appwrite\Event\Event $audits */
@ -457,7 +464,7 @@ App::post('/v1/database/collections/:collectionId/indexes')
throw new Exception('Collection not found', 404);
}
$success = $dbForExternal->createIndex($collectionId, $id, $type, $attributes, $lengths, $orders);
$success = $dbForExternal->addIndexInQueue($collectionId, $id, $type, $attributes, $lengths, $orders);
// Database->createIndex() does not return a document
// So we need to create one for the response
@ -470,6 +477,11 @@ App::post('/v1/database/collections/:collectionId/indexes')
'orders' => $orders,
]);
$database
->setParam('type', CREATE_TYPE_INDEX)
->setParam('document', $index)
;
$audits
->setParam('event', 'database.indexes.create')
->setParam('resource', 'database/indexes/'.$index->getId())

View file

@ -9,7 +9,7 @@ use Utopia\Abuse\Adapters\TimeLimit;
use Utopia\Storage\Device\Local;
use Utopia\Storage\Storage;
App::init(function ($utopia, $request, $response, $project, $user, $register, $events, $audits, $usage, $deletes, $dbForInternal) {
App::init(function ($utopia, $request, $response, $project, $user, $register, $events, $audits, $usage, $deletes, $database, $dbForInternal) {
/** @var Utopia\App $utopia */
/** @var Utopia\Swoole\Request $request */
/** @var Appwrite\Utopia\Response $response */
@ -20,6 +20,7 @@ App::init(function ($utopia, $request, $response, $project, $user, $register, $e
/** @var Appwrite\Event\Event $audits */
/** @var Appwrite\Event\Event $usage */
/** @var Appwrite\Event\Event $deletes */
/** @var Appwrite\Event\Event $database */
/** @var Appwrite\Event\Event $functions */
/** @var Utopia\Database\Database $dbForInternal */
@ -109,7 +110,10 @@ App::init(function ($utopia, $request, $response, $project, $user, $register, $e
->setParam('projectId', $project->getId())
;
}, ['utopia', 'request', 'response', 'project', 'user', 'register', 'events', 'audits', 'usage', 'deletes', 'dbForInternal'], 'api');
$database
->setParam('projectId', $project->getId())
;
}, ['utopia', 'request', 'response', 'project', 'user', 'register', 'events', 'audits', 'usage', 'deletes', 'database', 'dbForInternal'], 'api');
App::init(function ($utopia, $request, $response, $project, $user) {
@ -166,7 +170,7 @@ App::init(function ($utopia, $request, $response, $project, $user) {
}, ['utopia', 'request', 'response', 'project', 'user'], 'auth');
App::shutdown(function ($utopia, $request, $response, $project, $events, $audits, $usage, $deletes, $mode) {
App::shutdown(function ($utopia, $request, $response, $project, $events, $audits, $usage, $deletes, $database, $mode) {
/** @var Utopia\App $utopia */
/** @var Utopia\Swoole\Request $request */
/** @var Appwrite\Utopia\Response $response */
@ -175,6 +179,7 @@ App::shutdown(function ($utopia, $request, $response, $project, $events, $audits
/** @var Appwrite\Event\Event $audits */
/** @var Appwrite\Event\Event $usage */
/** @var Appwrite\Event\Event $deletes */
/** @var Appwrite\Event\Event $database */
/** @var Appwrite\Event\Event $functions */
/** @var bool $mode */
@ -204,6 +209,10 @@ App::shutdown(function ($utopia, $request, $response, $project, $events, $audits
if (!empty($deletes->getParam('type')) && !empty($deletes->getParam('document'))) {
$deletes->trigger();
}
if (!empty($database->getParam('type')) && !empty($database->getParam('document'))) {
$database->trigger();
}
$route = $utopia->match($request);
if (App::getEnv('_APP_USAGE_STATS', 'enabled') == 'enabled'
@ -218,4 +227,4 @@ App::shutdown(function ($utopia, $request, $response, $project, $events, $audits
;
}
}, ['utopia', 'request', 'response', 'project', 'events', 'audits', 'usage', 'deletes', 'mode'], 'api');
}, ['utopia', 'request', 'response', 'project', 'events', 'audits', 'usage', 'deletes', 'database', 'mode'], 'api');

View file

@ -29,9 +29,65 @@ class DatabaseV1 extends Worker
public function run(): void
{
$collections = Config::getParam('collections2');
$projectId = $this->args['projectId'] ?? '';
$type = $this->args['type'] ?? '';
switch (strval($type)) {
case CREATE_TYPE_ATTRIBUTE:
$attribute = $this->args['document'] ?? '';
$attribute = new Document($attribute);
$this->createAttribute($attribute, $projectId);
break;
case CREATE_TYPE_INDEX:
$attribute = $this->args['document'] ?? '';
$attribute = new Document($attribute);
$this->createAttribute($attribute, $projectId);
break;
var_dump($collections);
// case DELETE_TYPE_DOCUMENT:
// $document = $this->args['document'] ?? '';
// $document = new Document($document);
// switch ($document->getCollection()) {
// case Database::SYSTEM_COLLECTION_PROJECTS:
// $this->deleteProject($document);
// break;
// case Database::SYSTEM_COLLECTION_FUNCTIONS:
// $this->deleteFunction($document, $projectId);
// break;
// case Database::SYSTEM_COLLECTION_USERS:
// $this->deleteUser($document, $projectId);
// break;
// case Database::SYSTEM_COLLECTION_COLLECTIONS:
// $this->deleteDocuments($document, $projectId);
// break;
// default:
// Console::error('No lazy delete operation available for document of type: '.$document->getCollection());
// break;
// }
// break;
// case DELETE_TYPE_EXECUTIONS:
// $this->deleteExecutionLogs($this->args['timestamp']);
// break;
// case DELETE_TYPE_AUDIT:
// $this->deleteAuditLogs($this->args['timestamp']);
// break;
// case DELETE_TYPE_ABUSE:
// $this->deleteAbuseLogs($this->args['timestamp']);
// break;
// case DELETE_TYPE_CERTIFICATES:
// $document = new Document($this->args['document']);
// $this->deleteCertificates($document);
// break;
default:
Console::error('No database operation for type: '.$type);
break;
}
}
@ -39,6 +95,48 @@ class DatabaseV1 extends Worker
{
}
/**
* @param Document $attribute
*/
protected function createAttribute($attribute, $projectId)
{
$dbForExternal = $this->getExternalDB($projectId);
$collectionId = $attribute->getCollection();
$id = $attribute->getAttribute('$id');
$type = $attribute->getAttribute('type');
$size = $attribute->getAttribute('size');
$required = $attribute->getAttribute('required');
$signed = $attribute->getAttribute('signed');
$array = $attribute->getAttribute('array');
$filters = $attribute->getAttribute('filters');
$success = $dbForExternal->createAttribute($collectionId, $id, $type, $size, $required, $signed, $array, $filters);
if ($success) {
$removed = $dbForExternal->removeAttributeInQueue($collectionId, $id);
}
}
/**
* @param Document $index
*/
protected function createIndex($index, $projectId)
{
$dbForExternal = $this->getExternalDB($projectId);
$collectionId = $index->getCollection();
$id = $index->getAttribute('$id');
$type = $index->getAttribute('type');
$attributes = $index->getAttribute('attributes');
$lengths = $index->getAttribute('lengths');
$orders = $index->getAttribute('orders');
$success = $dbForExternal->createIndex($collectionId, $id, $type, $attributes, $lengths, $orders);
if ($success) {
$dbForExternal->removeIndexInQueue($collectionId, $id);
}
}
/**
* @param string $projectId
*