WIP updating hook calls
This commit is contained in:
@@ -22,7 +22,7 @@ const create = async (args) => {
|
||||
// 3. Execute before create field-level hooks
|
||||
// /////////////////////////////////////
|
||||
|
||||
options.data = await executeFieldHooks(args.config.fields, args.data, 'beforeCreate');
|
||||
options.data = await executeFieldHooks(args.config.fields, args.data, 'beforeCreate', args.data);
|
||||
|
||||
// /////////////////////////////////////
|
||||
// 4. Execute before collection hook
|
||||
|
||||
@@ -77,8 +77,7 @@ const find = async (args) => {
|
||||
if (locale && doc.setLocale) {
|
||||
doc.setLocale(locale, fallbackLocale);
|
||||
}
|
||||
const hookedDoc = await executeFieldHooks(args.config.fields, doc, 'afterRead');
|
||||
return hookedDoc;
|
||||
return executeFieldHooks(options, args.config.fields, doc, 'afterRead', doc);
|
||||
})),
|
||||
};
|
||||
|
||||
@@ -89,18 +88,23 @@ const find = async (args) => {
|
||||
const { afterRead } = args.config.hooks;
|
||||
|
||||
if (typeof afterRead === 'function') {
|
||||
result = await afterRead(options, result);
|
||||
result = {
|
||||
...result,
|
||||
docs: await Promise.all(result.docs.map(async (doc) => {
|
||||
const json = doc.toJSON({ virtuals: true });
|
||||
return afterRead({
|
||||
options,
|
||||
doc,
|
||||
json,
|
||||
}) || json;
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
// /////////////////////////////////////
|
||||
// 6. Return results
|
||||
// /////////////////////////////////////
|
||||
|
||||
result = {
|
||||
...result,
|
||||
docs: await Promise.all(result.docs.map(async doc => doc.toJSON({ virtuals: true }))),
|
||||
};
|
||||
|
||||
return result;
|
||||
} catch (err) {
|
||||
throw err;
|
||||
|
||||
@@ -67,13 +67,13 @@ const findByID = async (args) => {
|
||||
result.setLocale(locale, fallbackLocale);
|
||||
}
|
||||
|
||||
result = result.toJSON({ virtuals: true });
|
||||
let json = result.toJSON({ virtuals: true });
|
||||
|
||||
// /////////////////////////////////////
|
||||
// 4. Execute after collection field-level hooks
|
||||
// /////////////////////////////////////
|
||||
|
||||
result = await executeFieldHooks(args.config.fields, result, 'afterRead');
|
||||
result = await executeFieldHooks(options, options.config.fields, result, 'afterRead', result);
|
||||
|
||||
// /////////////////////////////////////
|
||||
// 5. Execute after collection hook
|
||||
@@ -82,14 +82,18 @@ const findByID = async (args) => {
|
||||
const { afterRead } = args.config.hooks;
|
||||
|
||||
if (typeof afterRead === 'function') {
|
||||
result = await afterRead(options, result);
|
||||
json = await afterRead({
|
||||
...options,
|
||||
result,
|
||||
json,
|
||||
}) || json;
|
||||
}
|
||||
|
||||
// /////////////////////////////////////
|
||||
// 6. Return results
|
||||
// /////////////////////////////////////
|
||||
|
||||
return result;
|
||||
return json;
|
||||
} catch (err) {
|
||||
throw err;
|
||||
}
|
||||
|
||||
@@ -1,20 +1,23 @@
|
||||
const executeFieldHooks = async (fields, data, hookName) => {
|
||||
const executeFieldHooks = async (operation, fields, value, hookName, data = null) => {
|
||||
const fullData = data || value;
|
||||
if (Array.isArray(data)) {
|
||||
const postHookData = await Promise.all(data.map(async (row) => {
|
||||
const rowData = await executeFieldHooks(fields, row, hookName);
|
||||
return rowData;
|
||||
return Promise.all(data.map(async (row) => {
|
||||
return executeFieldHooks(operation, fields, fullData, row, hookName);
|
||||
}));
|
||||
|
||||
return postHookData;
|
||||
}
|
||||
|
||||
const postHookData = { ...data };
|
||||
const postHookData = Object.create(fullData);
|
||||
|
||||
const hookPromises = [];
|
||||
|
||||
fields.forEach((field) => {
|
||||
if (typeof field.hooks[hookName] === 'function' && data[field.name]) {
|
||||
const hookPromise = async () => {
|
||||
postHookData[field.name] = await field.hooks[hookName](data[field.name]);
|
||||
postHookData[field.name] = await field.hooks[hookName]({
|
||||
...operation,
|
||||
data: fullData,
|
||||
value: data[field.name],
|
||||
});
|
||||
};
|
||||
|
||||
hookPromises.push(hookPromise());
|
||||
@@ -22,7 +25,7 @@ const executeFieldHooks = async (fields, data, hookName) => {
|
||||
|
||||
if (field.fields && data[field.name]) {
|
||||
const hookPromise = async () => {
|
||||
postHookData[field.name] = await executeFieldHooks(field.fields, data[field.name], hookName);
|
||||
postHookData[field.name] = await executeFieldHooks(operation, field.fields, fullData, hookName, data[field.name]);
|
||||
};
|
||||
|
||||
hookPromises.push(hookPromise());
|
||||
|
||||
@@ -62,7 +62,6 @@ const register = async (args) => {
|
||||
Object.assign(user, modelData);
|
||||
|
||||
let result = await Model.register(user, data.password);
|
||||
result = result.toJSON({ virtuals: true });
|
||||
|
||||
await passport.authenticate('local');
|
||||
|
||||
@@ -79,8 +78,7 @@ const register = async (args) => {
|
||||
// /////////////////////////////////////
|
||||
// 7. Return user
|
||||
// /////////////////////////////////////
|
||||
|
||||
return result;
|
||||
return result.toJSON({ virtuals: true });
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user