使用 mikroorm 和 mongodb 与用户和角色建立多对多关系,过滤不会应用于 Nest js 中的嵌套角色数组中

问题描述 投票:0回答:1

我正在 user.service.ts 中的此查询中的嵌套多对多关系角色中应用过滤器,如下所示:

const users = await this.repo.findAndCount({roles: {slug: 'admin'}});

但是我得到了空记录。怎么了?

这是我的用户模型

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);
}

这是我的榜样

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);
}

此角色存储库

export class RoleRepository extends AppRepository {
// custom methods
}

这是用户仓库

import { User } from "./user.model";
import { AppRepository } from "src/confugrations/database/mikro-orm/apprepository/app.repository";

export class UserRepository extends AppRepository {
// custom methods
}

这是常见的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();
    }
}
node.js mongodb nestjs nest mikro-orm
1个回答
0
投票

在 MikroORM 查询中需要 populate。另外,我会调用变量 [users, count] 而不仅仅是 users,因为您使用的是 findAndCount 方法,该方法返回一个包含用户实体列表和与查询匹配的用户总数的数组。

const [users, count] = await this.repo.findAndCount(
  {
    roles: { slug: 'admin' }
  },
  {
    populate: ['roles']
  }
);

另一种方法是使用 QueryBuilder 创建查询:

const [users, count] = await this.repo.createQueryBuilder('user')
  .leftJoinAndSelect('user.roles', 'role')
  .where({ 'role.slug': 'admin' })
  .getResultAndCount();
© www.soinside.com 2019 - 2024. All rights reserved.