Commit | Line | Data |
---|---|---|
5c38ecb0 JB |
1 | #!/usr/bin/env node |
2 | ||
66a7748d | 3 | const fs = require('node:fs') |
e8bdfa71 | 4 | |
66a7748d | 5 | const { MongoClient } = require('mongodb') |
8114d10e | 6 | |
e8bdfa71 J |
7 | // This script deletes charging stations |
8 | // Filter charging stations by id pattern | |
9 | ||
7f774a55 | 10 | // Use Case: e-mobility-charging-stations-simulator creates thousands of charging stations, which are not longer needed. |
e8bdfa71 J |
11 | // Delete these charging stations all at once |
12 | ||
13 | // Config | |
66a7748d | 14 | const config = JSON.parse(fs.readFileSync('scriptConfig.json', 'utf8')) |
e8bdfa71 J |
15 | |
16 | // Mongo Connection and Query | |
acd36c7a | 17 | if (config?.mongoConnectionString) { |
66a7748d JB |
18 | // eslint-disable-next-line n/handle-callback-err |
19 | MongoClient.connect(config.mongoConnectionString, async function (_err, client) { | |
20 | const db = client.db() | |
e8bdfa71 J |
21 | |
22 | for await (const tenantID of config.tenantIDs) { | |
e7aeea18 | 23 | const response = await db |
14ecae6a | 24 | .collection(`${tenantID}.chargingstations`) |
66a7748d | 25 | .deleteMany({ _id: { $regex: config.idPattern } }) |
32de5a57 | 26 | console.info( |
e7aeea18 JB |
27 | response.deletedCount, |
28 | `Charging Stations with id = %${config.idPattern}% deleted. TenantID =`, | |
66a7748d JB |
29 | tenantID |
30 | ) | |
e8bdfa71 | 31 | } |
66a7748d JB |
32 | client.close() |
33 | }) | |
e8bdfa71 | 34 | } |