Skip to content

Commit

Permalink
Fix o2m updates not saving revisions nested
Browse files Browse the repository at this point in the history
  • Loading branch information
rijkvanzanten committed May 25, 2021
1 parent 2337b5b commit 95307ce
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 12 deletions.
6 changes: 3 additions & 3 deletions api/src/services/items.ts
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ export class ItemsService<Item extends AnyItem = AnyItem> implements AbstractSer
const primaryKeys: PrimaryKey[] = [];

for (const payload of payloads) {
const primaryKey = await service.upsertOne(payload, { autoPurgeCache: false });
const primaryKey = await service.upsertOne(payload, { ...(opts || {}), autoPurgeCache: false });
primaryKeys.push(primaryKey);
}

Expand All @@ -576,7 +576,7 @@ export class ItemsService<Item extends AnyItem = AnyItem> implements AbstractSer
/**
* Delete multiple items by query
*/
async deleteByQuery(query: Query): Promise<PrimaryKey[]> {
async deleteByQuery(query: Query, opts?: MutationOptions): Promise<PrimaryKey[]> {
const primaryKeyField = this.schema.collections[this.collection].primary;
const readQuery = cloneDeep(query);
readQuery.fields = [primaryKeyField];
Expand All @@ -592,7 +592,7 @@ export class ItemsService<Item extends AnyItem = AnyItem> implements AbstractSer

if (keys.length === 0) return [];

return await this.deleteMany(keys);
return await this.deleteMany(keys, opts);
}

/**
Expand Down
44 changes: 35 additions & 9 deletions api/src/services/payload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,9 @@ export class PayloadService {
.first());

if (exists) {
await itemsService.updateOne(relatedPrimaryKey, relatedRecord);
await itemsService.updateOne(relatedPrimaryKey, relatedRecord, {
onRevisionCreate: (id) => revisions.push(id),
});
} else {
relatedPrimaryKey = await itemsService.createOne(relatedRecord, {
onRevisionCreate: (id) => revisions.push(id),
Expand Down Expand Up @@ -474,7 +476,9 @@ export class PayloadService {
});
}

const savedPrimaryKeys = await itemsService.upsertMany(relatedRecords);
const savedPrimaryKeys = await itemsService.upsertMany(relatedRecords, {
onRevisionCreate: (id) => revisions.push(id),
});

const query: Query = {
filter: {
Expand All @@ -495,9 +499,16 @@ export class PayloadService {

// Nullify all related items that aren't included in the current payload
if (relation.meta.one_deselect_action === 'delete') {
// There's no revision for a deletion
await itemsService.deleteByQuery(query);
} else {
await itemsService.updateByQuery(query, { [relation.field]: null });
await itemsService.updateByQuery(
query,
{ [relation.field]: null },
{
onRevisionCreate: (id) => revisions.push(id),
}
);
}
}
// "Updates" object w/ create/update/delete
Expand All @@ -511,18 +522,27 @@ export class PayloadService {
alterations.create.map((item) => ({
...item,
[relation.field]: parent || payload[currentPrimaryKeyField],
}))
})),
{
onRevisionCreate: (id) => revisions.push(id),
}
);
}

if (alterations.update) {
const primaryKeyField = this.schema.collections[this.collection].primary;

for (const item of alterations.update) {
await itemsService.updateOne(item[primaryKeyField], {
...item,
[relation.field]: parent || payload[currentPrimaryKeyField],
});
await itemsService.updateOne(
item[primaryKeyField],
{
...item,
[relation.field]: parent || payload[currentPrimaryKeyField],
},
{
onRevisionCreate: (id) => revisions.push(id),
}
);
}
}

Expand All @@ -547,7 +567,13 @@ export class PayloadService {
if (relation.meta.one_deselect_action === 'delete') {
await itemsService.deleteByQuery(query);
} else {
await itemsService.updateByQuery(query, { [relation.field]: null });
await itemsService.updateByQuery(
query,
{ [relation.field]: null },
{
onRevisionCreate: (id) => revisions.push(id),
}
);
}
}
}
Expand Down

0 comments on commit 95307ce

Please sign in to comment.