Skip to content

Commit

Permalink
fix: move search logic outside utils
Browse files Browse the repository at this point in the history
  • Loading branch information
pawelTshDev committed Mar 28, 2024
1 parent 1be66ab commit 5435e3d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 16 deletions.
10 changes: 8 additions & 2 deletions functions/example-lambda/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,23 @@ import { zodValidator } from "../../shared/middleware/zod-validator";
import { httpCorsConfigured } from "../../shared/middleware/http-cors-configured";
import { httpErrorHandlerConfigured } from "../../shared/middleware/http-error-handler-configured";
import { createFindManyOptions, makePaginationResult } from "../../shared/pagination-utils/pagination-utils";
import { Like } from "typeorm";

const connectToDb = dataSource.initialize();
const config = createConfig(process.env);
const userRepository = dataSource.getRepository(ExampleModel);

const lambdaHandler = async (event: ExampleLambdaPayload) => {
winstonLogger.info(`Hello from ${config.appName}. Example param is: ${event.queryStringParameters.exampleParam}`);
const queryParams = event.queryStringParameters;
winstonLogger.info(`Hello from ${config.appName}. Example param is: ${queryParams.exampleParam}`);

await connectToDb;

const findOptions = createFindManyOptions(userRepository, event.queryStringParameters);
const findOptions = createFindManyOptions(userRepository, queryParams);

if (queryParams.search) {
findOptions.where = { ...findOptions.where, email: Like(`%${queryParams.search}%`) };
}
const [data, total] = await userRepository.findAndCount(findOptions);

return awsLambdaResponse(
Expand Down
16 changes: 2 additions & 14 deletions shared/pagination-utils/pagination-utils.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
import {
FindManyOptions,
FindOperator,
FindOptionsOrder,
FindOptionsWhere,
Like,
ObjectLiteral,
Repository,
} from "typeorm";
import { FindManyOptions, FindOperator, FindOptionsOrder, FindOptionsWhere, ObjectLiteral, Repository } from "typeorm";

export interface PaginationResult<T> {
meta: {
Expand Down Expand Up @@ -57,7 +49,7 @@ export function createFindManyOptions<T extends ObjectLiteral>(
repository: Repository<T>,
queryParams: PaginationParamsDto,
): FindManyOptions<T> {
const { page: pageString, limit: limitString, sort, filter, search } = queryParams;
const { page: pageString, limit: limitString, sort, filter } = queryParams;
const page = Number(pageString);
const limit = Number(limitString);
const findOptions: FindManyOptions = {};
Expand All @@ -75,10 +67,6 @@ export function createFindManyOptions<T extends ObjectLiteral>(
findOptions.where = getAvailableFilters(filter, repository);
}

if (search) {
findOptions.where = { ...findOptions.where, email: Like(`%${search}%`) };
}

return findOptions;
}

Expand Down

0 comments on commit 5435e3d

Please sign in to comment.