forked from hungpv-3077/delete_cosmos_db_records
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
55 lines (43 loc) · 1.26 KB
/
index.js
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
45
46
47
48
49
50
51
52
53
54
55
const { CosmosClient } = require("@azure/cosmos");
require("dotenv").config();
const endpoint = process.env.END_POINT;
const key = process.env.KEY;
const databaseName = process.env.DATABASE_NAME;
const containerName = process.env.CONTAINER_NAME;
const client = new CosmosClient({ endpoint, key });
const limit = 100;
var sum = 0;
async function main() {
const { database } = await client.databases.createIfNotExists({
id: databaseName,
});
const { container } = await database.containers.createIfNotExists({
id: containerName,
});
let times = 1;
while (true) {
console.log(`* Times: ${times}`);
const numberResource = await loop(container);
times++;
if (!numberResource) {
break;
}
}
console.log(`* Total: ${sum}`);
}
async function loop(container) {
const { resources } = await container.items
.query(`SELECT * from c OFFSET 0 LIMIT ${limit}`)
.fetchAll();
await deleteBatch(container, resources);
return resources.length;
}
async function deleteBatch(container, resources) {
const promises = resources.map((resource) =>
container.item(resource.id, resource.partitionKey).delete()
);
await Promise.all(promises);
console.log(`Deleted ${resources.length} items\r\n`);
sum += resources.length;
}
main();