revises hook arrays to run synchronously where necessary
This commit is contained in:
@@ -28,15 +28,17 @@ const register = async (args) => {
|
||||
}
|
||||
|
||||
// /////////////////////////////////////
|
||||
// 2. Execute before register hook
|
||||
// 2. Execute before create hook
|
||||
// /////////////////////////////////////
|
||||
|
||||
collectionConfig.hooks.beforeRegister.forEach(async (hook) => {
|
||||
await collectionConfig.hooks.beforeCreate.reduce(async (priorHook, hook) => {
|
||||
await priorHook;
|
||||
|
||||
data = (await hook({
|
||||
data,
|
||||
req,
|
||||
})) || data;
|
||||
});
|
||||
}, Promise.resolve());
|
||||
|
||||
// /////////////////////////////////////
|
||||
// 3. Execute field-level hooks, access, and validation
|
||||
@@ -82,15 +84,17 @@ const register = async (args) => {
|
||||
});
|
||||
|
||||
// /////////////////////////////////////
|
||||
// 8. Execute after register hook
|
||||
// 8. Execute after create hook
|
||||
// /////////////////////////////////////
|
||||
|
||||
collectionConfig.hooks.afterRegister.forEach(async (hook) => {
|
||||
collectionConfig.hooks.afterCreate.reduce(async (priorHook, hook) => {
|
||||
await priorHook;
|
||||
|
||||
result = await hook({
|
||||
doc: result,
|
||||
req: args.req,
|
||||
}) || result;
|
||||
});
|
||||
}, Promise.resolve());
|
||||
|
||||
// /////////////////////////////////////
|
||||
// 9. Return user
|
||||
|
||||
@@ -56,13 +56,15 @@ const update = async (args) => {
|
||||
// 2. Execute before update hook
|
||||
// /////////////////////////////////////
|
||||
|
||||
collectionConfig.hooks.beforeUpdate.forEach(async (hook) => {
|
||||
await collectionConfig.hooks.beforeUpdate.reduce(async (priorHook, hook) => {
|
||||
await priorHook;
|
||||
|
||||
data = (await hook({
|
||||
data,
|
||||
req,
|
||||
originalDoc,
|
||||
})) || data;
|
||||
});
|
||||
}, Promise.resolve());
|
||||
|
||||
// /////////////////////////////////////
|
||||
// 3. Merge updates into existing data
|
||||
@@ -119,12 +121,14 @@ const update = async (args) => {
|
||||
// 8. Execute after update hook
|
||||
// /////////////////////////////////////
|
||||
|
||||
collectionConfig.hooks.afterUpdate.forEach(async (hook) => {
|
||||
collectionConfig.hooks.afterUpdate.reduce(async (priorHook, hook) => {
|
||||
await priorHook;
|
||||
|
||||
user = await hook({
|
||||
doc: user,
|
||||
req,
|
||||
}) || user;
|
||||
});
|
||||
}, Promise.resolve());
|
||||
|
||||
// /////////////////////////////////////
|
||||
// 9. Return user
|
||||
|
||||
@@ -36,12 +36,14 @@ const create = async (args) => {
|
||||
// 2. Execute before collection hook
|
||||
// /////////////////////////////////////
|
||||
|
||||
collectionConfig.hooks.beforeCreate.forEach(async (hook) => {
|
||||
collectionConfig.hooks.beforeCreate.reduce(async (priorHook, hook) => {
|
||||
await priorHook;
|
||||
|
||||
data = (await hook({
|
||||
data,
|
||||
req,
|
||||
})) || data;
|
||||
});
|
||||
}, Promise.resolve());
|
||||
|
||||
// /////////////////////////////////////
|
||||
// 3. Execute field-level access, hooks, and validation
|
||||
@@ -123,12 +125,14 @@ const create = async (args) => {
|
||||
// 7. Execute after collection hook
|
||||
// /////////////////////////////////////
|
||||
|
||||
collectionConfig.hooks.afterCreate.forEach(async (hook) => {
|
||||
collectionConfig.hooks.afterCreate.reduce(async (priorHook, hook) => {
|
||||
await priorHook;
|
||||
|
||||
result = await hook({
|
||||
doc: result,
|
||||
req: args.req,
|
||||
}) || result;
|
||||
});
|
||||
}, Promise.resolve());
|
||||
|
||||
// /////////////////////////////////////
|
||||
// 8. Return results
|
||||
|
||||
@@ -89,9 +89,11 @@ const deleteQuery = async (args) => {
|
||||
// 4. Execute after collection hook
|
||||
// /////////////////////////////////////
|
||||
|
||||
collectionConfig.hooks.afterDelete.forEach(async (hook) => {
|
||||
collectionConfig.hooks.afterDelete.reduce(async (priorHook, hook) => {
|
||||
await priorHook;
|
||||
|
||||
result = await hook({ req, id, doc: result }) || result;
|
||||
});
|
||||
}, Promise.resolve());
|
||||
|
||||
// /////////////////////////////////////
|
||||
// 5. Return results
|
||||
|
||||
@@ -125,15 +125,13 @@ const find = async (args) => {
|
||||
...result,
|
||||
docs: await Promise.all(result.docs.map(async (doc) => {
|
||||
let docRef = doc;
|
||||
const afterReadHooks = [];
|
||||
|
||||
collectionConfig.hooks.afterRead.forEach((hook) => {
|
||||
afterReadHooks.push(async () => {
|
||||
docRef = await hook({ req, query, doc }) || doc;
|
||||
});
|
||||
});
|
||||
collectionConfig.hooks.afterRead.reduce(async (priorHook, hook) => {
|
||||
await priorHook;
|
||||
|
||||
docRef = await hook({ req, query, doc }) || doc;
|
||||
}, Promise.resolve());
|
||||
|
||||
await Promise.all(afterReadHooks);
|
||||
return docRef;
|
||||
})),
|
||||
};
|
||||
|
||||
@@ -98,13 +98,15 @@ const findByID = async (args) => {
|
||||
// 5. Execute after collection hook
|
||||
// /////////////////////////////////////
|
||||
|
||||
collectionConfig.hooks.afterRead.forEach(async (hook) => {
|
||||
collectionConfig.hooks.afterRead.reduce(async (priorHook, hook) => {
|
||||
await priorHook;
|
||||
|
||||
result = await hook({
|
||||
req,
|
||||
query,
|
||||
doc: result,
|
||||
}) || result;
|
||||
});
|
||||
}, Promise.resolve());
|
||||
|
||||
// /////////////////////////////////////
|
||||
// 6. Return results
|
||||
|
||||
@@ -70,13 +70,15 @@ const update = async (args) => {
|
||||
|
||||
let { data } = args;
|
||||
|
||||
collectionConfig.hooks.beforeUpdate.forEach(async (hook) => {
|
||||
await collectionConfig.hooks.beforeUpdate.reduce(async (priorHook, hook) => {
|
||||
await priorHook;
|
||||
|
||||
data = (await hook({
|
||||
data,
|
||||
req,
|
||||
originalDoc,
|
||||
})) || data;
|
||||
});
|
||||
}, Promise.resolve());
|
||||
|
||||
// /////////////////////////////////////
|
||||
// 3. Merge updates into existing data
|
||||
@@ -162,12 +164,14 @@ const update = async (args) => {
|
||||
// 8. Execute after collection hook
|
||||
// /////////////////////////////////////
|
||||
|
||||
collectionConfig.hooks.afterUpdate.forEach(async (hook) => {
|
||||
collectionConfig.hooks.afterUpdate.reduce(async (priorHook, hook) => {
|
||||
await priorHook;
|
||||
|
||||
doc = await hook({
|
||||
doc,
|
||||
req,
|
||||
}) || doc;
|
||||
});
|
||||
}, Promise.resolve());
|
||||
|
||||
// /////////////////////////////////////
|
||||
// 9. Return updated document
|
||||
|
||||
@@ -219,8 +219,6 @@ const sanitizeCollection = (collections, collection) => {
|
||||
if (collection.auth) {
|
||||
if (!sanitized.hooks.beforeLogin) sanitized.hooks.beforeLogin = [];
|
||||
if (!sanitized.hooks.afterLogin) sanitized.hooks.afterLogin = [];
|
||||
if (!sanitized.hooks.beforeRegister) sanitized.hooks.beforeRegister = [];
|
||||
if (!sanitized.hooks.afterRegister) sanitized.hooks.afterRegister = [];
|
||||
|
||||
let authFields = baseAuthFields;
|
||||
|
||||
|
||||
@@ -77,12 +77,14 @@ const findOne = async (args) => {
|
||||
// 5. Execute after collection hook
|
||||
// /////////////////////////////////////
|
||||
|
||||
globalConfig.hooks.afterRead.forEach(async (hook) => {
|
||||
globalConfig.hooks.afterRead.reduce(async (priorHook, hook) => {
|
||||
await priorHook;
|
||||
|
||||
doc = await hook({
|
||||
req,
|
||||
doc,
|
||||
}) || doc;
|
||||
});
|
||||
}, Promise.resolve());
|
||||
|
||||
// /////////////////////////////////////
|
||||
// 6. Return results
|
||||
|
||||
@@ -44,13 +44,15 @@ const update = async (args) => {
|
||||
|
||||
let { data } = args;
|
||||
|
||||
globalConfig.hooks.beforeUpdate.forEach(async (hook) => {
|
||||
await globalConfig.hooks.beforeUpdate.reduce(async (priorHook, hook) => {
|
||||
await priorHook;
|
||||
|
||||
data = (await hook({
|
||||
data,
|
||||
req,
|
||||
originalDoc: global,
|
||||
})) || data;
|
||||
});
|
||||
}, Promise.resolve());
|
||||
|
||||
// /////////////////////////////////////
|
||||
// 4. Merge updates into existing data
|
||||
@@ -95,12 +97,14 @@ const update = async (args) => {
|
||||
// 8. Execute after global hook
|
||||
// /////////////////////////////////////
|
||||
|
||||
globalConfig.hooks.afterUpdate.forEach(async (hook) => {
|
||||
globalConfig.hooks.afterUpdate.reduce(async (priorHook, hook) => {
|
||||
await priorHook;
|
||||
|
||||
global = await hook({
|
||||
doc: global,
|
||||
req,
|
||||
}) || global;
|
||||
});
|
||||
}, Promise.resolve());
|
||||
|
||||
// /////////////////////////////////////
|
||||
// 9. Return global
|
||||
|
||||
Reference in New Issue
Block a user