Created
March 13, 2024 04:52
-
-
Save Jhoem/e9454a86943c7e55f6f14c4e3fb1cbde to your computer and use it in GitHub Desktop.
Mikro orm sortable columns excluding relations
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
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