Make scripts really node.js scripts
[e-mobility-charging-stations-simulator.git] / src / scripts / deleteChargingStations.js
CommitLineData
5c38ecb0
JB
1#!/usr/bin/env node
2
5fdab605
JB
3import MongoClient from 'mongodb';
4import fs from 'fs';
e8bdfa71
J
5
6// This script deletes charging stations
7// Filter charging stations by id pattern
8
9// Use Case: ev-simulator creates thousands of charging stations, which are not longer needed.
10// Delete these charging stations all at once
11
12// Config
5fdab605 13const config = JSON.parse(fs.readFileSync('scriptConfig.json', 'utf8'));
e8bdfa71
J
14
15// Mongo Connection and Query
36a16ec2 16if (config?.mongoConnectionString) {
e8bdfa71
J
17 MongoClient.connect(config.mongoConnectionString, {
18 useUnifiedTopology: true,
19 useNewUrlParser: true
5fdab605 20 }, async function(err, client) {
e8bdfa71
J
21 const db = client.db('evse');
22
23 for await (const tenantID of config.tenantIDs) {
5fdab605
JB
24 const response = await db.collection(tenantID + '.chargingstations').deleteMany(
25 { _id: { '$regex': config.idPattern } }
e8bdfa71
J
26 );
27 console.log(response.deletedCount, `Charging Stations with id = %${config.idPattern}% deleted. TenantID =`, tenantID);
28 }
29 client.close();
30 });
31}