Skip to content

Commit

Permalink
add recursive for deletemany when using ids
Browse files Browse the repository at this point in the history
  • Loading branch information
skadefro committed Oct 8, 2022
1 parent bf93c8b commit 532da3f
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 12 deletions.
11 changes: 8 additions & 3 deletions OpenFlow/src/DatabaseConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2964,7 +2964,7 @@ export class DatabaseConnection extends events.EventEmitter {
for (let i = 0; i < collections.length; i++) {
let collection = collections[i];
// var res = await this.DeleteMany(query, null, collection.name, null, jwt, span);
var res = await this.DeleteMany({}, null, collection.name, doc._id, jwt, span);
var res = await this.DeleteMany({}, null, collection.name, doc._id, false, jwt, span);
Logger.instanse.info("DatabaseConnection", "DeleteOne", "[" + user.username + "][" + collection.name + "] Deleted " + res + " items from " + collection.name + " cleaning up after company " + doc.name);
}
// }
Expand Down Expand Up @@ -3049,7 +3049,7 @@ export class DatabaseConnection extends events.EventEmitter {
continue;
}
let startTime = new Date();
var res = await this.DeleteMany({ "$or": [{ "_createdbyid": doc._id }, { "_modifiedbyid": doc._id }] }, null, collection.name, doc._id, jwt, span);
var res = await this.DeleteMany({ "$or": [{ "_createdbyid": doc._id }, { "_modifiedbyid": doc._id }] }, null, collection.name, doc._id, false, jwt, span);
// @ts-ignore
var timeDiff = ((new Date()) - startTime); //in ms
Logger.instanse.info("DatabaseConnection", "DeleteOne", "[" + user.username + "][" + collection.name + "] Deleted " + res + " items from " + collection.name + " cleaning up after user " + doc.name + " (" + timeDiff + "ms)");
Expand Down Expand Up @@ -3108,7 +3108,7 @@ export class DatabaseConnection extends events.EventEmitter {
* @param {string} jwt JWT of user who is doing the delete, ensuring rights
* @returns Promise<void>
*/
async DeleteMany(query: string | any, ids: string[], collectionname: string, queryas: string, jwt: string, parent: Span): Promise<number> {
async DeleteMany(query: string | any, ids: string[], collectionname: string, queryas: string, recursive: boolean, jwt: string, parent: Span): Promise<number> {
if (NoderedUtil.IsNullUndefinded(ids) && NoderedUtil.IsNullUndefinded(query)) { throw Error("id cannot be null"); }
const span: Span = Logger.otel.startSubSpan("db.DeleteMany", parent);
try {
Expand Down Expand Up @@ -3201,6 +3201,11 @@ export class DatabaseConnection extends events.EventEmitter {
}
Logger.instanse.verbose("DatabaseConnection", "DeleteMany", "[" + user.username + "][" + collectionname + "] deleted " + deletecounter + " files in database");
return deletecounter;
} else if (recursive && !NoderedUtil.IsNullUndefinded(ids) && ids.length > 0) {
for (let i = 0; i < ids.length; i++) {
await this.DeleteOne(ids[i], collectionname, recursive, jwt, span);
}
return ids.length;
} else {
let bulkInsert = this.db.collection(collectionname + "_hist").initializeUnorderedBulkOp();
let bulkRemove = this.db.collection(collectionname).initializeUnorderedBulkOp()
Expand Down
12 changes: 6 additions & 6 deletions OpenFlow/src/Messages/Message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1474,7 +1474,7 @@ export class Message {
try {
msg = DeleteManyMessage.assign(this.data);
if (NoderedUtil.IsNullEmpty(msg.jwt)) { msg.jwt = this.jwt; }
msg.affectedrows = await Config.db.DeleteMany(msg.query, msg.ids, msg.collectionname, null, msg.jwt, span);
msg.affectedrows = await Config.db.DeleteMany(msg.query, msg.ids, msg.collectionname, null, msg.recursive, msg.jwt, span);
delete msg.ids;
} catch (error) {
if (NoderedUtil.IsNullUndefinded(msg)) { (msg as any) = {}; }
Expand Down Expand Up @@ -5202,15 +5202,15 @@ export class Message {
msg.result = await Config.db._UpdateOne(null, wiq as any, "mq", 1, true, jwt, parent);

if (msg.purge) {
await Config.db.DeleteMany({ "_type": "workitem", "wiqid": wiq._id }, null, "workitems", null, jwt, parent);
await Config.db.DeleteMany({ "_type": "workitem", "wiqid": wiq._id }, null, "workitems", null, false, jwt, parent);
var items = await Config.db.query<WorkitemQueue>({ query: { "_type": "workitem", "wiqid": wiq._id }, collectionname: "workitems", top: 1, jwt }, parent);
if (items.length > 0) {
}
items = await Config.db.query<WorkitemQueue>({ query: { "_type": "workitem", "wiqid": wiq._id }, collectionname: "workitems", top: 1, jwt }, parent);
if (items.length > 0) {
throw new Error("Failed purging workitemqueue " + wiq.name);
}
await Config.db.DeleteMany({ "metadata.wiqid": wiq._id }, null, "fs.files", null, jwt, parent);
await Config.db.DeleteMany({ "metadata.wiqid": wiq._id }, null, "fs.files", null, false, jwt, parent);
}
} catch (error) {
await handleError(null, error);
Expand Down Expand Up @@ -5249,15 +5249,15 @@ export class Message {
user = this.tuser;

if (msg.purge) {
await Config.db.DeleteMany({ "_type": "workitem", "wiqid": wiq._id }, null, "workitems", null, jwt, parent);
await Config.db.DeleteMany({ "_type": "workitem", "wiqid": wiq._id }, null, "workitems", null, false, jwt, parent);
var items = await Config.db.query<WorkitemQueue>({ query: { "_type": "workitem", "wiqid": wiq._id }, collectionname: "workitems", top: 1, jwt }, parent);
if (items.length > 0) {
items = await Config.db.query<WorkitemQueue>({ query: { "_type": "workitem", "wiqid": wiq._id }, collectionname: "workitems", top: 1, jwt }, parent);
}
if (items.length > 0) {
throw new Error("Failed purging workitemqueue " + wiq.name);
}
await Config.db.DeleteMany({ "metadata.wiqid": wiq._id }, null, "fs.files", null, jwt, parent);
await Config.db.DeleteMany({ "metadata.wiqid": wiq._id }, null, "fs.files", null, false, jwt, parent);
} else {
var items = await Config.db.query<WorkitemQueue>({ query: { "_type": "workitem", "wiqid": wiq._id }, collectionname: "workitems", top: 1, jwt }, parent);
if (items.length > 0) {
Expand Down Expand Up @@ -5296,7 +5296,7 @@ export class Message {
switch (msg.command) {
case "dumpwebsocketclients":
if (!this.tuser.HasRoleId(WellknownIds.admins)) throw new Error("Access denied");
await Config.db.DeleteMany({ "_type": "websocketclient" }, null, "websocketclients", null, jwt, parent);
await Config.db.DeleteMany({ "_type": "websocketclient" }, null, "websocketclients", null, false, jwt, parent);
amqpwrapper.Instance().send("openflow", "", { "command": "dumpwebsocketclients" }, 10000, null, "", 1);
break;
case "killwebsocketclient":
Expand Down
2 changes: 1 addition & 1 deletion OpenFlow/src/OAuthProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ export class Account {
return token.item;
}
static async RemoveTokenRequest(code: string, parent: Span) {
let tokens = await Config.db.DeleteMany({ _type: "tokenrequest", "code": code }, null, "oauthtokens", null, Crypt.rootToken(), parent);
let tokens = await Config.db.DeleteMany({ _type: "tokenrequest", "code": code }, null, "oauthtokens", null, false, Crypt.rootToken(), parent);
return tokens[0];
}
}
2 changes: 1 addition & 1 deletion OpenFlowNodeRED/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
},
"dependencies": {
"@nodemailer/mailparser2": "^1.0.3",
"@openiap/openflow-api": "^2.1.1",
"@openiap/openflow-api": "^2.1.2",
"@opentelemetry/api-metrics": "^0.32.0",
"@opentelemetry/exporter-metrics-otlp-grpc": "^0.32.0",
"@opentelemetry/exporter-trace-otlp-grpc": "^0.32.0",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"dependencies": {
"@fortawesome/fontawesome-free": "^5.15.3",
"@kubernetes/client-node": "0.17.0",
"@openiap/openflow-api": "^2.1.1",
"@openiap/openflow-api": "^2.1.2",
"@opentelemetry/api-metrics": "^0.32.0",
"@opentelemetry/exporter-metrics-otlp-grpc": "^0.32.0",
"@opentelemetry/exporter-trace-otlp-grpc": "^0.32.0",
Expand Down

0 comments on commit 532da3f

Please sign in to comment.