Skip to content

Commit

Permalink
fix(database): fix sql findByIdAndUpdate associations (#561)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisPdgn committed Mar 23, 2023
1 parent ed8bc0b commit 8eac300
Showing 1 changed file with 20 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ export class SQLSchema extends SequelizeSchema {
const data = await this.model
.findByPk(id, {
nest: true,
include: this.constructAssociationInclusion(assocs).concat(
include: this.constructAssociationInclusion({}).concat(
this.constructRelationInclusion(populate),
),
transaction: t,
Expand All @@ -178,26 +178,28 @@ export class SQLSchema extends SequelizeSchema {
for (const assoc in associationObjects) {
if (!associationObjects.hasOwnProperty(assoc)) continue;
if (Array.isArray(associationObjects[assoc])) {
associationObjects[assoc].forEach((obj: Indexable) => {
for (const obj of associationObjects[assoc]) {
if (obj.hasOwnProperty('_id')) {
promises.push(
(this.associations[assoc] as SQLSchema).findByIdAndUpdate(
(this.associations[assoc] as SQLSchema[])[0].findByIdAndUpdate(
obj._id,
obj,
undefined,
t,
),
);
} else {
promises.push(async () => {
const returned = await (this.associations[assoc] as SQLSchema).create(
obj,
t,
);
doc[`add${assoc.charAt(0).toUpperCase() + assoc.slice(1)}`](returned);
});
promises.push(
(this.associations[assoc] as SQLSchema[])[0]
.create(obj, t)
.then(r => {
doc[`add${assoc.charAt(0).toUpperCase() + assoc.slice(1)}`](
r._id,
);
}),
);
}
});
}
} else {
if (assoc.hasOwnProperty('_id')) {
promises.push(
Expand All @@ -209,13 +211,13 @@ export class SQLSchema extends SequelizeSchema {
),
);
} else {
promises.push(async () => {
const returned = (this.associations[assoc] as SQLSchema).create(
associationObjects[assoc],
t,
);
doc[`set${assoc.charAt(0).toUpperCase() + assoc.slice(1)}`](returned);
});
promises.push(
(this.associations[assoc] as SQLSchema)
.create(associationObjects[assoc], t)
.then(r => {
doc[`set${assoc.charAt(0).toUpperCase() + assoc.slice(1)}`](r._id);
}),
);
}
}
}
Expand Down

0 comments on commit 8eac300

Please sign in to comment.