-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5a26dd1
commit 3186680
Showing
7 changed files
with
178 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* eslint-disable @typescript-eslint/explicit-member-accessibility */ | ||
/* eslint-disable import/extensions */ | ||
import { QueryOrderMap } from '@mikro-orm/core'; | ||
import { SortOrderMap } from '../../../shared/interface/find-options'; | ||
import { PersonDo } from '../domain/person.do'; | ||
import { PersonEntity } from './person.entity'; | ||
|
||
export class PersonSortingMapper { | ||
static mapDOSortOrderToQueryOrder(sort: SortOrderMap<PersonDo<boolean>>): QueryOrderMap<PersonEntity> { | ||
const queryOrderMap: SortOrderMap<PersonEntity | undefined> = { | ||
id: sort.id, | ||
firstName: sort.firstName, | ||
lastName: sort.lastName, | ||
birthDate: sort.birthDate, | ||
}; | ||
Object.keys(queryOrderMap) | ||
.filter((key) => queryOrderMap[key] === undefined) | ||
.forEach((key) => delete queryOrderMap[key]); | ||
return queryOrderMap; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* eslint-disable @typescript-eslint/explicit-member-accessibility */ | ||
|
||
// TODO should Refactor with german | ||
import { Scope } from '../../../shared/repo/scope.js'; | ||
import { PersonEntity } from './person.entity.js'; | ||
export class PersonScope extends Scope<PersonEntity> { | ||
byFirstName(firstName: string | undefined): this { | ||
if (firstName) { | ||
this.addQuery({ firstName: { $re: firstName } }); | ||
} | ||
return this; | ||
} | ||
|
||
byLastName(lastName: string | undefined): this { | ||
if (lastName) { | ||
this.addQuery({ lastName: { $re: lastName } }); | ||
} | ||
return this; | ||
} | ||
|
||
byBirthDate(birthDate: Date | undefined): this { | ||
if (birthDate) { | ||
this.addQuery({ birthDate }); | ||
} | ||
return this; | ||
} | ||
|
||
/* byBirthPlace(birthPlace: string | undefined): this { | ||
if (birthPlace) { | ||
this.addQuery({ birthPlace }); | ||
} | ||
return this; | ||
} */ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
export interface IPagination { | ||
skip?: number; | ||
limit?: number; | ||
} | ||
|
||
export enum SortOrder { | ||
asc = 'asc', | ||
desc = 'desc', | ||
} | ||
|
||
export type SortOrderMap<T> = Partial<Record<keyof T, SortOrder>>; | ||
|
||
export interface IFindOptions<T> { | ||
pagination?: IPagination; | ||
order?: SortOrderMap<T>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export class Page<T> { | ||
public data: T[]; | ||
|
||
public total: number; | ||
|
||
public constructor(data: T[], total: number) { | ||
this.data = data; | ||
this.total = total; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/** | ||
* When this query is added ($and) to an existing query, | ||
* it should ensure an empty result | ||
*/ | ||
export const EmptyResultQuery = { $and: [{ id: false }] }; | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* eslint-disable @typescript-eslint/explicit-member-accessibility */ | ||
/* eslint-disable no-underscore-dangle */ | ||
|
||
import { FilterQuery } from '@mikro-orm/core'; | ||
import { EmptyResultQuery } from '../query/empty-result.query.js'; | ||
|
||
type EmptyResultQueryType = typeof EmptyResultQuery; | ||
|
||
type ScopeOperator = '$and' | '$or'; | ||
|
||
export class Scope<T> { | ||
private _queries: FilterQuery<T | EmptyResultQueryType>[] = []; | ||
|
||
private _operator: ScopeOperator; | ||
|
||
private _allowEmptyQuery: boolean; | ||
|
||
constructor(operator: ScopeOperator = '$and') { | ||
this._operator = operator; | ||
this._allowEmptyQuery = false; | ||
} | ||
|
||
get query(): FilterQuery<T> { | ||
if (this._queries.length === 0) { | ||
if (this._allowEmptyQuery) { | ||
return {} as FilterQuery<T>; | ||
} | ||
return EmptyResultQuery as FilterQuery<T>; | ||
} | ||
// eslint-disable-next-line @typescript-eslint/typedef | ||
const query = this._queries.length > 1 ? { [this._operator]: this._queries } : this._queries[0]; | ||
return query as FilterQuery<T>; | ||
} | ||
|
||
addQuery(query: FilterQuery<T | EmptyResultQueryType>): void { | ||
this._queries.push(query); | ||
} | ||
|
||
allowEmptyQuery(isEmptyQueryAllowed: boolean): Scope<T> { | ||
this._allowEmptyQuery = isEmptyQueryAllowed; | ||
return this; | ||
} | ||
} |