mirror of
https://github.com/appwrite/appwrite.git
synced 2026-05-26 13:51:13 +00:00
feat(controllers): implement before cursor pagination
This commit is contained in:
@@ -93,7 +93,7 @@ $attributesCallback = function ($collectionId, $attribute, $response, $dbForInte
|
||||
throw new Exception('Attribute already exists', 409);
|
||||
}
|
||||
|
||||
$dbForInternal->purgeDocument('collections', $collectionId);
|
||||
$dbForInternal->deleteCachedCollection($collectionId);
|
||||
|
||||
$database
|
||||
->setParam('type', DATABASE_TYPE_CREATE_ATTRIBUTE)
|
||||
@@ -186,12 +186,13 @@ App::get('/v1/database/collections')
|
||||
->param('search', '', new Text(256), 'Search term to filter your list results. Max length: 256 chars.', true)
|
||||
->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)
|
||||
->param('cursor', '', 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)
|
||||
->param('cursorDirection', Database::CURSOR_AFTER, new WhiteList([Database::CURSOR_AFTER, Database::CURSOR_BEFORE]), 'Direction of the cursor.', true)
|
||||
->param('orderType', 'ASC', new WhiteList(['ASC', 'DESC'], true), 'Order result by ASC or DESC order.', true)
|
||||
->inject('response')
|
||||
->inject('dbForInternal')
|
||||
->inject('usage')
|
||||
->action(function ($search, $limit, $offset, $after, $orderType, $response, $dbForInternal, $usage) {
|
||||
->action(function ($search, $limit, $offset, $cursor, $cursorDirection, $orderType, $response, $dbForInternal, $usage) {
|
||||
/** @var Appwrite\Utopia\Response $response */
|
||||
/** @var Utopia\Database\Database $dbForInternal */
|
||||
|
||||
@@ -201,18 +202,18 @@ App::get('/v1/database/collections')
|
||||
$queries[] = new Query('name', Query::TYPE_SEARCH, [$search]);
|
||||
}
|
||||
|
||||
if (!empty($after)) {
|
||||
$afterCollection = $dbForInternal->getDocument('collections', $after);
|
||||
if (!empty($cursor)) {
|
||||
$cursorCollection = $dbForInternal->getDocument('collections', $cursor);
|
||||
|
||||
if ($afterCollection->isEmpty()) {
|
||||
throw new Exception("Collection '{$after}' for the 'after' value not found.", 400);
|
||||
if ($cursorCollection->isEmpty()) {
|
||||
throw new Exception("Collection '{$cursor}' for the 'cursor' value not found.", 400);
|
||||
}
|
||||
}
|
||||
|
||||
$usage->setParam('database.collections.read', 1);
|
||||
|
||||
$response->dynamic(new Document([
|
||||
'collections' => $dbForInternal->find('collections', $queries, $limit, $offset, [], [$orderType], $afterCollection ?? null),
|
||||
'collections' => $dbForInternal->find('collections', $queries, $limit, $offset, [], [$orderType], $cursorCollection ?? null, $cursorDirection),
|
||||
'sum' => $dbForInternal->count('collections', $queries, APP_LIMIT_COUNT),
|
||||
]), Response::MODEL_COLLECTION_LIST);
|
||||
});
|
||||
@@ -1060,7 +1061,7 @@ App::delete('/v1/database/collections/:collectionId/attributes/:attributeId')
|
||||
}
|
||||
|
||||
$attribute = $dbForInternal->updateDocument('attributes', $attribute->getId(), $attribute->setAttribute('status', 'deleting'));
|
||||
$dbForInternal->purgeDocument('collections', $collectionId);
|
||||
$dbForInternal->deleteCachedCollection($collectionId);
|
||||
|
||||
$database
|
||||
->setParam('type', DATABASE_TYPE_DELETE_ATTRIBUTE)
|
||||
@@ -1167,7 +1168,7 @@ App::post('/v1/database/collections/:collectionId/indexes')
|
||||
throw new Exception('Index already exists', 409);
|
||||
}
|
||||
|
||||
$dbForInternal->purgeDocument('collections', $collectionId);
|
||||
$dbForInternal->deleteCachedCollection($collectionId);
|
||||
|
||||
$database
|
||||
->setParam('type', DATABASE_TYPE_CREATE_INDEX)
|
||||
@@ -1312,7 +1313,7 @@ App::delete('/v1/database/collections/:collectionId/indexes/:indexId')
|
||||
}
|
||||
|
||||
$index = $dbForInternal->updateDocument('indexes', $index->getId(), $index->setAttribute('status', 'deleting'));
|
||||
$dbForInternal->purgeDocument('collections', $collectionId);
|
||||
$dbForInternal->deleteCachedCollection($collectionId);
|
||||
|
||||
$database
|
||||
->setParam('type', DATABASE_TYPE_DELETE_INDEX)
|
||||
@@ -1365,7 +1366,7 @@ App::post('/v1/database/collections/:collectionId/documents')
|
||||
/** @var Utopia\Database\Document $user */
|
||||
/** @var Appwrite\Event\Event $audits */
|
||||
/** @var Appwrite\Stats\Stats $usage */
|
||||
|
||||
|
||||
$data = (\is_string($data)) ? \json_decode($data, true) : $data; // Cast to JSON array
|
||||
|
||||
if (empty($data)) {
|
||||
@@ -1375,7 +1376,7 @@ App::post('/v1/database/collections/:collectionId/documents')
|
||||
if (isset($data['$id'])) {
|
||||
throw new Exception('$id is not allowed for creating new documents, try update instead', 400);
|
||||
}
|
||||
|
||||
|
||||
$collection = $dbForInternal->getDocument('collections', $collectionId);
|
||||
|
||||
if ($collection->isEmpty()) {
|
||||
@@ -1427,14 +1428,15 @@ App::get('/v1/database/collections/:collectionId/documents')
|
||||
->param('queries', [], new ArrayList(new Text(128)), 'Array of query strings.', true)
|
||||
->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)
|
||||
->param('cursor', '', 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)
|
||||
->param('cursorDirection', Database::CURSOR_AFTER, new WhiteList([Database::CURSOR_AFTER, Database::CURSOR_BEFORE]), 'Direction of the cursor.', true)
|
||||
->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)
|
||||
->inject('response')
|
||||
->inject('dbForInternal')
|
||||
->inject('dbForExternal')
|
||||
->inject('usage')
|
||||
->action(function ($collectionId, $queries, $limit, $offset, $after, $orderAttributes, $orderTypes, $response, $dbForInternal, $dbForExternal, $usage) {
|
||||
->action(function ($collectionId, $queries, $limit, $offset, $cursor, $cursorDirection, $orderAttributes, $orderTypes, $response, $dbForInternal, $dbForExternal, $usage) {
|
||||
/** @var Appwrite\Utopia\Response $response */
|
||||
/** @var Utopia\Database\Database $dbForInternal */
|
||||
/** @var Utopia\Database\Database $dbForExternal */
|
||||
@@ -1457,22 +1459,22 @@ App::get('/v1/database/collections/:collectionId/documents')
|
||||
throw new Exception($validator->getDescription(), 400);
|
||||
}
|
||||
|
||||
if (!empty($after)) {
|
||||
$afterDocument = $dbForExternal->getDocument($collectionId, $after);
|
||||
if (!empty($cursor)) {
|
||||
$cursorDocument = $dbForExternal->getDocument($collectionId, $cursor);
|
||||
|
||||
if ($afterDocument->isEmpty()) {
|
||||
throw new Exception("Document '{$after}' for the 'after' value not found.", 400);
|
||||
if ($cursorDocument->isEmpty()) {
|
||||
throw new Exception("Document '{$cursor}' for the 'cursor' value not found.", 400);
|
||||
}
|
||||
}
|
||||
|
||||
$usage
|
||||
->setParam('database.documents.read', 1)
|
||||
->setParam('collectionId', $collectionId)
|
||||
;
|
||||
|
||||
;
|
||||
|
||||
$response->dynamic(new Document([
|
||||
'sum' => $dbForExternal->count($collectionId, $queries, APP_LIMIT_COUNT),
|
||||
'documents' => $dbForExternal->find($collectionId, $queries, $limit, $offset, $orderAttributes, $orderTypes, $afterDocument ?? null),
|
||||
'documents' => $dbForExternal->find($collectionId, $queries, $limit, $offset, $orderAttributes, $orderTypes, $cursorDocument ?? null, $cursorDirection),
|
||||
]), Response::MODEL_DOCUMENT_LIST);
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user