enables lean mongoose queries for all find operations

This commit is contained in:
James
2020-10-05 10:22:15 -04:00
parent d85cbde7a7
commit 7f47cdacbb
8 changed files with 38 additions and 46 deletions

View File

@@ -1,6 +1,7 @@
const { isValidObjectId } = require('mongoose');
const { ValidationError } = require('../errors');
const executeAccess = require('../auth/executeAccess');
const sanitizeFallbackLocale = require('../localization/sanitizeFallbackLocale');
async function performFieldOperations(entityConfig, args) {
const {
@@ -12,10 +13,14 @@ async function performFieldOperations(entityConfig, args) {
id,
req: {
payloadAPI,
locale,
},
overrideAccess,
reduceLocales,
} = args;
const fallbackLocale = sanitizeFallbackLocale(req.fallbackLocale);
const recursivePerformFieldOperations = performFieldOperations.bind(this);
let depth = 0;
@@ -275,6 +280,13 @@ async function performFieldOperations(entityConfig, args) {
fields.forEach((field) => {
const dataCopy = data;
if (reduceLocales && field.name && field.localized && locale !== 'all' && typeof data[field.name] === 'object') {
let localizedValue = data[field.name][locale];
if (typeof localizedValue === 'undefined' && fallbackLocale) localizedValue = data[field.name][fallbackLocale];
if (typeof localizedValue === 'undefined') localizedValue = null;
dataCopy[field.name] = localizedValue;
}
if (field.type === 'upload') {
if (data[field.name] === '') dataCopy[field.name] = null;
}