-
Notifications
You must be signed in to change notification settings - Fork 2
/
spDeleteLogItems.txt
44 lines (44 loc) · 1.61 KB
/
spDeleteLogItems.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
function spDeleteLogItems(query) {
var collection = getContext().getCollection();
var collectionLink = collection.getSelfLink();
var response = getContext().getResponse();
var responseBody = {
deleted: 0,
continuation: true
};
if (!query) throw new Error("The query is undefined or null.");
tryQueryAndDelete();
function tryQueryAndDelete(continuation) {
var requestOptions = {continuation: continuation};
var isAccepted = collection.queryDocuments(collectionLink, query, {}, function (err, retrievedDocs, responseOptions) {
if (err) throw err;
if (retrievedDocs.length > 0) {
console.log(2);
tryDelete(retrievedDocs);
} else if (responseOptions.continuation) {
tryQueryAndDelete(responseOptions.continuation);
} else {
responseBody.continuation = false;
response.setBody(responseBody);
}
});
if (!isAccepted) {
response.setBody(responseBody);
}
}
function tryDelete(documents) {
if (documents.length > 0) {
var isAccepted = collection.deleteDocument(documents[0]._self, {}, function (err, responseOptions) {
if (err) throw err;
responseBody.deleted++;
documents.shift();
tryDelete(documents);
});
if (!isAccepted) {
response.setBody(responseBody);
}
} else {
tryQueryAndDelete();
}
}
}