Skip to content

Commit

Permalink
fix: $returning, order, _update
Browse files Browse the repository at this point in the history
- rm '$returning' for id !== null
- consider order for '_get';
- simplify '_update' & unset '$select' to get all fields
  • Loading branch information
fratzinger committed Feb 29, 2024
1 parent fc0ed82 commit a4331e9
Showing 1 changed file with 27 additions and 37 deletions.
64 changes: 27 additions & 37 deletions src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,9 @@ export class SequelizeAdapter<
const { filters, query: where } = this.filterQuery(params);

if (id === null) {
const order = getOrder(filters.$sort);

const q: FindOptions = {
where,
order,
order: getOrder(filters.$sort),
limit: filters.$limit,
offset: filters.$skip,
attributes: filters.$select,
Expand All @@ -204,6 +202,7 @@ export class SequelizeAdapter<

const q: FindOptions = {
where,
order: getOrder(filters.$sort),
limit: 1,
attributes: filters.$select,
raw: this.raw,
Expand All @@ -229,6 +228,8 @@ export class SequelizeAdapter<
/**
* returns either the model instance / jsonified object for an id or all unpaginated
* items for `params` if id is null
*
* @deprecated Use `_get` or `_find` instead. `_getOrFind` will be removed in a future release.
*/
async _getOrFind (id: Id, _params?: ServiceParams): Promise<Result>
async _getOrFind (id: null, _params?: ServiceParams): Promise<Result[]>
Expand Down Expand Up @@ -385,7 +386,6 @@ export class SequelizeAdapter<
paginate: false,
query: {
[this.id]: ids.length === 1 ? ids[0] : { $in: ids },
$limit: ids.length,
$select: params?.query?.$select
}
});
Expand All @@ -407,14 +407,10 @@ export class SequelizeAdapter<
...params.sequelize,
where: { [this.id]: id }
});
} catch (error){
} catch (error) {
errorHandler(error);
}

if (params.$returning === false) {
return [];
}

const result = await this._get(id, {
...params,
query: { $select: params?.query?.$select }
Expand All @@ -424,16 +420,22 @@ export class SequelizeAdapter<
}

async _update (id: Id, data: Data, params: ServiceParams = {} as ServiceParams): Promise<Result> {
const { query, filters } = this.filterQuery(params);

// Force the {raw: false} option as the instance
// is needed to properly update
const seqOptions = {
...params.sequelize,
raw: false
};

const instance = await this._get(id, { sequelize: seqOptions, query } as ServiceParams) as Model
const instance = await this._get(id, {
...params,
query: {
...params.query,
// we need all fields to properly update the instance
$select: undefined
},
sequelize: seqOptions
} as ServiceParams) as Model

const itemToUpdate = Object.keys(instance.toJSON()).reduce((result: Record<string, any>, key) => {
// @ts-ignore
Expand All @@ -444,21 +446,16 @@ export class SequelizeAdapter<

try {
await instance.update(itemToUpdate, seqOptions);
} catch (error) {
return errorHandler(error);
}

const item = await this._get(id, {
query: { $select: filters.$select },
sequelize: {
...params.sequelize,
raw: typeof params.sequelize?.raw === 'boolean'
? params.sequelize.raw
: this.raw
}
} as ServiceParams);
const result = await this._get(id, {
...params,
query: { $select: params.query?.$select }
});

return item;
} catch (err: any) {
return errorHandler(err);
}
return result;
}

async _remove (id: null, params?: ServiceParams): Promise<Result[]>
Expand All @@ -470,11 +467,11 @@ export class SequelizeAdapter<

const Model = this.ModelWithScope(params);

const $select = params.$returning === false
? [this.id]
: params?.query?.$select

if (id === null) {
const $select = params.$returning === false
? [this.id]
: params?.query?.$select

const items = await this._find({
...params,
paginate: false,
Expand Down Expand Up @@ -504,10 +501,7 @@ export class SequelizeAdapter<
return items;
}

const item = await this._get(id, {
...params,
query: { ...params.query, $select }
});
const item = await this._get(id, params);

try {
await Model.destroy({
Expand All @@ -519,10 +513,6 @@ export class SequelizeAdapter<
errorHandler(error);
}

if (params.$returning === false) {
return [];
}

return item
}
}

0 comments on commit a4331e9

Please sign in to comment.