fix: option parser test (#195)
This commit is contained in:
parent
9c422b7879
commit
78e89b76ef
@ -179,17 +179,18 @@ export class OptionsParser {
|
|||||||
|
|
||||||
protected parseAppends(appends: Appends, filterParams: any) {
|
protected parseAppends(appends: Appends, filterParams: any) {
|
||||||
if (!appends) return filterParams;
|
if (!appends) return filterParams;
|
||||||
const associations = this.model.associations;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* set include params
|
* set include params
|
||||||
* @param includeRoot
|
* @param includeRoot
|
||||||
* @param appends
|
* @param appends
|
||||||
*/
|
*/
|
||||||
const setInclude = (queryParams: any, append: string) => {
|
const setInclude = (model: ModelCtor<any>, queryParams: any, append: string) => {
|
||||||
const appendFields = append.split('.');
|
const appendFields = append.split('.');
|
||||||
const appendAssociation = appendFields[0];
|
const appendAssociation = appendFields[0];
|
||||||
|
|
||||||
|
const associations = model.associations;
|
||||||
|
|
||||||
// if append length less or equal 2
|
// if append length less or equal 2
|
||||||
// example:
|
// example:
|
||||||
// appends: ['posts']
|
// appends: ['posts']
|
||||||
@ -260,19 +261,17 @@ export class OptionsParser {
|
|||||||
attributes,
|
attributes,
|
||||||
};
|
};
|
||||||
} else {
|
} 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
|
// handle every appends
|
||||||
for (const append of appends) {
|
for (const append of appends) {
|
||||||
const appendFields = append.split('.');
|
setInclude(this.model, filterParams, append);
|
||||||
|
|
||||||
if (!associations[appendFields[0]]) {
|
|
||||||
throw new Error(`${append} is not a valid association`);
|
|
||||||
}
|
|
||||||
|
|
||||||
setInclude(filterParams, append);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
debug('filter params: %o', filterParams);
|
debug('filter params: %o', filterParams);
|
||||||
|
Loading…
Reference in New Issue
Block a user