fix: option parser test (#195)

This commit is contained in:
ChengLei Shao 2022-02-17 17:41:30 +08:00 committed by GitHub
parent 9c422b7879
commit 78e89b76ef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -179,17 +179,18 @@ export class OptionsParser {
protected parseAppends(appends: Appends, filterParams: any) {
if (!appends) return filterParams;
const associations = this.model.associations;
/**
* set include params
* @param includeRoot
* @param appends
*/
const setInclude = (queryParams: any, append: string) => {
const setInclude = (model: ModelCtor<any>, queryParams: any, append: string) => {
const appendFields = append.split('.');
const appendAssociation = appendFields[0];
const associations = model.associations;
// if append length less or equal 2
// example:
// appends: ['posts']
@ -260,19 +261,17 @@ export class OptionsParser {
attributes,
};
} else {
setInclude(queryParams['include'][existIncludeIndex], appendFields.filter((_, index) => index !== 0).join('.'));
setInclude(
model.associations[queryParams['include'][existIncludeIndex].association].target,
queryParams['include'][existIncludeIndex],
appendFields.filter((_, index) => index !== 0).join('.'),
);
}
};
// handle every appends
for (const append of appends) {
const appendFields = append.split('.');
if (!associations[appendFields[0]]) {
throw new Error(`${append} is not a valid association`);
}
setInclude(filterParams, append);
setInclude(this.model, filterParams, append);
}
debug('filter params: %o', filterParams);