mirror of
https://github.com/directus/directus.git
synced 2026-04-25 03:00:53 -04:00
Only unflatten item properties that needs to be unflattened (#8814)
* Only unflatten item properties that needs to be unflattened * Delete leftover aggregate keys * Update api/src/services/payload.ts Co-authored-by: Rijk van Zanten <rijkvanzanten@me.com>
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import { format, parseISO } from 'date-fns';
|
||||
import Joi from 'joi';
|
||||
import { Knex } from 'knex';
|
||||
import { clone, cloneDeep, isObject, isPlainObject, omit, isNil } from 'lodash';
|
||||
import { clone, cloneDeep, isObject, isPlainObject, omit, pick, isNil } from 'lodash';
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
import getDatabase from '../database';
|
||||
import { ForbiddenException, InvalidPayloadException } from '../exceptions';
|
||||
@@ -124,7 +124,7 @@ export class PayloadService {
|
||||
action: Action,
|
||||
payload: Partial<Item> | Partial<Item>[]
|
||||
): Promise<Partial<Item> | Partial<Item>[]> {
|
||||
let processedPayload = toArray(payload);
|
||||
const processedPayload = toArray(payload);
|
||||
|
||||
if (processedPayload.length === 0) return [];
|
||||
|
||||
@@ -166,7 +166,9 @@ export class PayloadService {
|
||||
});
|
||||
}
|
||||
|
||||
processedPayload = processedPayload.map((item: Record<string, any>) => unflatten(item, { delimiter: '->' }));
|
||||
if (action === 'read') {
|
||||
this.processAggregates(processedPayload);
|
||||
}
|
||||
|
||||
if (Array.isArray(payload)) {
|
||||
return processedPayload;
|
||||
@@ -175,6 +177,16 @@ export class PayloadService {
|
||||
return processedPayload[0];
|
||||
}
|
||||
|
||||
processAggregates(payload: Partial<Item>[]) {
|
||||
const aggregateKeys = Object.keys(payload[0]).filter((key) => key.includes('->'));
|
||||
if (aggregateKeys.length) {
|
||||
for (const item of payload) {
|
||||
Object.assign(item, unflatten(pick(item, aggregateKeys), { delimiter: '->' }));
|
||||
aggregateKeys.forEach((key) => delete item[key]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async processField(
|
||||
field: SchemaOverview['collections'][string]['fields'][string],
|
||||
payload: Partial<Item>,
|
||||
|
||||
Reference in New Issue
Block a user