forked from open-runtimes/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request open-runtimes#64 from kritikash18/main
function to wipe-appwrite-collection
- Loading branch information
Showing
4 changed files
with
221 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# Wipe an Appwrite collection | ||
|
||
A Node 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 node/wipe_appwrite_collection | ||
``` | ||
|
||
2. Enter this function folder and build the code: | ||
``` | ||
docker run --rm --interactive --tty --volume $PWD:/usr/code openruntimes/node:v2-18.0 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.js --rm --interactive --tty --volume $PWD/code.tar.gz:/tmp/code.tar.gz:ro openruntimes/node:v2-18.0 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 Node runtime [README](https://github.com/open-runtimes/open-runtimes/tree/main/runtimes/node-18.0). | ||
|
||
4. Execute function: | ||
|
||
``` | ||
curl http:https://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 node/wipe_appwrite_collection | ||
``` | ||
|
||
2. Enter this function folder and build the code: | ||
``` | ||
docker run --rm --interactive --tty --volume $PWD:/usr/code openruntimes/node:18.0 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.js 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 Node 18.0. Other versions may work but are not guaranteed to work as they haven't been tested. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
const sdk = require('node-appwrite'); | ||
|
||
module.exports = async function (req, res) { | ||
const client = new sdk.Client(); | ||
const databases = new sdk.Databases(client); | ||
|
||
if(!req.variables["APPWRITE_FUNCTION_ENDPOINT"] || !req.variables["APPWRITE_FUNCTION_API_KEY"] || !req.variables["APPWRITE_FUNCTION_PROJECT_ID"]) { | ||
res.json({success: false, message: "Variables missing."}); | ||
return; | ||
} | ||
|
||
client | ||
.setEndpoint(req.variables["APPWRITE_FUNCTION_ENDPOINT"]) | ||
.setProject(req.variables["APPWRITE_FUNCTION_PROJECT_ID"]) | ||
.setKey(req.variables["APPWRITE_FUNCTION_API_KEY"]); | ||
|
||
try{ | ||
const payload = JSON.parse(req.payload ?? '{}'); | ||
if(!payload.databaseId || !payload.collectionId) { | ||
res.json({success: false, message: "Invalid payload."}); | ||
return; | ||
} | ||
|
||
let sum = 0; | ||
let done = false; | ||
|
||
while(!done) { | ||
const response = await databases.listDocuments(payload.databaseId, payload.collectionId); | ||
const documents = response.documents; | ||
|
||
for(const document of documents) { | ||
await databases.deleteDocument(payload.databaseId, payload.collectionId, document.$id); | ||
sum++; | ||
} | ||
|
||
if(documents.length === 0) { | ||
done = true; | ||
} | ||
} | ||
|
||
res.send({success: true, sum: sum}); | ||
} catch(e) { | ||
res.json({success: false, message: "Unexpected error: " + e}); | ||
} | ||
|
||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"name": "wipe_appwrite_collection", | ||
"version": "1.0.0", | ||
"description": "Delete all documents from a collection", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"author": "Kritika Sharma", | ||
"license": "ISC", | ||
"dependencies": { | ||
"node-appwrite": "^8.1.0" | ||
} | ||
} |