Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make buildQuery available when override service methods #85

Merged
merged 1 commit into from
Jun 5, 2019

Conversation

Diluka
Copy link
Contributor

@Diluka Diluka commented May 30, 2019

change buildQuery's access modifier to protected, so it can be used in derived classes.

recently I often encountered this use case which I need manually add some filters:

async getMany(query: RequestParamsParsed, options: RestfulOptions) {
    const statusFilter = _.chain(query.filter).remove(o => o.field === 'status').first().value();
    // @ts-ignore
    const builder = await this.buildQuery(query, options);

    if (statusFilter) {
      switch (statusFilter.operator) {
        case 'eq':
          builder.andWhere(...this.statusToWhere(builder.alias, statusFilter.value));
          break;
        case 'in':
          builder.andWhere(..._.chain(statusFilter.value)
            .map(v => this.statusToWhere(builder.alias, v))
            .reduce((acc: [string[], any], v) => {
              acc[0].push(`(${v[0]})`);
              _.assign(acc[1], v[1]);
              return acc;
            }, [[], {}])
            .thru(([a, b]) => [`(${a.join(' OR ')})`, b])
            .value());
          break;
        default:
          throw new BadRequestException('status仅支持eq或者in');
      }
    }

    const mergedOptions = Object.assign({}, this.options, options);
    if (this.decidePagination(query, mergedOptions)) {
      const [data, total] = await builder.getManyAndCount();
      const limit = builder.expressionMap.take;
      const offset = builder.expressionMap.skip;
      return this.createPageInfo(data, total, limit, offset);
    }
    return builder.getMany();
  }

change `buildQuery`'s access modifier to `protected`, so it can be used in derived classes.
@ignatvilesov
Copy link

Hi @Diluka @zMotivat0r

We found this feature very useful to build custom queries.
Could you please confirm that this PR will be merged and released in v4.0.0?

@michaelyali
Copy link
Member

@ignatvilesov, confirm

@michaelyali michaelyali merged commit 4108beb into master Jun 5, 2019
@michaelyali michaelyali deleted the Diluka-patch-1 branch June 7, 2019 18:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants