Skip to content

Commit

Permalink
Support for unfiltered count
Browse files Browse the repository at this point in the history
  • Loading branch information
john-gom committed Oct 9, 2023
1 parent c203ab0 commit c24d55b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/app.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Body, Controller, Get, Post, Query } from '@nestjs/common';
import { Body, Controller, Get, Post, Query, All } from '@nestjs/common';
import { ImportService } from './domain/services/import.service';
import { QueryService } from './domain/services/query.service';

Expand Down Expand Up @@ -27,7 +27,7 @@ export class AppController {
return await this.queryService.aggregate(body);
}

@Post('count')
@All('count')
async count(@Body() body: any) {
return await this.queryService.count(body);
}
Expand Down
9 changes: 9 additions & 0 deletions src/domain/services/query.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,15 @@ describe('count', () => {
expect(response).toBe(1);
});
});
it('should cope with no filters', async () => {
await createTestingModule([DomainModule], async (app) => {
const { originValue, aminoValue, neucleotideValue } =
await createTestTags(app);
const queryService = app.get(QueryService);
const response = await queryService.count(null);
expect(response).toBeGreaterThan(2);
});
});
});

describe('aggregate', () => {
Expand Down
28 changes: 15 additions & 13 deletions src/domain/services/query.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,29 +89,31 @@ export class QueryService {
const start = Date.now();
this.logger.debug(body);

const tags = Object.keys(body);
const tag = tags[0];
const tags = Object.keys(body ?? {});
const tag = tags?.[0];
const { entity, column } = this.getEntityAndColumn(tag);
const qb = this.em.createQueryBuilder(entity, 'pt');
qb.select(`count(*) count`);
qb.where('not pt.obsolete');

let matchValue = body[tag];
const not = matchValue?.['$ne'];
if (not) {
matchValue = not;
let whereLog = [];
if (tag) {
let matchValue = body[tag];
const not = matchValue?.['$ne'];
whereLog.push(`${tag} ${not ? '!=' : '=='} ${matchValue}`);
if (not) {
matchValue = not;
}
qb.andWhere(`${not ? 'NOT ' : ''}pt.${column} = ?`, [matchValue]);
delete body[tag];
whereLog.push(...this.addMatches(body, qb));
}
qb.andWhere(`${not ? 'NOT ' : ''}pt.${column} = ?`, [matchValue]);
delete body[tag];
const whereLog = this.addMatches(body, qb);

this.logger.debug(qb.getFormattedQuery());
const results = await qb.execute();
const response = results[0].count;
this.logger.log(
`Processed ${tag} ${not ? '!=' : '=='} ${matchValue}${
whereLog.length ? ` and ${whereLog.join(' and ')}` : ''
} in ${Date.now() - start} ms. Count: ${response}`,
`Processed ${whereLog.join(' and ')} in ${Date.now() - start} ms. Count: ${response}`,
);
return parseInt(response);
}
Expand All @@ -136,7 +138,7 @@ export class QueryService {
private getEntityAndColumn(tag: any) {
let entity: EntityName<object>;
let column = 'value';
if (MAPPED_FIELDS.includes(tag)) {
if (!tag || MAPPED_FIELDS.includes(tag)) {
entity = Product;
column = tag;
} else {
Expand Down

0 comments on commit c24d55b

Please sign in to comment.