Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Example functions to show off what you can achieve with Open Runtimes.
| Compresss Imagse | | | | | |[✅](/ruby/compress-image)| |
| Send a HTTP Request | | | | | | [✅](/ruby/send-http-request) | |
| Wipe Appwrite Bucket | | | [✅](/node/wipe_appwrite_bucket) | | | | |
| Wipe Appwrite Collection | | | [✅](/node/wipe_appwrite_collection/) | [✅](/php/wipe_appwrite_collection/) | | [✅](/ruby/wipe-appwrite-collection/) | |
| Detect Language with Deepgram | | | | | [✅](/python/deepgram-language-detection) | | |
| Summarize Audio with Deepgram | | | | | |[✅](/ruby/deepgram-audio-summary)| |
| Summarize Audio with Deepgram | | | | | [✅](/python/deepgram-audio-summary) | | |
Expand Down
89 changes: 89 additions & 0 deletions php/wipe_appwrite_collection/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# Wipe an Appwrite collection

A PHP Cloud Function that wipes out all the documents inside a collection.

_Example input:_

```json
{
"databaseId": "stage",
"collectionId": "profiles"
}
```

_Example output (success):_

```json
{
"success": true,
"sum": 2
}
```

_Example output (failure):_

```json
{
"success": false,
"message": "Collection not found."
}
```

## 📝 Environment Variables

List of environment variables used by this cloud function:

- **APPWRITE_FUNCTION_ENDPOINT** - Endpoint of your Appwrite server
- **APPWRITE_FUNCTION_API_KEY** - Appwrite API Key
- **APPWRITE_FUNCTION_PROJECT_ID** - Appwrite project ID. If running on Appwrite, this variable is provided automatically.

## 🚀 Deployment

1. Clone this repository, and enter this function folder:

```
$ git clone https://github.com/open-runtimes/examples.git && cd examples
$ cd php/wipe_appwrite_collection
```

2. Enter this function folder and build the code:
```
docker run --rm --interactive --tty --volume $PWD:/usr/code openruntimes/php:v2-8.1 sh /usr/local/src/build.sh
```
As a result, a `code.tar.gz` file will be generated.

3. Start the Open Runtime:
```
docker run -p 3000:3000 -e INTERNAL_RUNTIME_KEY=secret-key -e INTERNAL_RUNTIME_ENTRYPOINT=index.php --rm --interactive --tty --volume $PWD/code.tar.gz:/tmp/code.tar.gz:ro openruntimes/php:v2-8.1 sh /usr/local/src/start.sh
```

Your function is now listening on port `3000`, and you can execute it by sending `POST` request with appropriate authorization headers. To learn more about runtime, you can visit PHP runtime [README](https://github.com/open-runtimes/open-runtimes/tree/main/runtimes/php-8.1).

4. Execute function:

```
curl http://localhost:3000/ -d '{"variables":{"APPWRITE_FUNCTION_ENDPOINT":"YOUR_ENDPOINT","APPWRITE_FUNCTION_PROJECT_ID":"YOUR_PROJECT_ID","APPWRITE_FUNCTION_API_KEY":"YOUR_API_KEY"},"payload":"{\"databaseId\":\"stage\",\"collectionId\":\"profiles\"}"}' -H "X-Internal-Challenge: secret-key" -H "Content-Type: application/json"
```

## 🚀 Deployment using Appwrite

1. Clone this repository, and enter this function folder:
```
$ git clone https://github.com/open-runtimes/examples.git && cd examples
$ cd php/wipe_appwrite_collection
```

2. Enter this function folder and build the code:
```
docker run --rm --interactive --tty --volume $PWD:/usr/code openruntimes/php:v2-8.1 sh /usr/local/src/build.sh
```
As a result, a `code.tar.gz` file will be generated.

3. Add a new function to appwrite server. Upload the code.tar.gz file manually. Set index.php as entrypoint.

4. Execute the function. Pass collection ID and database ID as shown in example input and execute the function.


## 📝 Notes
- This function is designed for use with Appwrite Cloud Functions. You can learn more about it in [Appwrite docs](https://appwrite.io/docs/functions).
- This example is compatible with PHP 8.1. Other versions may work but are not guaranteed to work as they haven't been tested.
11 changes: 11 additions & 0 deletions php/wipe_appwrite_collection/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "user/function",
"description": "",
"type": "library",
"license": "ISC",
"authors": [],
"require": {
"php": ">=8.0.0",
"appwrite/appwrite": "7.0.*"
}
}
77 changes: 77 additions & 0 deletions php/wipe_appwrite_collection/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

require_once('vendor/autoload.php');

use Appwrite\Client;
use Appwrite\Services\Databases;

return function ($req, $res) {
$invalPayloadResponse = [
'success' => false,
'message' => 'Payload is invalid.'
];
try {
$payload = \json_decode($req['payload'], true, flags: JSON_THROW_ON_ERROR);
$databaseId = \trim($payload['databaseId']);
$collectionId = \trim($payload['collectionId']);
} catch(\Exception $err) {
\var_dump($err);
$res->json($invalPayloadResponse);
return;
}

if(empty($databaseId) || empty($collectionId)) {
$res->json($invalPayloadResponse);
return;
}

// Make sure we have envirnment variables required to execute
if(!$req['variables']['APPWRITE_FUNCTION_ENDPOINT'] ||
!$req['variables']['APPWRITE_FUNCTION_PROJECT_ID'] ||
!$req['variables']['APPWRITE_FUNCTION_API_KEY']) {
$res->json([
'success' => false,
'message' => 'Please provide all required variables.'
]);
return;
}

$client = new Client();
$databases = new Databases($client);


$client
->setEndpoint($req['variables']['APPWRITE_FUNCTION_ENDPOINT'])
->setProject($req['variables']['APPWRITE_FUNCTION_PROJECT_ID'])
->setKey($req['variables']['APPWRITE_FUNCTION_API_KEY']);

$done = false;
$sum = 0;
try {
while (!$done) {

$documents = $databases->listDocuments($databaseId, $collectionId)['documents'];
foreach($documents as $document) {
\var_dump($document);
$databases->deleteDocument($databaseId, $collectionId, $document['$id']);
$sum++;
}

if (count($documents) === 0) {
$done = true;
}
}
} catch(\Exception $err) {
$res->json([
'success' => false,
'message' => $err->getMessage()
]);
return;
}

// Return success result
$res->json([
'success' => true,
'sum' => $sum
]);
};