add script to delete message ids from unlinked channels from db

This commit is contained in:
JandereDev 2022-05-26 18:04:42 +02:00
parent d51b7f2807
commit 800b9ed492
No known key found for this signature in database
GPG key ID: 5D5E18ACB990F57A

View file

@ -0,0 +1,50 @@
/**
* When executed, this file scans the entire `messages` database collection
* and deletes entries belonging to channels which do no longer have a bridge
* configuration associated. Reads mongo URI from $DB_STRING env var.
*/
import Mongo from 'mongodb';
if (!process.env.DB_STRING) {
console.error('$DB_STRING not provided.');
process.exit(1);
}
const mongo = new Mongo.MongoClient(process.env.DB_STRING);
(async () => {
await mongo.connect();
const client = mongo.db();
const messages = client.collection('bridged_messages');
const res = messages.aggregate([{
$lookup: {
from: 'bridge_config',
localField: 'channels.discord',
foreignField: 'discord',
as: 'bridgeConfig',
}
}]);
let buf: string[] = [];
const execute = async () => {
const ids = [ ...buf ];
buf.length = 0;
if (ids.length) {
console.log('Deleting ' + ids.length + ' entries');
await messages.deleteMany({ _id: { $in: ids } });
}
}
res.on('data', data => {
if (!data.bridgeConfig?.length) buf.push(data._id);
if (buf.length >= 500) execute();
});
res.on('end', () => {
execute().then(() => process.exit(0));
});
})();