From ce3d540633c6616c6f8cec94efb7d910cebd7032 Mon Sep 17 00:00:00 2001 From: Isaac Abadi Date: Fri, 13 Aug 2021 19:40:06 -0600 Subject: [PATCH] Forces file registration to avoid registering a file that already exists in an atomic fasion --- backend/db.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/backend/db.js b/backend/db.js index 802694d..9442150 100644 --- a/backend/db.js +++ b/backend/db.js @@ -623,7 +623,22 @@ exports.insertRecordIntoTable = async (table, doc, replaceFilter = null) => { return true; } - if (replaceFilter) await database.collection(table).deleteMany(replaceFilter); + if (replaceFilter) { + const output = await database.collection(table).bulkWrite([ + { + deleteMany: { + filter: replaceFilter + } + }, + { + insertOne: { + document: doc + } + } + ]); + logger.debug(`Inserted doc into ${table} with filter: ${JSON.stringify(replaceFilter)}`); + return !!(output['result']['ok']); + } const output = await database.collection(table).insertOne(doc); logger.debug(`Inserted doc into ${table}`);