mirror of
https://github.com/vale981/Vulcan
synced 2025-03-04 17:21:37 -05:00
Refactor and clean up mutators some more
This commit is contained in:
parent
602aff7820
commit
595ed5a545
1 changed files with 338 additions and 134 deletions
|
@ -28,7 +28,12 @@ to the client.
|
|||
*/
|
||||
|
||||
import { runCallbacks, runCallbacksAsync } from '../modules/index.js';
|
||||
import { validateDocument, validateData, dataToModifier, modifierToData } from '../modules/validation.js';
|
||||
import {
|
||||
validateDocument,
|
||||
validateData,
|
||||
dataToModifier,
|
||||
modifierToData,
|
||||
} from '../modules/validation.js';
|
||||
import { registerSetting } from '../modules/settings.js';
|
||||
import { debug, debugGroup, debugGroupEnd } from '../modules/debug.js';
|
||||
import { throwError } from '../modules/errors.js';
|
||||
|
@ -39,49 +44,70 @@ import isEmpty from 'lodash/isEmpty';
|
|||
|
||||
registerSetting('database', 'mongo', 'Which database to use for your back-end');
|
||||
|
||||
export const createMutator = async ({ collection, document, data, currentUser, validate, context }) => {
|
||||
export const createMutator = async ({
|
||||
collection,
|
||||
document,
|
||||
data,
|
||||
currentUser,
|
||||
validate,
|
||||
context,
|
||||
}) => {
|
||||
// OpenCRUD backwards compatibility: accept either data or document
|
||||
// we don't want to modify the original document
|
||||
document = data || document;
|
||||
|
||||
const { collectionName, typeName } = collection.options;
|
||||
|
||||
debug('');
|
||||
debugGroup(`--------------- start \x1b[36m${collectionName} Create Mutator\x1b[0m ---------------`);
|
||||
debug(`validate: ${validate}`);
|
||||
debug(document || data);
|
||||
|
||||
// we don't want to modify the original document
|
||||
let newDocument = Object.assign({}, document || data);
|
||||
|
||||
const schema = collection.simpleSchema()._schema;
|
||||
|
||||
const properties = { currentUser, collection, context, document: newDocument };
|
||||
startDebugMutator(collectionName, 'Update', { validate, document });
|
||||
|
||||
/*
|
||||
|
||||
Properties
|
||||
|
||||
*/
|
||||
const properties = { data, currentUser, collection, context, document: document };
|
||||
|
||||
/*
|
||||
|
||||
Validation
|
||||
|
||||
*/
|
||||
if (validate) {
|
||||
|
||||
let validationErrors = [];
|
||||
|
||||
validationErrors = validationErrors.concat(validateDocument(newDocument, collection, context));
|
||||
|
||||
validationErrors = validationErrors.concat(validateDocument(document, collection, context));
|
||||
// run validation callbacks
|
||||
validationErrors = await runCallbacks({ name: `${typeName.toLowerCase()}.create.validate`, iterator: validationErrors, properties, });
|
||||
validationErrors = await runCallbacks({ name: '*.create.validate', iterator: validationErrors, properties, });
|
||||
validationErrors = await runCallbacks({
|
||||
name: `${typeName.toLowerCase()}.create.validate`,
|
||||
iterator: validationErrors,
|
||||
properties,
|
||||
});
|
||||
validationErrors = await runCallbacks({
|
||||
name: '*.create.validate',
|
||||
iterator: validationErrors,
|
||||
properties,
|
||||
});
|
||||
// OpenCRUD backwards compatibility
|
||||
newDocument = await runCallbacks(`${collectionName.toLowerCase()}.new.validate`, newDocument, currentUser, validationErrors);
|
||||
|
||||
document = await runCallbacks(
|
||||
`${collectionName.toLowerCase()}.new.validate`,
|
||||
document,
|
||||
currentUser,
|
||||
validationErrors
|
||||
);
|
||||
if (validationErrors.length) {
|
||||
throwError({ id: 'app.validation_error', data: {break: true, errors: validationErrors}});
|
||||
throwError({ id: 'app.validation_error', data: { break: true, errors: validationErrors } });
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
// if user is logged in, check if userId field is in the schema and add it to document if needed
|
||||
if (currentUser) {
|
||||
const userIdInSchema = Object.keys(schema).find(key => key === 'userId');
|
||||
if (!!userIdInSchema && !newDocument.userId) newDocument.userId = currentUser._id;
|
||||
if (!!userIdInSchema && !document.userId) document.userId = currentUser._id;
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
run onCreate step
|
||||
onCreate
|
||||
|
||||
note: cannot use forEach with async/await.
|
||||
See https://stackoverflow.com/a/37576787/649299
|
||||
|
@ -89,19 +115,19 @@ export const createMutator = async ({ collection, document, data, currentUser, v
|
|||
note: clone arguments in case callbacks modify them
|
||||
|
||||
*/
|
||||
for(let fieldName of Object.keys(schema)) {
|
||||
for (let fieldName of Object.keys(schema)) {
|
||||
let autoValue;
|
||||
if (schema[fieldName].onCreate) {
|
||||
// OpenCRUD backwards compatibility: keep both newDocument and data for now, but phase our newDocument eventually
|
||||
// OpenCRUD backwards compatibility: keep both newDocument and data for now, but phase out newDocument eventually
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
autoValue = await schema[fieldName].onCreate({ newDocument: clone(newDocument), data: clone(newDocument), currentUser, context });
|
||||
autoValue = await schema[fieldName].onCreate(properties);
|
||||
} else if (schema[fieldName].onInsert) {
|
||||
// OpenCRUD backwards compatibility
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
autoValue = await schema[fieldName].onInsert(clone(newDocument), currentUser);
|
||||
autoValue = await schema[fieldName].onInsert(clone(document), currentUser);
|
||||
}
|
||||
if (typeof autoValue !== 'undefined') {
|
||||
newDocument[fieldName] = autoValue;
|
||||
document[fieldName] = autoValue;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -111,122 +137,212 @@ export const createMutator = async ({ collection, document, data, currentUser, v
|
|||
// post.userAgent = this.connection.httpHeaders['user-agent'];
|
||||
// }
|
||||
|
||||
// run sync callbacks
|
||||
newDocument = await runCallbacks({ name: `${typeName.toLowerCase()}.create.before`, iterator: newDocument, properties, });
|
||||
newDocument = await runCallbacks({ name: '*.create.before', iterator: newDocument, properties, });
|
||||
// OpenCRUD backwards compatibility
|
||||
newDocument = await runCallbacks(`${collectionName.toLowerCase()}.new.before`, newDocument, currentUser);
|
||||
newDocument = await runCallbacks(`${collectionName.toLowerCase()}.new.sync`, newDocument, currentUser);
|
||||
/*
|
||||
|
||||
// add _id to document
|
||||
newDocument._id = await Connectors.create(collection, newDocument);
|
||||
|
||||
// run any post-operation sync callbacks
|
||||
newDocument = await runCallbacks({ name: `${typeName.toLowerCase()}.create.after`, iterator: newDocument, properties, });
|
||||
newDocument = await runCallbacks({ name: '*.create.after', iterator: newDocument, properties, });
|
||||
// OpenCRUD backwards compatibility
|
||||
newDocument = await runCallbacks(`${collectionName.toLowerCase()}.new.after`, newDocument, currentUser);
|
||||
|
||||
// get fresh copy of document from db
|
||||
newDocument = await Connectors.get(collection, newDocument._id);
|
||||
Before
|
||||
|
||||
// run async callbacks
|
||||
// note: query for document to get fresh document with collection-hooks effects applied
|
||||
await runCallbacksAsync({ name: `${typeName.toLowerCase()}.create.async`, properties, });
|
||||
await runCallbacksAsync({ name: '*.create.async', properties, });
|
||||
*/
|
||||
document = await runCallbacks({
|
||||
name: `${typeName.toLowerCase()}.create.before`,
|
||||
iterator: document,
|
||||
properties,
|
||||
});
|
||||
document = await runCallbacks({ name: '*.create.before', iterator: document, properties });
|
||||
// OpenCRUD backwards compatibility
|
||||
await runCallbacksAsync(`${collectionName.toLowerCase()}.new.async`, newDocument, currentUser, collection);
|
||||
document = await runCallbacks(
|
||||
`${collectionName.toLowerCase()}.new.before`,
|
||||
document,
|
||||
currentUser
|
||||
);
|
||||
document = await runCallbacks(`${collectionName.toLowerCase()}.new.sync`, document, currentUser);
|
||||
|
||||
debug('\x1b[33m=> created new document: \x1b[0m');
|
||||
debug(newDocument);
|
||||
debugGroupEnd();
|
||||
debug(`--------------- end \x1b[36m${collectionName} Create Mutator\x1b[0m ---------------`);
|
||||
debug('');
|
||||
/*
|
||||
|
||||
return { data: newDocument };
|
||||
DB Operation
|
||||
|
||||
*/
|
||||
document._id = await Connectors.create(collection, document);
|
||||
|
||||
/*
|
||||
|
||||
After
|
||||
|
||||
*/
|
||||
// run any post-operation sync callbacks
|
||||
document = await runCallbacks({
|
||||
name: `${typeName.toLowerCase()}.create.after`,
|
||||
iterator: document,
|
||||
properties,
|
||||
});
|
||||
document = await runCallbacks({ name: '*.create.after', iterator: document, properties });
|
||||
// OpenCRUD backwards compatibility
|
||||
document = await runCallbacks(`${collectionName.toLowerCase()}.new.after`, document, currentUser);
|
||||
|
||||
// note: query for document to get fresh document with collection-hooks effects applied
|
||||
document = await Connectors.get(collection, document._id);
|
||||
|
||||
/*
|
||||
|
||||
Async
|
||||
|
||||
*/
|
||||
// note: make sure properties.document is up to date
|
||||
await runCallbacksAsync({
|
||||
name: `${typeName.toLowerCase()}.create.async`,
|
||||
properties: { ...properties, document: document },
|
||||
});
|
||||
await runCallbacksAsync({ name: '*.create.async', properties });
|
||||
// OpenCRUD backwards compatibility
|
||||
await runCallbacksAsync(
|
||||
`${collectionName.toLowerCase()}.new.async`,
|
||||
document,
|
||||
currentUser,
|
||||
collection
|
||||
);
|
||||
|
||||
endDebugMutator(collectionName, 'Create', { document });
|
||||
|
||||
return { data: document };
|
||||
};
|
||||
|
||||
|
||||
export const updateMutator = async ({ collection, documentId, selector, data, set = {}, unset = {}, currentUser, validate, context, document }) => {
|
||||
|
||||
export const updateMutator = async ({
|
||||
collection,
|
||||
documentId,
|
||||
selector,
|
||||
data,
|
||||
set = {},
|
||||
unset = {},
|
||||
currentUser,
|
||||
validate,
|
||||
context,
|
||||
document: oldDocument,
|
||||
}) => {
|
||||
const { collectionName, typeName } = collection.options;
|
||||
|
||||
const schema = collection.simpleSchema()._schema;
|
||||
|
||||
// OpenCRUD backwards compatibility
|
||||
selector = selector || { _id: documentId };
|
||||
data = data || modifierToData({ $set: set, $unset: unset });
|
||||
|
||||
startDebugMutator(collectionName, 'Update', { selector, data });
|
||||
|
||||
if (isEmpty(selector)) {
|
||||
throw new Error('Selector cannot be empty');
|
||||
}
|
||||
|
||||
// get original document from database or arguments
|
||||
document = document || await Connectors.get(collection, selector);
|
||||
oldDocument = oldDocument || (await Connectors.get(collection, selector));
|
||||
|
||||
if (!document) {
|
||||
throw new Error(`Could not find document to update for selector: ${JSON.stringify(selector)}`);
|
||||
}
|
||||
|
||||
|
||||
// get a "preview" of the new document
|
||||
let newDocument = { ...document, ...data};
|
||||
newDocument = pickBy(newDocument, f => f !== null);
|
||||
let document = { ...oldDocument, ...data };
|
||||
document = pickBy(document, f => f !== null);
|
||||
|
||||
const properties = { data, oldDocument: document, newDocument, currentUser, collection, context };
|
||||
/*
|
||||
|
||||
// OpenCRUD backwards compatibility
|
||||
properties.document = newDocument;
|
||||
Properties
|
||||
|
||||
debug('');
|
||||
debugGroup(`--------------- start \x1b[36m${collectionName} Update Mutator\x1b[0m ---------------`);
|
||||
debug('// collectionName: ', collectionName);
|
||||
debug('// selector: ', selector);
|
||||
debug('// data: ', data);
|
||||
*/
|
||||
const properties = { data, oldDocument, document, currentUser, collection, context };
|
||||
|
||||
/*
|
||||
|
||||
Validation
|
||||
|
||||
*/
|
||||
if (validate) {
|
||||
|
||||
let validationErrors = [];
|
||||
|
||||
validationErrors = validationErrors.concat(validateData(data, document, collection, context));
|
||||
|
||||
validationErrors = await runCallbacks({ name: `${typeName.toLowerCase()}.update.validate`, iterator: validationErrors, properties, });
|
||||
validationErrors = await runCallbacks({ name: '*.update.validate', iterator: validationErrors, properties, });
|
||||
validationErrors = validationErrors.concat(validateData(data, document, collection, context));
|
||||
|
||||
validationErrors = await runCallbacks({
|
||||
name: `${typeName.toLowerCase()}.update.validate`,
|
||||
iterator: validationErrors,
|
||||
properties,
|
||||
});
|
||||
validationErrors = await runCallbacks({
|
||||
name: '*.update.validate',
|
||||
iterator: validationErrors,
|
||||
properties,
|
||||
});
|
||||
// OpenCRUD backwards compatibility
|
||||
data = modifierToData(await runCallbacks(`${collectionName.toLowerCase()}.edit.validate`, dataToModifier(data), document, currentUser, validationErrors));
|
||||
data = modifierToData(
|
||||
await runCallbacks(
|
||||
`${collectionName.toLowerCase()}.edit.validate`,
|
||||
dataToModifier(data),
|
||||
document,
|
||||
currentUser,
|
||||
validationErrors
|
||||
)
|
||||
);
|
||||
|
||||
if (validationErrors.length) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.log('// validationErrors');
|
||||
// eslint-disable-next-line no-console
|
||||
console.log(validationErrors);
|
||||
throwError({ id: 'app.validation_error', data: {break: true, errors: validationErrors}});
|
||||
throwError({ id: 'app.validation_error', data: { break: true, errors: validationErrors } });
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// run onUpdate step
|
||||
for(let fieldName of Object.keys(schema)) {
|
||||
/*
|
||||
|
||||
onUpdate
|
||||
|
||||
*/
|
||||
for (let fieldName of Object.keys(schema)) {
|
||||
let autoValue;
|
||||
if (schema[fieldName].onUpdate) {
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
autoValue = await schema[fieldName].onUpdate({ data: clone(data), document, currentUser, newDocument, context });
|
||||
autoValue = await schema[fieldName].onUpdate(properties);
|
||||
} else if (schema[fieldName].onEdit) {
|
||||
// OpenCRUD backwards compatibility
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
autoValue = await schema[fieldName].onEdit(dataToModifier(clone(data)), document, currentUser, newDocument);
|
||||
autoValue = await schema[fieldName].onEdit(
|
||||
dataToModifier(clone(data)),
|
||||
document,
|
||||
currentUser,
|
||||
document
|
||||
);
|
||||
}
|
||||
if (typeof autoValue !== 'undefined') {
|
||||
data[fieldName] = autoValue;
|
||||
}
|
||||
}
|
||||
|
||||
// run sync callbacks
|
||||
data = await runCallbacks({ name: `${typeName.toLowerCase()}.update.before`, iterator: data, properties,});
|
||||
data = await runCallbacks({ name: '*.update.before', iterator: data, properties,});
|
||||
/*
|
||||
|
||||
Before
|
||||
|
||||
*/
|
||||
data = await runCallbacks({
|
||||
name: `${typeName.toLowerCase()}.update.before`,
|
||||
iterator: data,
|
||||
properties,
|
||||
});
|
||||
data = await runCallbacks({ name: '*.update.before', iterator: data, properties });
|
||||
// OpenCRUD backwards compatibility
|
||||
data = modifierToData(await runCallbacks(`${collectionName.toLowerCase()}.edit.before`, dataToModifier(data), document, currentUser, newDocument));
|
||||
data = modifierToData(await runCallbacks(`${collectionName.toLowerCase()}.edit.sync`, dataToModifier(data), document, currentUser, newDocument));
|
||||
data = modifierToData(
|
||||
await runCallbacks(
|
||||
`${collectionName.toLowerCase()}.edit.before`,
|
||||
dataToModifier(data),
|
||||
document,
|
||||
currentUser,
|
||||
document
|
||||
)
|
||||
);
|
||||
data = modifierToData(
|
||||
await runCallbacks(
|
||||
`${collectionName.toLowerCase()}.edit.sync`,
|
||||
dataToModifier(data),
|
||||
document,
|
||||
currentUser,
|
||||
document
|
||||
)
|
||||
);
|
||||
|
||||
// update connector requires a modifier, so get it from data
|
||||
const modifier = dataToModifier(data);
|
||||
|
@ -239,12 +355,17 @@ export const updateMutator = async ({ collection, documentId, selector, data, se
|
|||
delete modifier.$unset;
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
DB Operation
|
||||
|
||||
*/
|
||||
if (!_.isEmpty(modifier)) {
|
||||
// update document
|
||||
await Connectors.update(collection, selector, modifier, { removeEmptyStrings: false });
|
||||
|
||||
// get fresh copy of document from db
|
||||
newDocument = await Connectors.get(collection, selector);
|
||||
document = await Connectors.get(collection, selector);
|
||||
|
||||
// TODO: add support for caching by other indexes to Dataloader
|
||||
// https://github.com/VulcanJS/Vulcan/issues/2000
|
||||
|
@ -254,38 +375,58 @@ export const updateMutator = async ({ collection, documentId, selector, data, se
|
|||
}
|
||||
}
|
||||
|
||||
// run any post-operation sync callbacks
|
||||
newDocument = await runCallbacks({ name: `${typeName.toLowerCase()}.update.after`, iterator: newDocument, properties, });
|
||||
newDocument = await runCallbacks({ name: '*.update.after', iterator: newDocument, properties, });
|
||||
// OpenCRUD backwards compatibility
|
||||
newDocument = await runCallbacks(`${collectionName.toLowerCase()}.edit.after`, newDocument, document, currentUser);
|
||||
/*
|
||||
|
||||
After
|
||||
|
||||
*/
|
||||
document = await runCallbacks({
|
||||
name: `${typeName.toLowerCase()}.update.after`,
|
||||
iterator: document,
|
||||
properties,
|
||||
});
|
||||
document = await runCallbacks({ name: '*.update.after', iterator: document, properties });
|
||||
// OpenCRUD backwards compatibility
|
||||
document = await runCallbacks(
|
||||
`${collectionName.toLowerCase()}.edit.after`,
|
||||
document,
|
||||
oldDocument,
|
||||
currentUser
|
||||
);
|
||||
|
||||
/*
|
||||
|
||||
Async
|
||||
|
||||
*/
|
||||
// run async callbacks
|
||||
await runCallbacksAsync({ name: `${typeName.toLowerCase()}.update.async`, properties,});
|
||||
await runCallbacksAsync({ name: '*.update.async', properties,});
|
||||
await runCallbacksAsync({ name: `${typeName.toLowerCase()}.update.async`, properties });
|
||||
await runCallbacksAsync({ name: '*.update.async', properties });
|
||||
// OpenCRUD backwards compatibility
|
||||
await runCallbacksAsync(`${collectionName.toLowerCase()}.edit.async`, newDocument, document, currentUser, collection);
|
||||
await runCallbacksAsync(
|
||||
`${collectionName.toLowerCase()}.edit.async`,
|
||||
document,
|
||||
oldDocument,
|
||||
currentUser,
|
||||
collection
|
||||
);
|
||||
|
||||
debug('\x1b[33m=> updated document with modifier: \x1b[0m');
|
||||
debug('// modifier: ', modifier);
|
||||
debugGroupEnd();
|
||||
debug(`--------------- end \x1b[36m${collectionName} Update Mutator\x1b[0m ---------------`);
|
||||
debug('');
|
||||
endDebugMutator(collectionName, 'Update', { modifier });
|
||||
|
||||
return { data: newDocument };
|
||||
return { data: document };
|
||||
};
|
||||
|
||||
export const deleteMutator = async ({ collection, documentId, selector, currentUser, validate, context, document }) => {
|
||||
|
||||
export const deleteMutator = async ({
|
||||
collection,
|
||||
documentId,
|
||||
selector,
|
||||
currentUser,
|
||||
validate,
|
||||
context,
|
||||
document,
|
||||
}) => {
|
||||
const { collectionName, typeName } = collection.options;
|
||||
|
||||
debug('');
|
||||
debugGroup(`--------------- start \x1b[36m${collectionName} Delete Mutator\x1b[0m ---------------`);
|
||||
debug('// collectionName: ', collectionName);
|
||||
debug('// selector: ', selector);
|
||||
|
||||
const schema = collection.simpleSchema()._schema;
|
||||
|
||||
// OpenCRUD backwards compatibility
|
||||
selector = selector || { _id: documentId };
|
||||
|
||||
|
@ -293,38 +434,62 @@ export const deleteMutator = async ({ collection, documentId, selector, currentU
|
|||
throw new Error('Selector cannot be empty');
|
||||
}
|
||||
|
||||
document = document || await Connectors.get(collection, selector);
|
||||
document = document || (await Connectors.get(collection, selector));
|
||||
|
||||
if (!document) {
|
||||
throw new Error(`Could not find document to delete for selector: ${JSON.stringify(selector)}`);
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
Properties
|
||||
|
||||
*/
|
||||
const properties = { document, currentUser, collection, context };
|
||||
|
||||
if (validate) {
|
||||
/*
|
||||
|
||||
Validation
|
||||
|
||||
*/
|
||||
if (validate) {
|
||||
let validationErrors = [];
|
||||
|
||||
validationErrors = await runCallbacks({ name: `${typeName.toLowerCase()}.delete.validate`, iterator: validationErrors, properties, });
|
||||
validationErrors = await runCallbacks({ name: '*.delete.validate', iterator: validationErrors, properties, });
|
||||
validationErrors = await runCallbacks({
|
||||
name: `${typeName.toLowerCase()}.delete.validate`,
|
||||
iterator: validationErrors,
|
||||
properties,
|
||||
});
|
||||
validationErrors = await runCallbacks({
|
||||
name: '*.delete.validate',
|
||||
iterator: validationErrors,
|
||||
properties,
|
||||
});
|
||||
// OpenCRUD backwards compatibility
|
||||
document = await runCallbacks(`${collectionName.toLowerCase()}.remove.validate`, document, currentUser);
|
||||
document = await runCallbacks(
|
||||
`${collectionName.toLowerCase()}.remove.validate`,
|
||||
document,
|
||||
currentUser
|
||||
);
|
||||
|
||||
if (validationErrors.length) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.log('// validationErrors');
|
||||
// eslint-disable-next-line no-console
|
||||
console.log(validationErrors);
|
||||
throwError({id: 'app.validation_error', data: {break: true, errors: validationErrors}});
|
||||
throwError({ id: 'app.validation_error', data: { break: true, errors: validationErrors } });
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// run onRemove step
|
||||
for(let fieldName of Object.keys(schema)) {
|
||||
/*
|
||||
|
||||
onDelete
|
||||
|
||||
*/
|
||||
for (let fieldName of Object.keys(schema)) {
|
||||
if (schema[fieldName].onDelete) {
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
await schema[fieldName].onDelete({ document, currentUser, context });
|
||||
await schema[fieldName].onDelete(properties);
|
||||
} else if (schema[fieldName].onRemove) {
|
||||
// OpenCRUD backwards compatibility
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
|
@ -332,12 +497,26 @@ export const deleteMutator = async ({ collection, documentId, selector, currentU
|
|||
}
|
||||
}
|
||||
|
||||
await runCallbacks({ name: `${typeName.toLowerCase()}.delete.before`, iterator: document, properties, });
|
||||
await runCallbacks({ name: '*.delete.before', iterator: document, properties, });
|
||||
/*
|
||||
|
||||
Before
|
||||
|
||||
*/
|
||||
await runCallbacks({
|
||||
name: `${typeName.toLowerCase()}.delete.before`,
|
||||
iterator: document,
|
||||
properties,
|
||||
});
|
||||
await runCallbacks({ name: '*.delete.before', iterator: document, properties });
|
||||
// OpenCRUD backwards compatibility
|
||||
await runCallbacks(`${collectionName.toLowerCase()}.remove.before`, document, currentUser);
|
||||
await runCallbacks(`${collectionName.toLowerCase()}.remove.sync`, document, currentUser);
|
||||
|
||||
/*
|
||||
|
||||
DB Operation
|
||||
|
||||
*/
|
||||
await Connectors.delete(collection, selector);
|
||||
|
||||
// TODO: add support for caching by other indexes to Dataloader
|
||||
|
@ -346,14 +525,22 @@ export const deleteMutator = async ({ collection, documentId, selector, currentU
|
|||
collection.loader.clear(selector.documentId);
|
||||
}
|
||||
|
||||
await runCallbacksAsync({ name: `${typeName.toLowerCase()}.delete.async`, properties, });
|
||||
await runCallbacksAsync({ name: '*.delete.async', properties, });
|
||||
// OpenCRUD backwards compatibility
|
||||
await runCallbacksAsync(`${collectionName.toLowerCase()}.remove.async`, document, currentUser, collection);
|
||||
/*
|
||||
|
||||
debugGroupEnd();
|
||||
debug(`--------------- end \x1b[36m${collectionName} Delete Mutator\x1b[0m ---------------`);
|
||||
debug('');
|
||||
Async
|
||||
|
||||
*/
|
||||
await runCallbacksAsync({ name: `${typeName.toLowerCase()}.delete.async`, properties });
|
||||
await runCallbacksAsync({ name: '*.delete.async', properties });
|
||||
// OpenCRUD backwards compatibility
|
||||
await runCallbacksAsync(
|
||||
`${collectionName.toLowerCase()}.remove.async`,
|
||||
document,
|
||||
currentUser,
|
||||
collection
|
||||
);
|
||||
|
||||
endDebugMutator(collectionName, 'Delete');
|
||||
|
||||
return { data: document };
|
||||
};
|
||||
|
@ -365,3 +552,20 @@ export const removeMutation = deleteMutator;
|
|||
export const newMutator = createMutator;
|
||||
export const editMutator = updateMutator;
|
||||
export const removeMutator = deleteMutator;
|
||||
|
||||
const startDebugMutator = (name, action, properties) => {
|
||||
debug('');
|
||||
debugGroup(`--------------- start \x1b[36m${name} ${action} Mutator\x1b[0m ---------------`);
|
||||
Object.keys(properties).forEach(p => {
|
||||
debug(`// ${p}: `, properties[p]);
|
||||
});
|
||||
};
|
||||
|
||||
const endDebugMutator = (name, action, properties) => {
|
||||
Object.keys(properties).forEach(p => {
|
||||
debug(`// ${p}: `, properties[p]);
|
||||
});
|
||||
debugGroupEnd();
|
||||
debug(`--------------- end \x1b[36m${name} ${action} Mutator\x1b[0m ---------------`);
|
||||
debug('');
|
||||
};
|
||||
|
|
Loading…
Add table
Reference in a new issue