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.
- Loading branch information
Showing
2 changed files
with
39 additions
and
25 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
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 |
---|---|---|
@@ -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}); | ||
} | ||
|
||
}; |