I am working with Mikro-ORM in NestJS, and have been able to do most things. I have abstracted my data services out of the controller services, so that different engines (or sources) could be used. I am unable to generate the orderBy clauses though, as these seem to require a QueryOrderMap of my entity. However, in specifying the sort order, which is an enum, will not map against my entity field types.
For instance, I have an entity defined:
@Entity({ tableName: 'users'})
@Index({ name: 'users_userid', properties: ['UserId'] })
export class UserEntity
@Property({ length: 20 }) @Unique() public UserId: string;
@Property({ length: 32 }) @Unique() public Name: string;
@Property({ length: 64 }) @Unique() public Email: string;
@Property() public Active: boolean;
@Property() public DailyStatus: boolean;
@Property() public Monitor: boolean;
@Property() public MinimumHoursPerWeek: number;
...
When working with the database, I can use my service, which has the data engine injected into the service with the repository, so that I can have a common class to wrap functions such as find, findOne, create, etc. This allows me to add additional logic in these functions as well, without having to repeat it in each service for each controller.
For instance, the create method can then decide if the record is a duplicate or not, during the insert, and not repeat that in all services. This is all working well. However, I have the find to return records. I now want to sort the return, but the issue I have is that the orderBy clause would be dynamic.
For instance, I want to specify some fields of my Entity that I would sort on. As such, I want to simply pass a string[] with the field names in the order to sort.
Normally, with Mikro-ORM, you do a find, and you can add the orderBy as `{ Active: QueryOption.DESC, UserId: QueryOption.ASC } as part of the FindOptions. From Mikro-ORM:
find<P extends string = never>(where: FilterQuery<T>, options?: FindOptions<T, P>): Promise<Loaded<T, P>[]>;
export interface FindOptions<T, P extends string = never> {
...
orderBy?: (QueryOrderMap<T> & {
0?: never;
}) | QueryOrderMap<T>[];
While I have been able to build the Find easy enough, using the generic , I cannot do the same with the QueryOrderMap, as it is an interface, and in this case, the generic is not my class, but needs the enumerations per field name for sorting.
Again, from Mikro-ORM:
export declare type QueryOrderKeysFlat = QueryOrder | QueryOrderNumeric | keyof typeof QueryOrder;
export declare type QueryOrderKeys<T> = QueryOrderKeysFlat | QueryOrderMap<T>;
export declare type QueryOrderMap<T> = {
[K in keyof T as ExcludeFunctions<T, K>]?: QueryOrderKeys<ExpandProperty<T[K]>>;
};
Any help on how to build the QueryOrderMap would be appreciated.