@nestjs-filter-grammar/mikroorm
v0.1.3
Published
MikroORM adapter for @nestjs-filter-grammar — translates FilterTree into MikroORM FilterQuery objects
Maintainers
Readme
@nestjs-filter-grammar/mikroorm
MikroORM adapter for nestjs-filter-grammar. Translates FilterTree and SortEntry[] into MikroORM FilterQuery and QueryOrderMap objects.
Installation
npm install @nestjs-filter-grammar/mikroorm @nestjs-filter-grammar/corePeer Dependencies
@mikro-orm/core>= 6.0.0
API
applyFilter(tree, options?): Record<string, MikroOrmQueryValue>
Converts a FilterTree into a MikroORM FilterQuery object.
import { applyFilter } from '@nestjs-filter-grammar/mikroorm';
const users = await em.find(User, applyFilter(filterTree));Output examples:
| Input | Output |
|-------|--------|
| status=active | { status: { $eq: 'active' } } |
| status!=deleted | { status: { $ne: 'deleted' } } |
| status=active,pending | { status: { $in: ['active', 'pending'] } } |
| name*~john | { name: { $ilike: '%john%' } } |
| status=active;age>=18 | { $and: [{ status: { $eq: 'active' } }, { age: { $gte: '18' } }] } |
| status=active\|status=pending | { $or: [{ status: { $eq: 'active' } }, { status: { $eq: 'pending' } }] } |
applySort(entries, options?): Record<string, MikroOrmQueryValue>
Converts SortEntry[] into a MikroORM QueryOrderMap.
import { applySort } from '@nestjs-filter-grammar/mikroorm';
const users = await em.find(User, {}, { orderBy: applySort(sortEntries) });
// orderBy: { name: 'asc', age: 'desc' }Column Mapping
String rename:
applyFilter(tree, {
columnMap: { name: 'userName' },
});
// { userName: { $eq: 'John' } }Callback for nested relations:
applyFilter(tree, {
columnMap: {
department: (operator, values) => ({
department: { name: { $eq: values[0].value } },
}),
},
});Sort column mapping:
applySort(entries, {
columnMap: {
department: (direction) => ({ department: { name: direction } }),
},
});
// { department: { name: 'asc' } }Full Example
@Controller('users')
export class UsersController {
constructor(private readonly em: EntityManager) {}
@Get()
async findAll(@Filter(UserQuery, { optional: true }) { filter, sort }: FilterResult) {
return this.em.find(User,
filter ? applyFilter(filter) : {},
{ orderBy: sort ? applySort(sort) : {} },
);
}
}