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

- Fix the interface. #707

Merged
merged 4 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/mysql-common/src/enums/enums.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./filtering-operator.enum";
export * from "./sort-order.enum";
8 changes: 8 additions & 0 deletions packages/mysql-common/src/enums/filtering-operator.enum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export enum FilteringOperatorEnum {
Equal = "eq",
NotEqual = "ne",
GreaterThan = "gt",
GreaterThanOrEqual = "gte",
LessThan = "lt",
LessThanOrEqual = "lte",
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,9 @@ export interface SearchQueryParametersInterface {
* Separated by commas string of `field:asc|desc` to sort the results.
*/
sort?: string;

/**
* Separated by commas string of `filter:field:operator:value` to filter the results.
*/
filters?: string;
}
1 change: 1 addition & 0 deletions packages/mysql-common/src/models/models.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./search-field-filter.model";
export * from "./search-query.model";
export * from "./search-query-field.model";
export * from "./search-result.model";
24 changes: 24 additions & 0 deletions packages/mysql-common/src/models/search-field-filter.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {FilteringOperatorEnum} from "../enums/filtering-operator.enum";

export class SearchFieldFilter {
/**
* The name of the field to filter.
*/
field: string;

/**
* The value to filter.
*/
value: string;

/**
* The operator to use to filter.
*/
operator: FilteringOperatorEnum;

constructor(field: string, operator: FilteringOperatorEnum, value: string) {
this.field = field;
this.value = value;
this.operator = operator;
}
}
24 changes: 24 additions & 0 deletions packages/mysql-common/src/models/search-query.model.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {SearchQueryField} from "./search-query-field.model";
import {SortOrderEnum} from "../enums/sort-order.enum";
import {SearchQueryParametersInterface} from "../interfaces/search-query-parameters.interface";
import {SearchFieldFilter} from "./search-field-filter.model";
import {FilteringOperatorEnum} from "../enums/filtering-operator.enum";

export class SearchQuery {
/**
Expand All @@ -25,6 +27,11 @@ export class SearchQuery {
*/
query?: string;

/**
* The filters to apply to the search.
*/
filters: SearchFieldFilter[] = [];

constructor(options?: Partial<SearchQuery>) {
this.fields = options?.fields ?? [];
this.page = options?.page ?? 1;
Expand All @@ -41,6 +48,14 @@ export class SearchQuery {
return this.fields.find((field) => field.field === fieldName);
}

/**
* This method adds a filter to the search query.
* @param filter
*/
addFilter(filter: SearchFieldFilter) {
this.filters.push(filter);
}

clearSort(field: string) {
const fieldToClear = this.getField(field);

Expand Down Expand Up @@ -119,6 +134,14 @@ export class SearchQuery {
});
}

if(queryStrings.filters) {
queryStrings.filters.split(",").forEach((filter) => {
const [fieldName, operator, value] = filter.split(":");

this.addFilter(new SearchFieldFilter(fieldName, operator as FilteringOperatorEnum, value));
});
}

if(queryStrings.excludeFieldsFromResponse) {
queryStrings.excludeFieldsFromResponse?.split(",").forEach((fieldName) => {
const field = this.getField(fieldName);
Expand All @@ -144,6 +167,7 @@ export class SearchQuery {
excludeFieldsFromResponse: this.fields.filter((field) => field.exclude).map((field) => field.field).join(","),
fields: this.fields.filter((field) => field.includeExplicitly).map((field) => field.field).join(","),
sort: this.fields.filter((field) => field.order !== undefined).map((field) => `${field.field}:${field.order}`).join(","),
filters: this.filters.map((filter) => `${filter.field}:${filter.operator}:${filter.value}`).join(",")
};
}
}
38 changes: 37 additions & 1 deletion packages/mysql/src/clients/mysql.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {MysqlModuleKeyname} from "../mysql.module.keyname";
import {createPool, Pool} from "mysql2/promise";
import {LogHandlerInterface} from "@pristine-ts/logging";
import {DataMapper} from "@pristine-ts/data-mapping-common";
import {SearchQuery, SearchResult} from "@pristine-ts/mysql-common";
import {SearchQuery, SearchResult, FilteringOperatorEnum} from "@pristine-ts/mysql-common";
import {ServiceDefinitionTagEnum, tag} from "@pristine-ts/common";
import {MysqlConfig} from "../configs/mysql.config";
import {MysqlConfigProviderInterface} from "../interfaces/mysql-config-provider.interface";
Expand Down Expand Up @@ -342,6 +342,42 @@ export class MysqlClient implements MysqlClientInterface {
fieldsToSearch.forEach(field => sqlValues.push("%" + query.query + "%"));
}

if(query.filters.length > 0) {
query.filters.forEach(filter => {
const column = this.getColumnName(classType, filter.field);

let operator = null;

switch (filter.operator as FilteringOperatorEnum) {
case FilteringOperatorEnum.Equal:
operator = "=";
break;
case FilteringOperatorEnum.NotEqual:
operator = "!=";
break;
case FilteringOperatorEnum.GreaterThan:
operator = ">";
break;
case FilteringOperatorEnum.GreaterThanOrEqual:
operator = ">=";
break;
case FilteringOperatorEnum.LessThan:
operator = "<";
break;
case FilteringOperatorEnum.LessThanOrEqual:
operator = "<=";
break;
}

if(operator === null) {
return;
}

sql += " AND " + column + " " + operator + " ?";
sqlValues.push(filter.value);
});
}

//
// ORDERING
//
Expand Down
28 changes: 14 additions & 14 deletions packages/mysql/src/interfaces/mysql-client.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import {SearchQuery, SearchResult} from "@pristine-ts/mysql-common";
export interface MysqlClientInterface {
/**
* This method returns a pool of connections to the database.
* @param databaseName
* @param configUniqueKeyname
* @param force
*/
getPool(databaseName: string, force: boolean): Promise<Pool>;
getPool(configUniqueKeyname: string, force: boolean): Promise<Pool>;

/**
* This method returns the table metadata for a given class.
Expand Down Expand Up @@ -52,11 +52,11 @@ export interface MysqlClientInterface {

/**
* This method returns the column name for a given class and property name.
* @param databaseName
* @param configUniqueKeyname
* @param sqlStatement
* @param values
*/
executeSql(databaseName: string, sqlStatement: string, values: any[]): Promise<any>
executeSql(configUniqueKeyname: string, sqlStatement: string, values: any[]): Promise<any>

/**
* This method maps the results to a given class type.
Expand All @@ -67,39 +67,39 @@ export interface MysqlClientInterface {

/**
* This method returns a single element from the database.
* @param databaseName
* @param configUniqueKeyname
* @param classType
* @param primaryKey
*/
get<T extends { [key: string]: any; }>(databaseName: string, classType: { new(): T; }, primaryKey: string | number): Promise<T | null>
get<T extends { [key: string]: any; }>(configUniqueKeyname: string, classType: { new(): T; }, primaryKey: string | number): Promise<T | null>

/**
* This method creates a new element in the database.
* @param databaseName
* @param configUniqueKeyname
* @param element
*/
create<T extends { [key: string]: any; }>(databaseName: string, element: T): Promise<void>
create<T extends { [key: string]: any; }>(configUniqueKeyname: string, element: T): Promise<void>

/**
* This method updates an element in the database.
* @param databaseName
* @param configUniqueKeyname
* @param element
*/
update<T extends { [key: string]: any; }>(databaseName: string, element: T): Promise<void>
update<T extends { [key: string]: any; }>(configUniqueKeyname: string, element: T): Promise<void>

/**
* This method deletes an element in the database.
* @param databaseName
* @param configUniqueKeyname
* @param classType
* @param primaryKey
*/
delete<T extends { [key: string]: any; }>(databaseName: string, classType: { new(): T; }, primaryKey: string | number): Promise<void>
delete<T extends { [key: string]: any; }>(configUniqueKeyname: string, classType: { new(): T; }, primaryKey: string | number): Promise<void>

/**
* This method searches the database.
* @param databaseName
* @param configUniqueKeyname
* @param classType
* @param query
*/
search<T extends { [key: string]: any; }>(databaseName: string, classType: { new(): T; }, query: SearchQuery): Promise<SearchResult<T>>
search<T extends { [key: string]: any; }>(configUniqueKeyname: string, classType: { new(): T; }, query: SearchQuery): Promise<SearchResult<T>>
}
Loading