Reputation: 1
I am applying the filter in the nested many to many relations roles in this query in user.service.ts like this:
const users = await this.repo.findAndCount({roles: {slug: 'admin'}});
but I have get empty records. What is wrong?
This is my User model
import { Collection, Entity, EntityRepositoryType, Enum, Filter, ManyToMany, PrimaryKey, Property, Unique } from '@mikro-orm/core';
import { ObjectId } from '@mikro-orm/mongodb';
import { UserRepository } from './user.repository';
import { UserStatus } from 'src/constants/UserStatus';
import { UserType } from 'src/constants/UserType';
import { Role } from 'src/role/role.model';
import { MainEntity } from 'src/helper/db/base.entity';
@entity({ repository: () => UserRepository })
export class User extends MainEntity {
[EntityRepositoryType]? : UserRepository;
@Property()
firstName!: string;
@Property()
lastName!: string;
@Property()
password!: string;
@Property()
@unique()
email!: string;
@Property({nullable: true})
countryCode!: string;
@Property({nullable: true})
phone!: string;
@Property()
@enum(() => UserType)
userType!: string;
@Property()
@enum(() => UserStatus)
status!: UserStatus;
@manytomany(() => Role, role => role.users, { owner: true, eager: true })
roles = new Collection(this);
}
This is my Role model
import { Collection, Entity, EntityRepositoryType, Enum, ManyToMany, PrimaryKey, Property, Unique } from '@mikro-orm/core';
import { ObjectId } from '@mikro-orm/mongodb';
import { RoleRepository } from './role.repository';
import { User } from 'src/user/user.model';
import { RoleEnum } from 'src/constants/Role';
import { Exclude, Expose } from 'class-transformer';
import { Permission } from 'src/permission/permission.model';
import { MainEntity } from 'src/helper/db/base.entity';
@entity({ repository: () => RoleRepository })
export class Role extends MainEntity {
[EntityRepositoryType]?: RoleRepository;
@expose()
@Property({ unique: true })
@enum(() => RoleEnum)
role!: RoleEnum;
@expose()
@Property({ unique: true })
slug!: string;
// @exclude()
@manytomany(() => User, user => user.roles)
users = new Collection(this);
@manytomany(() => Permission, permission => permission.roles, { owner: true, eager: true})
permissions = new Collection(this);
}
This roleRepository
export class RoleRepository extends AppRepository {
// custom methods
}
This is user repo
import { User } from "./user.model";
import { AppRepository } from "src/confugrations/database/mikro-orm/apprepository/app.repository";
export class UserRepository extends AppRepository {
// custom methods
}
This is common AppRepo
import { AnyEntity, EntityManager, EntityRepository, FilterQuery, ObjectId } from "@mikro-orm/mongodb";
export class AppRepository extends EntityRepository {
persist(entity: AnyEntity | AnyEntity[]): EntityManager {
return this.em.persist(entity);
}
async persistAndFlush(entity: AnyEntity | AnyEntity[]): Promise<void> {
await this.em.persistAndFlush(entity);
}
remove(entity: AnyEntity): EntityManager {
return this.em.remove(entity);
}
async removeAndFlush(entity: AnyEntity): Promise<void> {
await this.em.removeAndFlush(entity);
}
async flush(): Promise<void> {
return this.em.flush();
}
}
Upvotes: 0
Views: 56
Reputation: 1
In MikroORM queries expect populate. Also, I would call the variable [users, count] instead of just users because you are using the findAndCount method, which returns an array with the list of user entities and the total number of users that match the query.
const [users, count] = await this.repo.findAndCount(
{
roles: { slug: 'admin' }
},
{
populate: ['roles']
}
);
Another way is to create the query with QueryBuilder:
const [users, count] = await this.repo.createQueryBuilder('user')
.leftJoinAndSelect('user.roles', 'role')
.where({ 'role.slug': 'admin' })
.getResultAndCount();
Upvotes: 0