2019-05-09 18:54:39 +12:00
< ? php
use Utopia\App ;
2022-02-07 05:50:48 +13:00
use Appwrite\Extend\Exception ;
2021-08-16 09:09:40 +12:00
use Utopia\Audit\Audit ;
2021-06-10 09:11:51 +12:00
use Utopia\Validator\Boolean ;
2021-07-22 02:26:08 +12:00
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 ;
2020-06-19 08:13:03 +12:00
use Utopia\Validator\JSON ;
2021-08-17 11:21:00 +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-08-12 13:05:19 +12:00
use Utopia\Database\Validator\Authorization ;
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 ;
2021-07-24 02:59:55 +12:00
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 ;
2021-06-12 08:06:54 +12:00
use Utopia\Database\Exception\Structure as StructureException ;
2021-11-04 05:38:06 +13:00
use Appwrite\Auth\Auth ;
2021-08-17 11:21:00 +12:00
use Appwrite\Network\Validator\Email ;
use Appwrite\Network\Validator\IP ;
use Appwrite\Network\Validator\URL ;
2022-01-19 00:05:04 +13:00
use Appwrite\Utopia\Database\Validator\CustomId ;
2020-10-31 08:53:27 +13:00
use Appwrite\Utopia\Response ;
2021-12-13 06:58:17 +13:00
use Appwrite\Detector\Detector ;
2021-12-28 01:45:23 +13:00
use Appwrite\Event\Event ;
use Appwrite\Stats\Stats ;
2021-07-27 13:00:36 +12:00
2021-08-17 11:21:00 +12:00
/**
* Create attribute of varying type
*
2021-08-24 06:29:05 +12:00
* @ param string $collectionId
2021-08-23 04:34:10 +12:00
* @ param Utopia\Database\Document $attribute
* @ param Appwrite\Utopia\Response $response
2021-12-28 01:45:23 +13:00
* @ param Utopia\Database\Database $dbForProject
2021-08-23 04:34:10 +12:00
* @ param Appwrite\Event\Event $database
* @ param Appwrite\Event\Event $audits
2021-09-13 00:57:02 +12:00
* @ param Appwrite\Stats\Stats $usage
2021-08-17 11:21:00 +12:00
*
* @ return Document Newly created attribute document
*/
2021-12-28 01:45:23 +13:00
function createAttribute ( string $collectionId , Document $attribute , Response $response , Database $dbForProject , Event $database , Event $audits , Stats $usage ) : Document
2021-08-17 11:21:00 +12:00
{
2021-12-17 04:04:30 +13:00
$key = $attribute -> getAttribute ( 'key' );
2021-07-27 13:00:36 +12:00
$type = $attribute -> getAttribute ( 'type' , '' );
$size = $attribute -> getAttribute ( 'size' , 0 );
$required = $attribute -> getAttribute ( 'required' , true );
$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-12-17 04:04:30 +13:00
$default = $attribute -> getAttribute ( 'default' );
2021-07-27 13:00:36 +12:00
2021-12-28 01:45:23 +13:00
$collection = $dbForProject -> getDocument ( 'collections' , $collectionId );
2021-07-27 13:00:36 +12:00
if ( $collection -> isEmpty ()) {
2022-02-07 05:50:48 +13:00
throw new Exception ( 'Collection not found' , 404 , Exception :: COLLECTION_NOT_FOUND );
2021-07-27 13:00:36 +12:00
}
2021-08-22 09:48:07 +12:00
if ( ! empty ( $format )) {
if ( ! Structure :: hasFormat ( $format , $type )) {
2022-02-07 05:50:48 +13:00
throw new Exception ( " Format { $format } not available for { $type } attributes. " , 400 , Exception :: ATTRIBUTE_FORMAT_UNSUPPORTED );
2021-07-28 06:19:37 +12:00
}
2021-07-27 13:00:36 +12:00
}
2021-12-28 01:45:23 +13:00
// Must throw here since dbForProject->createAttribute is performed by db worker
2021-08-26 08:43:15 +12:00
if ( $required && $default ) {
2022-02-07 05:50:48 +13:00
throw new Exception ( 'Cannot set default value for required attribute' , 400 , Exception :: ATTRIBUTE_DEFAULT_UNSUPPORTED );
2021-08-26 08:43:15 +12:00
}
2021-10-27 09:14:25 +13:00
if ( $array && $default ) {
2022-02-07 05:50:48 +13:00
throw new Exception ( 'Cannot set default value for array attributes' , 400 , Exception :: ATTRIBUTE_DEFAULT_UNSUPPORTED );
2021-10-27 09:14:25 +13:00
}
2021-08-21 16:48:28 +12:00
try {
2021-10-06 02:57:57 +13:00
$attribute = new Document ([
2021-12-17 04:04:30 +13:00
'$id' => $collectionId . '_' . $key ,
'key' => $key ,
2021-08-21 16:48:28 +12:00
'collectionId' => $collectionId ,
'type' => $type ,
2021-10-05 13:23:15 +13:00
'status' => 'processing' , // processing, available, failed, deleting, stuck
2021-08-21 16:48:28 +12:00
'size' => $size ,
'required' => $required ,
'signed' => $signed ,
2021-08-28 05:12:16 +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-11-02 13:47:07 +13:00
'filters' => $filters ,
2021-10-06 02:57:57 +13:00
]);
2021-09-01 13:13:58 +12:00
2021-12-28 01:45:23 +13:00
$dbForProject -> checkAttribute ( $collection , $attribute );
$attribute = $dbForProject -> createDocument ( 'attributes' , $attribute );
2021-09-10 05:15:30 +12:00
}
catch ( DuplicateException $exception ) {
2022-02-07 05:50:48 +13:00
throw new Exception ( 'Attribute already exists' , 409 , Exception :: ATTRIBUTE_ALREADY_EXISTS );
2021-08-21 16:48:28 +12:00
}
2021-09-10 05:15:30 +12:00
catch ( LimitException $exception ) {
2022-02-07 05:50:48 +13:00
throw new Exception ( 'Attribute limit exceeded' , 400 , Exception :: ATTRIBUTE_LIMIT_EXCEEDED );
2021-08-21 16:48:28 +12:00
}
2021-12-28 01:45:23 +13:00
$dbForProject -> deleteCachedDocument ( 'collections' , $collectionId );
2022-02-08 01:25:15 +13:00
$dbForProject -> deleteCachedCollection ( 'collection_' . $collection -> getInternalId ());
2021-07-27 13:00:36 +12:00
2021-08-26 07:36:45 +12:00
// Pass clone of $attribute object to workers
// so we can later modify Document to fit response model
$clone = clone $attribute ;
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-08-26 07:36:45 +12:00
-> setParam ( 'document' , $clone )
2021-07-27 13:00:36 +12:00
;
2021-08-15 23:08:32 +12:00
$usage -> setParam ( 'database.collections.update' , 1 );
2021-07-27 13:00:36 +12:00
$audits
-> setParam ( 'event' , 'database.attributes.create' )
2021-12-29 04:48:36 +13:00
-> setParam ( 'resource' , 'collection/' . $collectionId )
2021-08-26 07:36:45 +12:00
-> setParam ( 'data' , $clone )
2021-07-27 13:00:36 +12:00
;
$response -> setStatusCode ( Response :: STATUS_CODE_CREATED );
2021-08-17 11:21:00 +12:00
return $attribute ;
2021-07-27 13:00:36 +12:00
};
2020-06-21 23:56:42 +12:00
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' )
2019-06-09 01:13:19 +12:00
-> 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 )
2022-01-13 11:05:25 +13:00
-> 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.' )
2022-04-19 08:15:22 +12:00
-> param ( 'permission' , null , new WhiteList ([ 'document' , 'collection' ]), 'Specifies the permissions model used in this collection, which accepts either \'collection\' or \'document\'. For \'collection\' level permission, the permissions specified in read and write params are applied to all documents in the collection. For \'document\' level permissions, read and write permissions are specified in each document. [learn more about permissions](https://appwrite.io/docs/permissions) and get a full list of available permissions.' )
2021-12-11 01:27:11 +13: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](https://appwrite.io/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](https://appwrite.io/docs/permissions) and get a full list of available permissions.' )
2020-12-27 04:05:04 +13:00
-> inject ( 'response' )
2021-12-28 01:45:23 +13:00
-> inject ( 'dbForProject' )
2020-12-27 04:05:04 +13:00
-> inject ( 'audits' )
2021-08-15 23:08:32 +12:00
-> inject ( 'usage' )
2021-12-28 01:45:23 +13:00
-> action ( function ( $collectionId , $name , $permission , $read , $write , $response , $dbForProject , $audits , $usage ) {
2020-10-30 02:50:49 +13:00
/** @var Appwrite\Utopia\Response $response */
2021-12-28 01:45:23 +13:00
/** @var Utopia\Database\Database $dbForProject*/
2020-07-06 02:19:59 +12:00
/** @var Appwrite\Event\Event $audits */
2021-08-15 23:08:32 +12:00
/** @var Appwrite\Stats\Stats $usage */
2021-06-16 01:38:24 +12:00
2021-12-28 01:45:23 +13:00
$collectionId = $collectionId == 'unique()' ? $dbForProject -> getId () : $collectionId ;
2021-07-06 08:27:20 +12:00
2021-08-17 05:28:33 +12:00
try {
2022-02-08 01:25:15 +13:00
$dbForProject -> createDocument ( 'collections' , new Document ([
2021-08-17 05:28:33 +12:00
'$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-12-14 01:42:04 +13:00
'enabled' => true ,
2021-08-17 05:28:33 +12:00
'name' => $name ,
'search' => implode ( ' ' , [ $collectionId , $name ]),
]));
2022-02-08 01:25:15 +13:00
$collection = $dbForProject -> getDocument ( 'collections' , $collectionId );
2022-01-24 21:20:02 +13:00
2022-02-08 01:25:15 +13:00
$dbForProject -> createCollection ( 'collection_' . $collection -> getInternalId ());
2021-08-19 07:00:26 +12:00
} catch ( DuplicateException $th ) {
2022-02-07 05:50:48 +13:00
throw new Exception ( 'Collection already exists' , 409 , Exception :: COLLECTION_ALREADY_EXISTS );
2022-01-24 21:20:02 +13:00
} catch ( LimitException $th ) {
2022-02-07 05:50:48 +13:00
throw new Exception ( 'Collection limit exceeded' , 400 , Exception :: COLLECTION_LIMIT_EXCEEDED );
2021-08-17 05:28:33 +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.create' )
2021-12-29 04:48:36 +13:00
-> setParam ( 'resource' , 'collection/' . $collectionId )
2021-06-11 06:19:42 +12:00
-> setParam ( 'data' , $collection -> getArrayCopy ())
2020-06-30 09:43:34 +12:00
;
2021-08-15 23:08:32 +12:00
$usage -> setParam ( 'database.collections.create' , 1 );
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 )
2021-12-15 00:21:44 +13:00
-> param ( 'limit' , 25 , new Range ( 0 , 100 ), 'Maximum number of collection to return in response. By default will return maximum 25 results. Maximum of 100 results allowed per request.' , true )
-> param ( 'offset' , 0 , new Range ( 0 , APP_LIMIT_COUNT ), 'Offset value. The default value is 0. Use this param to manage pagination. [learn more about pagination](https://appwrite.io/docs/pagination)' , true )
2021-10-05 23:30:33 +13:00
-> 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 )
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-12-28 01:45:23 +13:00
-> inject ( 'dbForProject' )
2021-08-15 23:08:32 +12:00
-> inject ( 'usage' )
2021-12-28 01:45:23 +13:00
-> action ( function ( $search , $limit , $offset , $cursor , $cursorDirection , $orderType , $response , $dbForProject , $usage ) {
2020-10-30 02:50:49 +13:00
/** @var Appwrite\Utopia\Response $response */
2021-12-28 01:45:23 +13:00
/** @var Utopia\Database\Database $dbForProject */
2020-06-30 09:43:34 +12:00
2021-10-05 23:30:33 +13:00
if ( ! empty ( $cursor )) {
2021-12-28 01:45:23 +13:00
$cursorCollection = $dbForProject -> getDocument ( 'collections' , $cursor );
2021-08-17 05:28:33 +12:00
2021-10-05 23:30:33 +13:00
if ( $cursorCollection -> isEmpty ()) {
2022-02-09 11:56:11 +13:00
throw new Exception ( " Collection ' { $cursor } ' for the 'cursor' value not found. " , 400 , Exception :: GENERAL_CURSOR_NOT_FOUND );
2021-08-07 00:35:57 +12:00
}
2021-08-17 05:28:33 +12:00
}
2020-06-30 09:43:34 +12:00
2021-08-19 01:42:03 +12:00
$queries = [];
2021-08-07 00:35:57 +12:00
2021-08-19 01:42:03 +12:00
if ( ! empty ( $search )) {
2022-01-19 20:57:18 +13:00
$queries [] = new Query ( 'search' , Query :: TYPE_SEARCH , [ $search ]);
2021-08-07 00:35:57 +12:00
}
2021-08-15 23:08:32 +12:00
$usage -> setParam ( 'database.collections.read' , 1 );
2021-07-26 02:47:18 +12:00
$response -> dynamic ( new Document ([
2021-12-28 01:45:23 +13:00
'collections' => $dbForProject -> find ( 'collections' , $queries , $limit , $offset , [], [ $orderType ], $cursorCollection ? ? null , $cursorDirection ),
2022-02-27 22:57:09 +13:00
'total' => $dbForProject -> 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 )
2021-12-11 01:27:11 +13:00
-> param ( 'collectionId' , '' , new UID (), 'Collection ID.' )
2020-12-27 04:05:04 +13:00
-> inject ( 'response' )
2021-12-28 01:45:23 +13:00
-> inject ( 'dbForProject' )
2021-08-15 23:08:32 +12:00
-> inject ( 'usage' )
2021-12-28 01:45:23 +13:00
-> action ( function ( $collectionId , $response , $dbForProject , $usage ) {
2020-10-30 02:50:49 +13:00
/** @var Appwrite\Utopia\Response $response */
2021-12-28 01:45:23 +13:00
/** @var Utopia\Database\Database $dbForProject */
2021-06-12 06:07:05 +12:00
2021-12-28 01:45:23 +13:00
$collection = $dbForProject -> getDocument ( 'collections' , $collectionId );
2020-06-30 09:43:34 +12:00
2021-06-12 02:25:52 +12:00
if ( $collection -> isEmpty ()) {
2022-02-07 05:50:48 +13:00
throw new Exception ( 'Collection not found' , 404 , Exception :: COLLECTION_NOT_FOUND );
2020-02-01 11:34:07 +13:00
}
2020-06-30 09:43:34 +12:00
2021-08-15 23:08:32 +12:00
$usage -> setParam ( 'database.collections.read' , 1 );
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-27 06:15:36 +12:00
App :: get ( '/v1/database/usage' )
2021-08-21 00:10:52 +12:00
-> desc ( 'Get usage stats for the database' )
2021-08-20 20:24:45 +12:00
-> groups ([ 'api' , 'database' ])
-> label ( 'scope' , 'collections.read' )
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN ])
-> label ( 'sdk.namespace' , 'database' )
-> label ( 'sdk.method' , 'getUsage' )
2021-08-27 06:15:36 +12:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
2021-08-28 05:34:43 +12:00
-> label ( 'sdk.response.model' , Response :: MODEL_USAGE_DATABASE )
2021-08-20 20:24:45 +12:00
-> param ( 'range' , '30d' , new WhiteList ([ '24h' , '7d' , '30d' , '90d' ], true ), 'Date range.' , true )
-> inject ( 'response' )
2021-12-28 01:45:23 +13:00
-> inject ( 'dbForProject' )
-> action ( function ( $range , $response , $dbForProject ) {
2021-08-20 20:24:45 +12:00
/** @var Appwrite\Utopia\Response $response */
/** @var Utopia\Database\Database $dbForConsole */
2021-12-28 01:45:23 +13:00
/** @var Utopia\Database\Database $dbForProject */
2021-08-20 20:24:45 +12:00
/** @var Utopia\Registry\Registry $register */
2021-08-27 06:15:36 +12:00
$usage = [];
2021-08-20 20:24:45 +12:00
if ( App :: getEnv ( '_APP_USAGE_STATS' , 'enabled' ) == 'enabled' ) {
2021-10-28 11:17:15 +13:00
$periods = [
2021-08-20 20:24:45 +12:00
'24h' => [
2021-09-06 18:43:20 +12:00
'period' => '30m' ,
2021-08-20 20:24:45 +12:00
'limit' => 48 ,
],
'7d' => [
'period' => '1d' ,
'limit' => 7 ,
],
'30d' => [
'period' => '1d' ,
'limit' => 30 ,
],
'90d' => [
'period' => '1d' ,
'limit' => 90 ,
],
];
$metrics = [
2021-08-27 06:15:36 +12:00
'database.documents.count' ,
'database.collections.count' ,
2021-08-20 20:24:45 +12:00
'database.collections.create' ,
'database.collections.read' ,
'database.collections.update' ,
'database.collections.delete' ,
'database.documents.create' ,
'database.documents.read' ,
'database.documents.update' ,
'database.documents.delete'
];
2021-08-27 06:15:36 +12:00
$stats = [];
2021-08-20 20:24:45 +12:00
2021-12-28 01:45:23 +13:00
Authorization :: skip ( function () use ( $dbForProject , $periods , $range , $metrics , & $stats ) {
2021-08-29 04:25:48 +12:00
foreach ( $metrics as $metric ) {
2021-10-28 11:17:15 +13:00
$limit = $periods [ $range ][ 'limit' ];
$period = $periods [ $range ][ 'period' ];
2021-10-28 08:57:20 +13:00
2021-12-28 01:45:23 +13:00
$requestDocs = $dbForProject -> find ( 'stats' , [
2021-10-28 08:57:20 +13:00
new Query ( 'period' , Query :: TYPE_EQUAL , [ $period ]),
2021-08-29 04:25:48 +12:00
new Query ( 'metric' , Query :: TYPE_EQUAL , [ $metric ]),
2021-10-28 08:57:20 +13:00
], $limit , 0 , [ 'time' ], [ Database :: ORDER_DESC ]);
2021-10-27 02:19:28 +13:00
2021-08-29 04:25:48 +12:00
$stats [ $metric ] = [];
foreach ( $requestDocs as $requestDoc ) {
$stats [ $metric ][] = [
'value' => $requestDoc -> getAttribute ( 'value' ),
'date' => $requestDoc -> getAttribute ( 'time' ),
];
}
2021-10-28 08:57:20 +13:00
// backfill metrics with empty values for graphs
$backfill = $limit - \count ( $requestDocs );
while ( $backfill > 0 ) {
$last = $limit - $backfill - 1 ; // array index of last added metric
$diff = match ( $period ) { // convert period to seconds for unix timestamp math
'30m' => 1800 ,
'1d' => 86400 ,
};
$stats [ $metric ][] = [
'value' => 0 ,
2021-10-28 11:17:51 +13:00
'date' => ( $stats [ $metric ][ $last ][ 'date' ] ? ? \time ()) - $diff , // time of last metric minus period
2021-10-28 08:57:20 +13:00
];
$backfill -- ;
}
2021-10-28 11:31:25 +13:00
// TODO@kodumbeats explore performance if query is ordered by time ASC
2021-08-29 04:25:48 +12:00
$stats [ $metric ] = array_reverse ( $stats [ $metric ]);
2021-10-27 02:19:28 +13:00
}
2021-08-29 04:25:48 +12:00
});
2021-08-20 22:04:57 +12:00
2021-08-27 06:15:36 +12:00
$usage = new Document ([
2021-08-20 22:04:57 +12:00
'range' => $range ,
2021-10-27 02:19:28 +13:00
'documentsCount' => $stats [ " database.documents.count " ],
'collectionsCount' => $stats [ " database.collections.count " ],
'documentsCreate' => $stats [ " database.documents.create " ],
'documentsRead' => $stats [ " database.documents.read " ],
'documentsUpdate' => $stats [ " database.documents.update " ],
'documentsDelete' => $stats [ " database.documents.delete " ],
'collectionsCreate' => $stats [ " database.collections.create " ],
'collectionsRead' => $stats [ " database.collections.read " ],
'collectionsUpdate' => $stats [ " database.collections.update " ],
'collectionsDelete' => $stats [ " database.collections.delete " ],
2021-08-20 22:04:57 +12:00
]);
}
2021-08-27 06:15:36 +12:00
2021-08-28 05:34:43 +12:00
$response -> dynamic ( $usage , Response :: MODEL_USAGE_DATABASE );
2021-08-20 20:24:45 +12:00
});
2021-08-20 23:38:57 +12:00
App :: get ( '/v1/database/:collectionId/usage' )
2021-08-21 00:10:52 +12:00
-> desc ( 'Get usage stats for a collection' )
2021-08-20 23:38:57 +12:00
-> groups ([ 'api' , 'database' ])
-> label ( 'scope' , 'collections.read' )
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN ])
-> label ( 'sdk.namespace' , 'database' )
2021-09-01 08:55:13 +12:00
-> label ( 'sdk.method' , 'getCollectionUsage' )
2021-08-27 06:15:36 +12:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
2021-08-28 05:34:43 +12:00
-> label ( 'sdk.response.model' , Response :: MODEL_USAGE_COLLECTION )
2021-08-20 23:38:57 +12:00
-> param ( 'range' , '30d' , new WhiteList ([ '24h' , '7d' , '30d' , '90d' ], true ), 'Date range.' , true )
2021-12-11 01:27:11 +13:00
-> param ( 'collectionId' , '' , new UID (), 'Collection ID.' )
2021-08-20 23:38:57 +12:00
-> inject ( 'response' )
2021-12-28 01:45:23 +13:00
-> inject ( 'dbForProject' )
-> action ( function ( $range , $collectionId , $response , $dbForProject ) {
2021-08-20 23:38:57 +12:00
/** @var Appwrite\Utopia\Response $response */
2021-12-28 01:45:23 +13:00
/** @var Utopia\Database\Database $dbForProject */
2021-08-20 23:38:57 +12:00
/** @var Utopia\Registry\Registry $register */
2022-02-08 01:25:15 +13:00
$collectionDocument = $dbForProject -> getDocument ( 'collections' , $collectionId );
$collection = $dbForProject -> getCollection ( 'collection_' . $collectionDocument -> getInternalId ());
2021-08-20 23:38:57 +12:00
if ( $collection -> isEmpty ()) {
2022-02-07 05:50:48 +13:00
throw new Exception ( 'Collection not found' , 404 , Exception :: COLLECTION_NOT_FOUND );
2021-08-20 23:38:57 +12:00
}
2021-12-29 04:48:36 +13:00
2021-08-27 06:15:36 +12:00
$usage = [];
2021-08-20 23:38:57 +12:00
if ( App :: getEnv ( '_APP_USAGE_STATS' , 'enabled' ) == 'enabled' ) {
2021-10-28 11:17:15 +13:00
$periods = [
2021-08-20 23:38:57 +12:00
'24h' => [
2021-09-06 18:43:20 +12:00
'period' => '30m' ,
2021-08-20 23:38:57 +12:00
'limit' => 48 ,
],
'7d' => [
'period' => '1d' ,
'limit' => 7 ,
],
'30d' => [
'period' => '1d' ,
'limit' => 30 ,
],
'90d' => [
'period' => '1d' ,
'limit' => 90 ,
],
];
$metrics = [
2021-08-27 06:15:36 +12:00
" database.collections. $collectionId .documents.count " ,
2021-08-20 23:38:57 +12:00
" database.collections. $collectionId .documents.create " ,
" database.collections. $collectionId .documents.read " ,
" database.collections. $collectionId .documents.update " ,
" database.collections. $collectionId .documents.delete " ,
];
2021-08-27 06:15:36 +12:00
$stats = [];
2021-08-29 04:25:48 +12:00
2021-12-28 01:45:23 +13:00
Authorization :: skip ( function () use ( $dbForProject , $periods , $range , $metrics , & $stats ) {
2021-08-29 04:25:48 +12:00
foreach ( $metrics as $metric ) {
2021-10-28 11:17:15 +13:00
$limit = $periods [ $range ][ 'limit' ];
$period = $periods [ $range ][ 'period' ];
2021-10-28 08:57:20 +13:00
2021-12-28 01:45:23 +13:00
$requestDocs = $dbForProject -> find ( 'stats' , [
2021-10-28 08:57:20 +13:00
new Query ( 'period' , Query :: TYPE_EQUAL , [ $period ]),
2021-08-29 04:25:48 +12:00
new Query ( 'metric' , Query :: TYPE_EQUAL , [ $metric ]),
2021-10-28 08:57:20 +13:00
], $limit , 0 , [ 'time' ], [ Database :: ORDER_DESC ]);
2021-10-27 02:19:28 +13:00
2021-08-29 04:25:48 +12:00
$stats [ $metric ] = [];
foreach ( $requestDocs as $requestDoc ) {
$stats [ $metric ][] = [
'value' => $requestDoc -> getAttribute ( 'value' ),
'date' => $requestDoc -> getAttribute ( 'time' ),
];
}
2021-10-28 08:57:20 +13:00
// backfill metrics with empty values for graphs
$backfill = $limit - \count ( $requestDocs );
while ( $backfill > 0 ) {
$last = $limit - $backfill - 1 ; // array index of last added metric
$diff = match ( $period ) { // convert period to seconds for unix timestamp math
'30m' => 1800 ,
'1d' => 86400 ,
};
$stats [ $metric ][] = [
'value' => 0 ,
2021-10-28 11:17:51 +13:00
'date' => ( $stats [ $metric ][ $last ][ 'date' ] ? ? \time ()) - $diff , // time of last metric minus period
2021-10-28 08:57:20 +13:00
];
$backfill -- ;
}
2021-08-29 04:25:48 +12:00
$stats [ $metric ] = array_reverse ( $stats [ $metric ]);
2021-12-29 04:48:36 +13:00
}
2021-08-29 04:25:48 +12:00
});
2021-08-20 23:38:57 +12:00
2021-08-27 06:15:36 +12:00
$usage = new Document ([
2021-08-20 23:38:57 +12:00
'range' => $range ,
2021-10-27 02:19:28 +13:00
'documentsCount' => $stats [ " database.collections. $collectionId .documents.count " ],
'documentsCreate' => $stats [ " database.collections. $collectionId .documents.create " ],
'documentsRead' => $stats [ " database.collections. $collectionId .documents.read " ],
'documentsUpdate' => $stats [ " database.collections. $collectionId .documents.update " ],
'documentsDelete' => $stats [ " database.collections. $collectionId .documents.delete " ]
2021-08-20 23:38:57 +12:00
]);
}
2021-08-27 06:15:36 +12:00
2021-08-28 05:34:43 +12:00
$response -> dynamic ( $usage , Response :: MODEL_USAGE_COLLECTION );
2021-08-20 23:38:57 +12:00
});
2021-08-20 20:24:45 +12: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 )
2021-12-11 01:27:11 +13:00
-> param ( 'collectionId' , '' , new UID (), 'Collection ID.' )
2021-12-15 00:21:44 +13:00
-> param ( 'limit' , 25 , new Range ( 0 , 100 ), 'Maximum number of logs to return in response. By default will return maximum 25 results. Maximum of 100 results allowed per request.' , true )
-> param ( 'offset' , 0 , new Range ( 0 , APP_LIMIT_COUNT ), 'Offset value. The default value is 0. Use this value to manage pagination. [learn more about pagination](https://appwrite.io/docs/pagination)' , true )
2021-08-11 06:03:32 +12:00
-> inject ( 'response' )
2021-12-28 01:45:23 +13:00
-> inject ( 'dbForProject' )
2021-08-11 06:03:32 +12:00
-> inject ( 'locale' )
-> inject ( 'geodb' )
2021-12-28 01:45:23 +13:00
-> action ( function ( $collectionId , $limit , $offset , $response , $dbForProject , $locale , $geodb ) {
2021-08-11 06:03:32 +12:00
/** @var Appwrite\Utopia\Response $response */
/** @var Utopia\Database\Document $project */
2021-12-28 01:45:23 +13:00
/** @var Utopia\Database\Database $dbForProject */
2021-08-11 06:03:32 +12:00
/** @var Utopia\Locale\Locale $locale */
/** @var MaxMind\Db\Reader $geodb */
2022-02-08 01:25:15 +13:00
$collectionDocument = $dbForProject -> getDocument ( 'collections' , $collectionId );
$collection = $dbForProject -> getCollection ( 'collection_' . $collectionDocument -> getInternalId ());
2021-08-11 06:03:32 +12:00
if ( $collection -> isEmpty ()) {
2022-02-07 05:50:48 +13:00
throw new Exception ( 'Collection not found' , 404 , Exception :: COLLECTION_NOT_FOUND );
2021-08-11 06:03:32 +12:00
}
2021-12-28 01:45:23 +13:00
$audit = new Audit ( $dbForProject );
2021-12-29 04:48:36 +13:00
$resource = 'collection/' . $collectionId ;
2021-11-17 03:54:29 +13:00
$logs = $audit -> getLogsByResource ( $resource , $limit , $offset );
2021-08-11 06:03:32 +12:00
$output = [];
foreach ( $logs as $i => & $log ) {
$log [ 'userAgent' ] = ( ! empty ( $log [ 'userAgent' ])) ? $log [ 'userAgent' ] : 'UNKNOWN' ;
2021-12-13 06:58:17 +13:00
$detector = new Detector ( $log [ 'userAgent' ]);
$detector -> skipBotDetection (); // OPTIONAL: If called, bot detection will completely be skipped (bots will be detected as regular devices then)
2021-08-11 06:03:32 +12:00
2021-12-13 06:58:17 +13:00
$os = $detector -> getOS ();
$client = $detector -> getClient ();
$device = $detector -> getDevice ();
2021-08-11 06:03:32 +12:00
$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' ],
2021-12-13 06:58:17 +13:00
'osCode' => $os [ 'osCode' ],
'osName' => $os [ 'osName' ],
'osVersion' => $os [ 'osVersion' ],
'clientType' => $client [ 'clientType' ],
'clientCode' => $client [ 'clientCode' ],
'clientName' => $client [ 'clientName' ],
'clientVersion' => $client [ 'clientVersion' ],
'clientEngine' => $client [ 'clientEngine' ],
'clientEngineVersion' => $client [ 'clientEngineVersion' ],
'deviceName' => $device [ 'deviceName' ],
'deviceBrand' => $device [ 'deviceBrand' ],
2021-12-13 06:59:12 +13:00
'deviceModel' => $device [ 'deviceModel' ]
2021-08-11 06:03:32 +12:00
]);
$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' );
}
}
2021-11-17 03:54:29 +13:00
$response -> dynamic ( new Document ([
2022-02-27 22:57:09 +13:00
'total' => $audit -> countLogsByResource ( $resource ),
2021-11-17 03:54:29 +13:00
'logs' => $output ,
]), Response :: MODEL_LOG_LIST );
2021-08-11 06:03:32 +12:00
});
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' ])
2019-06-09 01:13:19 +12:00
-> 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' )
2019-06-09 20:51:10 +12:00
-> 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 )
2021-12-11 01:27:11 +13:00
-> param ( 'collectionId' , '' , new UID (), 'Collection ID.' )
2021-07-29 20:09:24 +12:00
-> param ( 'name' , null , new Text ( 128 ), 'Collection name. Max length: 128 chars.' )
2021-12-11 01:27:11 +13: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](https://appwrite.io/docs/permissions) and get a full list of available permissions.' )
-> param ( 'read' , null , new Permissions (), 'An array of strings with read permissions. By default inherits the existing read permissions. [learn more about permissions](https://appwrite.io/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](https://appwrite.io/docs/permissions) and get a full list of available permissions.' , true )
2021-12-14 01:42:04 +13:00
-> param ( 'enabled' , true , new Boolean (), 'Is collection enabled?' , true )
2020-12-27 04:05:04 +13:00
-> inject ( 'response' )
2021-12-28 01:45:23 +13:00
-> inject ( 'dbForProject' )
2020-12-27 04:05:04 +13:00
-> inject ( 'audits' )
2021-08-15 23:08:32 +12:00
-> inject ( 'usage' )
2021-12-28 01:45:23 +13:00
-> action ( function ( $collectionId , $name , $permission , $read , $write , $enabled , $response , $dbForProject , $audits , $usage ) {
2020-10-30 02:50:49 +13:00
/** @var Appwrite\Utopia\Response $response */
2021-12-28 01:45:23 +13:00
/** @var Utopia\Database\Database $dbForProject */
2020-07-06 02:19:59 +12:00
/** @var Appwrite\Event\Event $audits */
2021-08-15 23:08:32 +12:00
/** @var Appwrite\Stats\Stats $usage */
2019-05-09 18:54:39 +12:00
2021-12-28 01:45:23 +13:00
$collection = $dbForProject -> getDocument ( 'collections' , $collectionId );
2019-10-01 17:57:41 +13:00
2021-06-17 08:36:18 +12:00
if ( $collection -> isEmpty ()) {
2022-02-07 05:50:48 +13:00
throw new Exception ( 'Collection not found' , 404 , Exception :: COLLECTION_NOT_FOUND );
2020-06-30 09:43:34 +12:00
}
2019-10-01 17:57:41 +13:00
2021-12-14 01:42:04 +13:00
$read ? ? = $collection -> getRead () ? ? []; // By default inherit read permissions
$write ? ? = $collection -> getWrite () ? ? []; // By default inherit write permissions
$enabled ? ? = $collection -> getAttribute ( 'enabled' , true );
2019-05-09 18:54:39 +12:00
2020-06-30 09:43:34 +12:00
try {
2021-12-29 04:48:36 +13:00
$collection = $dbForProject -> updateDocument ( 'collections' , $collectionId , $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-12-14 01:42:04 +13:00
-> setAttribute ( 'enabled' , $enabled )
2021-08-17 05:28:33 +12:00
-> setAttribute ( 'search' , implode ( ' ' , [ $collectionId , $name ]))
);
2021-08-19 16:05:44 +12:00
}
catch ( AuthorizationException $exception ) {
2022-02-07 05:50:48 +13:00
throw new Exception ( 'Unauthorized permissions' , 401 , Exception :: USER_UNAUTHORIZED );
2021-08-19 16:05:44 +12:00
}
catch ( StructureException $exception ) {
2022-02-07 05:50:48 +13:00
throw new Exception ( 'Bad structure. ' . $exception -> getMessage (), 400 , Exception :: DOCUMENT_INVALID_STRUCTURE );
2021-08-17 05:28:33 +12:00
}
2020-01-05 04:45:28 +13:00
2021-08-15 23:08:32 +12:00
$usage -> setParam ( 'database.collections.update' , 1 );
2020-07-06 02:19:59 +12:00
$audits
2020-06-30 09:43:34 +12:00
-> setParam ( 'event' , 'database.collections.update' )
2021-12-29 04:48:36 +13:00
-> setParam ( 'resource' , 'collection/' . $collectionId )
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' ])
2019-06-09 01:13:19 +12:00
-> 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 )
2021-12-11 01:27:11 +13:00
-> param ( 'collectionId' , '' , new UID (), 'Collection ID.' )
2020-12-27 04:05:04 +13:00
-> inject ( 'response' )
2021-12-28 01:45:23 +13:00
-> inject ( 'dbForProject' )
2020-12-27 04:05:04 +13:00
-> inject ( 'events' )
-> inject ( 'audits' )
-> inject ( 'deletes' )
2021-08-15 23:08:32 +12:00
-> inject ( 'usage' )
2021-12-28 01:45:23 +13:00
-> action ( function ( $collectionId , $response , $dbForProject , $events , $audits , $deletes , $usage ) {
2020-10-30 02:50:49 +13:00
/** @var Appwrite\Utopia\Response $response */
2021-12-28 01:45:23 +13:00
/** @var Utopia\Database\Database $dbForProject */
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 */
2021-08-15 23:08:32 +12:00
/** @var Appwrite\Stats\Stats $audits */
2019-05-09 18:54:39 +12:00
2021-12-28 01:45:23 +13:00
$collection = $dbForProject -> getDocument ( 'collections' , $collectionId );
2019-05-09 18:54:39 +12:00
2021-06-12 02:25:52 +12:00
if ( $collection -> isEmpty ()) {
2022-02-07 05:50:48 +13:00
throw new Exception ( 'Collection not found' , 404 , Exception :: COLLECTION_NOT_FOUND );
2020-06-30 09:43:34 +12:00
}
2020-01-05 04:45:28 +13:00
2021-12-28 01:45:23 +13:00
if ( ! $dbForProject -> deleteDocument ( 'collections' , $collectionId )) {
2022-02-07 09:49:21 +13:00
throw new Exception ( 'Failed to remove collection from DB' , 500 , Exception :: GENERAL_SERVER_ERROR );
2021-08-17 05:28:33 +12:00
}
2020-12-05 09:47:02 +13:00
2022-02-08 01:25:15 +13:00
$dbForProject -> deleteCachedCollection ( 'collection_' . $collection -> getInternalId ());
2021-11-02 13:48:38 +13:00
2021-09-03 04:45:03 +12:00
$deletes
-> setParam ( 'type' , DELETE_TYPE_DOCUMENT )
-> setParam ( 'document' , $collection )
;
2021-08-15 23:08:32 +12:00
$usage -> setParam ( 'database.collections.delete' , 1 );
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-12-29 04:48:36 +13:00
-> setParam ( 'resource' , 'collection/' . $collectionId )
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
2021-07-22 01:23:12 +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' )
2021-08-14 22:16:00 +12:00
-> 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' )
2021-07-22 01:23:12 +12:00
-> label ( 'sdk.method' , 'createStringAttribute' )
2021-09-28 13:26:33 +13:00
-> label ( 'sdk.description' , '/docs/references/database/create-string-attribute.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 )
2021-10-01 08:37:21 +13:00
-> label ( 'sdk.response.model' , Response :: MODEL_ATTRIBUTE_STRING )
2021-12-11 01:27:11 +13:00
-> param ( 'collectionId' , '' , new UID (), 'Collection ID. You can create a new collection using the Database service [server integration](https://appwrite.io/docs/server/database#createCollection).' )
2021-12-17 04:04:30 +13:00
-> param ( 'key' , '' , new Key (), 'Attribute Key.' )
2021-10-27 09:51:40 +13:00
-> param ( 'size' , null , new Range ( 1 , APP_DATABASE_ATTRIBUTE_STRING_MAX_LENGTH , Range :: TYPE_INTEGER ), 'Attribute size for text attributes, in number of characters.' )
2021-06-10 09:11:51 +12:00
-> param ( 'required' , null , new Boolean (), 'Is attribute required?' )
2021-07-22 01:23:12 +12:00
-> 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-12-28 01:45:23 +13:00
-> inject ( 'dbForProject' )
2021-06-19 04:13:37 +12:00
-> inject ( 'database' )
2021-06-09 08:12:14 +12:00
-> inject ( 'audits' )
2021-08-15 23:08:32 +12:00
-> inject ( 'usage' )
2021-12-28 01:45:23 +13:00
-> action ( function ( $collectionId , $key , $size , $required , $default , $array , $response , $dbForProject , $database , $audits , $usage ) {
2021-03-25 04:40:33 +13:00
/** @var Appwrite\Utopia\Response $response */
2021-12-28 01:45:23 +13:00
/** @var Utopia\Database\Database $dbForProject*/
2021-06-19 06:27:14 +12:00
/** @var Appwrite\Event\Event $database */
2021-03-25 04:40:33 +13:00
/** @var Appwrite\Event\Event $audits */
2021-08-15 23:08:32 +12:00
/** @var Appwrite\Stats\Stats $usage */
2021-03-25 04:40:33 +13:00
2021-08-17 11:21:00 +12:00
// Ensure attribute default is within required size
$validator = new Text ( $size );
if ( ! is_null ( $default ) && ! $validator -> isValid ( $default )) {
2022-02-07 05:50:48 +13:00
throw new Exception ( $validator -> getDescription (), 400 , Exception :: ATTRIBUTE_VALUE_INVALID );
2021-08-17 11:21:00 +12:00
}
2021-08-25 01:49:58 +12:00
$attribute = createAttribute ( $collectionId , new Document ([
2021-12-17 04:04:30 +13:00
'key' => $key ,
2021-07-27 13:00:36 +12:00
'type' => Database :: VAR_STRING ,
'size' => $size ,
'required' => $required ,
'default' => $default ,
'array' => $array ,
2021-12-28 01:45:23 +13:00
]), $response , $dbForProject , $database , $audits , $usage );
2021-08-17 11:21:00 +12:00
$response -> dynamic ( $attribute , Response :: MODEL_ATTRIBUTE_STRING );
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' )
2021-08-14 22:16:00 +12:00
-> 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' )
2021-09-28 13:26:33 +13:00
-> label ( 'sdk.description' , '/docs/references/database/create-email-attribute.md' )
2021-07-27 13:00:36 +12:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_CREATED )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
2021-10-01 08:37:21 +13:00
-> label ( 'sdk.response.model' , Response :: MODEL_ATTRIBUTE_EMAIL )
2021-12-13 23:10:03 +13:00
-> param ( 'collectionId' , '' , new UID (), 'Collection ID. You can create a new collection using the Database service [server integration](https://appwrite.io/docs/server/database#createCollection).' )
2021-12-17 04:04:30 +13:00
-> param ( 'key' , '' , new Key (), 'Attribute Key.' )
2021-07-27 13:00:36 +12:00
-> param ( 'required' , null , new Boolean (), 'Is attribute required?' )
2021-08-25 01:46:41 +12:00
-> param ( 'default' , null , new Email (), 'Default value for attribute when not provided. Cannot be set when attribute is required.' , true )
2021-07-27 13:00:36 +12:00
-> param ( 'array' , false , new Boolean (), 'Is attribute an array?' , true )
-> inject ( 'response' )
2021-12-28 01:45:23 +13:00
-> inject ( 'dbForProject' )
2021-07-27 13:00:36 +12:00
-> inject ( 'database' )
-> inject ( 'audits' )
2021-08-15 23:08:32 +12:00
-> inject ( 'usage' )
2021-12-28 01:45:23 +13:00
-> action ( function ( $collectionId , $key , $required , $default , $array , $response , $dbForProject , $database , $audits , $usage ) {
2021-07-27 13:00:36 +12:00
/** @var Appwrite\Utopia\Response $response */
2021-12-28 01:45:23 +13:00
/** @var Utopia\Database\Database $dbForProject*/
2021-07-27 13:00:36 +12:00
/** @var Appwrite\Event\Event $database */
/** @var Appwrite\Event\Event $audits */
2021-08-15 23:08:32 +12:00
/** @var Appwrite\Stats\Stats $usage */
2021-07-22 01:23:12 +12:00
2021-08-25 01:49:58 +12:00
$attribute = createAttribute ( $collectionId , new Document ([
2021-12-17 04:04:30 +13:00
'key' => $key ,
2021-07-27 13:00:36 +12:00
'type' => Database :: VAR_STRING ,
'size' => 254 ,
'required' => $required ,
'default' => $default ,
'array' => $array ,
2021-08-24 06:29:05 +12:00
'format' => APP_DATABASE_ATTRIBUTE_EMAIL ,
2021-12-28 01:45:23 +13:00
]), $response , $dbForProject , $database , $audits , $usage );
2021-08-17 11:21:00 +12:00
$response -> dynamic ( $attribute , Response :: MODEL_ATTRIBUTE_EMAIL );
2021-07-27 13:00:36 +12:00
});
2021-07-22 01:23:12 +12:00
2021-09-11 08:09:11 +12:00
App :: post ( '/v1/database/collections/:collectionId/attributes/enum' )
-> desc ( 'Create Enum Attribute' )
-> groups ([ 'api' , 'database' ])
-> label ( 'event' , 'database.attributes.create' )
-> label ( 'scope' , 'collections.write' )
-> label ( 'sdk.namespace' , 'database' )
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_KEY ])
-> label ( 'sdk.method' , 'createEnumAttribute' )
-> label ( 'sdk.description' , '/docs/references/database/create-attribute-enum.md' )
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_CREATED )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
2021-10-07 15:25:03 +13:00
-> label ( 'sdk.response.model' , Response :: MODEL_ATTRIBUTE_ENUM )
2021-12-11 01:27:11 +13:00
-> param ( 'collectionId' , '' , new UID (), 'Collection ID. You can create a new collection using the Database service [server integration](https://appwrite.io/docs/server/database#createCollection).' )
2021-12-17 04:04:30 +13:00
-> param ( 'key' , '' , new Key (), 'Attribute Key.' )
2021-09-11 08:09:11 +12:00
-> param ( 'elements' , [], new ArrayList ( new Text ( 0 )), 'Array of elements in enumerated type. Uses length of longest element to determine size.' )
-> 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-12-28 01:45:23 +13:00
-> inject ( 'dbForProject' )
2021-09-11 08:09:11 +12:00
-> inject ( 'database' )
-> inject ( 'audits' )
2021-10-07 15:25:03 +13:00
-> inject ( 'usage' )
2021-12-28 01:45:23 +13:00
-> action ( function ( $collectionId , $key , $elements , $required , $default , $array , $response , $dbForProject , $database , $audits , $usage ) {
2021-09-11 08:09:11 +12:00
/** @var Appwrite\Utopia\Response $response */
2021-12-28 01:45:23 +13:00
/** @var Utopia\Database\Database $dbForProject*/
2021-09-11 08:09:11 +12:00
/** @var Appwrite\Event\Event $database */
/** @var Appwrite\Event\Event $audits */
2021-10-07 15:25:03 +13:00
/** @var Appwrite\Stats\Stats $usage */
2021-09-11 08:09:11 +12:00
// use length of longest string as attribute size
$size = 0 ;
foreach ( $elements as $element ) {
$length = \strlen ( $element );
2021-10-08 07:30:52 +13:00
if ( $length === 0 ) {
2022-02-07 05:50:48 +13:00
throw new Exception ( 'Each enum element must not be empty' , 400 , Exception :: ATTRIBUTE_VALUE_INVALID );
2021-10-08 07:30:52 +13:00
}
2021-09-11 08:09:11 +12:00
$size = ( $length > $size ) ? $length : $size ;
}
2021-12-16 23:15:55 +13:00
if ( ! is_null ( $default ) && ! in_array ( $default , $elements )) {
2022-02-07 05:50:48 +13:00
throw new Exception ( 'Default value not found in elements' , 400 , Exception :: ATTRIBUTE_VALUE_INVALID );
2021-12-16 23:15:55 +13:00
}
2021-09-11 08:09:11 +12:00
$attribute = createAttribute ( $collectionId , new Document ([
2021-12-17 04:04:30 +13:00
'key' => $key ,
2021-09-11 08:09:11 +12:00
'type' => Database :: VAR_STRING ,
'size' => $size ,
'required' => $required ,
'default' => $default ,
'array' => $array ,
'format' => APP_DATABASE_ATTRIBUTE_ENUM ,
'formatOptions' => [ 'elements' => $elements ],
2021-12-28 01:45:23 +13:00
]), $response , $dbForProject , $database , $audits , $usage );
2021-09-11 08:09:11 +12:00
$response -> dynamic ( $attribute , Response :: MODEL_ATTRIBUTE_ENUM );
});
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' )
2021-08-14 22:16:00 +12:00
-> 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' )
2021-09-28 13:26:33 +13:00
-> label ( 'sdk.description' , '/docs/references/database/create-ip-attribute.md' )
2021-07-27 13:00:36 +12:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_CREATED )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
2021-10-01 08:37:21 +13:00
-> label ( 'sdk.response.model' , Response :: MODEL_ATTRIBUTE_IP )
2021-12-11 01:27:11 +13:00
-> param ( 'collectionId' , '' , new UID (), 'Collection ID. You can create a new collection using the Database service [server integration](https://appwrite.io/docs/server/database#createCollection).' )
2021-12-17 04:04:30 +13:00
-> param ( 'key' , '' , new Key (), 'Attribute Key.' )
2021-07-27 13:00:36 +12:00
-> param ( 'required' , null , new Boolean (), 'Is attribute required?' )
2021-08-25 01:46:41 +12:00
-> param ( 'default' , null , new IP (), 'Default value for attribute when not provided. Cannot be set when attribute is required.' , true )
2021-07-27 13:00:36 +12:00
-> param ( 'array' , false , new Boolean (), 'Is attribute an array?' , true )
-> inject ( 'response' )
2021-12-28 01:45:23 +13:00
-> inject ( 'dbForProject' )
2021-07-27 13:00:36 +12:00
-> inject ( 'database' )
-> inject ( 'audits' )
2021-08-15 23:08:32 +12:00
-> inject ( 'usage' )
2021-12-28 01:45:23 +13:00
-> action ( function ( $collectionId , $key , $required , $default , $array , $response , $dbForProject , $database , $audits , $usage ) {
2021-07-27 13:00:36 +12:00
/** @var Appwrite\Utopia\Response $response */
2021-12-28 01:45:23 +13:00
/** @var Utopia\Database\Database $dbForProject*/
2021-07-27 13:00:36 +12:00
/** @var Appwrite\Event\Event $database */
/** @var Appwrite\Event\Event $audits */
2021-08-15 23:08:32 +12:00
/** @var Appwrite\Stats\Stats $usage */
2021-06-11 01:15:00 +12:00
2021-08-25 01:49:58 +12:00
$attribute = createAttribute ( $collectionId , new Document ([
2021-12-17 04:04:30 +13:00
'key' => $key ,
2021-07-27 13:00:36 +12:00
'type' => Database :: VAR_STRING ,
'size' => 39 ,
'required' => $required ,
'default' => $default ,
'array' => $array ,
2021-08-24 06:29:05 +12:00
'format' => APP_DATABASE_ATTRIBUTE_IP ,
2021-12-28 01:45:23 +13:00
]), $response , $dbForProject , $database , $audits , $usage );
2021-08-17 11:21:00 +12:00
$response -> dynamic ( $attribute , Response :: MODEL_ATTRIBUTE_IP );
2021-07-27 13:00:36 +12:00
});
2021-06-24 02:21:32 +12:00
2021-07-27 13:00:36 +12:00
App :: post ( '/v1/database/collections/:collectionId/attributes/url' )
2021-09-23 13:29:56 +12:00
-> desc ( 'Create URL Attribute' )
2021-07-27 13:00:36 +12:00
-> groups ([ 'api' , 'database' ])
-> label ( 'event' , 'database.attributes.create' )
2021-08-14 22:16:00 +12:00
-> 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' )
2021-09-28 13:26:33 +13:00
-> label ( 'sdk.description' , '/docs/references/database/create-url-attribute.md' )
2021-07-27 13:00:36 +12:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_CREATED )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
2021-10-01 08:37:21 +13:00
-> label ( 'sdk.response.model' , Response :: MODEL_ATTRIBUTE_URL )
2021-12-11 01:27:11 +13:00
-> param ( 'collectionId' , '' , new UID (), 'Collection ID. You can create a new collection using the Database service [server integration](https://appwrite.io/docs/server/database#createCollection).' )
2021-12-17 04:04:30 +13:00
-> param ( 'key' , '' , new Key (), 'Attribute Key.' )
2021-07-27 13:00:36 +12:00
-> param ( 'required' , null , new Boolean (), 'Is attribute required?' )
2021-08-25 01:46:41 +12:00
-> param ( 'default' , null , new URL (), 'Default value for attribute when not provided. Cannot be set when attribute is required.' , true )
2021-07-27 13:00:36 +12:00
-> param ( 'array' , false , new Boolean (), 'Is attribute an array?' , true )
-> inject ( 'response' )
2021-12-28 01:45:23 +13:00
-> inject ( 'dbForProject' )
2021-07-27 13:00:36 +12:00
-> inject ( 'database' )
-> inject ( 'audits' )
2021-08-15 23:08:32 +12:00
-> inject ( 'usage' )
2021-12-28 01:45:23 +13:00
-> action ( function ( $collectionId , $key , $required , $default , $array , $response , $dbForProject , $database , $audits , $usage ) {
2021-07-27 13:00:36 +12:00
/** @var Appwrite\Utopia\Response $response */
/** @var Appwrite\Event\Event $database */
/** @var Appwrite\Event\Event $audits */
2021-08-15 23:08:32 +12:00
/** @var Appwrite\Stats\Stats $usage */
2021-06-10 09:11:51 +12:00
2021-08-25 01:49:58 +12:00
$attribute = createAttribute ( $collectionId , new Document ([
2021-12-17 04:04:30 +13:00
'key' => $key ,
2021-07-27 13:00:36 +12:00
'type' => Database :: VAR_STRING ,
2021-08-23 00:59:44 +12:00
'size' => 2000 ,
2021-06-10 09:11:51 +12:00
'required' => $required ,
2021-07-03 05:29:03 +12:00
'default' => $default ,
2021-06-10 09:11:51 +12:00
'array' => $array ,
2021-08-24 06:29:05 +12:00
'format' => APP_DATABASE_ATTRIBUTE_URL ,
2021-12-28 01:45:23 +13:00
]), $response , $dbForProject , $database , $audits , $usage );
2021-08-17 11:21:00 +12:00
$response -> dynamic ( $attribute , Response :: MODEL_ATTRIBUTE_URL );
2021-07-22 01:23:12 +12:00
});
2021-03-25 04:40:33 +13:00
2021-07-22 01:23:12 +12:00
App :: post ( '/v1/database/collections/:collectionId/attributes/integer' )
-> desc ( 'Create Integer Attribute' )
-> groups ([ 'api' , 'database' ])
-> label ( 'event' , 'database.attributes.create' )
2021-08-14 22:16:00 +12:00
-> label ( 'scope' , 'collections.write' )
2021-07-22 01:23:12 +12:00
-> label ( 'sdk.namespace' , 'database' )
2021-08-03 08:11:24 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_KEY ])
2021-07-22 01:23:12 +12:00
-> label ( 'sdk.method' , 'createIntegerAttribute' )
2021-09-28 13:26:33 +13:00
-> label ( 'sdk.description' , '/docs/references/database/create-integer-attribute.md' )
2021-07-22 01:23:12 +12:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_CREATED )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
2021-10-01 08:37:21 +13:00
-> label ( 'sdk.response.model' , Response :: MODEL_ATTRIBUTE_INTEGER )
2021-12-11 01:27:11 +13:00
-> param ( 'collectionId' , '' , new UID (), 'Collection ID. You can create a new collection using the Database service [server integration](https://appwrite.io/docs/server/database#createCollection).' )
2021-12-17 04:04:30 +13:00
-> param ( 'key' , '' , new Key (), 'Attribute Key.' )
2021-07-22 01:23:12 +12:00
-> param ( 'required' , null , new Boolean (), 'Is attribute required?' )
2021-07-24 09:38:14 +12:00
-> 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 )
2021-07-22 01:23:12 +12:00
-> 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-12-28 01:45:23 +13:00
-> inject ( 'dbForProject' )
2021-07-22 01:23:12 +12:00
-> inject ( 'database' )
-> inject ( 'audits' )
2021-08-15 23:08:32 +12:00
-> inject ( 'usage' )
2021-12-28 01:45:23 +13:00
-> action ( function ( $collectionId , $key , $required , $min , $max , $default , $array , $response , $dbForProject , $database , $audits , $usage ) {
2021-07-22 01:23:12 +12:00
/** @var Appwrite\Utopia\Response $response */
2021-12-28 01:45:23 +13:00
/** @var Utopia\Database\Database $dbForProject*/
2021-07-22 01:23:12 +12:00
/** @var Appwrite\Event\Event $database */
/** @var Appwrite\Event\Event $audits */
2021-08-15 23:08:32 +12:00
/** @var Appwrite\Stats\Stats $usage */
2021-06-19 04:13:37 +12:00
2021-08-17 11:21:00 +12:00
// Ensure attribute default is within range
2021-08-24 06:29:05 +12:00
$min = ( is_null ( $min )) ? PHP_INT_MIN : \intval ( $min );
$max = ( is_null ( $max )) ? PHP_INT_MAX : \intval ( $max );
2021-10-26 09:10:39 +13:00
if ( $min > $max ) {
2022-02-07 05:50:48 +13:00
throw new Exception ( 'Minimum value must be lesser than maximum value' , 400 , Exception :: ATTRIBUTE_VALUE_INVALID );
2021-10-26 09:10:39 +13:00
}
2021-08-17 11:21:00 +12:00
$validator = new Range ( $min , $max , Database :: VAR_INTEGER );
if ( ! is_null ( $default ) && ! $validator -> isValid ( $default )) {
2022-02-07 05:50:48 +13:00
throw new Exception ( $validator -> getDescription (), 400 , Exception :: ATTRIBUTE_VALUE_INVALID );
2021-08-17 11:21:00 +12:00
}
2021-12-30 03:50:21 +13:00
$size = $max > 2147483647 ? 8 : 4 ; // Automatically create BigInt depending on max value
2021-08-25 01:49:58 +12:00
$attribute = createAttribute ( $collectionId , new Document ([
2021-12-17 04:04:30 +13:00
'key' => $key ,
2021-07-27 13:00:36 +12:00
'type' => Database :: VAR_INTEGER ,
2021-12-30 03:50:21 +13:00
'size' => $size ,
2021-07-22 01:23:12 +12:00
'required' => $required ,
'default' => $default ,
'array' => $array ,
2021-08-24 06:29:05 +12:00
'format' => APP_DATABASE_ATTRIBUTE_INT_RANGE ,
2021-08-22 09:48:07 +12:00
'formatOptions' => [
2021-07-28 06:19:37 +12:00
'min' => $min ,
'max' => $max ,
2021-08-22 09:48:07 +12:00
],
2021-12-28 01:45:23 +13:00
]), $response , $dbForProject , $database , $audits , $usage );
2021-08-17 11:21:00 +12:00
2021-08-25 06:57:34 +12:00
$formatOptions = $attribute -> getAttribute ( 'formatOptions' , []);
if ( ! empty ( $formatOptions )) {
$attribute -> setAttribute ( 'min' , \intval ( $formatOptions [ 'min' ]));
$attribute -> setAttribute ( 'max' , \intval ( $formatOptions [ 'max' ]));
}
2021-08-17 11:21:00 +12:00
$response -> dynamic ( $attribute , Response :: MODEL_ATTRIBUTE_INTEGER );
2021-07-22 01:23:12 +12:00
});
2021-03-25 04:40:33 +13:00
2021-07-22 01:23:12 +12:00
App :: post ( '/v1/database/collections/:collectionId/attributes/float' )
-> desc ( 'Create Float Attribute' )
-> groups ([ 'api' , 'database' ])
-> label ( 'event' , 'database.attributes.create' )
2021-08-14 22:16:00 +12:00
-> label ( 'scope' , 'collections.write' )
2021-07-22 01:23:12 +12:00
-> label ( 'sdk.namespace' , 'database' )
2021-08-03 08:11:24 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_KEY ])
2021-07-22 01:23:12 +12:00
-> label ( 'sdk.method' , 'createFloatAttribute' )
2021-09-28 13:26:33 +13:00
-> label ( 'sdk.description' , '/docs/references/database/create-float-attribute.md' )
2021-07-22 01:23:12 +12:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_CREATED )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
2021-10-01 08:37:21 +13:00
-> label ( 'sdk.response.model' , Response :: MODEL_ATTRIBUTE_FLOAT )
2021-12-11 01:27:11 +13:00
-> param ( 'collectionId' , '' , new UID (), 'Collection ID. You can create a new collection using the Database service [server integration](https://appwrite.io/docs/server/database#createCollection).' )
2021-12-17 04:04:30 +13:00
-> param ( 'key' , '' , new Key (), 'Attribute Key.' )
2021-07-22 01:23:12 +12:00
-> param ( 'required' , null , new Boolean (), 'Is attribute required?' )
2021-07-24 09:38:14 +12:00
-> 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 )
2021-07-22 02:26:08 +12:00
-> param ( 'default' , null , new FloatValidator (), 'Default value for attribute when not provided. Cannot be set when attribute is required.' , true )
2021-07-22 01:23:12 +12:00
-> param ( 'array' , false , new Boolean (), 'Is attribute an array?' , true )
-> inject ( 'response' )
2021-12-28 01:45:23 +13:00
-> inject ( 'dbForProject' )
2021-07-22 01:23:12 +12:00
-> inject ( 'database' )
-> inject ( 'audits' )
2021-08-15 23:08:32 +12:00
-> inject ( 'usage' )
2021-12-28 01:45:23 +13:00
-> action ( function ( $collectionId , $key , $required , $min , $max , $default , $array , $response , $dbForProject , $database , $audits , $usage ) {
2021-07-22 01:23:12 +12:00
/** @var Appwrite\Utopia\Response $response */
2021-12-28 01:45:23 +13:00
/** @var Utopia\Database\Database $dbForProject*/
2021-07-22 01:23:12 +12:00
/** @var Appwrite\Event\Event $database */
/** @var Appwrite\Event\Event $audits */
2021-08-15 23:08:32 +12:00
/** @var Appwrite\Stats\Stats $usage */
2021-07-22 01:23:12 +12:00
2021-08-17 11:21:00 +12:00
// Ensure attribute default is within range
2021-11-02 13:48:14 +13:00
$min = ( is_null ( $min )) ? - PHP_FLOAT_MAX : \floatval ( $min );
2021-08-24 06:29:05 +12:00
$max = ( is_null ( $max )) ? PHP_FLOAT_MAX : \floatval ( $max );
2021-10-26 09:10:39 +13:00
if ( $min > $max ) {
2022-02-07 05:50:48 +13:00
throw new Exception ( 'Minimum value must be lesser than maximum value' , 400 , Exception :: ATTRIBUTE_VALUE_INVALID );
2021-10-26 09:10:39 +13:00
}
2021-12-15 10:01:58 +13:00
// Ensure default value is a float
if ( ! is_null ( $default )) {
$default = \floatval ( $default );
}
2021-10-26 09:10:39 +13:00
2021-08-17 11:21:00 +12:00
$validator = new Range ( $min , $max , Database :: VAR_FLOAT );
if ( ! is_null ( $default ) && ! $validator -> isValid ( $default )) {
2022-02-07 05:50:48 +13:00
throw new Exception ( $validator -> getDescription (), 400 , Exception :: ATTRIBUTE_VALUE_INVALID );
2021-08-17 11:21:00 +12:00
}
2021-08-25 01:49:58 +12:00
$attribute = createAttribute ( $collectionId , new Document ([
2021-12-17 04:04:30 +13:00
'key' => $key ,
2021-07-27 13:00:36 +12:00
'type' => Database :: VAR_FLOAT ,
2021-07-22 01:23:12 +12:00
'required' => $required ,
2021-07-27 13:00:36 +12:00
'size' => 0 ,
2021-07-22 01:23:12 +12:00
'default' => $default ,
2021-07-27 13:00:36 +12:00
'array' => $array ,
2021-08-24 06:29:05 +12:00
'format' => APP_DATABASE_ATTRIBUTE_FLOAT_RANGE ,
2021-08-22 09:48:07 +12:00
'formatOptions' => [
2021-07-28 06:19:37 +12:00
'min' => $min ,
'max' => $max ,
2021-08-22 09:48:07 +12:00
],
2021-12-28 01:45:23 +13:00
]), $response , $dbForProject , $database , $audits , $usage );
2021-08-17 11:21:00 +12:00
2021-08-25 06:57:34 +12:00
$formatOptions = $attribute -> getAttribute ( 'formatOptions' , []);
if ( ! empty ( $formatOptions )) {
$attribute -> setAttribute ( 'min' , \floatval ( $formatOptions [ 'min' ]));
$attribute -> setAttribute ( 'max' , \floatval ( $formatOptions [ 'max' ]));
}
2021-08-17 11:21:00 +12:00
$response -> dynamic ( $attribute , Response :: MODEL_ATTRIBUTE_FLOAT );
2021-07-22 01:23:12 +12:00
});
App :: post ( '/v1/database/collections/:collectionId/attributes/boolean' )
-> desc ( 'Create Boolean Attribute' )
-> groups ([ 'api' , 'database' ])
-> label ( 'event' , 'database.attributes.create' )
2021-08-14 22:16:00 +12:00
-> label ( 'scope' , 'collections.write' )
2021-07-22 01:23:12 +12:00
-> label ( 'sdk.namespace' , 'database' )
2021-08-03 08:11:24 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_KEY ])
2021-07-22 01:23:12 +12:00
-> label ( 'sdk.method' , 'createBooleanAttribute' )
2021-09-28 13:26:33 +13:00
-> label ( 'sdk.description' , '/docs/references/database/create-boolean-attribute.md' )
2021-07-22 01:23:12 +12:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_CREATED )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
2021-10-01 08:37:21 +13:00
-> label ( 'sdk.response.model' , Response :: MODEL_ATTRIBUTE_BOOLEAN )
2021-12-11 01:27:11 +13:00
-> param ( 'collectionId' , '' , new UID (), 'Collection ID. You can create a new collection using the Database service [server integration](https://appwrite.io/docs/server/database#createCollection).' )
2021-12-17 04:04:30 +13:00
-> param ( 'key' , '' , new Key (), 'Attribute Key.' )
2021-07-22 01:23:12 +12:00
-> 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-12-28 01:45:23 +13:00
-> inject ( 'dbForProject' )
2021-07-22 01:23:12 +12:00
-> inject ( 'database' )
-> inject ( 'audits' )
2021-08-15 23:08:32 +12:00
-> inject ( 'usage' )
2021-12-28 01:45:23 +13:00
-> action ( function ( $collectionId , $key , $required , $default , $array , $response , $dbForProject , $database , $audits , $usage ) {
2021-07-22 01:23:12 +12:00
/** @var Appwrite\Utopia\Response $response */
2021-12-28 01:45:23 +13:00
/** @var Utopia\Database\Database $dbForProject*/
2021-07-22 01:23:12 +12:00
/** @var Appwrite\Event\Event $database */
/** @var Appwrite\Event\Event $audits */
2021-08-15 23:08:32 +12:00
/** @var Appwrite\Stats\Stats $usage */
2021-07-22 01:23:12 +12:00
2021-08-25 01:49:58 +12:00
$attribute = createAttribute ( $collectionId , new Document ([
2021-12-17 04:04:30 +13:00
'key' => $key ,
2021-07-27 13:00:36 +12:00
'type' => Database :: VAR_BOOLEAN ,
'size' => 0 ,
2021-06-10 09:11:51 +12:00
'required' => $required ,
2021-07-03 05:29:03 +12:00
'default' => $default ,
2021-06-10 09:11:51 +12:00
'array' => $array ,
2021-12-28 01:45:23 +13:00
]), $response , $dbForProject , $database , $audits , $usage );
2021-08-17 11:21:00 +12:00
$response -> dynamic ( $attribute , Response :: MODEL_ATTRIBUTE_BOOLEAN );
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' ])
2021-08-14 22:16:00 +12:00
-> 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 )
2021-12-11 01:27:11 +13:00
-> param ( 'collectionId' , '' , new UID (), 'Collection ID. You can create a new collection using the Database service [server integration](https://appwrite.io/docs/server/database#createCollection).' )
2021-03-26 08:52:57 +13:00
-> inject ( 'response' )
2021-12-28 01:45:23 +13:00
-> inject ( 'dbForProject' )
2021-08-15 23:08:32 +12:00
-> inject ( 'usage' )
2021-12-28 01:45:23 +13:00
-> action ( function ( $collectionId , $response , $dbForProject , $usage ) {
2021-03-26 08:52:57 +13:00
/** @var Appwrite\Utopia\Response $response */
2021-12-28 01:45:23 +13:00
/** @var Utopia\Database\Database $dbForProject */
2021-06-09 08:12:14 +12:00
2021-12-28 01:45:23 +13:00
$collection = $dbForProject -> getDocument ( 'collections' , $collectionId );
2021-03-26 08:52:57 +13:00
2021-06-12 02:25:52 +12:00
if ( $collection -> isEmpty ()) {
2022-02-07 05:50:48 +13:00
throw new Exception ( 'Collection not found' , 404 , Exception :: COLLECTION_NOT_FOUND );
2021-06-11 01:15:00 +12:00
}
2021-08-28 12:32:44 +12:00
$attributes = $collection -> getAttribute ( 'attributes' );
2021-06-18 03:53:06 +12:00
2021-08-15 23:08:32 +12:00
$usage -> setParam ( 'database.collections.read' , 1 );
2021-07-26 02:47:18 +12:00
$response -> dynamic ( new Document ([
2022-02-27 22:57:09 +13:00
'total' => \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-12-17 04:04:30 +13:00
App :: get ( '/v1/database/collections/:collectionId/attributes/:key' )
2021-03-26 08:52:57 +13:00
-> desc ( 'Get Attribute' )
-> groups ([ 'api' , 'database' ])
2021-08-14 22:16:00 +12:00
-> 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 )
2021-10-01 08:03:18 +13:00
-> label ( 'sdk.response.model' , [
Response :: MODEL_ATTRIBUTE_BOOLEAN ,
Response :: MODEL_ATTRIBUTE_INTEGER ,
Response :: MODEL_ATTRIBUTE_FLOAT ,
Response :: MODEL_ATTRIBUTE_EMAIL ,
2021-10-07 15:25:03 +13:00
Response :: MODEL_ATTRIBUTE_ENUM ,
2021-10-01 08:03:18 +13:00
Response :: MODEL_ATTRIBUTE_URL ,
Response :: MODEL_ATTRIBUTE_IP ,
Response :: MODEL_ATTRIBUTE_STRING ,]) // needs to be last, since its condition would dominate any other string attribute
2021-12-11 01:27:11 +13:00
-> param ( 'collectionId' , '' , new UID (), 'Collection ID. You can create a new collection using the Database service [server integration](https://appwrite.io/docs/server/database#createCollection).' )
2021-12-17 04:04:30 +13:00
-> param ( 'key' , '' , new Key (), 'Attribute Key.' )
2021-03-26 08:52:57 +13:00
-> inject ( 'response' )
2021-12-28 01:45:23 +13:00
-> inject ( 'dbForProject' )
2021-08-15 23:08:32 +12:00
-> inject ( 'usage' )
2021-12-28 01:45:23 +13:00
-> action ( function ( $collectionId , $key , $response , $dbForProject , $usage ) {
2021-03-26 08:52:57 +13:00
/** @var Appwrite\Utopia\Response $response */
2021-12-28 01:45:23 +13:00
/** @var Utopia\Database\Database $dbForProject */
2021-06-09 08:12:14 +12:00
2021-12-28 01:45:23 +13:00
$collection = $dbForProject -> getDocument ( 'collections' , $collectionId );
2021-03-26 08:52:57 +13:00
2021-12-28 06:07:35 +13:00
if ( $collection -> isEmpty ()) {
2022-02-07 05:50:48 +13:00
throw new Exception ( 'Collection not found' , 404 , Exception :: COLLECTION_NOT_FOUND );
2021-06-11 01:15:00 +12:00
}
2021-12-28 06:07:35 +13:00
$attribute = $dbForProject -> getDocument ( 'attributes' , $collectionId . '_' . $key );
2021-08-26 07:37:07 +12:00
2021-12-28 06:07:35 +13:00
if ( $attribute -> isEmpty ()) {
2022-02-07 05:50:48 +13:00
throw new Exception ( 'Attribute not found' , 404 , Exception :: ATTRIBUTE_NOT_FOUND );
2021-06-10 09:11:51 +12:00
}
2021-08-26 07:37:07 +12:00
// Select response model based on type and format
2021-08-17 11:21:00 +12:00
$type = $attribute -> getAttribute ( 'type' );
2021-08-25 06:01:53 +12:00
$format = $attribute -> getAttribute ( 'format' );
2021-08-17 11:21:00 +12:00
$model = match ( $type ) {
Database :: VAR_BOOLEAN => Response :: MODEL_ATTRIBUTE_BOOLEAN ,
Database :: VAR_INTEGER => Response :: MODEL_ATTRIBUTE_INTEGER ,
Database :: VAR_FLOAT => Response :: MODEL_ATTRIBUTE_FLOAT ,
2021-08-25 06:01:53 +12:00
Database :: VAR_STRING => match ( $format ) {
2021-08-26 07:37:07 +12:00
APP_DATABASE_ATTRIBUTE_EMAIL => Response :: MODEL_ATTRIBUTE_EMAIL ,
2021-09-11 08:14:12 +12:00
APP_DATABASE_ATTRIBUTE_ENUM => Response :: MODEL_ATTRIBUTE_ENUM ,
2021-08-17 11:21:00 +12:00
APP_DATABASE_ATTRIBUTE_IP => Response :: MODEL_ATTRIBUTE_IP ,
APP_DATABASE_ATTRIBUTE_URL => Response :: MODEL_ATTRIBUTE_URL ,
default => Response :: MODEL_ATTRIBUTE_STRING ,
},
default => Response :: MODEL_ATTRIBUTE ,
};
2021-08-25 06:01:53 +12:00
2021-08-15 23:08:32 +12:00
$usage -> setParam ( 'database.collections.read' , 1 );
2021-09-13 00:57:02 +12:00
2021-08-17 11:21:00 +12:00
$response -> dynamic ( $attribute , $model );
2021-03-26 08:52:57 +13:00
});
2021-12-17 04:04:30 +13:00
App :: delete ( '/v1/database/collections/:collectionId/attributes/:key' )
2021-03-25 04:40:33 +13:00
-> desc ( 'Delete Attribute' )
-> groups ([ 'api' , 'database' ])
2021-08-14 22:16:00 +12:00
-> 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 )
2021-12-11 01:27:11 +13:00
-> param ( 'collectionId' , '' , new UID (), 'Collection ID. You can create a new collection using the Database service [server integration](https://appwrite.io/docs/server/database#createCollection).' )
2021-12-17 04:04:30 +13:00
-> param ( 'key' , '' , new Key (), 'Attribute Key.' )
2021-03-25 04:40:33 +13:00
-> inject ( 'response' )
2021-12-28 01:45:23 +13:00
-> inject ( 'dbForProject' )
2021-06-19 06:27:14 +12:00
-> inject ( 'database' )
2021-03-25 04:40:33 +13:00
-> inject ( 'events' )
-> inject ( 'audits' )
2021-08-15 23:08:32 +12:00
-> inject ( 'usage' )
2021-12-28 01:45:23 +13:00
-> action ( function ( $collectionId , $key , $response , $dbForProject , $database , $events , $audits , $usage ) {
2021-03-25 04:40:33 +13:00
/** @var Appwrite\Utopia\Response $response */
2021-12-28 01:45:23 +13:00
/** @var Utopia\Database\Database $dbForProject */
2021-06-19 06:27:14 +12:00
/** @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-15 23:08:32 +12:00
/** @var Appwrite\Stats\Stats $usage */
2021-03-25 04:40:33 +13:00
2021-12-28 01:45:23 +13:00
$collection = $dbForProject -> getDocument ( 'collections' , $collectionId );
2021-03-25 04:40:33 +13:00
2021-06-12 02:25:52 +12:00
if ( $collection -> isEmpty ()) {
2022-02-07 05:50:48 +13:00
throw new Exception ( 'Collection not found' , 404 , Exception :: COLLECTION_NOT_FOUND );
2021-06-11 01:15:00 +12:00
}
2021-12-28 01:45:23 +13:00
$attribute = $dbForProject -> getDocument ( 'attributes' , $collectionId . '_' . $key );
2021-08-09 10:07:59 +12:00
2021-12-28 06:07:35 +13:00
if ( $attribute -> isEmpty ()) {
2022-02-07 05:50:48 +13:00
throw new Exception ( 'Attribute not found' , 404 , Exception :: ATTRIBUTE_NOT_FOUND );
2021-06-10 09:11:51 +12:00
}
2021-10-05 13:23:15 +13:00
// Only update status if removing available attribute
if ( $attribute -> getAttribute ( 'status' === 'available' )) {
2021-12-28 01:45:23 +13:00
$attribute = $dbForProject -> updateDocument ( 'attributes' , $attribute -> getId (), $attribute -> setAttribute ( 'status' , 'deleting' ));
2021-10-05 13:23:15 +13:00
}
2021-12-28 01:45:23 +13:00
$dbForProject -> deleteCachedDocument ( 'collections' , $collectionId );
2022-02-08 01:25:15 +13:00
$dbForProject -> deleteCachedCollection ( 'collection_' . $collection -> getInternalId ());
2021-08-21 05:02:44 +12:00
2021-06-19 06:27:14 +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 )
2021-06-19 06:27:14 +12:00
-> setParam ( 'document' , $attribute )
;
2021-06-09 08:12:14 +12:00
2021-08-15 23:08:32 +12:00
$usage -> setParam ( 'database.collections.update' , 1 );
2021-10-26 14:12:27 +13:00
// Select response model based on type and format
$type = $attribute -> getAttribute ( 'type' );
$format = $attribute -> getAttribute ( 'format' );
$model = match ( $type ) {
Database :: VAR_BOOLEAN => Response :: MODEL_ATTRIBUTE_BOOLEAN ,
Database :: VAR_INTEGER => Response :: MODEL_ATTRIBUTE_INTEGER ,
Database :: VAR_FLOAT => Response :: MODEL_ATTRIBUTE_FLOAT ,
Database :: VAR_STRING => match ( $format ) {
APP_DATABASE_ATTRIBUTE_EMAIL => Response :: MODEL_ATTRIBUTE_EMAIL ,
APP_DATABASE_ATTRIBUTE_ENUM => Response :: MODEL_ATTRIBUTE_ENUM ,
APP_DATABASE_ATTRIBUTE_IP => Response :: MODEL_ATTRIBUTE_IP ,
APP_DATABASE_ATTRIBUTE_URL => Response :: MODEL_ATTRIBUTE_URL ,
default => Response :: MODEL_ATTRIBUTE_STRING ,
},
default => Response :: MODEL_ATTRIBUTE ,
};
2021-06-10 09:11:51 +12:00
$events
2021-10-26 14:12:27 +13:00
-> setParam ( 'payload' , $response -> output ( $attribute , $model ))
2021-06-10 09:11:51 +12:00
;
2021-03-25 04:40:33 +13:00
$audits
-> setParam ( 'event' , 'database.attributes.delete' )
2021-12-29 04:48:36 +13:00
-> setParam ( 'resource' , 'collection/' . $collectionId )
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' )
2021-08-14 22:16:00 +12:00
-> 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 )
2021-12-11 01:27:11 +13:00
-> param ( 'collectionId' , '' , new UID (), 'Collection ID. You can create a new collection using the Database service [server integration](https://appwrite.io/docs/server/database#createCollection).' )
2021-12-17 04:04:30 +13:00
-> param ( 'key' , null , new Key (), 'Index Key.' )
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 )
2020-12-27 04:05:04 +13:00
-> inject ( 'response' )
2021-12-28 01:45:23 +13:00
-> inject ( 'dbForProject' )
2021-06-19 04:13:37 +12:00
-> inject ( 'database' )
2020-12-27 04:05:04 +13:00
-> inject ( 'audits' )
2021-08-15 23:08:32 +12:00
-> inject ( 'usage' )
2021-12-28 01:45:23 +13:00
-> action ( function ( $collectionId , $key , $type , $attributes , $orders , $response , $dbForProject , $database , $audits , $usage ) {
2020-10-30 02:50:49 +13:00
/** @var Appwrite\Utopia\Response $response */
2021-12-28 01:45:23 +13:00
/** @var Utopia\Database\Database $dbForProject */
2021-07-03 10:22:49 +12:00
/** @var Appwrite\Event\Event $database */
2020-07-06 02:19:59 +12:00
/** @var Appwrite\Event\Event $audits */
2021-08-15 23:08:32 +12:00
/** @var Appwrite\Stats\Stats $audits */
2019-10-01 17:57:41 +13:00
2021-12-28 01:45:23 +13:00
$collection = $dbForProject -> getDocument ( 'collections' , $collectionId );
2019-05-09 18:54:39 +12:00
2021-06-12 02:25:52 +12:00
if ( $collection -> isEmpty ()) {
2022-02-07 05:50:48 +13:00
throw new Exception ( 'Collection not found' , 404 , Exception :: COLLECTION_NOT_FOUND );
2020-06-30 09:43:34 +12:00
}
2020-03-09 04:38:51 +13:00
2021-12-28 01:45:23 +13:00
$count = $dbForProject -> count ( 'indexes' , [
2021-08-25 11:35:32 +12:00
new Query ( 'collectionId' , Query :: TYPE_EQUAL , [ $collectionId ])
], 61 );
$limit = 64 - MariaDB :: getNumberOfDefaultIndexes ();
if ( $count >= $limit ) {
2022-02-07 05:50:48 +13:00
throw new Exception ( 'Index limit exceeded' , 400 , Exception :: INDEX_LIMIT_EXCEEDED );
2021-08-25 11:35:32 +12:00
}
2021-07-03 12:20:09 +12:00
// Convert Document[] to array of attribute metadata
2021-12-28 01:45:23 +13:00
$oldAttributes = \array_map ( fn ( $a ) => $a -> getArrayCopy (), $collection -> getAttribute ( 'attributes' ));
2021-07-03 10:23:58 +12:00
2021-06-24 02:21:32 +12:00
// lengths hidden by default
$lengths = [];
2021-12-17 04:04:30 +13:00
foreach ( $attributes as $i => $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 ) {
2022-02-07 05:50:48 +13:00
throw new Exception ( 'Unknown attribute: ' . $attribute , 400 , Exception :: ATTRIBUTE_UNKNOWN );
2021-07-03 10:23:58 +12:00
}
2021-10-05 13:43:27 +13:00
$attributeStatus = $oldAttributes [ $attributeIndex ][ 'status' ];
2021-07-03 12:20:09 +12:00
$attributeType = $oldAttributes [ $attributeIndex ][ 'type' ];
$attributeSize = $oldAttributes [ $attributeIndex ][ 'size' ];
2021-07-03 10:23:58 +12:00
2021-10-05 13:43:27 +13:00
// ensure attribute is available
if ( $attributeStatus !== 'available' ) {
2022-02-07 05:50:48 +13:00
throw new Exception ( 'Attribute not available: ' . $oldAttributes [ $attributeIndex ][ 'key' ], 400 , Exception :: ATTRIBUTE_NOT_AVAILABLE );
2021-10-05 13:43:27 +13:00
}
// set attribute size as index length only for strings
2021-12-17 04:04:30 +13:00
$lengths [ $i ] = ( $attributeType === Database :: VAR_STRING ) ? $attributeSize : null ;
2020-06-30 09:43:34 +12:00
}
2019-05-09 18:54:39 +12:00
2021-08-23 03:00:00 +12:00
try {
2021-12-28 01:45:23 +13:00
$index = $dbForProject -> createDocument ( 'indexes' , new Document ([
2021-12-17 04:04:30 +13:00
'$id' => $collectionId . '_' . $key ,
'key' => $key ,
2021-10-05 13:23:15 +13:00
'status' => 'processing' , // processing, available, failed, deleting, stuck
2021-08-23 03:00:00 +12:00
'collectionId' => $collectionId ,
'type' => $type ,
'attributes' => $attributes ,
'lengths' => $lengths ,
'orders' => $orders ,
]));
} catch ( DuplicateException $th ) {
2022-02-07 05:50:48 +13:00
throw new Exception ( 'Index already exists' , 409 , Exception :: INDEX_ALREADY_EXISTS );
2020-06-30 09:43:34 +12:00
}
2019-05-09 18:54:39 +12:00
2021-12-28 01:45:23 +13:00
$dbForProject -> deleteCachedDocument ( 'collections' , $collectionId );
2020-06-30 09:43:34 +12:00
2021-06-19 04:13:37 +12: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 )
2021-06-19 04:13:37 +12:00
-> setParam ( 'document' , $index )
2020-10-31 21:42:41 +13:00
;
2019-05-09 18:54:39 +12:00
2021-08-15 23:08:32 +12:00
$usage -> setParam ( 'database.collections.update' , 1 );
2020-06-30 09:43:34 +12:00
2021-03-24 10:19:19 +13:00
$audits
-> setParam ( 'event' , 'database.indexes.create' )
2021-12-29 04:48:36 +13:00
-> setParam ( 'resource' , 'collection/' . $collectionId )
2021-06-10 09:11:51 +12:00
-> setParam ( 'data' , $index -> getArrayCopy ())
2021-03-24 10:19:19 +13:00
;
2021-06-10 09:11:51 +12:00
$response -> setStatusCode ( Response :: STATUS_CODE_CREATED );
2021-07-26 02:47:18 +12:00
$response -> dynamic ( $index , Response :: MODEL_INDEX );
2020-12-27 04:05:04 +13:00
});
2020-02-01 11:34:07 +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' )
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-03-25 04:40:33 +13:00
-> label ( 'sdk.namespace' , 'database' )
-> label ( 'sdk.method' , 'listIndexes' )
-> label ( 'sdk.description' , '/docs/references/database/list-indexes.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 )
2021-03-25 04:40:33 +13:00
-> label ( 'sdk.response.model' , Response :: MODEL_INDEX_LIST )
2021-12-11 01:27:11 +13:00
-> param ( 'collectionId' , '' , new UID (), 'Collection ID. You can create a new collection using the Database service [server integration](https://appwrite.io/docs/server/database#createCollection).' )
2020-12-27 04:05:04 +13:00
-> inject ( 'response' )
2021-12-28 01:45:23 +13:00
-> inject ( 'dbForProject' )
2021-08-15 23:08:32 +12:00
-> inject ( 'usage' )
2021-12-28 01:45:23 +13:00
-> action ( function ( $collectionId , $response , $dbForProject , $usage ) {
2020-10-30 02:50:49 +13:00
/** @var Appwrite\Utopia\Response $response */
2021-12-28 01:45:23 +13:00
/** @var Utopia\Database\Database $dbForProject */
2021-06-10 09:11:51 +12:00
2021-12-28 01:45:23 +13:00
$collection = $dbForProject -> getDocument ( 'collections' , $collectionId );
2020-06-30 09:43:34 +12:00
2021-06-12 02:25:52 +12:00
if ( $collection -> isEmpty ()) {
2022-02-07 05:50:48 +13:00
throw new Exception ( 'Collection not found' , 404 , Exception :: COLLECTION_NOT_FOUND );
2020-02-01 11:34:07 +13:00
}
2020-06-30 09:43:34 +12:00
2021-06-10 09:11:51 +12:00
$indexes = $collection -> getAttribute ( 'indexes' );
2021-03-25 04:40:33 +13:00
2021-08-15 23:08:32 +12:00
$usage -> setParam ( 'database.collections.read' , 1 );
2021-07-26 02:47:18 +12:00
$response -> dynamic ( new Document ([
2022-02-27 22:57:09 +13:00
'total' => \count ( $indexes ),
2021-12-14 13:42:54 +13:00
'indexes' => $indexes ,
2021-06-10 09:11:51 +12:00
]), Response :: MODEL_INDEX_LIST );
2020-12-27 04:05:04 +13:00
});
2020-02-01 11:34:07 +13:00
2021-12-17 04:04:30 +13:00
App :: get ( '/v1/database/collections/:collectionId/indexes/:key' )
2021-03-26 08:52:57 +13:00
-> desc ( 'Get Index' )
2020-06-26 06:32:12 +12:00
-> groups ([ 'api' , 'database' ])
2021-08-14 22:16:00 +12:00
-> label ( 'scope' , 'collections.read' )
2021-04-16 19:22:17 +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' )
2020-11-12 10:02:24 +13:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
2021-03-26 08:52:57 +13:00
-> label ( 'sdk.response.model' , Response :: MODEL_INDEX )
2021-12-11 01:27:11 +13:00
-> param ( 'collectionId' , '' , new UID (), 'Collection ID. You can create a new collection using the Database service [server integration](https://appwrite.io/docs/server/database#createCollection).' )
2021-12-17 04:04:30 +13:00
-> param ( 'key' , null , new Key (), 'Index Key.' )
2020-12-27 04:05:04 +13:00
-> inject ( 'response' )
2021-12-28 01:45:23 +13:00
-> inject ( 'dbForProject' )
2021-08-15 23:08:32 +12:00
-> inject ( 'usage' )
2021-12-28 01:45:23 +13:00
-> action ( function ( $collectionId , $key , $response , $dbForProject , $usage ) {
2020-10-30 02:50:49 +13:00
/** @var Appwrite\Utopia\Response $response */
2021-12-28 01:45:23 +13:00
/** @var Utopia\Database\Database $dbForProject */
2019-05-09 18:54:39 +12:00
2021-12-28 01:45:23 +13:00
$collection = $dbForProject -> getDocument ( 'collections' , $collectionId );
2019-10-01 17:57:41 +13:00
2021-06-12 02:25:52 +12:00
if ( $collection -> isEmpty ()) {
2022-02-07 05:50:48 +13:00
throw new Exception ( 'Collection not found' , 404 , Exception :: COLLECTION_NOT_FOUND );
2020-06-30 09:43:34 +12:00
}
2019-10-01 17:57:41 +13:00
2021-06-10 09:11:51 +12:00
$indexes = $collection -> getAttribute ( 'indexes' );
2019-05-09 18:54:39 +12:00
2021-08-17 07:24:15 +12:00
// Search for index
2021-12-17 04:04:30 +13:00
$indexIndex = array_search ( $key , array_column ( $indexes , 'key' ));
2019-05-09 18:54:39 +12:00
2021-06-10 09:11:51 +12:00
if ( $indexIndex === false ) {
2022-02-07 05:50:48 +13:00
throw new Exception ( 'Index not found' , 404 , Exception :: INDEX_NOT_FOUND );
2020-06-30 09:43:34 +12:00
}
2019-05-09 18:54:39 +12:00
2021-06-18 05:03:24 +12:00
$index = new Document ([ \array_merge ( $indexes [ $indexIndex ], [
2021-06-10 09:11:51 +12:00
'collectionId' => $collectionId ,
])]);
2020-01-05 04:45:28 +13:00
2021-08-15 23:08:32 +12:00
$usage -> setParam ( 'database.collections.read' , 1 );
2020-06-30 09:43:34 +12:00
2021-07-26 02:47:18 +12:00
$response -> dynamic ( $index , Response :: MODEL_INDEX );
2020-12-27 04:05:04 +13:00
});
2019-05-09 18:54:39 +12:00
2021-12-17 04:04:30 +13:00
App :: delete ( '/v1/database/collections/:collectionId/indexes/:key' )
2021-03-26 08:52:57 +13:00
-> desc ( 'Delete Index' )
2020-06-26 06:32:12 +12:00
-> groups ([ 'api' , 'database' ])
2019-06-09 01:13:19 +12:00
-> label ( 'scope' , 'collections.write' )
2021-03-26 08:52:57 +13:00
-> label ( 'event' , 'database.indexes.delete' )
2021-04-16 19:22:17 +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' )
2020-11-12 10:02:24 +13:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_NOCONTENT )
-> label ( 'sdk.response.model' , Response :: MODEL_NONE )
2021-12-11 01:27:11 +13:00
-> param ( 'collectionId' , null , new UID (), 'Collection ID. You can create a new collection using the Database service [server integration](https://appwrite.io/docs/server/database#createCollection).' )
2021-12-17 04:04:30 +13:00
-> param ( 'key' , '' , new Key (), 'Index Key.' )
2020-12-27 04:05:04 +13:00
-> inject ( 'response' )
2021-12-28 01:45:23 +13:00
-> inject ( 'dbForProject' )
2021-06-19 06:27:14 +12:00
-> inject ( 'database' )
2020-12-27 04:05:04 +13:00
-> inject ( 'events' )
-> inject ( 'audits' )
2021-08-15 23:08:32 +12:00
-> inject ( 'usage' )
2021-12-28 01:45:23 +13:00
-> action ( function ( $collectionId , $key , $response , $dbForProject , $database , $events , $audits , $usage ) {
2020-10-30 02:50:49 +13:00
/** @var Appwrite\Utopia\Response $response */
2021-12-28 01:45:23 +13:00
/** @var Utopia\Database\Database $dbForProject */
2021-06-19 06:27:14 +12:00
/** @var Appwrite\Event\Event $database */
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 */
2021-08-15 23:08:32 +12:00
/** @var Appwrite\Stats\Stats $usage */
2019-05-09 18:54:39 +12:00
2021-12-28 01:45:23 +13:00
$collection = $dbForProject -> getDocument ( 'collections' , $collectionId );
2019-05-09 18:54:39 +12:00
2021-06-12 06:07:05 +12:00
if ( $collection -> isEmpty ()) {
2022-02-07 05:50:48 +13:00
throw new Exception ( 'Collection not found' , 404 , Exception :: COLLECTION_NOT_FOUND );
2020-06-30 09:43:34 +12:00
}
2020-01-05 04:45:28 +13:00
2021-12-28 01:45:23 +13:00
$index = $dbForProject -> getDocument ( 'indexes' , $collectionId . '_' . $key );
2021-06-10 09:11:51 +12:00
2021-08-23 16:06:53 +12:00
if ( empty ( $index -> getId ())) {
2022-02-07 05:50:48 +13:00
throw new Exception ( 'Index not found' , 404 , Exception :: INDEX_NOT_FOUND );
2020-06-30 09:43:34 +12:00
}
2020-12-05 09:47:02 +13:00
2021-10-05 13:23:15 +13:00
// Only update status if removing available index
if ( $index -> getAttribute ( 'status' ) === 'available' ) {
2021-12-28 01:45:23 +13:00
$index = $dbForProject -> updateDocument ( 'indexes' , $index -> getId (), $index -> setAttribute ( 'status' , 'deleting' ));
2021-10-05 13:23:15 +13:00
}
2021-12-28 01:45:23 +13:00
$dbForProject -> deleteCachedDocument ( 'collections' , $collectionId );
2021-08-23 16:06:53 +12:00
2021-06-19 06:27:14 +12:00
$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 )
2021-06-19 06:27:14 +12:00
-> setParam ( 'document' , $index )
2020-12-05 09:47:02 +13:00
;
2021-08-15 23:08:32 +12:00
$usage -> setParam ( 'database.collections.update' , 1 );
2020-12-07 11:14:57 +13:00
$events
2021-07-26 02:47:18 +12:00
-> setParam ( 'payload' , $response -> output ( $index , Response :: MODEL_INDEX ))
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
2021-03-26 08:52:57 +13:00
-> setParam ( 'event' , 'database.indexes.delete' )
2021-12-29 04:48:36 +13:00
-> setParam ( 'resource' , 'collection/' . $collectionId )
2021-03-26 08:52:57 +13:00
-> setParam ( 'data' , $index -> getArrayCopy ())
2020-06-30 09:43:34 +12:00
;
2021-03-19 09:32:16 +13:00
$response -> dynamic ( new Document (), Response :: MODEL_NONE );
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 :: 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 )
2021-04-08 20:39:23 +12:00
-> label ( 'sdk.response.model' , Response :: MODEL_DOCUMENT )
2022-01-13 11:05:25 +13:00
-> param ( 'documentId' , '' , new CustomId (), 'Document 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.' )
2022-01-05 05:06:16 +13:00
-> param ( 'collectionId' , null , new UID (), 'Collection ID. You can create a new collection using the Database service [server integration](https://appwrite.io/docs/server/database#createCollection). Make sure to define attributes before creating documents.' )
2020-09-11 02:40:14 +12:00
-> param ( 'data' , [], new JSON (), 'Document data as JSON object.' )
2021-12-11 01:27:11 +13: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](https://appwrite.io/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](https://appwrite.io/docs/permissions) and get a full list of available permissions.' , true )
2020-12-27 04:05:04 +13:00
-> inject ( 'response' )
2021-12-28 01:45:23 +13:00
-> inject ( 'dbForProject' )
2021-03-22 11:17:20 +13:00
-> inject ( 'user' )
2020-12-27 04:05:04 +13:00
-> inject ( 'audits' )
2021-08-15 23:08:32 +12:00
-> inject ( 'usage' )
2021-12-17 07:12:06 +13:00
-> inject ( 'events' )
2021-12-14 01:42:04 +13:00
-> inject ( 'mode' )
2021-12-28 01:45:23 +13:00
-> action ( function ( $documentId , $collectionId , $data , $read , $write , $response , $dbForProject , $user , $audits , $usage , $events , $mode ) {
2020-10-30 02:50:49 +13:00
/** @var Appwrite\Utopia\Response $response */
2021-12-28 01:45:23 +13:00
/** @var Utopia\Database\Database $dbForProject */
2021-06-11 06:19:42 +12:00
/** @var Utopia\Database\Document $user */
2020-07-06 02:19:59 +12:00
/** @var Appwrite\Event\Event $audits */
2021-08-15 23:08:32 +12:00
/** @var Appwrite\Stats\Stats $usage */
2021-12-17 07:12:06 +13:00
/** @var Appwrite\Event\Event $events */
2021-12-14 01:42:04 +13:00
/** @var string $mode */
2021-10-05 23:30:33 +13:00
2020-06-30 09:43:34 +12:00
$data = ( \is_string ( $data )) ? \json_decode ( $data , true ) : $data ; // Cast to JSON array
if ( empty ( $data )) {
2022-02-07 05:50:48 +13:00
throw new Exception ( 'Missing payload' , 400 , Exception :: DOCUMENT_MISSING_PAYLOAD );
2020-06-30 09:43:34 +12:00
}
2020-02-01 11:34:07 +13:00
2020-06-30 09:43:34 +12:00
if ( isset ( $data [ '$id' ])) {
2022-02-07 05:50:48 +13:00
throw new Exception ( '$id is not allowed for creating new documents, try update instead' , 400 , Exception :: DOCUMENT_INVALID_STRUCTURE );
2020-06-30 09:43:34 +12:00
}
2020-02-01 11:34:07 +13:00
2021-12-17 07:12:06 +13:00
/**
* Skip Authorization to get the collection . Needed in case of empty permissions for document level permissions .
2021-12-27 23:45:24 +13:00
*
* @ var Document $collection
2021-12-17 07:12:06 +13:00
*/
2021-12-28 01:45:23 +13:00
$collection = Authorization :: skip ( fn () => $dbForProject -> getDocument ( 'collections' , $collectionId ));
2020-02-01 11:34:07 +13:00
2021-12-18 01:51:07 +13:00
if ( $collection -> isEmpty () || ! $collection -> getAttribute ( 'enabled' )) {
if ( ! ( $mode === APP_MODE_ADMIN && Auth :: isPrivilegedUser ( Authorization :: getRoles ()))) {
2022-02-07 05:50:48 +13:00
throw new Exception ( 'Collection not found' , 404 , Exception :: COLLECTION_NOT_FOUND );
2020-06-30 09:43:34 +12:00
}
}
2020-02-01 11:34:07 +13:00
2021-08-12 13:05:19 +12:00
// Check collection permissions when enforced
2021-08-28 13:45:15 +12:00
if ( $collection -> getAttribute ( 'permission' ) === 'collection' ) {
2021-08-28 13:45:42 +12:00
$validator = new Authorization ( 'write' );
2021-08-12 13:05:19 +12:00
if ( ! $validator -> isValid ( $collection -> getWrite ())) {
2022-02-07 05:50:48 +13:00
throw new Exception ( 'Unauthorized permissions' , 401 , Exception :: USER_UNAUTHORIZED );
2021-08-12 13:05:19 +12:00
}
}
2020-02-01 11:34:07 +13:00
2021-06-16 01:38:24 +12:00
$data [ '$collection' ] = $collection -> getId (); // Adding this param to make API easier for developers
2021-12-28 01:45:23 +13:00
$data [ '$id' ] = $documentId == 'unique()' ? $dbForProject -> getId () : $documentId ;
2021-06-11 06:19:42 +12:00
$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
2021-12-10 23:56:11 +13:00
// Users can only add their roles to documents, API keys and Admin users can add any
2021-12-11 06:52:33 +13:00
$roles = Authorization :: getRoles ();
2020-02-01 11:34:07 +13:00
2021-12-10 23:56:11 +13:00
if ( ! Auth :: isAppUser ( $roles ) && ! Auth :: isPrivilegedUser ( $roles )) {
foreach ( $data [ '$read' ] as $read ) {
if ( ! Authorization :: isRole ( $read )) {
2022-02-08 11:39:53 +13:00
// TODO: Isn't this a 401: Unauthorized Error ?
2022-02-07 10:33:48 +13:00
throw new Exception ( 'Read permissions must be one of: (' . \implode ( ', ' , $roles ) . ')' , 400 , Exception :: USER_UNAUTHORIZED );
2021-12-10 23:56:11 +13:00
}
2020-06-30 09:43:34 +12:00
}
2021-12-10 23:56:11 +13:00
foreach ( $data [ '$write' ] as $write ) {
if ( ! Authorization :: isRole ( $write )) {
2022-02-07 10:33:48 +13:00
throw new Exception ( 'Write permissions must be one of: (' . \implode ( ', ' , $roles ) . ')' , 400 , Exception :: USER_UNAUTHORIZED );
2021-12-10 23:56:11 +13:00
}
2020-06-30 09:43:34 +12:00
}
}
2020-02-01 11:34:07 +13:00
2021-06-12 08:06:54 +12:00
try {
2021-08-28 13:45:15 +12:00
if ( $collection -> getAttribute ( 'permission' ) === 'collection' ) {
2021-08-12 13:26:31 +12:00
/** @var Document $document */
2022-02-08 01:25:15 +13:00
$document = Authorization :: skip ( fn () => $dbForProject -> createDocument ( 'collection_' . $collection -> getInternalId (), new Document ( $data )));
2021-08-12 13:26:31 +12:00
} else {
2022-02-08 01:25:15 +13:00
$document = $dbForProject -> createDocument ( 'collection_' . $collection -> getInternalId (), new Document ( $data ));
2020-04-19 01:57:28 +12:00
}
2021-12-27 23:45:24 +13:00
$document -> setAttribute ( '$collection' , $collectionId );
2020-06-30 09:43:34 +12:00
}
2021-08-16 09:09:40 +12:00
catch ( StructureException $exception ) {
2022-02-07 05:50:48 +13:00
throw new Exception ( $exception -> getMessage (), 400 , Exception :: DOCUMENT_INVALID_STRUCTURE );
2020-06-30 09:43:34 +12:00
}
2021-08-22 09:48:07 +12:00
catch ( DuplicateException $exception ) {
2022-02-07 05:50:48 +13:00
throw new Exception ( 'Document already exists' , 409 , Exception :: DOCUMENT_ALREADY_EXISTS );
2021-08-22 09:48:07 +12:00
}
2020-02-01 11:34:07 +13:00
2021-12-17 07:12:06 +13:00
$events -> setParam ( 'collection' , $collection -> getArrayCopy ());
2021-08-15 23:08:32 +12:00
$usage
-> setParam ( 'database.documents.create' , 1 )
2021-08-21 00:22:04 +12:00
-> setParam ( 'collectionId' , $collectionId )
2021-10-27 09:14:25 +13:00
;
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' )
2021-08-30 00:06:33 +12:00
-> setParam ( 'resource' , 'document/' . $document -> getId ())
2021-06-11 06:19:42 +12:00
-> 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' ])
2019-06-09 01:13:19 +12:00
-> 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 )
2021-12-11 01:27:11 +13:00
-> param ( 'collectionId' , '' , new UID (), 'Collection ID. You can create a new collection using the Database service [server integration](https://appwrite.io/docs/server/database#createCollection).' )
2022-03-15 09:54:55 +13:00
-> param ( 'queries' , [], new ArrayList ( new Text ( 0 ), 100 ), 'Array of query strings generated using the Query class provided by the SDK. [Learn more about queries](https://appwrite.io/docs/database#querying-documents).' , true )
2021-12-15 00:21:44 +13:00
-> param ( 'limit' , 25 , new Range ( 0 , 100 ), 'Maximum number of documents to return in response. By default will return maximum 25 results. Maximum of 100 results allowed per request.' , true )
-> param ( 'offset' , 0 , new Range ( 0 , APP_LIMIT_COUNT ), 'Offset value. The default value is 0. Use this value to manage pagination. [learn more about pagination](https://appwrite.io/docs/pagination)' , 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. [learn more about pagination](https://appwrite.io/docs/pagination)' , true )
2021-10-05 23:30:33 +13:00
-> param ( 'cursorDirection' , Database :: CURSOR_AFTER , new WhiteList ([ Database :: CURSOR_AFTER , Database :: CURSOR_BEFORE ]), 'Direction of the cursor.' , 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-12-28 01:45:23 +13:00
-> inject ( 'dbForProject' )
2021-08-15 23:08:32 +12:00
-> inject ( 'usage' )
2021-12-14 01:42:04 +13:00
-> inject ( 'mode' )
2021-12-28 01:45:23 +13:00
-> action ( function ( $collectionId , $queries , $limit , $offset , $cursor , $cursorDirection , $orderAttributes , $orderTypes , $response , $dbForProject , $usage , $mode ) {
2020-10-30 02:50:49 +13:00
/** @var Appwrite\Utopia\Response $response */
2021-12-28 01:45:23 +13:00
/** @var Utopia\Database\Database $dbForProject */
2021-08-15 23:08:32 +12:00
/** @var Appwrite\Stats\Stats $usage */
2021-12-14 01:42:04 +13:00
/** @var string $mode */
2019-05-09 18:54:39 +12:00
2021-12-17 07:12:06 +13:00
/**
* Skip Authorization to get the collection . Needed in case of empty permissions for document level permissions .
2021-12-18 01:51:07 +13:00
*
* @ var Utopia\Database\Document $collection
2021-12-17 07:12:06 +13:00
*/
2021-12-28 01:45:23 +13:00
$collection = Authorization :: skip ( fn () => $dbForProject -> getDocument ( 'collections' , $collectionId ));
2019-05-09 18:54:39 +12:00
2021-12-18 01:51:07 +13:00
if ( $collection -> isEmpty () || ! $collection -> getAttribute ( 'enabled' )) {
if ( ! ( $mode === APP_MODE_ADMIN && Auth :: isPrivilegedUser ( Authorization :: getRoles ()))) {
2022-02-07 05:50:48 +13:00
throw new Exception ( 'Collection not found' , 404 , Exception :: COLLECTION_NOT_FOUND );
2021-12-18 01:51:07 +13:00
}
2020-06-30 09:43:34 +12:00
}
2020-06-22 00:12:13 +12:00
2021-08-12 13:05:19 +12:00
// Check collection permissions when enforced
2021-08-28 13:45:15 +12:00
if ( $collection -> getAttribute ( 'permission' ) === 'collection' ) {
2021-08-28 13:45:42 +12:00
$validator = new Authorization ( 'read' );
2021-08-12 13:05:19 +12:00
if ( ! $validator -> isValid ( $collection -> getRead ())) {
2022-02-07 05:50:48 +13:00
throw new Exception ( 'Unauthorized permissions' , 401 , Exception :: USER_UNAUTHORIZED );
2021-08-12 13:05:19 +12:00
}
}
2021-12-29 06:57:24 +13:00
$queries = \array_map ( function ( $query ) {
$query = Query :: parse ( $query );
if ( \count ( $query -> getValues ()) > 100 ) {
2022-02-09 10:34:26 +13:00
throw new Exception ( " You cannot use more than 100 query values on attribute ' { $query -> getAttribute () } ' " , 400 , Exception :: GENERAL_QUERY_LIMIT_EXCEEDED );
2021-12-29 06:57:24 +13:00
}
return $query ;
}, $queries );
2020-06-30 09:43:34 +12:00
2021-11-03 12:43:10 +13:00
if ( ! empty ( $queries )) {
$validator = new QueriesValidator ( new QueryValidator ( $collection -> getAttribute ( 'attributes' , [])), $collection -> getAttribute ( 'indexes' , []), true );
if ( ! $validator -> isValid ( $queries )) {
2022-02-09 10:34:26 +13:00
throw new Exception ( $validator -> getDescription (), 400 , Exception :: GENERAL_QUERY_INVALID );
2021-11-03 12:43:10 +13:00
}
2021-07-06 08:27:20 +12:00
}
2021-10-08 07:08:02 +13:00
$cursorDocument = null ;
2021-10-05 23:30:33 +13:00
if ( ! empty ( $cursor )) {
2021-12-28 06:00:52 +13:00
$cursorDocument = $collection -> getAttribute ( 'permission' ) === 'collection'
2022-02-08 01:25:15 +13:00
? Authorization :: skip ( fn () => $dbForProject -> getDocument ( 'collection_' . $collection -> getInternalId (), $cursor ))
: $dbForProject -> getDocument ( 'collection_' . $collection -> getInternalId (), $cursor );
2021-08-06 07:01:00 +12:00
2021-10-05 23:30:33 +13:00
if ( $cursorDocument -> isEmpty ()) {
2022-02-09 11:56:11 +13:00
throw new Exception ( " Document ' { $cursor } ' for the 'cursor' value not found. " , 400 , Exception :: GENERAL_CURSOR_NOT_FOUND );
2021-08-06 07:01:00 +12:00
}
}
2020-06-30 09:43:34 +12:00
2021-08-28 13:45:15 +12:00
if ( $collection -> getAttribute ( 'permission' ) === 'collection' ) {
2021-08-12 13:26:31 +12:00
/** @var Document[] $documents */
2022-02-08 01:25:15 +13:00
$documents = Authorization :: skip ( fn () => $dbForProject -> find ( 'collection_' . $collection -> getInternalId (), $queries , $limit , $offset , $orderAttributes , $orderTypes , $cursorDocument ? ? null , $cursorDirection ));
2022-03-01 07:50:54 +13:00
$total = Authorization :: skip ( fn () => $dbForProject -> count ( 'collection_' . $collection -> getInternalId (), $queries , APP_LIMIT_COUNT ));
2021-08-12 13:26:31 +12:00
} else {
2022-02-08 01:25:15 +13:00
$documents = $dbForProject -> find ( 'collection_' . $collection -> getInternalId (), $queries , $limit , $offset , $orderAttributes , $orderTypes , $cursorDocument ? ? null , $cursorDirection );
2022-03-01 07:50:54 +13:00
$total = $dbForProject -> count ( 'collection_' . $collection -> getInternalId (), $queries , APP_LIMIT_COUNT );
2021-08-12 13:26:31 +12:00
}
2021-12-27 23:45:24 +13:00
/**
* Reset $collection attribute to remove prefix .
*/
$documents = array_map ( fn ( Document $document ) => $document -> setAttribute ( '$collection' , $collectionId ), $documents );
2021-08-15 23:08:32 +12:00
$usage
-> setParam ( 'database.documents.read' , 1 )
2021-08-21 00:22:04 +12:00
-> setParam ( 'collectionId' , $collectionId )
2020-06-30 09:43:34 +12:00
;
2021-07-26 02:47:18 +12:00
$response -> dynamic ( new Document ([
2022-02-27 22:57:09 +13:00
'total' => $total ,
2021-08-12 13:26:31 +12:00
'documents' => $documents ,
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' ])
2019-06-09 01:13:19 +12:00
-> 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 )
2021-04-08 20:39:23 +12:00
-> label ( 'sdk.response.model' , Response :: MODEL_DOCUMENT )
2021-12-11 01:27:11 +13:00
-> param ( 'collectionId' , null , new UID (), 'Collection ID. You can create a new collection using the Database service [server integration](https://appwrite.io/docs/server/database#createCollection).' )
-> param ( 'documentId' , null , new UID (), 'Document ID.' )
2020-12-27 04:05:04 +13:00
-> inject ( 'response' )
2021-12-28 01:45:23 +13:00
-> inject ( 'dbForProject' )
2021-08-15 23:08:32 +12:00
-> inject ( 'usage' )
2021-12-14 01:42:04 +13:00
-> inject ( 'mode' )
2021-12-28 01:45:23 +13:00
-> action ( function ( $collectionId , $documentId , $response , $dbForProject , $usage , $mode ) {
2020-10-30 02:50:49 +13:00
/** @var Appwrite\Utopia\Response $response */
2021-12-28 01:45:23 +13:00
/** @var Utopia\Database\Database $dbForProject */
2021-12-14 01:42:04 +13:00
/** @var string $mode */
2019-05-09 18:54:39 +12:00
2021-12-17 07:12:06 +13:00
/**
* Skip Authorization to get the collection . Needed in case of empty permissions for document level permissions .
*/
2021-12-28 01:45:23 +13:00
$collection = Authorization :: skip ( fn () => $dbForProject -> getDocument ( 'collections' , $collectionId ));
2019-05-09 18:54:39 +12:00
2021-12-18 01:51:07 +13:00
if ( $collection -> isEmpty () || ! $collection -> getAttribute ( 'enabled' )) {
if ( ! ( $mode === APP_MODE_ADMIN && Auth :: isPrivilegedUser ( Authorization :: getRoles ()))) {
2022-02-07 05:50:48 +13:00
throw new Exception ( 'Collection not found' , 404 , Exception :: COLLECTION_NOT_FOUND );
2021-12-18 01:51:07 +13:00
}
2021-06-11 06:19:42 +12:00
}
2021-08-12 13:05:19 +12:00
// Check collection permissions when enforced
2021-08-28 13:45:15 +12:00
if ( $collection -> getAttribute ( 'permission' ) === 'collection' ) {
2021-08-28 13:45:42 +12:00
$validator = new Authorization ( 'read' );
2021-08-12 13:05:19 +12:00
if ( ! $validator -> isValid ( $collection -> getRead ())) {
2022-02-07 05:50:48 +13:00
throw new Exception ( 'Unauthorized permissions' , 401 , Exception :: USER_UNAUTHORIZED );
2021-08-12 13:05:19 +12:00
}
}
2021-08-28 13:45:15 +12:00
if ( $collection -> getAttribute ( 'permission' ) === 'collection' ) {
2021-08-12 13:26:31 +12:00
/** @var Document $document */
2022-02-08 01:25:15 +13:00
$document = Authorization :: skip ( fn () => $dbForProject -> getDocument ( 'collection_' . $collection -> getInternalId (), $documentId ));
2021-08-12 13:26:31 +12:00
} else {
2022-02-08 01:25:15 +13:00
$document = $dbForProject -> getDocument ( 'collection_' . $collection -> getInternalId (), $documentId );
2021-08-12 13:26:31 +12:00
}
2019-05-09 18:54:39 +12:00
2021-12-27 23:45:24 +13:00
/**
* Reset $collection attribute to remove prefix .
*/
$document -> setAttribute ( '$collection' , $collectionId );
2019-05-09 18:54:39 +12:00
2021-06-11 06:19:42 +12:00
if ( $document -> isEmpty ()) {
2022-02-07 05:50:48 +13:00
throw new Exception ( 'No document found' , 404 , Exception :: DOCUMENT_NOT_FOUND );
2020-06-30 09:43:34 +12:00
}
2019-05-09 18:54:39 +12:00
2021-08-15 23:08:32 +12:00
$usage
-> setParam ( 'database.documents.read' , 1 )
2021-08-21 00:22:04 +12:00
-> setParam ( 'collectionId' , $collectionId )
2022-01-18 05:25:20 +13:00
;
2021-08-15 23:08:32 +12:00
2021-04-08 20:39:23 +12:00
$response -> dynamic ( $document , Response :: MODEL_DOCUMENT );
2020-12-27 04:05:04 +13:00
});
2019-05-09 18:54:39 +12:00
2021-08-29 19:43:09 +12:00
App :: get ( '/v1/database/collections/:collectionId/documents/:documentId/logs' )
-> desc ( 'List Document Logs' )
-> groups ([ 'api' , 'database' ])
-> label ( 'scope' , 'documents.read' )
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN ])
-> label ( 'sdk.namespace' , 'database' )
-> label ( 'sdk.method' , 'listDocumentLogs' )
-> label ( 'sdk.description' , '/docs/references/database/get-document-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 )
2021-12-11 01:27:11 +13:00
-> param ( 'collectionId' , '' , new UID (), 'Collection ID.' )
-> param ( 'documentId' , null , new UID (), 'Document ID.' )
2021-12-15 00:21:44 +13:00
-> param ( 'limit' , 25 , new Range ( 0 , 100 ), 'Maximum number of logs to return in response. By default will return maximum 25 results. Maximum of 100 results allowed per request.' , true )
-> param ( 'offset' , 0 , new Range ( 0 , APP_LIMIT_COUNT ), 'Offset value. The default value is 0. Use this value to manage pagination. [learn more about pagination](https://appwrite.io/docs/pagination)' , true )
2021-08-29 19:43:09 +12:00
-> inject ( 'response' )
2021-12-28 01:45:23 +13:00
-> inject ( 'dbForProject' )
2021-08-29 19:43:09 +12:00
-> inject ( 'locale' )
-> inject ( 'geodb' )
2021-12-28 01:45:23 +13:00
-> action ( function ( $collectionId , $documentId , $limit , $offset , $response , $dbForProject , $locale , $geodb ) {
2021-08-29 19:43:09 +12:00
/** @var Appwrite\Utopia\Response $response */
/** @var Utopia\Database\Document $project */
2021-12-28 01:45:23 +13:00
/** @var Utopia\Database\Database $dbForProject */
2021-08-29 19:43:09 +12:00
/** @var Utopia\Locale\Locale $locale */
/** @var MaxMind\Db\Reader $geodb */
2021-12-28 01:45:23 +13:00
$collection = $dbForProject -> getDocument ( 'collections' , $collectionId );
2021-08-29 19:43:09 +12:00
if ( $collection -> isEmpty ()) {
2022-02-07 05:50:48 +13:00
throw new Exception ( 'Collection not found' , 404 , Exception :: COLLECTION_NOT_FOUND );
2021-08-29 19:43:09 +12:00
}
2022-02-08 01:25:15 +13:00
$document = $dbForProject -> getDocument ( 'collection_' . $collection -> getInternalId (), $documentId );
2021-08-29 19:43:09 +12:00
if ( $document -> isEmpty ()) {
2022-02-07 05:50:48 +13:00
throw new Exception ( 'No document found' , 404 , Exception :: DOCUMENT_NOT_FOUND );
2021-08-29 19:43:09 +12:00
}
2021-12-28 01:45:23 +13:00
$audit = new Audit ( $dbForProject );
2021-11-18 23:33:42 +13:00
$resource = 'document/' . $document -> getId ();
$logs = $audit -> getLogsByResource ( $resource , $limit , $offset );
2021-08-29 19:43:09 +12:00
$output = [];
foreach ( $logs as $i => & $log ) {
$log [ 'userAgent' ] = ( ! empty ( $log [ 'userAgent' ])) ? $log [ 'userAgent' ] : 'UNKNOWN' ;
2021-12-13 06:59:12 +13:00
$detector = new Detector ( $log [ 'userAgent' ]);
$detector -> skipBotDetection (); // OPTIONAL: If called, bot detection will completely be skipped (bots will be detected as regular devices then)
2021-08-29 19:43:09 +12:00
2021-12-13 06:59:12 +13:00
$os = $detector -> getOS ();
$client = $detector -> getClient ();
$device = $detector -> getDevice ();
2021-08-29 19:43:09 +12:00
$output [ $i ] = new Document ([
'event' => $log [ 'event' ],
'userId' => $log [ 'userId' ],
'userEmail' => $log [ 'data' ][ 'userEmail' ] ? ? null ,
'userName' => $log [ 'data' ][ 'userName' ] ? ? null ,
'mode' => $log [ 'data' ][ 'mode' ] ? ? null ,
'ip' => $log [ 'ip' ],
'time' => $log [ 'time' ],
2021-12-13 06:59:12 +13:00
'osCode' => $os [ 'osCode' ],
'osName' => $os [ 'osName' ],
'osVersion' => $os [ 'osVersion' ],
'clientType' => $client [ 'clientType' ],
'clientCode' => $client [ 'clientCode' ],
'clientName' => $client [ 'clientName' ],
'clientVersion' => $client [ 'clientVersion' ],
'clientEngine' => $client [ 'clientEngine' ],
'clientEngineVersion' => $client [ 'clientEngineVersion' ],
'deviceName' => $device [ 'deviceName' ],
'deviceBrand' => $device [ 'deviceBrand' ],
'deviceModel' => $device [ 'deviceModel' ]
2021-08-29 19:43:09 +12:00
]);
$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' );
}
}
2021-11-18 23:33:42 +13:00
$response -> dynamic ( new Document ([
2022-02-27 22:57:09 +13:00
'total' => $audit -> countLogsByResource ( $resource ),
2021-11-18 23:33:42 +13:00
'logs' => $output ,
]), Response :: MODEL_LOG_LIST );
2021-08-29 19:43:09 +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' )
2019-06-09 01:13:19 +12: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' )
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 )
2021-04-08 20:39:23 +12:00
-> label ( 'sdk.response.model' , Response :: MODEL_DOCUMENT )
2022-01-05 05:06:16 +13:00
-> param ( 'collectionId' , null , new UID (), 'Collection ID.' )
2021-12-11 01:27:11 +13:00
-> param ( 'documentId' , null , new UID (), 'Document ID.' )
2022-03-15 10:00:50 +13:00
-> param ( 'data' , [], new JSON (), 'Document data as JSON object. Include only attribute and value pairs to be updated.' )
2021-12-11 01:27:11 +13:00
-> param ( 'read' , null , new Permissions (), 'An array of strings with read permissions. By default inherits the existing read permissions. [learn more about permissions](https://appwrite.io/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](https://appwrite.io/docs/permissions) and get a full list of available permissions.' , true )
2020-12-27 04:05:04 +13:00
-> inject ( 'response' )
2021-12-28 01:45:23 +13:00
-> inject ( 'dbForProject' )
2020-12-27 04:05:04 +13:00
-> inject ( 'audits' )
2021-08-15 23:08:32 +12:00
-> inject ( 'usage' )
2021-12-17 07:12:06 +13:00
-> inject ( 'events' )
2021-12-14 01:42:04 +13:00
-> inject ( 'mode' )
2021-12-28 01:45:23 +13:00
-> action ( function ( $collectionId , $documentId , $data , $read , $write , $response , $dbForProject , $audits , $usage , $events , $mode ) {
2020-10-30 02:50:49 +13:00
/** @var Appwrite\Utopia\Response $response */
2021-12-28 01:45:23 +13:00
/** @var Utopia\Database\Database $dbForProject */
2020-07-06 02:19:59 +12:00
/** @var Appwrite\Event\Event $audits */
2021-08-15 23:08:32 +12:00
/** @var Appwrite\Stats\Stats $usage */
2021-12-17 07:12:06 +13:00
/** @var Appwrite\Event\Event $events */
2021-12-14 01:42:04 +13:00
/** @var string $mode */
2019-05-09 18:54:39 +12:00
2021-12-17 07:12:06 +13:00
/**
* Skip Authorization to get the collection . Needed in case of empty permissions for document level permissions .
*/
2021-12-28 01:45:23 +13:00
$collection = Authorization :: skip ( fn () => $dbForProject -> getDocument ( 'collections' , $collectionId ));
2019-05-09 18:54:39 +12:00
2021-12-18 01:51:07 +13:00
if ( $collection -> isEmpty () || ! $collection -> getAttribute ( 'enabled' )) {
if ( ! ( $mode === APP_MODE_ADMIN && Auth :: isPrivilegedUser ( Authorization :: getRoles ()))) {
2022-02-07 05:50:48 +13:00
throw new Exception ( 'Collection not found' , 404 , Exception :: COLLECTION_NOT_FOUND );
2021-12-18 01:51:07 +13:00
}
2020-06-30 09:43:34 +12:00
}
2019-09-17 07:03:24 +12:00
2021-08-12 13:05:19 +12:00
// Check collection permissions when enforced
2021-08-28 13:45:15 +12:00
if ( $collection -> getAttribute ( 'permission' ) === 'collection' ) {
2021-08-28 13:45:42 +12:00
$validator = new Authorization ( 'write' );
2021-08-12 13:05:19 +12:00
if ( ! $validator -> isValid ( $collection -> getWrite ())) {
2022-02-07 05:50:48 +13:00
throw new Exception ( 'Unauthorized permissions' , 401 , Exception :: USER_UNAUTHORIZED );
2021-08-12 13:05:19 +12:00
}
2021-12-17 07:12:06 +13:00
2022-02-08 01:25:15 +13:00
$document = Authorization :: skip ( fn () => $dbForProject -> getDocument ( 'collection_' . $collection -> getInternalId (), $documentId ));
2021-12-17 07:12:06 +13:00
} else {
2022-02-08 01:25:15 +13:00
$document = $dbForProject -> getDocument ( 'collection_' . $collection -> getInternalId (), $documentId );
2020-06-30 09:43:34 +12:00
}
2019-05-09 18:54:39 +12:00
2021-06-16 01:38:24 +12:00
if ( $document -> isEmpty ()) {
2022-02-07 05:50:48 +13:00
throw new Exception ( 'Document not found' , 404 , Exception :: DOCUMENT_NOT_FOUND );
2020-06-30 09:43:34 +12:00
}
2019-05-09 18:54:39 +12:00
2021-06-16 01:38:24 +12:00
$data = ( \is_string ( $data )) ? \json_decode ( $data , true ) : $data ; // Cast to JSON array
if ( empty ( $data )) {
2022-02-07 05:50:48 +13:00
throw new Exception ( 'Missing payload' , 400 , Exception :: DOCUMENT_MISSING_PAYLOAD );
2021-06-16 01:38:24 +12:00
}
if ( ! \is_array ( $data )) {
2022-02-07 05:50:48 +13:00
throw new Exception ( 'Data param should be a valid JSON object' , 400 , Exception :: DOCUMENT_INVALID_STRUCTURE );
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
2021-06-16 01:38:24 +12:00
$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
2021-12-10 23:56:11 +13:00
// Users can only add their roles to documents, API keys and Admin users can add any
2021-12-11 06:52:33 +13:00
$roles = Authorization :: getRoles ();
2021-12-10 23:56:11 +13:00
if ( ! Auth :: isAppUser ( $roles ) && ! Auth :: isPrivilegedUser ( $roles )) {
2022-01-26 03:51:04 +13:00
if ( ! is_null ( $read )) {
foreach ( $data [ '$read' ] as $read ) {
if ( ! Authorization :: isRole ( $read )) {
2022-02-09 05:56:03 +13:00
throw new Exception ( 'Read permissions must be one of: (' . \implode ( ', ' , $roles ) . ')' , 400 , Exception :: USER_UNAUTHORIZED );
2022-01-26 03:51:04 +13:00
}
2021-12-10 23:56:11 +13:00
}
2021-11-04 05:38:06 +13:00
}
2022-01-26 03:51:04 +13:00
if ( ! is_null ( $write )) {
foreach ( $data [ '$write' ] as $write ) {
if ( ! Authorization :: isRole ( $write )) {
2022-02-09 05:56:03 +13:00
throw new Exception ( 'Write permissions must be one of: (' . \implode ( ', ' , $roles ) . ')' , 400 , Exception :: USER_UNAUTHORIZED );
2022-01-26 03:51:04 +13:00
}
2021-12-10 23:56:11 +13:00
}
2021-11-04 05:38:06 +13:00
}
2020-06-30 09:43:34 +12:00
}
2020-10-31 08:53:27 +13:00
2020-06-30 09:43:34 +12:00
try {
2021-08-28 13:45:15 +12:00
if ( $collection -> getAttribute ( 'permission' ) === 'collection' ) {
2021-08-12 13:26:31 +12:00
/** @var Document $document */
2022-02-08 01:25:15 +13:00
$document = Authorization :: skip ( fn () => $dbForProject -> updateDocument ( 'collection_' . $collection -> getInternalId (), $document -> getId (), new Document ( $data )));
2021-08-12 13:26:31 +12:00
} else {
2022-02-08 01:25:15 +13:00
$document = $dbForProject -> updateDocument ( 'collection_' . $collection -> getInternalId (), $document -> getId (), new Document ( $data ));
2021-08-12 13:26:31 +12:00
}
2021-12-27 23:45:24 +13:00
/**
* Reset $collection attribute to remove prefix .
*/
$document -> setAttribute ( '$collection' , $collectionId );
2021-08-16 09:09:40 +12:00
}
catch ( AuthorizationException $exception ) {
2022-02-07 05:50:48 +13:00
throw new Exception ( 'Unauthorized permissions' , 401 , Exception :: USER_UNAUTHORIZED );
2021-08-16 09:09:40 +12:00
}
2021-08-19 06:49:49 +12:00
catch ( DuplicateException $exception ) {
2022-02-07 05:50:48 +13:00
throw new Exception ( 'Document already exists' , 409 , Exception :: DOCUMENT_ALREADY_EXISTS );
2020-06-30 09:43:34 +12:00
}
2021-08-16 09:09:40 +12:00
catch ( StructureException $exception ) {
2022-02-07 05:50:48 +13:00
throw new Exception ( $exception -> getMessage (), 400 , Exception :: DOCUMENT_INVALID_STRUCTURE );
2021-08-16 09:09:40 +12:00
}
2021-12-17 07:12:06 +13:00
$events -> setParam ( 'collection' , $collection -> getArrayCopy ());
2021-08-15 23:08:32 +12:00
$usage
-> setParam ( 'database.documents.update' , 1 )
2021-08-21 00:22:04 +12:00
-> setParam ( 'collectionId' , $collectionId )
;
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' )
2021-08-30 00:06:33 +12:00
-> setParam ( 'resource' , 'document/' . $document -> getId ())
2021-06-16 01:38:24 +12:00
-> 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' ])
2019-06-09 01:13:19 +12:00
-> 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 )
2021-12-11 01:27:11 +13:00
-> param ( 'collectionId' , null , new UID (), 'Collection ID. You can create a new collection using the Database service [server integration](https://appwrite.io/docs/server/database#createCollection).' )
-> param ( 'documentId' , null , new UID (), 'Document ID.' )
2020-12-27 04:05:04 +13:00
-> inject ( 'response' )
2021-12-28 01:45:23 +13:00
-> inject ( 'dbForProject' )
2020-12-27 04:05:04 +13:00
-> inject ( 'events' )
-> inject ( 'audits' )
2022-01-03 23:29:15 +13:00
-> inject ( 'deletes' )
2021-08-15 23:08:32 +12:00
-> inject ( 'usage' )
2021-12-14 01:42:04 +13:00
-> inject ( 'mode' )
2022-01-03 23:29:15 +13:00
-> action ( function ( $collectionId , $documentId , $response , $dbForProject , $events , $audits , $deletes , $usage , $mode ) {
2020-10-30 02:50:49 +13:00
/** @var Appwrite\Utopia\Response $response */
2021-12-28 01:45:23 +13:00
/** @var Utopia\Database\Database $dbForProject */
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 */
2022-01-03 23:29:15 +13:00
/** @var Appwrite\Event\Event $deletes */
2021-08-15 23:08:32 +12:00
/** @var Appwrite\Stats\Stats $usage */
2021-12-14 01:42:04 +13:00
/** @var string $mode */
2019-05-09 18:54:39 +12:00
2021-12-17 07:12:06 +13:00
/**
* Skip Authorization to get the collection . Needed in case of empty permissions for document level permissions .
*/
2021-12-28 01:45:23 +13:00
$collection = Authorization :: skip ( fn () => $dbForProject -> getDocument ( 'collections' , $collectionId ));
2019-05-09 18:54:39 +12:00
2021-12-18 01:51:07 +13:00
if ( $collection -> isEmpty () || ! $collection -> getAttribute ( 'enabled' )) {
if ( ! ( $mode === APP_MODE_ADMIN && Auth :: isPrivilegedUser ( Authorization :: getRoles ()))) {
2022-02-07 05:50:48 +13:00
throw new Exception ( 'Collection not found' , 404 , Exception :: COLLECTION_NOT_FOUND );
2021-12-18 01:51:07 +13:00
}
2020-06-30 09:43:34 +12:00
}
2019-05-09 18:54:39 +12:00
2021-08-12 13:05:19 +12:00
// Check collection permissions when enforced
2021-08-28 13:45:15 +12:00
if ( $collection -> getAttribute ( 'permission' ) === 'collection' ) {
2021-08-28 13:45:42 +12:00
$validator = new Authorization ( 'write' );
2021-08-12 13:05:19 +12:00
if ( ! $validator -> isValid ( $collection -> getWrite ())) {
2022-02-07 05:50:48 +13:00
throw new Exception ( 'Unauthorized permissions' , 401 , Exception :: USER_UNAUTHORIZED );
2021-08-12 13:05:19 +12:00
}
}
2019-05-09 18:54:39 +12:00
2021-08-28 13:45:15 +12:00
if ( $collection -> getAttribute ( 'permission' ) === 'collection' ) {
2021-08-12 13:26:31 +12:00
/** @var Document $document */
2022-02-08 01:25:15 +13:00
$document = Authorization :: skip ( fn () => $dbForProject -> getDocument ( 'collection_' . $collection -> getInternalId (), $documentId ));
2021-08-12 13:26:31 +12:00
} else {
2022-02-08 01:25:15 +13:00
$document = $dbForProject -> getDocument ( 'collection_' . $collection -> getInternalId (), $documentId );
2020-06-30 09:43:34 +12:00
}
2019-05-09 18:54:39 +12:00
2021-06-11 06:19:42 +12:00
if ( $document -> isEmpty ()) {
2022-02-07 05:50:48 +13:00
throw new Exception ( 'No document found' , 404 , Exception :: DOCUMENT_NOT_FOUND );
2020-06-30 09:43:34 +12:00
}
2021-12-17 07:12:06 +13:00
if ( $collection -> getAttribute ( 'permission' ) === 'collection' ) {
2022-02-08 01:25:15 +13:00
Authorization :: skip ( fn () => $dbForProject -> deleteDocument ( 'collection_' . $collection -> getInternalId (), $documentId ));
2021-12-17 07:12:06 +13:00
} else {
2022-02-08 01:25:15 +13:00
$dbForProject -> deleteDocument ( 'collection_' . $collection -> getInternalId (), $documentId );
2020-06-30 09:43:34 +12:00
}
2019-05-09 18:54:39 +12:00
2022-02-08 01:25:15 +13:00
$dbForProject -> deleteCachedDocument ( 'collection_' . $collection -> getInternalId (), $documentId );
2021-06-11 06:19:42 +12:00
2021-12-27 23:45:24 +13:00
/**
* Reset $collection attribute to remove prefix .
*/
$document -> setAttribute ( '$collection' , $collectionId );
2022-01-03 23:29:15 +13:00
$deletes
-> setParam ( 'type' , DELETE_TYPE_AUDIT )
-> setParam ( 'document' , $document )
;
2021-08-15 23:08:32 +12:00
$usage
-> setParam ( 'database.documents.delete' , 1 )
2021-08-21 00:22:04 +12:00
-> setParam ( 'collectionId' , $collectionId )
;
2021-08-15 23:08:32 +12:00
2020-12-07 11:14:57 +13:00
$events
2021-04-14 21:02:17 +12:00
-> setParam ( 'eventData' , $response -> output ( $document , Response :: MODEL_DOCUMENT ))
2021-12-17 07:12:06 +13:00
-> setParam ( 'collection' , $collection -> getArrayCopy ());
2020-06-30 09:43:34 +12:00
;
2021-04-14 21:02:17 +12:00
2020-07-06 02:19:59 +12:00
$audits
2020-06-30 09:43:34 +12:00
-> setParam ( 'event' , 'database.documents.delete' )
2021-08-30 00:06:33 +12:00
-> setParam ( 'resource' , 'document/' . $document -> getId ())
2020-10-31 08:53:27 +13:00
-> 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
2021-03-19 09:32:16 +13:00
$response -> dynamic ( new Document (), Response :: MODEL_NONE );
2021-07-07 23:46:44 +12:00
});