Skip to content

Commit

Permalink
Simplfy paramsToAdapter
Browse files Browse the repository at this point in the history
  • Loading branch information
DaddyWarbucks committed Feb 28, 2024
1 parent a83977f commit 6cd108d
Showing 1 changed file with 23 additions and 28 deletions.
51 changes: 23 additions & 28 deletions src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,35 +169,9 @@ export class SequelizeAdapter<

paramsToAdapter (id: NullableId, _params?: ServiceParams): FindOptions {
const params = _params || {} as ServiceParams;
if (id !== null) {
let { query: where } = this.filterQuery(params);

// explicitly set id
if (where[this.id] !== id) {
// If the id is already in the query, we need to add it to the $and array
// to prevent it from being overwritten.
if (this.id in where) {
const { and } = this.Op;
where = {
...where,
[and]: where[and] ? [...where[and], { [this.id]: id }] : { [this.id]: id }
};
} else {
// We can safely set the id in the where query
where[this.id] = id;
}
}
const { filters, query: where } = this.filterQuery(params);

// Attach 'where' constraints, if any were used.
const q: FindOptions = {
raw: this.raw,
where,
...params.sequelize
};

return q;
} else {
const { filters, query: where } = this.filterQuery(params);
if (id === null) {
const order = getOrder(filters.$sort);

const q: FindOptions = {
Expand Down Expand Up @@ -227,6 +201,27 @@ export class SequelizeAdapter<

return q;
}

const q: FindOptions = {
where,
raw: this.raw,
...params.sequelize
};

if (where[this.id] === id) {
return q;
}

if (this.id in where) {
const { and } = this.Op;
where[and] = where[and]
? [...where[and], { [this.id]: id }]
: { [this.id]: id };
} else {
where[this.id] = id;
}

return q;
}

/**
Expand Down

0 comments on commit 6cd108d

Please sign in to comment.