From eb44ffd120c768b628c25b6cf4ae6b6214b98aff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Mon, 23 Jan 2023 08:26:16 +0000 Subject: [PATCH] PR review changes --- node/wipe_appwrite_collection/README.md | 19 +++++++---- node/wipe_appwrite_collection/index.js | 45 ++++++++++++++----------- 2 files changed, 39 insertions(+), 25 deletions(-) diff --git a/node/wipe_appwrite_collection/README.md b/node/wipe_appwrite_collection/README.md index 5fc3841e..0d67d258 100644 --- a/node/wipe_appwrite_collection/README.md +++ b/node/wipe_appwrite_collection/README.md @@ -15,7 +15,8 @@ _Example output (success):_ ```json { - "success": true + "success": true, + "sum": 2 } ``` @@ -47,16 +48,22 @@ $ 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:17.0 sh /usr/local/src/build.sh +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:17.0 sh /usr/local/src/start.sh +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-17.0). +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://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 @@ -68,7 +75,7 @@ $ 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:17.0 sh /usr/local/src/build.sh +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. @@ -79,4 +86,4 @@ As a result, a `code.tar.gz` file will be generated. ## 📝 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 17.0. Other versions may work but are not guaranteed to work as they haven't been tested. \ No newline at end of file + - This example is compatible with Node 18.0. Other versions may work but are not guaranteed to work as they haven't been tested. \ No newline at end of file diff --git a/node/wipe_appwrite_collection/index.js b/node/wipe_appwrite_collection/index.js index f98f23d4..6509aa40 100644 --- a/node/wipe_appwrite_collection/index.js +++ b/node/wipe_appwrite_collection/index.js @@ -1,39 +1,46 @@ const sdk = require('node-appwrite'); module.exports = async function (req, res) { - - // Init SDK 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: "Project details not found."}); + 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"]) - ; + .setKey(req.variables["APPWRITE_FUNCTION_API_KEY"]); try{ - const payload = JSON.parse(req.payload); - if(!payload.databaseId || !payload.collectionId){ - res.json({success: false, message: "Problem with payload."}); + const payload = JSON.parse(req.payload ?? '{}'); + if(!payload.databaseId || !payload.collectionId) { + res.json({success: false, message: "Invalid payload."}); + return; } - const documentListPayload = await databases.listDocuments(payload.databaseId, payload.collectionId); - const documentIdList = documentListPayload?.documents?.map(doc => doc['$id']) ?? []; - if(!documentIdList.length){ - res.json({success: false, message: "Collection is empty."}); + + 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; + } } - documentIdList.forEach(async (docId) => { - await databases.deleteDocument(payload.databaseId, payload.collectionId, docId); - }); - res.send({success: true}); + + res.send({success: true, sum: sum}); } catch(e) { - res.json({success: false, message: "Collection not found."}); + res.json({success: false, message: "Unexpected error: " + e}); } };