Skip to content

Commit

Permalink
Use instance in update
Browse files Browse the repository at this point in the history
  • Loading branch information
DaddyWarbucks committed Mar 30, 2024
1 parent 8fc41d2 commit 0363acd
Showing 1 changed file with 9 additions and 16 deletions.
25 changes: 9 additions & 16 deletions src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,19 +293,17 @@ export class SequelizeAdapter<
return [];
}

const selected = isPresent(sequelize.attributes);

if (sequelize.raw) {
const result = instances.map((instance) => {
if (selected) {
if (isPresent(sequelize.attributes)) {
return select(instance.toJSON());
}
return instance.toJSON();
})
return result;
}

if (selected) {
if (isPresent(sequelize.attributes)) {
const result = instances.map((instance) => {
const result = select(instance.toJSON())
return Model.build(result, { isNewRecord: false });
Expand Down Expand Up @@ -469,30 +467,25 @@ export class SequelizeAdapter<
const sequelize = this.paramsToAdapter(id, params);
const select = selector(params, this.id);

const total = await Model
.count({ ...sequelize, attributes: undefined })
.catch(errorHandler);

if (!total) {
throw new NotFound(`No record found for id '${id}'`);
}
const instance = await this._get(id, {
...params,
sequelize: { ...params.sequelize, raw: false }
}) as Model;

const values = Object.values(Model.getAttributes())
.reduce((values, attribute: any) => {
const key = attribute.fieldName as string;
if (key === this.id) {
// @ts-ignore
values[key] = id;
return values
}
// @ts-ignore
values[key] = key in data ? data[key] : null;
return values;
}, {});

const instance = await Model
.build(values, { isNewRecord: false })
.update(_.omit(values, this.id), sequelize)
await instance
.set(values)
.update(values, sequelize)
.catch(errorHandler);

if (isPresent(sequelize.include)) {
Expand Down

0 comments on commit 0363acd

Please sign in to comment.