feat: simplifies collection update operation
This commit is contained in:
@@ -1,11 +1,9 @@
|
||||
import deepmerge from 'deepmerge';
|
||||
import httpStatus from 'http-status';
|
||||
import path from 'path';
|
||||
import { UploadedFile } from 'express-fileupload';
|
||||
import { Where, Document } from '../../types';
|
||||
import { Collection } from '../config/types';
|
||||
|
||||
import overwriteMerge from '../../utilities/overwriteMerge';
|
||||
import removeInternalFields from '../../utilities/removeInternalFields';
|
||||
import executeAccess from '../../auth/executeAccess';
|
||||
import { NotFound, Forbidden, APIError, FileUploadError } from '../../errors';
|
||||
@@ -133,9 +131,9 @@ async function update(incomingArgs: Arguments): Promise<Document> {
|
||||
overrideAccess,
|
||||
});
|
||||
|
||||
// /////////////////////////////////////
|
||||
// beforeValidate - Collection
|
||||
// /////////////////////////////////////
|
||||
// // /////////////////////////////////////
|
||||
// // beforeValidate - Collection
|
||||
// // /////////////////////////////////////
|
||||
|
||||
await collectionConfig.hooks.beforeValidate.reduce(async (priorHook, hook) => {
|
||||
await priorHook;
|
||||
@@ -163,12 +161,6 @@ async function update(incomingArgs: Arguments): Promise<Document> {
|
||||
})) || data;
|
||||
}, Promise.resolve());
|
||||
|
||||
// /////////////////////////////////////
|
||||
// Merge updates into existing data
|
||||
// /////////////////////////////////////
|
||||
|
||||
data = deepmerge(originalDoc, data, { arrayMerge: overwriteMerge });
|
||||
|
||||
// /////////////////////////////////////
|
||||
// beforeChange - Fields
|
||||
// /////////////////////////////////////
|
||||
|
||||
@@ -106,10 +106,33 @@ const traverseFields = (args: Arguments): void => {
|
||||
|
||||
if (field.localized && unflattenLocales) {
|
||||
unflattenLocaleActions.push(() => {
|
||||
data[field.name] = payload.config.localization.locales.reduce((locales, localeID) => ({
|
||||
...locales,
|
||||
[localeID]: localeID === locale ? data[field.name] : docWithLocales?.[field.name]?.[localeID],
|
||||
}), {});
|
||||
const localeData = payload.config.localization.locales.reduce((locales, localeID) => {
|
||||
let valueToSet;
|
||||
|
||||
if (localeID === locale) {
|
||||
if (data[field.name]) {
|
||||
valueToSet = data[field.name];
|
||||
} else if (docWithLocales?.[field.name]?.[localeID]) {
|
||||
valueToSet = docWithLocales?.[field.name]?.[localeID];
|
||||
}
|
||||
} else {
|
||||
valueToSet = docWithLocales?.[field.name]?.[localeID];
|
||||
}
|
||||
|
||||
if (valueToSet) {
|
||||
return {
|
||||
...locales,
|
||||
[localeID]: valueToSet,
|
||||
};
|
||||
}
|
||||
|
||||
return locales;
|
||||
}, {});
|
||||
|
||||
// If there are locales with data, set the data
|
||||
if (Object.keys(localeData).length > 0) {
|
||||
data[field.name] = localeData;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
import deepmerge from 'deepmerge';
|
||||
import overwriteMerge from '../../utilities/overwriteMerge';
|
||||
import executeAccess from '../../auth/executeAccess';
|
||||
import removeInternalFields from '../../utilities/removeInternalFields';
|
||||
|
||||
@@ -38,8 +36,6 @@ async function update(args) {
|
||||
if (globalJSON._id) {
|
||||
delete globalJSON._id;
|
||||
}
|
||||
} else {
|
||||
globalJSON = { globalType: slug };
|
||||
}
|
||||
|
||||
const originalDoc = await this.performFieldOperations(globalConfig, {
|
||||
@@ -96,12 +92,6 @@ async function update(args) {
|
||||
})) || data;
|
||||
}, Promise.resolve());
|
||||
|
||||
// /////////////////////////////////////
|
||||
// Merge updates into existing data
|
||||
// /////////////////////////////////////
|
||||
|
||||
data = deepmerge(originalDoc, data, { arrayMerge: overwriteMerge });
|
||||
|
||||
// /////////////////////////////////////
|
||||
// beforeChange - Fields
|
||||
// /////////////////////////////////////
|
||||
@@ -124,9 +114,10 @@ async function update(args) {
|
||||
global = await Model.findOneAndUpdate(
|
||||
{ globalType: slug },
|
||||
result,
|
||||
{ overwrite: true, new: true },
|
||||
{ new: true },
|
||||
);
|
||||
} else {
|
||||
result.globalType = slug;
|
||||
global = await Model.create(result);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user