mirror of
https://github.com/directus/directus.git
synced 2026-01-30 20:28:00 -05:00
Merge branch 'relational-updates' of https://github.com/directus/next into relational-updates
This commit is contained in:
@@ -439,8 +439,8 @@ export class ItemsService implements AbstractService {
|
||||
return await this.update(data, keys);
|
||||
}
|
||||
|
||||
upsert(data: Partial<Item>): Promise<PrimaryKey>;
|
||||
upsert(data: Partial<Item>[]): Promise<PrimaryKey[]>;
|
||||
upsert(data: Partial<Item>): Promise<PrimaryKey>;
|
||||
async upsert(data: Partial<Item> | Partial<Item>[]): Promise<PrimaryKey | PrimaryKey[]> {
|
||||
const primaryKeyField = await this.schemaInspector.primary(this.collection);
|
||||
const payloads = Array.isArray(data) ? data : [data];
|
||||
|
||||
@@ -347,10 +347,7 @@ export class PayloadService {
|
||||
const relationsToProcess = relations.filter((relation) => {
|
||||
if (!relation.one_field) return false;
|
||||
|
||||
return (
|
||||
payload.hasOwnProperty(relation.one_field) &&
|
||||
Array.isArray(payload[relation.one_field])
|
||||
);
|
||||
return payload.hasOwnProperty(relation.one_field);
|
||||
});
|
||||
|
||||
for (const relation of relationsToProcess) {
|
||||
@@ -360,37 +357,43 @@ export class PayloadService {
|
||||
});
|
||||
|
||||
const relatedRecords: Partial<Item>[] = [];
|
||||
let savedPrimaryKeys: PrimaryKey[] = [];
|
||||
|
||||
for (const relatedRecord of payload[relation.one_field!]) {
|
||||
let record = cloneDeep(relatedRecord);
|
||||
if (payload[relation.one_field!] && Array.isArray(payload[relation.one_field!])) {
|
||||
for (const relatedRecord of payload[relation.one_field!] || []) {
|
||||
let record = cloneDeep(relatedRecord);
|
||||
|
||||
if (typeof relatedRecord === 'string' || typeof relatedRecord === 'number') {
|
||||
const exists = !!(await this.knex
|
||||
.select(relation.many_primary)
|
||||
.from(relation.many_collection)
|
||||
.where({ [relation.many_primary]: record })
|
||||
.first());
|
||||
if (
|
||||
typeof relatedRecord === 'string' ||
|
||||
typeof relatedRecord === 'number'
|
||||
) {
|
||||
const exists = !!(await this.knex
|
||||
.select(relation.many_primary)
|
||||
.from(relation.many_collection)
|
||||
.where({ [relation.many_primary]: record })
|
||||
.first());
|
||||
|
||||
if (exists === false) {
|
||||
throw new ForbiddenException(undefined, {
|
||||
item: record,
|
||||
collection: relation.many_collection,
|
||||
});
|
||||
if (exists === false) {
|
||||
throw new ForbiddenException(undefined, {
|
||||
item: record,
|
||||
collection: relation.many_collection,
|
||||
});
|
||||
}
|
||||
|
||||
record = {
|
||||
[relation.many_primary]: relatedRecord,
|
||||
};
|
||||
}
|
||||
|
||||
record = {
|
||||
[relation.many_primary]: relatedRecord,
|
||||
};
|
||||
relatedRecords.push({
|
||||
...record,
|
||||
[relation.many_field]: parent || payload[relation.one_primary!],
|
||||
});
|
||||
}
|
||||
|
||||
relatedRecords.push({
|
||||
...record,
|
||||
[relation.many_field]: parent || payload[relation.one_primary!],
|
||||
});
|
||||
savedPrimaryKeys = await itemsService.upsert(relatedRecords);
|
||||
}
|
||||
|
||||
const primaryKeys = await itemsService.upsert(relatedRecords);
|
||||
|
||||
await itemsService.updateByQuery(
|
||||
{ [relation.many_field]: null },
|
||||
{
|
||||
@@ -403,7 +406,7 @@ export class PayloadService {
|
||||
},
|
||||
{
|
||||
[relation.many_primary]: {
|
||||
_nin: primaryKeys,
|
||||
_nin: savedPrimaryKeys,
|
||||
},
|
||||
},
|
||||
],
|
||||
|
||||
Reference in New Issue
Block a user