Skip to content

Commit

Permalink
PR review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Meldiron committed Jan 23, 2023
1 parent 8b672df commit eb44ffd
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 25 deletions.
19 changes: 13 additions & 6 deletions node/wipe_appwrite_collection/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ _Example output (success):_

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

Expand Down Expand Up @@ -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: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

Expand All @@ -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.

Expand All @@ -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.
- This example is compatible with Node 18.0. Other versions may work but are not guaranteed to work as they haven't been tested.
45 changes: 26 additions & 19 deletions node/wipe_appwrite_collection/index.js
Original file line number Diff line number Diff line change
@@ -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});
}

};

0 comments on commit eb44ffd

Please sign in to comment.