Skip to content

Instantly share code, notes, and snippets.

@Jhoem
Created March 13, 2024 04:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jhoem/e9454a86943c7e55f6f14c4e3fb1cbde to your computer and use it in GitHub Desktop.
Save Jhoem/e9454a86943c7e55f6f14c4e3fb1cbde to your computer and use it in GitHub Desktop.
Mikro orm sortable columns excluding relations
if (params.sort.length) {
// entity-level sortable columns only, doesn't include relationships
const sortableColumns: string[] = filter(
this.em.getMetadata(PicklistProductEntity.name).properties,
({ reference }) => reference === 'scalar'
).map(({ name }) => name);
if (params.sort.every((sort) => sortableColumns.includes(sort))) {
options.orderBy = params.sort.reduce((acc, key, i) => {
acc[key] = params.sortDirection
? params.sortDirection[i]
: SortDirection.ASC;
return acc;
}, {});
} else {
throw new BadRequestException(
`Invalid sort column. Must be in ${sortableColumns}`
);
}
} else {
options.orderBy = {
id: SortDirection.ASC,
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment