1
0
Fork 0
mirror of synced 2024-06-02 19:04:49 +12:00
appwrite/app/controllers/api/database.php

1383 lines
66 KiB
PHP
Raw Normal View History

2019-05-09 18:54:39 +12:00
<?php
use Utopia\App;
use Utopia\Exception;
2021-08-16 09:09:40 +12:00
use Utopia\Audit\Audit;
use Utopia\Validator\Boolean;
use Utopia\Validator\FloatValidator;
2021-07-06 08:27:20 +12:00
use Utopia\Validator\Integer;
2019-05-09 18:54:39 +12:00
use Utopia\Validator\Range;
use Utopia\Validator\WhiteList;
use Utopia\Validator\Text;
use Utopia\Validator\ArrayList;
use Utopia\Validator\JSON;
2021-08-16 09:09:40 +12:00
use Utopia\Database\Database;
use Utopia\Database\Document;
use Utopia\Database\Query;
2021-08-25 11:35:32 +12:00
use Utopia\Database\Adapter\MariaDB;
2021-06-23 07:34:42 +12:00
use Utopia\Database\Validator\Key;
2021-07-06 08:27:20 +12:00
use Utopia\Database\Validator\Permissions;
use Utopia\Database\Validator\QueryValidator;
use Utopia\Database\Validator\Queries as QueriesValidator;
use Utopia\Database\Validator\Structure;
2021-07-06 08:27:20 +12:00
use Utopia\Database\Validator\UID;
2021-06-16 02:24:51 +12:00
use Utopia\Database\Exception\Authorization as AuthorizationException;
2021-08-16 09:09:40 +12:00
use Utopia\Database\Exception\Duplicate as DuplicateException;
2021-07-30 11:36:46 +12:00
use Utopia\Database\Exception\Limit as LimitException;
use Utopia\Database\Exception\Structure as StructureException;
2021-08-17 07:24:15 +12:00
use Appwrite\Utopia\Response;
use Appwrite\Database\Validator\CustomId;
2021-08-11 06:03:32 +12:00
use DeviceDetector\DeviceDetector;
2021-08-22 09:48:07 +12:00
$attributesCallback = function ($collectionId, $attribute, $response, $dbForInternal, $database, $audits) {
/** @var Utopia\Database\Document $attribute*/
2021-07-27 13:00:36 +12:00
/** @var Appwrite\Utopia\Response $response */
2021-08-21 16:48:28 +12:00
/** @var Utopia\Database\Database $dbForInternal*/
2021-07-27 13:00:36 +12:00
/** @var Appwrite\Event\Event $database */
/** @var Appwrite\Event\Event $audits */
$attributeId = $attribute->getId();
$type = $attribute->getAttribute('type', '');
$size = $attribute->getAttribute('size', 0);
$required = $attribute->getAttribute('required', true);
$min = $attribute->getAttribute('min', null);
$max = $attribute->getAttribute('max', null);
$signed = $attribute->getAttribute('signed', true); // integers are signed by default
$array = $attribute->getAttribute('array', false);
2021-08-22 09:48:07 +12:00
$format = $attribute->getAttribute('format', '');
$formatOptions = $attribute->getAttribute('formatOptions', []);
2021-07-27 13:00:36 +12:00
$filters = $attribute->getAttribute('filters', []); // filters are hidden from the endpoint
2021-08-22 09:48:07 +12:00
$default = $attribute->getAttribute('default', null);
$default = (empty($default)) ? null : (int)$default;
2021-07-27 13:00:36 +12:00
2021-08-17 07:24:15 +12:00
$collection = $dbForInternal->getDocument('collections', $collectionId);
2021-07-27 13:00:36 +12:00
if ($collection->isEmpty()) {
throw new Exception('Collection not found', 404);
}
// TODO@kodumbeats how to depend on $size for Text validator length
// Ensure attribute default is within required size
if ($size > 0 && !\is_null($default)) {
$validator = new Text($size);
if (!$validator->isValid($default)) {
throw new Exception('Length of default attribute exceeds attribute size', 400);
}
}
2021-08-22 09:48:07 +12:00
if (!empty($format)) {
if (!Structure::hasFormat($format, $type)) {
throw new Exception("Format {$format} not available for {$type} attributes.", 400);
2021-07-28 06:19:37 +12:00
}
2021-07-27 13:00:36 +12:00
}
2021-08-21 16:48:28 +12:00
try {
$attribute = $dbForInternal->createDocument('attributes', new Document([
'$id' => $collectionId.'_'.$attributeId,
'key' => $attributeId,
'collectionId' => $collectionId,
'type' => $type,
2021-08-22 20:04:51 +12:00
'status' => 'processing', // processing, available, failed, deleting
2021-08-21 16:48:28 +12:00
'size' => $size,
'required' => $required,
'signed' => $signed,
2021-08-22 09:48:07 +12:00
'default' => $default,
2021-08-21 16:48:28 +12:00
'array' => $array,
'format' => $format,
2021-08-22 09:48:07 +12:00
'formatOptions' => $formatOptions,
2021-08-21 16:48:28 +12:00
'filters' => $filters,
]));
} catch (DuplicateException $th) {
throw new Exception('Attribute already exists', 409);
}
2021-08-22 09:48:07 +12:00
$dbForInternal->purgeDocument('collections', $collectionId);
2021-07-27 13:00:36 +12:00
$database
2021-08-09 11:46:14 +12:00
->setParam('type', DATABASE_TYPE_CREATE_ATTRIBUTE)
2021-08-19 16:05:44 +12:00
->setParam('collection', $collection)
2021-07-27 13:00:36 +12:00
->setParam('document', $attribute)
;
$audits
->setParam('event', 'database.attributes.create')
2021-08-13 23:19:16 +12:00
->setParam('resource', 'database/collection/'.$collection->getId())
2021-07-27 13:00:36 +12:00
->setParam('data', $attribute)
;
$response->setStatusCode(Response::STATUS_CODE_CREATED);
$response->dynamic($attribute, Response::MODEL_ATTRIBUTE);
};
2020-06-29 05:31:21 +12:00
App::post('/v1/database/collections')
2019-05-09 18:54:39 +12:00
->desc('Create Collection')
2020-06-26 06:32:12 +12:00
->groups(['api', 'database'])
2020-10-31 08:53:27 +13:00
->label('event', 'database.collections.create')
->label('scope', 'collections.write')
2021-04-16 19:22:17 +12:00
->label('sdk.auth', [APP_AUTH_TYPE_KEY])
2021-08-01 08:40:34 +12:00
->label('sdk.namespace', 'database')
2019-05-09 18:54:39 +12:00
->label('sdk.method', 'createCollection')
2019-10-08 20:09:35 +13:00
->label('sdk.description', '/docs/references/database/create-collection.md')
2020-11-12 10:02:24 +13:00
->label('sdk.response.code', Response::STATUS_CODE_CREATED)
->label('sdk.response.type', Response::CONTENT_TYPE_JSON)
->label('sdk.response.model', Response::MODEL_COLLECTION)
->param('collectionId', '', new CustomId(), 'Unique Id. Choose your own unique ID or pass the string `unique()` to auto generate it. Valid chars are a-z, A-Z, 0-9, period, hyphen, and underscore. Can\'t start with a special char. Max length is 36 chars.')
2021-07-29 20:09:24 +12:00
->param('name', '', new Text(128), 'Collection name. Max length: 128 chars.')
2021-08-19 16:05:44 +12:00
->param('permission', null, new WhiteList(['document', 'collection']), 'Permissions type model to use for reading documents in this collection. You can use collection-level permission set once on the collection using the `read` and `write` params, or you can set document-level permission where each document read and write params will decide who has access to read and write to each document individually. [learn more about permissions](/docs/permissions) and get a full list of available permissions.')
2021-07-06 08:27:20 +12:00
->param('read', null, new Permissions(), 'An array of strings with read permissions. By default no user is granted with any read permissions. [learn more about permissions](/docs/permissions) and get a full list of available permissions.')
->param('write', null, new Permissions(), 'An array of strings with write permissions. By default no user is granted with any write permissions. [learn more about permissions](/docs/permissions) and get a full list of available permissions.')
2020-12-27 04:05:04 +13:00
->inject('response')
2021-08-17 05:28:33 +12:00
->inject('dbForInternal')
->inject('dbForExternal')
2020-12-27 04:05:04 +13:00
->inject('audits')
2021-08-19 16:05:44 +12:00
->action(function ($collectionId, $name, $permission, $read, $write, $response, $dbForInternal, $dbForExternal, $audits) {
2020-10-30 02:50:49 +13:00
/** @var Appwrite\Utopia\Response $response */
/** @var Utopia\Database\Database $dbForExternal*/
2020-07-06 02:19:59 +12:00
/** @var Appwrite\Event\Event $audits */
2021-07-19 21:25:53 +12:00
$collectionId = $collectionId == 'unique()' ? $dbForExternal->getId() : $collectionId;
2021-07-06 08:27:20 +12:00
2021-08-17 05:28:33 +12:00
try {
2021-08-19 16:05:44 +12:00
$dbForExternal->createCollection($collectionId);
2021-08-17 05:28:33 +12:00
$collection = $dbForInternal->createDocument('collections', new Document([
'$id' => $collectionId,
2021-08-21 16:48:28 +12:00
'$read' => $read ?? [], // Collection permissions for collection documents (based on permission model)
'$write' => $write ?? [], // Collection permissions for collection documents (based on permission model)
'permission' => $permission, // Permissions model type (document vs collection)
2021-08-19 16:05:44 +12:00
'dateCreated' => time(),
'dateUpdated' => time(),
2021-08-17 05:28:33 +12:00
'name' => $name,
'search' => implode(' ', [$collectionId, $name]),
]));
} catch (DuplicateException $th) {
2021-08-17 05:28:33 +12:00
throw new Exception('Collection already exists', 409);
}
2019-05-09 18:54:39 +12:00
2020-07-06 02:19:59 +12:00
$audits
2020-06-30 09:43:34 +12:00
->setParam('event', 'database.collections.create')
->setParam('resource', 'database/collection/'.$collection->getId())
->setParam('data', $collection->getArrayCopy())
2020-06-30 09:43:34 +12:00
;
2021-05-27 22:09:14 +12:00
$response->setStatusCode(Response::STATUS_CODE_CREATED);
2021-07-26 02:47:18 +12:00
$response->dynamic($collection, Response::MODEL_COLLECTION);
2020-12-27 04:05:04 +13:00
});
2019-05-09 18:54:39 +12:00
2020-06-29 05:31:21 +12:00
App::get('/v1/database/collections')
2020-02-01 11:34:07 +13:00
->desc('List Collections')
2020-06-26 06:32:12 +12:00
->groups(['api', 'database'])
2020-02-01 11:34:07 +13:00
->label('scope', 'collections.read')
2021-04-16 19:22:17 +12:00
->label('sdk.auth', [APP_AUTH_TYPE_KEY])
2021-08-01 08:40:34 +12:00
->label('sdk.namespace', 'database')
2020-02-01 11:34:07 +13:00
->label('sdk.method', 'listCollections')
->label('sdk.description', '/docs/references/database/list-collections.md')
2020-11-12 10:02:24 +13:00
->label('sdk.response.code', Response::STATUS_CODE_OK)
->label('sdk.response.type', Response::CONTENT_TYPE_JSON)
->label('sdk.response.model', Response::MODEL_COLLECTION_LIST)
2021-07-06 07:19:18 +12:00
->param('search', '', new Text(256), 'Search term to filter your list results. Max length: 256 chars.', true)
2020-09-11 02:40:14 +12:00
->param('limit', 25, new Range(0, 100), 'Results limit value. By default will return maximum 25 results. Maximum of 100 results allowed per request.', true)
->param('offset', 0, new Range(0, 40000), 'Results offset. The default value is 0. Use this param to manage pagination.', true)
->param('after', '', new UID(), 'ID of the collection used as the starting point for the query, excluding the collection itself. Should be used for efficient pagination when working with large sets of data.', true)
2021-07-06 07:19:18 +12:00
->param('orderType', 'ASC', new WhiteList(['ASC', 'DESC'], true), 'Order result by ASC or DESC order.', true)
2020-12-27 04:05:04 +13:00
->inject('response')
2021-08-17 05:28:33 +12:00
->inject('dbForInternal')
->action(function ($search, $limit, $offset, $after, $orderType, $response, $dbForInternal) {
2020-10-30 02:50:49 +13:00
/** @var Appwrite\Utopia\Response $response */
2021-08-17 05:28:33 +12:00
/** @var Utopia\Database\Database $dbForInternal */
2020-06-30 09:43:34 +12:00
2021-08-17 05:28:33 +12:00
$queries = [];
if (!empty($search)) {
$queries[] = new Query('name', Query::TYPE_SEARCH, [$search]);
}
2020-06-30 09:43:34 +12:00
2021-08-07 00:35:57 +12:00
if (!empty($after)) {
$afterCollection = $dbForInternal->getDocument('collections', $after);
2021-08-07 00:35:57 +12:00
if ($afterCollection->isEmpty()) {
throw new Exception("Collection '{$after}' for the 'after' value not found.", 400);
2021-08-07 00:35:57 +12:00
}
}
2021-07-26 02:47:18 +12:00
$response->dynamic(new Document([
2021-08-23 16:06:53 +12:00
'collections' => $dbForInternal->find('collections', $queries, $limit, $offset, [], [$orderType], $afterCollection ?? null),
2021-08-17 07:24:15 +12:00
'sum' => $dbForInternal->count('collections', $queries, APP_LIMIT_COUNT),
2020-10-31 08:53:27 +13:00
]), Response::MODEL_COLLECTION_LIST);
2020-12-27 04:05:04 +13:00
});
2020-02-01 11:34:07 +13:00
2020-06-29 05:31:21 +12:00
App::get('/v1/database/collections/:collectionId')
2020-02-01 11:34:07 +13:00
->desc('Get Collection')
2020-06-26 06:32:12 +12:00
->groups(['api', 'database'])
2020-02-01 11:34:07 +13:00
->label('scope', 'collections.read')
2021-04-16 19:22:17 +12:00
->label('sdk.auth', [APP_AUTH_TYPE_KEY])
2021-08-01 08:40:34 +12:00
->label('sdk.namespace', 'database')
2020-02-01 11:34:07 +13:00
->label('sdk.method', 'getCollection')
->label('sdk.description', '/docs/references/database/get-collection.md')
2020-11-12 10:02:24 +13:00
->label('sdk.response.code', Response::STATUS_CODE_OK)
->label('sdk.response.type', Response::CONTENT_TYPE_JSON)
->label('sdk.response.model', Response::MODEL_COLLECTION)
2020-09-11 02:40:14 +12:00
->param('collectionId', '', new UID(), 'Collection unique ID.')
2020-12-27 04:05:04 +13:00
->inject('response')
2021-08-17 05:28:33 +12:00
->inject('dbForInternal')
->action(function ($collectionId, $response, $dbForInternal) {
2020-10-30 02:50:49 +13:00
/** @var Appwrite\Utopia\Response $response */
2021-08-17 05:28:33 +12:00
/** @var Utopia\Database\Database $dbForInternal */
2021-06-12 06:07:05 +12:00
2021-08-17 05:28:33 +12:00
$collection = $dbForInternal->getDocument('collections', $collectionId);
2020-06-30 09:43:34 +12:00
if ($collection->isEmpty()) {
2020-06-30 09:43:34 +12:00
throw new Exception('Collection not found', 404);
2020-02-01 11:34:07 +13:00
}
2020-06-30 09:43:34 +12:00
2021-07-26 02:47:18 +12:00
$response->dynamic($collection, Response::MODEL_COLLECTION);
2020-12-27 04:05:04 +13:00
});
2020-02-01 11:34:07 +13:00
2021-08-11 06:03:32 +12:00
App::get('/v1/database/collections/:collectionId/logs')
2021-08-14 22:13:24 +12:00
->desc('List Collection Logs')
2021-08-11 06:03:32 +12:00
->groups(['api', 'database'])
->label('scope', 'collections.read')
->label('sdk.auth', [APP_AUTH_TYPE_ADMIN])
->label('sdk.namespace', 'database')
2021-08-14 22:13:24 +12:00
->label('sdk.method', 'listCollectionLogs')
2021-08-11 06:03:32 +12:00
->label('sdk.description', '/docs/references/database/get-collection-logs.md')
->label('sdk.response.code', Response::STATUS_CODE_OK)
->label('sdk.response.type', Response::CONTENT_TYPE_JSON)
->label('sdk.response.model', Response::MODEL_LOG_LIST)
->param('collectionId', '', new UID(), 'Collection unique ID.')
->inject('response')
->inject('dbForInternal')
->inject('dbForExternal')
->inject('locale')
->inject('geodb')
->action(function ($collectionId, $response, $dbForInternal, $dbForExternal, $locale, $geodb) {
/** @var Appwrite\Utopia\Response $response */
/** @var Utopia\Database\Document $project */
/** @var Utopia\Database\Database $dbForInternal */
/** @var Utopia\Database\Database $dbForExternal */
/** @var Utopia\Locale\Locale $locale */
/** @var MaxMind\Db\Reader $geodb */
$collection = $dbForExternal->getCollection($collectionId);
if ($collection->isEmpty()) {
throw new Exception('Collection not found', 404);
}
$audit = new Audit($dbForInternal);
$logs = $audit->getLogsByResource('database/collection/'.$collection->getId());
$output = [];
foreach ($logs as $i => &$log) {
$log['userAgent'] = (!empty($log['userAgent'])) ? $log['userAgent'] : 'UNKNOWN';
$dd = new DeviceDetector($log['userAgent']);
$dd->skipBotDetection(); // OPTIONAL: If called, bot detection will completely be skipped (bots will be detected as regular devices then)
$dd->parse();
$os = $dd->getOs();
$osCode = (isset($os['short_name'])) ? $os['short_name'] : '';
$osName = (isset($os['name'])) ? $os['name'] : '';
$osVersion = (isset($os['version'])) ? $os['version'] : '';
$client = $dd->getClient();
$clientType = (isset($client['type'])) ? $client['type'] : '';
$clientCode = (isset($client['short_name'])) ? $client['short_name'] : '';
$clientName = (isset($client['name'])) ? $client['name'] : '';
$clientVersion = (isset($client['version'])) ? $client['version'] : '';
$clientEngine = (isset($client['engine'])) ? $client['engine'] : '';
$clientEngineVersion = (isset($client['engine_version'])) ? $client['engine_version'] : '';
$output[$i] = new Document([
'event' => $log['event'],
2021-08-13 23:19:16 +12:00
'userId' => $log['userId'],
2021-08-14 22:13:24 +12:00
'userEmail' => $log['data']['userEmail'] ?? null,
'userName' => $log['data']['userName'] ?? null,
'mode' => $log['data']['mode'] ?? null,
2021-08-11 06:03:32 +12:00
'ip' => $log['ip'],
'time' => $log['time'],
'osCode' => $osCode,
'osName' => $osName,
'osVersion' => $osVersion,
'clientType' => $clientType,
'clientCode' => $clientCode,
'clientName' => $clientName,
'clientVersion' => $clientVersion,
'clientEngine' => $clientEngine,
'clientEngineVersion' => $clientEngineVersion,
'deviceName' => $dd->getDeviceName(),
'deviceBrand' => $dd->getBrandName(),
'deviceModel' => $dd->getModel(),
]);
$record = $geodb->get($log['ip']);
if ($record) {
$output[$i]['countryCode'] = $locale->getText('countries.'.strtolower($record['country']['iso_code']), false) ? \strtolower($record['country']['iso_code']) : '--';
$output[$i]['countryName'] = $locale->getText('countries.'.strtolower($record['country']['iso_code']), $locale->getText('locale.country.unknown'));
} else {
$output[$i]['countryCode'] = '--';
$output[$i]['countryName'] = $locale->getText('locale.country.unknown');
}
}
$response->dynamic(new Document(['logs' => $output]), Response::MODEL_LOG_LIST);
});
2020-06-29 05:31:21 +12:00
App::put('/v1/database/collections/:collectionId')
2019-06-09 23:44:58 +12:00
->desc('Update Collection')
2020-06-26 06:32:12 +12:00
->groups(['api', 'database'])
->label('scope', 'collections.write')
2020-10-31 08:53:27 +13:00
->label('event', 'database.collections.update')
2021-04-16 19:22:17 +12:00
->label('sdk.auth', [APP_AUTH_TYPE_KEY])
2021-08-01 08:40:34 +12:00
->label('sdk.namespace', 'database')
->label('sdk.method', 'updateCollection')
2019-10-08 20:09:35 +13:00
->label('sdk.description', '/docs/references/database/update-collection.md')
2020-11-12 10:02:24 +13:00
->label('sdk.response.code', Response::STATUS_CODE_OK)
->label('sdk.response.type', Response::CONTENT_TYPE_JSON)
->label('sdk.response.model', Response::MODEL_COLLECTION)
2020-09-11 02:40:14 +12:00
->param('collectionId', '', new UID(), 'Collection unique ID.')
2021-07-29 20:09:24 +12:00
->param('name', null, new Text(128), 'Collection name. Max length: 128 chars.')
2021-08-19 16:05:44 +12:00
->param('permission', null, new WhiteList(['document', 'collection']), 'Permissions type model to use for reading documents in this collection. You can use collection-level permission set once on the collection using the `read` and `write` params, or you can set document-level permission where each document read and write params will decide who has access to read and write to each document individually. [learn more about permissions](/docs/permissions) and get a full list of available permissions.')
2021-07-06 08:27:20 +12:00
->param('read', null, new Permissions(), 'An array of strings with read permissions. By default inherits the existing read permissions. [learn more about permissions](/docs/permissions) and get a full list of available permissions.', true)
->param('write', null, new Permissions(), 'An array of strings with write permissions. By default inherits the existing write permissions. [learn more about permissions](/docs/permissions) and get a full list of available permissions.', true)
2020-12-27 04:05:04 +13:00
->inject('response')
2021-08-17 05:28:33 +12:00
->inject('dbForInternal')
2020-12-27 04:05:04 +13:00
->inject('audits')
2021-08-19 16:05:44 +12:00
->action(function ($collectionId, $name, $permission, $read, $write, $response, $dbForInternal, $audits) {
2020-10-30 02:50:49 +13:00
/** @var Appwrite\Utopia\Response $response */
2021-08-17 05:28:33 +12:00
/** @var Utopia\Database\Database $dbForInternal */
2020-07-06 02:19:59 +12:00
/** @var Appwrite\Event\Event $audits */
2019-05-09 18:54:39 +12:00
2021-08-17 05:28:33 +12:00
$collection = $dbForInternal->getDocument('collections', $collectionId);
2019-10-01 17:57:41 +13:00
2021-06-17 08:36:18 +12:00
if ($collection->isEmpty()) {
2020-06-30 09:43:34 +12:00
throw new Exception('Collection not found', 404);
}
2019-10-01 17:57:41 +13:00
2021-06-17 08:36:18 +12:00
$read = (is_null($read)) ? ($collection->getRead() ?? []) : $read; // By default inherit read permissions
$write = (is_null($write)) ? ($collection->getWrite() ?? []) : $write; // By default inherit write permissions
2019-05-09 18:54:39 +12:00
2020-06-30 09:43:34 +12:00
try {
2021-08-17 05:28:33 +12:00
$collection = $dbForInternal->updateDocument('collections', $collection->getId(), $collection
2021-08-21 16:48:28 +12:00
->setAttribute('$write', $write)
->setAttribute('$read', $read)
2021-08-17 05:28:33 +12:00
->setAttribute('name', $name)
2021-08-21 16:48:28 +12:00
->setAttribute('permission', $permission)
2021-08-19 16:05:44 +12:00
->setAttribute('dateUpdated', time())
2021-08-17 05:28:33 +12:00
->setAttribute('search', implode(' ', [$collectionId, $name]))
);
2021-08-19 16:05:44 +12:00
}
catch (AuthorizationException $exception) {
2020-08-30 16:49:24 +12:00
throw new Exception('Unauthorized permissions', 401);
2021-08-19 16:05:44 +12:00
}
catch (StructureException $exception) {
2020-06-30 09:43:34 +12:00
throw new Exception('Bad structure. '.$exception->getMessage(), 400);
2021-08-17 05:28:33 +12:00
}
2020-07-06 02:19:59 +12:00
$audits
2020-06-30 09:43:34 +12:00
->setParam('event', 'database.collections.update')
2021-08-13 23:19:16 +12:00
->setParam('resource', 'database/collection/'.$collection->getId())
2020-10-31 08:53:27 +13:00
->setParam('data', $collection->getArrayCopy())
2020-06-30 09:43:34 +12:00
;
2021-07-26 02:47:18 +12:00
$response->dynamic($collection, Response::MODEL_COLLECTION);
2020-12-27 04:05:04 +13:00
});
2019-05-09 18:54:39 +12:00
2020-06-29 05:31:21 +12:00
App::delete('/v1/database/collections/:collectionId')
2019-05-09 18:54:39 +12:00
->desc('Delete Collection')
2020-06-26 06:32:12 +12:00
->groups(['api', 'database'])
->label('scope', 'collections.write')
2020-10-31 08:53:27 +13:00
->label('event', 'database.collections.delete')
2021-04-16 19:22:17 +12:00
->label('sdk.auth', [APP_AUTH_TYPE_KEY])
2021-08-01 08:40:34 +12:00
->label('sdk.namespace', 'database')
2019-05-09 18:54:39 +12:00
->label('sdk.method', 'deleteCollection')
2019-10-08 20:09:35 +13:00
->label('sdk.description', '/docs/references/database/delete-collection.md')
2020-11-12 10:02:24 +13:00
->label('sdk.response.code', Response::STATUS_CODE_NOCONTENT)
->label('sdk.response.model', Response::MODEL_NONE)
2020-09-11 02:40:14 +12:00
->param('collectionId', '', new UID(), 'Collection unique ID.')
2020-12-27 04:05:04 +13:00
->inject('response')
2021-08-17 05:28:33 +12:00
->inject('dbForInternal')
->inject('dbForExternal')
2020-12-27 04:05:04 +13:00
->inject('events')
->inject('audits')
->inject('deletes')
2021-08-17 05:28:33 +12:00
->action(function ($collectionId, $response, $dbForInternal, $dbForExternal, $events, $audits, $deletes) {
2020-10-30 02:50:49 +13:00
/** @var Appwrite\Utopia\Response $response */
2021-08-17 05:28:33 +12:00
/** @var Utopia\Database\Database $dbForInternal */
/** @var Utopia\Database\Database $dbForExternal */
2020-12-07 11:14:57 +13:00
/** @var Appwrite\Event\Event $events */
2020-07-06 02:19:59 +12:00
/** @var Appwrite\Event\Event $audits */
2019-05-09 18:54:39 +12:00
2021-08-17 05:28:33 +12:00
$collection = $dbForInternal->getDocument('collections', $collectionId);
2019-05-09 18:54:39 +12:00
if ($collection->isEmpty()) {
2020-06-30 09:43:34 +12:00
throw new Exception('Collection not found', 404);
}
2021-08-19 16:05:44 +12:00
$dbForExternal->deleteCollection($collectionId); // TDOD move to DB worker
2021-08-17 05:28:33 +12:00
if (!$dbForInternal->deleteDocument('collections', $collectionId)) {
throw new Exception('Failed to remove collection from DB', 500);
}
2020-12-07 11:14:57 +13:00
$events
2021-07-26 02:47:18 +12:00
->setParam('eventData', $response->output($collection, Response::MODEL_COLLECTION))
2020-06-30 09:43:34 +12:00
;
2019-05-09 18:54:39 +12:00
2020-07-06 02:19:59 +12:00
$audits
2020-06-30 09:43:34 +12:00
->setParam('event', 'database.collections.delete')
2021-08-13 23:19:16 +12:00
->setParam('resource', 'database/collection/'.$collection->getId())
2020-10-31 08:53:27 +13:00
->setParam('data', $collection->getArrayCopy())
2020-06-30 09:43:34 +12:00
;
$response->noContent();
2020-12-27 04:05:04 +13:00
});
2019-05-09 18:54:39 +12:00
App::post('/v1/database/collections/:collectionId/attributes/string')
->desc('Create String Attribute')
2021-03-25 04:40:33 +13:00
->groups(['api', 'database'])
->label('event', 'database.attributes.create')
->label('scope', 'collections.write')
2021-08-01 08:40:34 +12:00
->label('sdk.auth', [APP_AUTH_TYPE_KEY])
2021-03-25 04:40:33 +13:00
->label('sdk.namespace', 'database')
->label('sdk.method', 'createStringAttribute')
->label('sdk.description', '/docs/references/database/create-attribute-string.md')
2021-03-25 04:40:33 +13:00
->label('sdk.response.code', Response::STATUS_CODE_CREATED)
->label('sdk.response.type', Response::CONTENT_TYPE_JSON)
->label('sdk.response.model', Response::MODEL_ATTRIBUTE)
->param('collectionId', '', new UID(), 'Collection unique ID. You can create a new collection using the Database service [server integration](/docs/server/database#createCollection).')
->param('attributeId', '', new Key(), 'Attribute ID.')
->param('size', null, new Integer(), 'Attribute size for text attributes, in number of characters.')
->param('required', null, new Boolean(), 'Is attribute required?')
->param('default', null, new Text(0), 'Default value for attribute when not provided. Cannot be set when attribute is required.', true)
2021-06-12 06:07:05 +12:00
->param('array', false, new Boolean(), 'Is attribute an array?', true)
2021-03-25 04:40:33 +13:00
->inject('response')
2021-08-17 07:24:15 +12:00
->inject('dbForInternal')
->inject('database')
2021-06-09 08:12:14 +12:00
->inject('audits')
2021-08-22 09:48:07 +12:00
->action(function ($collectionId, $attributeId, $size, $required, $default, $array, $response, $dbForInternal, $database, $audits) use ($attributesCallback) {
2021-03-25 04:40:33 +13:00
/** @var Appwrite\Utopia\Response $response */
2021-08-17 07:24:15 +12:00
/** @var Utopia\Database\Database $dbForInternal*/
/** @var Utopia\Database\Database $dbForExternal*/
/** @var Appwrite\Event\Event $database */
2021-03-25 04:40:33 +13:00
/** @var Appwrite\Event\Event $audits */
2021-08-19 16:05:44 +12:00
return $attributesCallback($collectionId, new Document([
2021-07-27 13:00:36 +12:00
'$id' => $attributeId,
'type' => Database::VAR_STRING,
'size' => $size,
'required' => $required,
'default' => $default,
'array' => $array,
2021-08-22 09:48:07 +12:00
]), $response, $dbForInternal, $database, $audits);
2021-07-27 13:00:36 +12:00
});
2021-06-11 01:15:00 +12:00
2021-07-27 13:00:36 +12:00
App::post('/v1/database/collections/:collectionId/attributes/email')
->desc('Create Email Attribute')
->groups(['api', 'database'])
->label('event', 'database.attributes.create')
->label('scope', 'collections.write')
2021-07-27 13:00:36 +12:00
->label('sdk.namespace', 'database')
2021-08-03 08:11:24 +12:00
->label('sdk.auth', [APP_AUTH_TYPE_KEY])
2021-07-27 13:00:36 +12:00
->label('sdk.method', 'createEmailAttribute')
->label('sdk.description', '/docs/references/database/create-attribute-email.md')
->label('sdk.response.code', Response::STATUS_CODE_CREATED)
->label('sdk.response.type', Response::CONTENT_TYPE_JSON)
->label('sdk.response.model', Response::MODEL_ATTRIBUTE)
->param('collectionId', '', new UID(), 'Collection unique ID. You can create a new collection using the Database service [server integration](/docs/server/database#createCollection).')
->param('attributeId', '', new Key(), 'Attribute ID.')
->param('required', null, new Boolean(), 'Is attribute required?')
->param('default', null, new Text(0), 'Default value for attribute when not provided. Cannot be set when attribute is required.', true)
->param('array', false, new Boolean(), 'Is attribute an array?', true)
->inject('response')
2021-08-17 07:24:15 +12:00
->inject('dbForInternal')
2021-07-27 13:00:36 +12:00
->inject('database')
->inject('audits')
2021-08-22 09:48:07 +12:00
->action(function ($collectionId, $attributeId, $required, $default, $array, $response, $dbForInternal, $database, $audits) use ($attributesCallback) {
2021-07-27 13:00:36 +12:00
/** @var Appwrite\Utopia\Response $response */
2021-08-17 07:24:15 +12:00
/** @var Utopia\Database\Database $dbForInternal*/
2021-07-27 13:00:36 +12:00
/** @var Appwrite\Event\Event $database */
/** @var Appwrite\Event\Event $audits */
2021-08-19 16:05:44 +12:00
return $attributesCallback($collectionId, new Document([
2021-07-27 13:00:36 +12:00
'$id' => $attributeId,
'type' => Database::VAR_STRING,
'size' => 254,
'required' => $required,
'default' => $default,
'array' => $array,
2021-08-22 09:48:07 +12:00
'format' => 'email',
]), $response, $dbForInternal, $database, $audits);
2021-07-27 13:00:36 +12:00
});
2021-07-27 13:00:36 +12:00
App::post('/v1/database/collections/:collectionId/attributes/ip')
->desc('Create IP Address Attribute')
->groups(['api', 'database'])
->label('event', 'database.attributes.create')
->label('scope', 'collections.write')
2021-07-27 13:00:36 +12:00
->label('sdk.namespace', 'database')
2021-08-03 08:11:24 +12:00
->label('sdk.auth', [APP_AUTH_TYPE_KEY])
2021-07-27 13:00:36 +12:00
->label('sdk.method', 'createIpAttribute')
->label('sdk.description', '/docs/references/database/create-attribute-ip.md')
->label('sdk.response.code', Response::STATUS_CODE_CREATED)
->label('sdk.response.type', Response::CONTENT_TYPE_JSON)
->label('sdk.response.model', Response::MODEL_ATTRIBUTE)
->param('collectionId', '', new UID(), 'Collection unique ID. You can create a new collection using the Database service [server integration](/docs/server/database#createCollection).')
->param('attributeId', '', new Key(), 'Attribute ID.')
->param('required', null, new Boolean(), 'Is attribute required?')
->param('default', null, new Text(0), 'Default value for attribute when not provided. Cannot be set when attribute is required.', true)
->param('array', false, new Boolean(), 'Is attribute an array?', true)
->inject('response')
2021-08-17 07:24:15 +12:00
->inject('dbForInternal')
2021-07-27 13:00:36 +12:00
->inject('database')
->inject('audits')
2021-08-22 09:48:07 +12:00
->action(function ($collectionId, $attributeId, $required, $default, $array, $response, $dbForInternal, $database, $audits) use ($attributesCallback) {
2021-07-27 13:00:36 +12:00
/** @var Appwrite\Utopia\Response $response */
2021-08-17 07:24:15 +12:00
/** @var Utopia\Database\Database $dbForInternal*/
2021-07-27 13:00:36 +12:00
/** @var Appwrite\Event\Event $database */
/** @var Appwrite\Event\Event $audits */
2021-06-11 01:15:00 +12:00
2021-08-19 16:05:44 +12:00
return $attributesCallback($collectionId, new Document([
2021-07-27 13:00:36 +12:00
'$id' => $attributeId,
'type' => Database::VAR_STRING,
'size' => 39,
'required' => $required,
'default' => $default,
'array' => $array,
2021-08-22 09:48:07 +12:00
'format' => 'ip',
]), $response, $dbForInternal, $database, $audits);
2021-07-27 13:00:36 +12:00
});
2021-07-27 13:00:36 +12:00
App::post('/v1/database/collections/:collectionId/attributes/url')
->desc('Create IP Address Attribute')
->groups(['api', 'database'])
->label('event', 'database.attributes.create')
->label('scope', 'collections.write')
2021-07-27 13:00:36 +12:00
->label('sdk.namespace', 'database')
2021-08-03 08:11:24 +12:00
->label('sdk.auth', [APP_AUTH_TYPE_KEY])
2021-07-27 13:00:36 +12:00
->label('sdk.method', 'createUrlAttribute')
->label('sdk.description', '/docs/references/database/create-attribute-url.md')
->label('sdk.response.code', Response::STATUS_CODE_CREATED)
->label('sdk.response.type', Response::CONTENT_TYPE_JSON)
->label('sdk.response.model', Response::MODEL_ATTRIBUTE)
->param('collectionId', '', new UID(), 'Collection unique ID. You can create a new collection using the Database service [server integration](/docs/server/database#createCollection).')
->param('attributeId', '', new Key(), 'Attribute ID.')
->param('required', null, new Boolean(), 'Is attribute required?')
->param('default', null, new Text(0), 'Default value for attribute when not provided. Cannot be set when attribute is required.', true)
->param('array', false, new Boolean(), 'Is attribute an array?', true)
->inject('response')
2021-08-17 07:24:15 +12:00
->inject('dbForInternal')
2021-07-27 13:00:36 +12:00
->inject('database')
->inject('audits')
2021-08-23 00:59:44 +12:00
->action(function ($collectionId, $attributeId, $required, $default, $array, $response, $dbForInternal, $database, $audits) use ($attributesCallback) {
2021-07-27 13:00:36 +12:00
/** @var Appwrite\Utopia\Response $response */
/** @var Utopia\Database\Database $dbForExternal*/
/** @var Appwrite\Event\Event $database */
/** @var Appwrite\Event\Event $audits */
2021-08-19 16:05:44 +12:00
return $attributesCallback($collectionId, new Document([
'$id' => $attributeId,
2021-07-27 13:00:36 +12:00
'type' => Database::VAR_STRING,
2021-08-23 00:59:44 +12:00
'size' => 2000,
'required' => $required,
2021-07-03 05:29:03 +12:00
'default' => $default,
'array' => $array,
2021-08-22 09:48:07 +12:00
'format' => 'url',
]), $response, $dbForInternal, $database, $audits);
});
2021-03-25 04:40:33 +13:00
App::post('/v1/database/collections/:collectionId/attributes/integer')
->desc('Create Integer Attribute')
->groups(['api', 'database'])
->label('event', 'database.attributes.create')
->label('scope', 'collections.write')
->label('sdk.namespace', 'database')
2021-08-03 08:11:24 +12:00
->label('sdk.auth', [APP_AUTH_TYPE_KEY])
->label('sdk.method', 'createIntegerAttribute')
->label('sdk.description', '/docs/references/database/create-attribute-integer.md')
->label('sdk.response.code', Response::STATUS_CODE_CREATED)
->label('sdk.response.type', Response::CONTENT_TYPE_JSON)
->label('sdk.response.model', Response::MODEL_ATTRIBUTE)
->param('collectionId', '', new UID(), 'Collection unique ID. You can create a new collection using the Database service [server integration](/docs/server/database#createCollection).')
->param('attributeId', '', new Key(), 'Attribute ID.')
->param('required', null, new Boolean(), 'Is attribute required?')
->param('min', null, new Integer(), 'Minimum value to enforce on new documents', true)
->param('max', null, new Integer(), 'Maximum value to enforce on new documents', true)
->param('default', null, new Integer(), 'Default value for attribute when not provided. Cannot be set when attribute is required.', true)
->param('array', false, new Boolean(), 'Is attribute an array?', true)
->inject('response')
2021-08-17 07:24:15 +12:00
->inject('dbForInternal')
->inject('database')
->inject('audits')
2021-08-22 09:48:07 +12:00
->action(function ($collectionId, $attributeId, $required, $min, $max, $default, $array, $response, $dbForInternal, $database, $audits) use ($attributesCallback) {
/** @var Appwrite\Utopia\Response $response */
2021-08-17 07:24:15 +12:00
/** @var Utopia\Database\Database $dbForInternal*/
/** @var Appwrite\Event\Event $database */
/** @var Appwrite\Event\Event $audits */
2021-08-19 16:05:44 +12:00
return $attributesCallback($collectionId, new Document([
'$id' => $attributeId,
2021-07-27 13:00:36 +12:00
'type' => Database::VAR_INTEGER,
'size' => 0,
'required' => $required,
'default' => $default,
'array' => $array,
2021-08-22 09:48:07 +12:00
'format' => 'int-range',
'formatOptions' => [
'min' => (is_null($min)) ? PHP_INT_MIN : \intval($min),
'max' => (is_null($max)) ? PHP_INT_MAX : \intval($max),
],
]), $response, $dbForInternal, $database, $audits);
});
2021-03-25 04:40:33 +13:00
App::post('/v1/database/collections/:collectionId/attributes/float')
->desc('Create Float Attribute')
->groups(['api', 'database'])
->label('event', 'database.attributes.create')
->label('scope', 'collections.write')
->label('sdk.namespace', 'database')
2021-08-03 08:11:24 +12:00
->label('sdk.auth', [APP_AUTH_TYPE_KEY])
->label('sdk.method', 'createFloatAttribute')
2021-07-27 13:00:36 +12:00
->label('sdk.description', '/docs/references/database/create-attribute-float.md')
->label('sdk.response.code', Response::STATUS_CODE_CREATED)
->label('sdk.response.type', Response::CONTENT_TYPE_JSON)
->label('sdk.response.model', Response::MODEL_ATTRIBUTE)
->param('collectionId', '', new UID(), 'Collection unique ID. You can create a new collection using the Database service [server integration](/docs/server/database#createCollection).')
->param('attributeId', '', new Key(), 'Attribute ID.')
->param('required', null, new Boolean(), 'Is attribute required?')
->param('min', null, new FloatValidator(), 'Minimum value to enforce on new documents', true)
->param('max', null, new FloatValidator(), 'Maximum value to enforce on new documents', true)
->param('default', null, new FloatValidator(), 'Default value for attribute when not provided. Cannot be set when attribute is required.', true)
->param('array', false, new Boolean(), 'Is attribute an array?', true)
->inject('response')
2021-08-17 07:24:15 +12:00
->inject('dbForInternal')
->inject('database')
->inject('audits')
2021-08-22 09:48:07 +12:00
->action(function ($collectionId, $attributeId, $required, $min, $max, $default, $array, $response, $dbForInternal, $database, $audits) use ($attributesCallback) {
/** @var Appwrite\Utopia\Response $response */
2021-08-17 07:24:15 +12:00
/** @var Utopia\Database\Database $dbForInternal*/
/** @var Appwrite\Event\Event $database */
/** @var Appwrite\Event\Event $audits */
2021-08-19 16:05:44 +12:00
return $attributesCallback($collectionId, new Document([
'$id' => $attributeId,
2021-07-27 13:00:36 +12:00
'type' => Database::VAR_FLOAT,
'required' => $required,
2021-07-27 13:00:36 +12:00
'size' => 0,
'default' => $default,
2021-07-27 13:00:36 +12:00
'array' => $array,
2021-08-22 09:48:07 +12:00
'format' => 'float-range',
'formatOptions' => [
'min' => (is_null($min)) ? PHP_FLOAT_MIN : \floatval($min),
'max' => (is_null($max)) ? PHP_FLOAT_MAX : \floatval($max),
],
]), $response, $dbForInternal, $database, $audits);
});
App::post('/v1/database/collections/:collectionId/attributes/boolean')
->desc('Create Boolean Attribute')
->groups(['api', 'database'])
->label('event', 'database.attributes.create')
->label('scope', 'collections.write')
->label('sdk.namespace', 'database')
2021-08-03 08:11:24 +12:00
->label('sdk.auth', [APP_AUTH_TYPE_KEY])
->label('sdk.method', 'createBooleanAttribute')
->label('sdk.description', '/docs/references/database/create-attribute-boolean.md')
->label('sdk.response.code', Response::STATUS_CODE_CREATED)
->label('sdk.response.type', Response::CONTENT_TYPE_JSON)
->label('sdk.response.model', Response::MODEL_ATTRIBUTE)
->param('collectionId', '', new UID(), 'Collection unique ID. You can create a new collection using the Database service [server integration](/docs/server/database#createCollection).')
->param('attributeId', '', new Key(), 'Attribute ID.')
->param('required', null, new Boolean(), 'Is attribute required?')
->param('default', null, new Boolean(), 'Default value for attribute when not provided. Cannot be set when attribute is required.', true)
->param('array', false, new Boolean(), 'Is attribute an array?', true)
->inject('response')
2021-08-17 07:24:15 +12:00
->inject('dbForInternal')
->inject('database')
->inject('audits')
2021-08-22 09:48:07 +12:00
->action(function ($collectionId, $attributeId, $required, $default, $array, $response, $dbForInternal, $database, $audits) use ($attributesCallback) {
/** @var Appwrite\Utopia\Response $response */
2021-08-17 07:24:15 +12:00
/** @var Utopia\Database\Database $dbForInternal*/
/** @var Appwrite\Event\Event $database */
/** @var Appwrite\Event\Event $audits */
2021-08-19 16:05:44 +12:00
return $attributesCallback($collectionId, new Document([
'$id' => $attributeId,
2021-07-27 13:00:36 +12:00
'type' => Database::VAR_BOOLEAN,
'size' => 0,
'required' => $required,
2021-07-03 05:29:03 +12:00
'default' => $default,
'array' => $array,
2021-08-22 09:48:07 +12:00
]), $response, $dbForInternal, $database, $audits);
2021-03-25 04:40:33 +13:00
});
2021-07-18 20:19:23 +12:00
App::get('/v1/database/collections/:collectionId/attributes')
2021-03-26 08:52:57 +13:00
->desc('List Attributes')
->groups(['api', 'database'])
->label('scope', 'collections.read')
2021-08-01 08:40:34 +12:00
->label('sdk.auth', [APP_AUTH_TYPE_KEY])
2021-03-26 08:52:57 +13:00
->label('sdk.namespace', 'database')
->label('sdk.method', 'listAttributes')
->label('sdk.description', '/docs/references/database/list-attributes.md')
->label('sdk.response.code', Response::STATUS_CODE_OK)
->label('sdk.response.type', Response::CONTENT_TYPE_JSON)
->label('sdk.response.model', Response::MODEL_ATTRIBUTE_LIST)
->param('collectionId', '', new UID(), 'Collection unique ID. You can create a new collection using the Database service [server integration](/docs/server/database#createCollection).')
2021-03-26 08:52:57 +13:00
->inject('response')
2021-08-17 07:24:15 +12:00
->inject('dbForInternal')
->action(function ($collectionId, $response, $dbForInternal) {
2021-03-26 08:52:57 +13:00
/** @var Appwrite\Utopia\Response $response */
2021-08-17 07:24:15 +12:00
/** @var Utopia\Database\Database $dbForInternal */
2021-06-09 08:12:14 +12:00
2021-08-17 07:24:15 +12:00
$collection = $dbForInternal->getDocument('collections', $collectionId);
2021-03-26 08:52:57 +13:00
if ($collection->isEmpty()) {
2021-06-11 01:15:00 +12:00
throw new Exception('Collection not found', 404);
}
2021-06-09 08:12:14 +12:00
$attributes = $collection->getAttributes();
2021-03-26 08:52:57 +13:00
$attributes = array_map(function ($attribute) use ($collection) {
2021-06-18 05:03:24 +12:00
return new Document([\array_merge($attribute, [
'collectionId' => $collection->getId(),
])]);
}, $attributes);
2021-07-26 02:47:18 +12:00
$response->dynamic(new Document([
'sum' => \count($attributes),
2021-06-09 08:12:14 +12:00
'attributes' => $attributes
]), Response::MODEL_ATTRIBUTE_LIST);
2021-03-26 08:52:57 +13:00
});
2021-07-18 20:19:23 +12:00
App::get('/v1/database/collections/:collectionId/attributes/:attributeId')
2021-03-26 08:52:57 +13:00
->desc('Get Attribute')
->groups(['api', 'database'])
->label('scope', 'collections.read')
2021-08-01 08:40:34 +12:00
->label('sdk.auth', [APP_AUTH_TYPE_KEY])
2021-03-26 08:52:57 +13:00
->label('sdk.namespace', 'database')
2021-07-18 20:19:23 +12:00
->label('sdk.method', 'getAttribute')
2021-03-26 08:52:57 +13:00
->label('sdk.description', '/docs/references/database/get-attribute.md')
->label('sdk.response.code', Response::STATUS_CODE_OK)
->label('sdk.response.type', Response::CONTENT_TYPE_JSON)
->label('sdk.response.model', Response::MODEL_ATTRIBUTE)
->param('collectionId', '', new UID(), 'Collection unique ID. You can create a new collection using the Database service [server integration](/docs/server/database#createCollection).')
2021-06-23 07:34:42 +12:00
->param('attributeId', '', new Key(), 'Attribute ID.')
2021-03-26 08:52:57 +13:00
->inject('response')
2021-08-17 07:24:15 +12:00
->inject('dbForInternal')
->action(function ($collectionId, $attributeId, $response, $dbForInternal) {
2021-03-26 08:52:57 +13:00
/** @var Appwrite\Utopia\Response $response */
2021-08-17 07:24:15 +12:00
/** @var Utopia\Database\Database $dbForInternal */
2021-06-09 08:12:14 +12:00
2021-08-17 07:24:15 +12:00
$collection = $dbForInternal->getDocument('collections', $collectionId);
2021-03-26 08:52:57 +13:00
2021-06-11 01:15:00 +12:00
if (empty($collection)) {
throw new Exception('Collection not found', 404);
}
2021-06-09 08:12:14 +12:00
$attributes = $collection->getAttributes();
2021-03-26 08:52:57 +13:00
// Search for attribute
$attributeIndex = array_search($attributeId, array_column($attributes, '$id'));
if ($attributeIndex === false) {
throw new Exception('Attribute not found', 404);
}
2021-06-18 05:03:24 +12:00
$attribute = new Document([\array_merge($attributes[$attributeIndex], [
'collectionId' => $collectionId,
])]);
2021-07-26 02:47:18 +12:00
$response->dynamic($attribute, Response::MODEL_ATTRIBUTE);
2021-03-26 08:52:57 +13:00
});
2021-03-25 04:40:33 +13:00
App::delete('/v1/database/collections/:collectionId/attributes/:attributeId')
->desc('Delete Attribute')
->groups(['api', 'database'])
->label('scope', 'collections.write')
2021-03-25 04:40:33 +13:00
->label('event', 'database.attributes.delete')
2021-08-01 08:40:34 +12:00
->label('sdk.auth', [APP_AUTH_TYPE_KEY])
2021-03-25 04:40:33 +13:00
->label('sdk.namespace', 'database')
->label('sdk.method', 'deleteAttribute')
->label('sdk.description', '/docs/references/database/delete-attribute.md')
->label('sdk.response.code', Response::STATUS_CODE_NOCONTENT)
->label('sdk.response.model', Response::MODEL_NONE)
->param('collectionId', '', new UID(), 'Collection unique ID. You can create a new collection using the Database service [server integration](/docs/server/database#createCollection).')
2021-06-23 07:34:42 +12:00
->param('attributeId', '', new Key(), 'Attribute ID.')
2021-03-25 04:40:33 +13:00
->inject('response')
2021-08-17 07:24:15 +12:00
->inject('dbForInternal')
->inject('database')
2021-03-25 04:40:33 +13:00
->inject('events')
->inject('audits')
2021-08-17 07:24:15 +12:00
->action(function ($collectionId, $attributeId, $response, $dbForInternal, $database, $events, $audits) {
2021-03-25 04:40:33 +13:00
/** @var Appwrite\Utopia\Response $response */
2021-08-17 07:24:15 +12:00
/** @var Utopia\Database\Database $dbForInternal */
/** @var Appwrite\Event\Event $database */
2021-03-25 04:40:33 +13:00
/** @var Appwrite\Event\Event $events */
/** @var Appwrite\Event\Event $audits */
2021-08-17 07:24:15 +12:00
$collection = $dbForInternal->getDocument('collections', $collectionId);
2021-03-25 04:40:33 +13:00
if ($collection->isEmpty()) {
2021-06-11 01:15:00 +12:00
throw new Exception('Collection not found', 404);
}
2021-08-21 16:48:28 +12:00
$attribute = $dbForInternal->getDocument('attributes', $collectionId.'_'.$attributeId);
2021-03-25 04:40:33 +13:00
2021-08-21 05:02:44 +12:00
if (empty($attribute->getId())) {
throw new Exception('Attribute not found', 404);
}
2021-08-22 20:04:51 +12:00
$attribute = $dbForInternal->updateDocument('attributes', $attribute->getId(), $attribute->setAttribute('status', 'deleting'));
2021-08-22 09:48:07 +12:00
$dbForInternal->purgeDocument('collections', $collectionId);
2021-08-21 05:02:44 +12:00
$database
2021-08-09 11:46:14 +12:00
->setParam('type', DATABASE_TYPE_DELETE_ATTRIBUTE)
2021-08-21 16:48:28 +12:00
->setParam('collection', $collection)
->setParam('document', $attribute)
;
2021-06-09 08:12:14 +12:00
$events
2021-07-26 02:47:18 +12:00
->setParam('payload', $response->output($attribute, Response::MODEL_ATTRIBUTE))
;
2021-03-25 04:40:33 +13:00
$audits
->setParam('event', 'database.attributes.delete')
2021-08-13 23:19:16 +12:00
->setParam('resource', 'database/collection/'.$collection->getId())
2021-03-25 04:40:33 +13:00
->setParam('data', $attribute->getArrayCopy())
;
$response->noContent();
});
2021-03-24 10:19:19 +13:00
App::post('/v1/database/collections/:collectionId/indexes')
->desc('Create Index')
->groups(['api', 'database'])
->label('event', 'database.indexes.create')
->label('scope', 'collections.write')
2021-08-01 08:40:34 +12:00
->label('sdk.auth', [APP_AUTH_TYPE_KEY])
2021-03-24 10:19:19 +13:00
->label('sdk.namespace', 'database')
->label('sdk.method', 'createIndex')
->label('sdk.description', '/docs/references/database/create-index.md')
->label('sdk.response.code', Response::STATUS_CODE_CREATED)
->label('sdk.response.type', Response::CONTENT_TYPE_JSON)
->label('sdk.response.model', Response::MODEL_INDEX)
->param('collectionId', '', new UID(), 'Collection unique ID. You can create a new collection using the Database service [server integration](/docs/server/database#createCollection).')
2021-08-09 19:08:49 +12:00
->param('indexId', null, new Key(), 'Index ID.')
2021-06-18 05:03:24 +12:00
->param('type', null, new WhiteList([Database::INDEX_KEY, Database::INDEX_FULLTEXT, Database::INDEX_UNIQUE, Database::INDEX_SPATIAL, Database::INDEX_ARRAY]), 'Index type.')
2021-06-23 07:34:42 +12:00
->param('attributes', null, new ArrayList(new Key()), 'Array of attributes to index.')
2021-07-06 08:27:20 +12:00
->param('orders', [], new ArrayList(new WhiteList(['ASC', 'DESC'], false, Database::VAR_STRING)), 'Array of index orders.', true)
2021-03-24 10:19:19 +13:00
->inject('response')
2021-08-17 07:24:15 +12:00
->inject('dbForInternal')
->inject('database')
->inject('audits')
->action(function ($collectionId, $indexId, $type, $attributes, $orders, $response, $dbForInternal, $database, $audits) {
2021-03-24 10:19:19 +13:00
/** @var Appwrite\Utopia\Response $response */
2021-08-17 07:24:15 +12:00
/** @var Utopia\Database\Database $dbForInternal */
2021-07-03 10:22:49 +12:00
/** @var Appwrite\Event\Event $database */
2021-03-24 10:19:19 +13:00
/** @var Appwrite\Event\Event $audits */
2021-08-17 07:24:15 +12:00
$collection = $dbForInternal->getDocument('collections', $collectionId);
2021-06-11 01:15:00 +12:00
if ($collection->isEmpty()) {
2021-06-11 01:15:00 +12:00
throw new Exception('Collection not found', 404);
}
2021-08-25 11:35:32 +12:00
$count = $dbForInternal->count('indexes', [
new Query('collectionId', Query::TYPE_EQUAL, [$collectionId])
], 61);
$limit = 64 - MariaDB::getNumberOfDefaultIndexes();
if ($count >= $limit) {
throw new Exception('Index limit exceeded', 400);
}
// Convert Document[] to array of attribute metadata
$oldAttributes = \array_map(function ($a) {
return $a->getArrayCopy();
}, $collection->getAttribute('attributes'));
2021-07-03 10:23:58 +12:00
// lengths hidden by default
$lengths = [];
2021-07-03 10:23:58 +12:00
// set attribute size as length for strings, null otherwise
foreach ($attributes as $key => $attribute) {
2021-07-03 10:23:58 +12:00
// find attribute metadata in collection document
2021-08-23 02:06:59 +12:00
$attributeIndex = \array_search($attribute, array_column($oldAttributes, 'key'));
2021-07-03 10:23:58 +12:00
if ($attributeIndex === false) {
throw new Exception('Unknown attribute: ' . $attribute, 400);
}
$attributeType = $oldAttributes[$attributeIndex]['type'];
$attributeSize = $oldAttributes[$attributeIndex]['size'];
2021-07-03 10:23:58 +12:00
// Only set length for indexes on strings
$lengths[$key] = ($attributeType === Database::VAR_STRING) ? $attributeSize : null;
2021-07-03 10:23:58 +12:00
}
2021-08-23 03:00:00 +12:00
try {
$index = $dbForInternal->createDocument('indexes', new Document([
'$id' => $collectionId.'_'.$indexId,
'key' => $indexId,
'status' => 'processing', // processing, available, failed, deleting
'collectionId' => $collectionId,
'type' => $type,
'attributes' => $attributes,
'lengths' => $lengths,
'orders' => $orders,
]));
} catch (DuplicateException $th) {
2021-08-25 11:35:32 +12:00
throw new Exception('Index already exists', 409);
2021-08-23 03:00:00 +12:00
}
$dbForInternal->purgeDocument('collections', $collectionId);
2021-03-24 10:19:19 +13:00
$database
2021-08-09 11:46:14 +12:00
->setParam('type', DATABASE_TYPE_CREATE_INDEX)
2021-08-23 04:36:26 +12:00
->setParam('collection', $collection)
->setParam('document', $index)
;
2021-03-24 10:19:19 +13:00
$audits
->setParam('event', 'database.indexes.create')
2021-08-13 23:19:16 +12:00
->setParam('resource', 'database/collection/'.$collection->getId())
->setParam('data', $index->getArrayCopy())
2021-03-24 10:19:19 +13:00
;
$response->setStatusCode(Response::STATUS_CODE_CREATED);
2021-07-26 02:47:18 +12:00
$response->dynamic($index, Response::MODEL_INDEX);
2021-03-24 10:19:19 +13:00
});
2021-07-18 20:19:23 +12:00
App::get('/v1/database/collections/:collectionId/indexes')
2021-03-25 04:40:33 +13:00
->desc('List Indexes')
->groups(['api', 'database'])
->label('scope', 'collections.read')
2021-08-01 08:40:34 +12:00
->label('sdk.auth', [APP_AUTH_TYPE_KEY])
2021-03-25 04:40:33 +13:00
->label('sdk.namespace', 'database')
->label('sdk.method', 'listIndexes')
->label('sdk.description', '/docs/references/database/list-indexes.md')
->label('sdk.response.code', Response::STATUS_CODE_OK)
->label('sdk.response.type', Response::CONTENT_TYPE_JSON)
->label('sdk.response.model', Response::MODEL_INDEX_LIST)
->param('collectionId', '', new UID(), 'Collection unique ID. You can create a new collection using the Database service [server integration](/docs/server/database#createCollection).')
2021-03-25 04:40:33 +13:00
->inject('response')
2021-08-17 07:24:15 +12:00
->inject('dbForInternal')
->action(function ($collectionId, $response, $dbForInternal) {
2021-03-25 04:40:33 +13:00
/** @var Appwrite\Utopia\Response $response */
2021-08-17 07:24:15 +12:00
/** @var Utopia\Database\Database $dbForInternal */
2021-08-17 07:24:15 +12:00
$collection = $dbForInternal->getDocument('collections', $collectionId);
2021-03-25 04:40:33 +13:00
if ($collection->isEmpty()) {
2021-06-11 01:15:00 +12:00
throw new Exception('Collection not found', 404);
}
$indexes = $collection->getAttribute('indexes');
2021-03-25 04:40:33 +13:00
$indexes = array_map(function ($index) use ($collection) {
2021-06-18 05:03:24 +12:00
return new Document([\array_merge($index, [
'collectionId' => $collection->getId(),
])]);
}, $indexes);
2021-07-26 02:47:18 +12:00
$response->dynamic(new Document([
'sum' => \count($indexes),
'attributes' => $indexes,
]), Response::MODEL_INDEX_LIST);
2021-03-25 04:40:33 +13:00
});
2021-07-18 20:19:23 +12:00
App::get('/v1/database/collections/:collectionId/indexes/:indexId')
2021-03-26 08:52:57 +13:00
->desc('Get Index')
->groups(['api', 'database'])
->label('scope', 'collections.read')
2021-08-01 08:40:34 +12:00
->label('sdk.auth', [APP_AUTH_TYPE_KEY])
2021-03-26 08:52:57 +13:00
->label('sdk.namespace', 'database')
2021-07-18 20:19:23 +12:00
->label('sdk.method', 'getIndex')
2021-03-26 08:52:57 +13:00
->label('sdk.description', '/docs/references/database/get-index.md')
->label('sdk.response.code', Response::STATUS_CODE_OK)
->label('sdk.response.type', Response::CONTENT_TYPE_JSON)
->label('sdk.response.model', Response::MODEL_INDEX)
->param('collectionId', '', new UID(), 'Collection unique ID. You can create a new collection using the Database service [server integration](/docs/server/database#createCollection).')
2021-06-23 07:34:42 +12:00
->param('indexId', null, new Key(), 'Index ID.')
2021-03-26 08:52:57 +13:00
->inject('response')
2021-08-17 07:24:15 +12:00
->inject('dbForInternal')
->action(function ($collectionId, $indexId, $response, $dbForInternal) {
2021-03-26 08:52:57 +13:00
/** @var Appwrite\Utopia\Response $response */
2021-08-17 07:24:15 +12:00
/** @var Utopia\Database\Database $dbForInternal */
2021-03-26 08:52:57 +13:00
2021-08-17 07:24:15 +12:00
$collection = $dbForInternal->getDocument('collections', $collectionId);
if ($collection->isEmpty()) {
2021-06-11 01:15:00 +12:00
throw new Exception('Collection not found', 404);
}
$indexes = $collection->getAttribute('indexes');
2021-03-26 08:52:57 +13:00
2021-08-17 07:24:15 +12:00
// Search for index
$indexIndex = array_search($indexId, array_column($indexes, '$id'));
if ($indexIndex === false) {
throw new Exception('Index not found', 404);
}
2021-06-18 05:03:24 +12:00
$index = new Document([\array_merge($indexes[$indexIndex], [
'collectionId' => $collectionId,
])]);
2021-07-26 02:47:18 +12:00
$response->dynamic($index, Response::MODEL_INDEX);
2021-03-26 08:52:57 +13:00
});
App::delete('/v1/database/collections/:collectionId/indexes/:indexId')
->desc('Delete Index')
->groups(['api', 'database'])
->label('scope', 'collections.write')
2021-03-26 08:52:57 +13:00
->label('event', 'database.indexes.delete')
2021-08-01 08:40:34 +12:00
->label('sdk.auth', [APP_AUTH_TYPE_KEY])
2021-03-26 08:52:57 +13:00
->label('sdk.namespace', 'database')
->label('sdk.method', 'deleteIndex')
->label('sdk.description', '/docs/references/database/delete-index.md')
->label('sdk.response.code', Response::STATUS_CODE_NOCONTENT)
->label('sdk.response.model', Response::MODEL_NONE)
->param('collectionId', null, new UID(), 'Collection unique ID. You can create a new collection using the Database service [server integration](/docs/server/database#createCollection).')
2021-06-23 07:34:42 +12:00
->param('indexId', '', new Key(), 'Index ID.')
2021-03-26 08:52:57 +13:00
->inject('response')
2021-08-17 07:24:15 +12:00
->inject('dbForInternal')
->inject('database')
2021-03-26 08:52:57 +13:00
->inject('events')
->inject('audits')
2021-08-17 07:24:15 +12:00
->action(function ($collectionId, $indexId, $response, $dbForInternal, $database, $events, $audits) {
2021-03-26 08:52:57 +13:00
/** @var Appwrite\Utopia\Response $response */
2021-08-17 07:24:15 +12:00
/** @var Utopia\Database\Database $dbForInternal */
/** @var Appwrite\Event\Event $database */
2021-03-26 08:52:57 +13:00
/** @var Appwrite\Event\Event $events */
/** @var Appwrite\Event\Event $audits */
2021-08-17 07:24:15 +12:00
$collection = $dbForInternal->getDocument('collections', $collectionId);
2021-06-12 06:07:05 +12:00
if ($collection->isEmpty()) {
2021-06-11 01:15:00 +12:00
throw new Exception('Collection not found', 404);
}
2021-08-23 16:06:53 +12:00
$index = $dbForInternal->getDocument('indexes', $collectionId.'_'.$indexId);
2021-08-23 16:06:53 +12:00
if (empty($index->getId())) {
throw new Exception('Index not found', 404);
2021-03-26 08:52:57 +13:00
}
2021-08-23 16:06:53 +12:00
$index = $dbForInternal->updateDocument('indexes', $index->getId(), $index->setAttribute('status', 'deleting'));
$dbForInternal->purgeDocument('collections', $collectionId);
$database
2021-08-09 11:46:14 +12:00
->setParam('type', DATABASE_TYPE_DELETE_INDEX)
2021-08-23 16:06:53 +12:00
->setParam('collection', $collection)
->setParam('document', $index)
;
2021-03-26 08:52:57 +13:00
$events
2021-07-26 02:47:18 +12:00
->setParam('payload', $response->output($index, Response::MODEL_INDEX))
2021-03-26 08:52:57 +13:00
;
$audits
->setParam('event', 'database.indexes.delete')
2021-08-13 23:19:16 +12:00
->setParam('resource', 'database/collection/'.$collection->getId())
2021-03-26 08:52:57 +13:00
->setParam('data', $index->getArrayCopy())
;
$response->noContent();
});
2020-06-29 05:31:21 +12:00
App::post('/v1/database/collections/:collectionId/documents')
2020-02-01 11:34:07 +13:00
->desc('Create Document')
2020-06-26 06:32:12 +12:00
->groups(['api', 'database'])
2020-10-31 08:53:27 +13:00
->label('event', 'database.documents.create')
2020-02-01 11:34:07 +13:00
->label('scope', 'documents.write')
2021-04-16 19:22:17 +12:00
->label('sdk.auth', [APP_AUTH_TYPE_SESSION, APP_AUTH_TYPE_KEY, APP_AUTH_TYPE_JWT])
2021-08-01 08:40:34 +12:00
->label('sdk.namespace', 'database')
2020-02-01 11:34:07 +13:00
->label('sdk.method', 'createDocument')
->label('sdk.description', '/docs/references/database/create-document.md')
2020-11-12 10:02:24 +13:00
->label('sdk.response.code', Response::STATUS_CODE_CREATED)
->label('sdk.response.type', Response::CONTENT_TYPE_JSON)
->label('sdk.response.model', Response::MODEL_DOCUMENT)
->param('documentId', '', new CustomId(), 'Unique Id. Choose your own unique ID or pass the string `unique()` to auto generate it. Valid chars are a-z, A-Z, 0-9, period, hyphen, and underscore. Can\'t start with a special char. Max length is 36 chars.')
2020-09-11 02:40:14 +12:00
->param('collectionId', null, new UID(), 'Collection unique ID. You can create a new collection with validation rules using the Database service [server integration](/docs/server/database#createCollection).')
->param('data', [], new JSON(), 'Document data as JSON object.')
2021-07-06 08:27:20 +12:00
->param('read', null, new Permissions(), 'An array of strings with read permissions. By default only the current user is granted with read permissions. [learn more about permissions](/docs/permissions) and get a full list of available permissions.', true)
->param('write', null, new Permissions(), 'An array of strings with write permissions. By default only the current user is granted with write permissions. [learn more about permissions](/docs/permissions) and get a full list of available permissions.', true)
2020-12-27 04:05:04 +13:00
->inject('response')
2021-08-17 07:24:15 +12:00
->inject('dbForInternal')
->inject('dbForExternal')
2021-03-22 11:17:20 +13:00
->inject('user')
2020-12-27 04:05:04 +13:00
->inject('audits')
2021-08-17 07:24:15 +12:00
->action(function ($documentId, $collectionId, $data, $read, $write, $response, $dbForInternal, $dbForExternal, $user, $audits) {
2020-10-30 02:50:49 +13:00
/** @var Appwrite\Utopia\Response $response */
2021-08-17 07:24:15 +12:00
/** @var Utopia\Database\Database $dbForInternal */
/** @var Utopia\Database\Database $dbForExternal */
/** @var Utopia\Database\Document $user */
2020-07-06 02:19:59 +12:00
/** @var Appwrite\Event\Event $audits */
2020-06-30 09:43:34 +12:00
$data = (\is_string($data)) ? \json_decode($data, true) : $data; // Cast to JSON array
if (empty($data)) {
throw new Exception('Missing payload', 400);
}
2020-02-01 11:34:07 +13:00
2020-06-30 09:43:34 +12:00
if (isset($data['$id'])) {
throw new Exception('$id is not allowed for creating new documents, try update instead', 400);
}
2021-08-17 07:24:15 +12:00
$collection = $dbForInternal->getDocument('collections', $collectionId);
2020-02-01 11:34:07 +13:00
2021-06-12 06:07:05 +12:00
if ($collection->isEmpty()) {
2020-06-30 09:43:34 +12:00
throw new Exception('Collection not found', 404);
}
2020-02-01 11:34:07 +13:00
$data['$collection'] = $collection->getId(); // Adding this param to make API easier for developers
2021-07-19 21:25:53 +12:00
$data['$id'] = $documentId == 'unique()' ? $dbForExternal->getId() : $documentId;
$data['$read'] = (is_null($read) && !$user->isEmpty()) ? ['user:'.$user->getId()] : $read ?? []; // By default set read permissions for user
$data['$write'] = (is_null($write) && !$user->isEmpty()) ? ['user:'.$user->getId()] : $write ?? []; // By default set write permissions for user
2020-02-01 11:34:07 +13:00
try {
2021-06-18 05:03:24 +12:00
$document = $dbForExternal->createDocument($collectionId, new Document($data));
2021-08-16 09:09:40 +12:00
}
catch (StructureException $exception) {
throw new Exception($exception->getMessage(), 400);
}
2021-08-22 09:48:07 +12:00
catch (DuplicateException $exception) {
throw new Exception('Document already exists', 409);
}
2020-02-01 11:34:07 +13:00
2020-07-06 02:19:59 +12:00
$audits
2020-06-30 09:43:34 +12:00
->setParam('event', 'database.documents.create')
->setParam('resource', 'database/document/'.$document->getId())
->setParam('data', $document->getArrayCopy())
2020-06-30 09:43:34 +12:00
;
2020-02-01 11:34:07 +13:00
2021-05-27 22:09:14 +12:00
$response->setStatusCode(Response::STATUS_CODE_CREATED);
2021-07-26 02:47:18 +12:00
$response->dynamic($document, Response::MODEL_DOCUMENT);
2020-12-27 04:05:04 +13:00
});
2020-02-01 11:34:07 +13:00
2020-06-29 05:31:21 +12:00
App::get('/v1/database/collections/:collectionId/documents')
2019-05-09 18:54:39 +12:00
->desc('List Documents')
2020-06-26 06:32:12 +12:00
->groups(['api', 'database'])
->label('scope', 'documents.read')
2021-04-16 19:22:17 +12:00
->label('sdk.auth', [APP_AUTH_TYPE_SESSION, APP_AUTH_TYPE_KEY, APP_AUTH_TYPE_JWT])
2021-08-01 08:40:34 +12:00
->label('sdk.namespace', 'database')
2019-05-09 18:54:39 +12:00
->label('sdk.method', 'listDocuments')
2019-10-08 20:09:35 +13:00
->label('sdk.description', '/docs/references/database/list-documents.md')
2020-11-12 10:02:24 +13:00
->label('sdk.response.code', Response::STATUS_CODE_OK)
->label('sdk.response.type', Response::CONTENT_TYPE_JSON)
->label('sdk.response.model', Response::MODEL_DOCUMENT_LIST)
->param('collectionId', '', new UID(), 'Collection unique ID. You can create a new collection using the Database service [server integration](/docs/server/database#createCollection).')
2021-06-15 07:55:36 +12:00
->param('queries', [], new ArrayList(new Text(128)), 'Array of query strings.', true)
2020-11-06 05:02:25 +13:00
->param('limit', 25, new Range(0, 100), 'Maximum number of documents to return in response. Use this value to manage pagination. By default will return maximum 25 results. Maximum of 100 results allowed per request.', true)
->param('offset', 0, new Range(0, 900000000), 'Offset value. The default value is 0. Use this param to manage pagination.', true)
->param('after', '', new UID(), 'ID of the document used as the starting point for the query, excluding the document itself. Should be used for efficient pagination when working with large sets of data.', true)
2021-06-15 07:55:36 +12:00
->param('orderAttributes', [], new ArrayList(new Text(128)), 'Array of attributes used to sort results.', true)
->param('orderTypes', [], new ArrayList(new WhiteList(['DESC', 'ASC'], true)), 'Array of order directions for sorting attribtues. Possible values are DESC for descending order, or ASC for ascending order.', true)
2020-12-27 04:05:04 +13:00
->inject('response')
2021-08-17 07:24:15 +12:00
->inject('dbForInternal')
2021-06-15 07:55:36 +12:00
->inject('dbForExternal')
->action(function ($collectionId, $queries, $limit, $offset, $after, $orderAttributes, $orderTypes, $response, $dbForInternal, $dbForExternal) {
2020-10-30 02:50:49 +13:00
/** @var Appwrite\Utopia\Response $response */
2021-08-23 19:27:09 +12:00
/** @var Utopia\Database\Database $dbForInternal */
2021-06-15 07:55:36 +12:00
/** @var Utopia\Database\Database $dbForExternal */
2019-05-09 18:54:39 +12:00
2021-08-17 07:24:15 +12:00
$collection = $dbForInternal->getDocument('collections', $collectionId);
2019-05-09 18:54:39 +12:00
2021-06-15 07:55:36 +12:00
if ($collection->isEmpty()) {
2020-06-30 09:43:34 +12:00
throw new Exception('Collection not found', 404);
}
2020-06-22 00:12:13 +12:00
2021-06-15 07:55:36 +12:00
$queries = \array_map(function ($query) {
return Query::parse($query);
}, $queries);
2020-06-30 09:43:34 +12:00
2021-07-06 08:27:20 +12:00
// TODO@kodumbeats use strict query validation
2021-08-23 19:27:09 +12:00
$validator = new QueriesValidator(new QueryValidator($collection->getAttribute('attributes', [])), $collection->getAttribute('indexes', []), false);
2021-07-06 08:27:20 +12:00
if (!$validator->isValid($queries)) {
throw new Exception($validator->getDescription(), 400);
}
2021-08-06 22:34:42 +12:00
if (!empty($after)) {
$afterDocument = $dbForExternal->getDocument($collectionId, $after);
2021-08-06 07:01:00 +12:00
2021-08-06 22:34:42 +12:00
if ($afterDocument->isEmpty()) {
throw new Exception("Document '{$after}' for the 'after' value not found.", 400);
2021-08-06 07:01:00 +12:00
}
}
2021-07-26 02:47:18 +12:00
$response->dynamic(new Document([
2021-08-16 09:32:27 +12:00
'sum' => $dbForExternal->count($collectionId, $queries, APP_LIMIT_COUNT),
'documents' => $dbForExternal->find($collectionId, $queries, $limit, $offset, $orderAttributes, $orderTypes, $afterDocument ?? null),
2021-06-15 07:55:36 +12:00
]), Response::MODEL_DOCUMENT_LIST);
2020-12-27 04:05:04 +13:00
});
2019-05-09 18:54:39 +12:00
2020-06-29 05:31:21 +12:00
App::get('/v1/database/collections/:collectionId/documents/:documentId')
2019-05-09 18:54:39 +12:00
->desc('Get Document')
2020-06-26 06:32:12 +12:00
->groups(['api', 'database'])
->label('scope', 'documents.read')
2021-04-16 19:22:17 +12:00
->label('sdk.auth', [APP_AUTH_TYPE_SESSION, APP_AUTH_TYPE_KEY, APP_AUTH_TYPE_JWT])
2021-08-01 08:40:34 +12:00
->label('sdk.namespace', 'database')
2019-05-09 18:54:39 +12:00
->label('sdk.method', 'getDocument')
2019-10-08 20:09:35 +13:00
->label('sdk.description', '/docs/references/database/get-document.md')
2020-11-12 10:02:24 +13:00
->label('sdk.response.code', Response::STATUS_CODE_OK)
->label('sdk.response.type', Response::CONTENT_TYPE_JSON)
->label('sdk.response.model', Response::MODEL_DOCUMENT)
->param('collectionId', null, new UID(), 'Collection unique ID. You can create a new collection using the Database service [server integration](/docs/server/database#createCollection).')
2020-09-11 02:40:14 +12:00
->param('documentId', null, new UID(), 'Document unique ID.')
2020-12-27 04:05:04 +13:00
->inject('response')
2021-08-22 09:48:07 +12:00
->inject('dbForInternal')
->inject('dbForExternal')
2021-08-17 07:24:15 +12:00
->action(function ($collectionId, $documentId, $response, $dbForInternal, $dbForExternal) {
2020-10-30 02:50:49 +13:00
/** @var Appwrite\Utopia\Response $response */
2021-08-17 07:24:15 +12:00
/** @var Utopia\Database\Database $$dbForInternal */
/** @var Utopia\Database\Database $dbForExternal */
2019-05-09 18:54:39 +12:00
2021-08-17 07:24:15 +12:00
$collection = $dbForInternal->getDocument('collections', $collectionId);
2019-05-09 18:54:39 +12:00
2021-06-12 06:07:05 +12:00
if ($collection->isEmpty()) {
throw new Exception('Collection not found', 404);
}
$document = $dbForExternal->getDocument($collectionId, $documentId);
if ($document->isEmpty()) {
2020-06-30 09:43:34 +12:00
throw new Exception('No document found', 404);
}
2019-05-09 18:54:39 +12:00
2021-07-26 02:47:18 +12:00
$response->dynamic($document, Response::MODEL_DOCUMENT);
2020-12-27 04:05:04 +13:00
});
2019-05-09 18:54:39 +12:00
2020-06-29 05:31:21 +12:00
App::patch('/v1/database/collections/:collectionId/documents/:documentId')
2019-05-09 18:54:39 +12:00
->desc('Update Document')
2020-06-26 06:32:12 +12:00
->groups(['api', 'database'])
2020-10-31 08:53:27 +13:00
->label('event', 'database.documents.update')
->label('scope', 'documents.write')
2021-04-16 19:22:17 +12:00
->label('sdk.auth', [APP_AUTH_TYPE_SESSION, APP_AUTH_TYPE_KEY, APP_AUTH_TYPE_JWT])
2021-08-01 08:40:34 +12:00
->label('sdk.namespace', 'database')
2019-05-09 18:54:39 +12:00
->label('sdk.method', 'updateDocument')
2019-10-08 20:09:35 +13:00
->label('sdk.description', '/docs/references/database/update-document.md')
2020-11-12 10:02:24 +13:00
->label('sdk.response.code', Response::STATUS_CODE_OK)
->label('sdk.response.type', Response::CONTENT_TYPE_JSON)
->label('sdk.response.model', Response::MODEL_DOCUMENT)
2020-09-11 02:40:14 +12:00
->param('collectionId', null, new UID(), 'Collection unique ID. You can create a new collection with validation rules using the Database service [server integration](/docs/server/database#createCollection).')
->param('documentId', null, new UID(), 'Document unique ID.')
->param('data', [], new JSON(), 'Document data as JSON object.')
2021-07-06 08:27:20 +12:00
->param('read', null, new Permissions(), 'An array of strings with read permissions. By default inherits the existing read permissions. [learn more about permissions](/docs/permissions) and get a full list of available permissions.', true)
->param('write', null, new Permissions(), 'An array of strings with write permissions. By default inherits the existing write permissions. [learn more about permissions](/docs/permissions) and get a full list of available permissions.', true)
2020-12-27 04:05:04 +13:00
->inject('response')
2021-08-17 07:24:15 +12:00
->inject('dbForInternal')
->inject('dbForExternal')
2020-12-27 04:05:04 +13:00
->inject('audits')
2021-08-17 07:24:15 +12:00
->action(function ($collectionId, $documentId, $data, $read, $write, $response, $dbForInternal, $dbForExternal, $audits) {
2020-10-30 02:50:49 +13:00
/** @var Appwrite\Utopia\Response $response */
2021-08-17 07:24:15 +12:00
/** @var Utopia\Database\Database $dbForInternal */
/** @var Utopia\Database\Database $dbForExternal */
2020-07-06 02:19:59 +12:00
/** @var Appwrite\Event\Event $audits */
2019-05-09 18:54:39 +12:00
2021-08-17 07:24:15 +12:00
$collection = $dbForInternal->getDocument('collections', $collectionId);
2019-09-17 07:03:24 +12:00
if ($collection->isEmpty()) {
throw new Exception('Collection not found', 404);
2020-06-30 09:43:34 +12:00
}
2019-05-09 18:54:39 +12:00
$document = $dbForExternal->getDocument($collectionId, $documentId);
if ($document->isEmpty()) {
throw new Exception('Document not found', 404);
2020-06-30 09:43:34 +12:00
}
2019-05-09 18:54:39 +12:00
$data = (\is_string($data)) ? \json_decode($data, true) : $data; // Cast to JSON array
if (empty($data)) {
throw new Exception('Missing payload', 400);
}
if (!\is_array($data)) {
throw new Exception('Data param should be a valid JSON object', 400);
2020-06-30 09:43:34 +12:00
}
2019-05-09 18:54:39 +12:00
2020-06-30 09:43:34 +12:00
$data = \array_merge($document->getArrayCopy(), $data);
2019-05-09 18:54:39 +12:00
2020-06-30 09:43:34 +12:00
$data['$collection'] = $collection->getId(); // Make sure user don't switch collectionID
$data['$id'] = $document->getId(); // Make sure user don't switch document unique ID
$data['$read'] = (is_null($read)) ? ($document->getRead() ?? []) : $read; // By default inherit read permissions
$data['$write'] = (is_null($write)) ? ($document->getWrite() ?? []) : $write; // By default inherit write permissions
2020-10-31 08:53:27 +13:00
2020-06-30 09:43:34 +12:00
try {
2021-06-18 05:03:24 +12:00
$document = $dbForExternal->updateDocument($collection->getId(), $document->getId(), new Document($data));
2021-08-16 09:09:40 +12:00
}
catch (AuthorizationException $exception) {
2020-08-30 16:49:24 +12:00
throw new Exception('Unauthorized permissions', 401);
2021-08-16 09:09:40 +12:00
}
catch (StructureException $exception) {
2020-06-30 09:43:34 +12:00
throw new Exception('Bad structure. '.$exception->getMessage(), 400);
2021-08-16 09:09:40 +12:00
}
2019-05-09 18:54:39 +12:00
2020-07-06 02:19:59 +12:00
$audits
2020-06-30 09:43:34 +12:00
->setParam('event', 'database.documents.update')
->setParam('resource', 'database/document/'.$document->getId())
->setParam('data', $document->getArrayCopy())
2020-06-30 09:43:34 +12:00
;
2019-05-09 18:54:39 +12:00
2021-07-26 02:47:18 +12:00
$response->dynamic($document, Response::MODEL_DOCUMENT);
2020-12-27 04:05:04 +13:00
});
2019-05-09 18:54:39 +12:00
2020-06-29 05:31:21 +12:00
App::delete('/v1/database/collections/:collectionId/documents/:documentId')
2019-05-09 18:54:39 +12:00
->desc('Delete Document')
2020-06-26 06:32:12 +12:00
->groups(['api', 'database'])
->label('scope', 'documents.write')
2020-10-31 08:53:27 +13:00
->label('event', 'database.documents.delete')
2021-04-16 19:22:17 +12:00
->label('sdk.auth', [APP_AUTH_TYPE_SESSION, APP_AUTH_TYPE_KEY, APP_AUTH_TYPE_JWT])
2021-08-01 08:40:34 +12:00
->label('sdk.namespace', 'database')
2019-05-09 18:54:39 +12:00
->label('sdk.method', 'deleteDocument')
2019-10-08 20:09:35 +13:00
->label('sdk.description', '/docs/references/database/delete-document.md')
2020-11-12 10:02:24 +13:00
->label('sdk.response.code', Response::STATUS_CODE_NOCONTENT)
->label('sdk.response.model', Response::MODEL_NONE)
->param('collectionId', null, new UID(), 'Collection unique ID. You can create a new collection using the Database service [server integration](/docs/server/database#createCollection).')
2020-09-11 02:40:14 +12:00
->param('documentId', null, new UID(), 'Document unique ID.')
2020-12-27 04:05:04 +13:00
->inject('response')
2021-08-17 07:24:15 +12:00
->inject('dbForInternal')
->inject('dbForExternal')
2020-12-27 04:05:04 +13:00
->inject('events')
->inject('audits')
2021-08-17 07:24:15 +12:00
->action(function ($collectionId, $documentId, $response, $dbForInternal, $dbForExternal, $events, $audits) {
2020-10-30 02:50:49 +13:00
/** @var Appwrite\Utopia\Response $response */
/** @var Utopia\Database\Database $dbForExternal */
2020-12-07 11:14:57 +13:00
/** @var Appwrite\Event\Event $events */
2020-07-06 02:19:59 +12:00
/** @var Appwrite\Event\Event $audits */
2019-05-09 18:54:39 +12:00
2021-08-17 07:24:15 +12:00
$collection = $dbForInternal->getDocument('collections', $collectionId);
2019-05-09 18:54:39 +12:00
2021-06-12 06:07:05 +12:00
if ($collection->isEmpty()) {
2020-06-30 09:43:34 +12:00
throw new Exception('Collection not found', 404);
}
$document = $dbForExternal->getDocument($collectionId, $documentId);
if ($document->isEmpty()) {
throw new Exception('No document found', 404);
2020-06-30 09:43:34 +12:00
}
2019-05-09 18:54:39 +12:00
2021-08-17 07:24:15 +12:00
$dbForExternal->deleteDocument($collectionId, $documentId);
2020-12-07 11:14:57 +13:00
$events
2021-07-26 02:47:18 +12:00
->setParam('eventData', $response->output($document, Response::MODEL_DOCUMENT))
2020-06-30 09:43:34 +12:00
;
2020-07-06 02:19:59 +12:00
$audits
2020-06-30 09:43:34 +12:00
->setParam('event', 'database.documents.delete')
2020-10-31 08:53:27 +13:00
->setParam('resource', 'database/document/'.$document->getId())
->setParam('data', $document->getArrayCopy()) // Audit document in case of malicious or disastrous action
2020-06-30 09:43:34 +12:00
;
2019-05-09 18:54:39 +12:00
2020-06-30 09:43:34 +12:00
$response->noContent();
2021-08-17 07:24:15 +12:00
});