diff --git a/packages/orama/src/methods/insert.ts b/packages/orama/src/methods/insert.ts index 1c69763bf..a64a06178 100644 --- a/packages/orama/src/methods/insert.ts +++ b/packages/orama/src/methods/insert.ts @@ -24,6 +24,8 @@ export function insert( } const asyncNeeded = + isAsyncFunction(orama.beforeInsert) || + isAsyncFunction(orama.afterInsert) || isAsyncFunction(orama.index.beforeInsert) || isAsyncFunction(orama.index.insert) || isAsyncFunction(orama.index.afterInsert) @@ -402,7 +404,14 @@ export function innerInsertMultiple( skipHooks?: boolean, timeout?: number ): Promise | string[] { - if (isAsyncFunction(runMultipleHook)) { + const asyncNeeded = + isAsyncFunction(orama.beforeInsert) || + isAsyncFunction(orama.afterInsert) || + isAsyncFunction(orama.index.beforeInsert) || + isAsyncFunction(orama.index.insert) || + isAsyncFunction(orama.index.afterInsert) + + if (asyncNeeded) { return innerInsertMultipleAsync(orama, docs, batchSize, language, skipHooks, timeout) } diff --git a/packages/orama/src/methods/update.ts b/packages/orama/src/methods/update.ts index edfd31861..dc63f1835 100644 --- a/packages/orama/src/methods/update.ts +++ b/packages/orama/src/methods/update.ts @@ -85,8 +85,8 @@ export function updateMultiple( isAsyncFunction(orama.beforeUpdate) || isAsyncFunction(orama.afterUpdate) || isAsyncFunction(orama.beforeUpdateMultiple) || - isAsyncFunction(orama.afterUpdateMultiple) - isAsyncFunction(orama.beforeRemoveMultiple) || + isAsyncFunction(orama.afterUpdateMultiple) || + isAsyncFunction(orama.beforeRemoveMultiple) || isAsyncFunction(orama.afterRemoveMultiple) || isAsyncFunction(orama.beforeInsertMultiple) || isAsyncFunction(orama.afterInsertMultiple) diff --git a/packages/orama/src/utils.ts b/packages/orama/src/utils.ts index b41a3edf0..39ed20350 100644 --- a/packages/orama/src/utils.ts +++ b/packages/orama/src/utils.ts @@ -338,7 +338,20 @@ export function isPromise(obj: any): obj is Promise { return !!obj && (typeof obj === 'object' || typeof obj === 'function') && typeof obj.then === 'function' } +/** + * Checks if the provided input is an async function or if the input is an array + * containing at least one async function. + * + * @param func - A single function or an array of functions to check. + * Non-function values are ignored. + * @returns `true` if the input is an async function or an array containing at least + * one async function, otherwise `false`. + */ export function isAsyncFunction(func: any): boolean { + if (Array.isArray(func)) { + return func.some(item => isAsyncFunction(item)); + } + return func?.constructor?.name === 'AsyncFunction' }