Skip to content

Commit

Permalink
Clean up upsert
Browse files Browse the repository at this point in the history
  • Loading branch information
thomassnielsen committed Nov 16, 2024
1 parent 8188a28 commit 6ab1e83
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions packages/server/src/api/rest/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,7 @@ class RequestHandler extends APIHandlerBase {
return error;
}

const matchFields = (requestBody as any).meta.matchFields;
const uniqueFields = Object.values(modelMeta.models[type].uniqueConstraints || {}).map((uf) => uf.fields);

if (
Expand All @@ -856,14 +857,12 @@ class RequestHandler extends APIHandlerBase {
return this.makeError('invalidPayload', 'Match fields must be unique fields', 400);
}

const matchFields = (requestBody as any).meta.matchFields;

const upsertPayload: any = {};
upsertPayload.where = this.makeUpsertWhere(matchFields, attributes, typeInfo);

upsertPayload.create = { ...attributes };
upsertPayload.update = {
...Object.fromEntries(Object.entries(attributes).filter((e) => !matchFields.includes(e[0]))),
const upsertPayload: any = {
where: this.makeUpsertWhere(matchFields, attributes, typeInfo),
create: { ...attributes },
update: {
...Object.fromEntries(Object.entries(attributes).filter((e) => !matchFields.includes(e[0]))),
},
};

if (relationships) {
Expand Down

0 comments on commit 6ab1e83

Please sign in to comment.