From 2a7f4eb88b17501491b2a33e00ae0962e1413ac2 Mon Sep 17 00:00:00 2001 From: Chris Steele Date: Tue, 15 Oct 2024 11:35:42 +0100 Subject: [PATCH] Wait for post insert/update/delete hooks to complete --- lib/AbstractApiModule.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/AbstractApiModule.js b/lib/AbstractApiModule.js index 13de901..0934ce2 100644 --- a/lib/AbstractApiModule.js +++ b/lib/AbstractApiModule.js @@ -540,7 +540,7 @@ class AbstractApiModule extends AbstractModule { const mongodb = await this.app.waitForModule('mongodb') const results = await mongodb.insert(options.collectionName, data, mongoOptions) - if (options.invokePostHook) this.postInsertHook.invoke(results) + if (options.invokePostHook) await this.postInsertHook.invoke(results) return results } @@ -585,7 +585,7 @@ class AbstractApiModule extends AbstractModule { }, options) const results = await mongodb.update(options.collectionName, query, formattedData, mongoOptions) - if (options.invokePostHook) this.postUpdateHook.invoke(originalDoc, results) + if (options.invokePostHook) await this.postUpdateHook.invoke(originalDoc, results) return results } @@ -607,7 +607,7 @@ class AbstractApiModule extends AbstractModule { } await this.preDeleteHook.invoke(originalDoc, options, mongoOptions) await mongodb.delete(options.collectionName, query, mongoOptions) - if (options.invokePostHook) this.postDeleteHook.invoke(originalDoc) + if (options.invokePostHook) await this.postDeleteHook.invoke(originalDoc) return originalDoc } @@ -625,7 +625,7 @@ class AbstractApiModule extends AbstractModule { const toDelete = await mongodb.find(options.collectionName, query) await mongodb.deleteMany(options.collectionName, query, mongoOptions) - if (options.invokePostHook) toDelete.forEach(d => this.postDeleteHook.invoke(d)) + if (options.invokePostHook) await Promise.all(toDelete.map(d => this.postDeleteHook.invoke(d))) return toDelete } }