【问题标题】:What is EntityManager, EntityRepository in mikro-orm and difference between that?mikro-orm 中的 EntityManager、EntityRepository 是什么以及它们之间的区别?
【发布时间】:2021-11-17 17:13:09
【问题描述】:

请用非常简单的语言和适当的例子进行解释,我已经按照官方文档进行了但我没有理解清楚。

【问题讨论】:

    标签: node.js orm mikro-orm


    【解决方案1】:

    EntityRepository 是一种从数据库访问实体的便捷方式,因为它处理单个实体,它带有实体名称,因此我们不必在 find()、findAll() 中传递它来电。

    例子:

    import { EntityRepository } from '@mikro-orm/postgresql';
    import { User } from './entities/user.entity';
    
    @Injectable()
    export class UserService {
        constructor(
          @InjectRepository(User)
          private readonly userRepository: EntityRepository<User>,
        ) {}
    
        async getUserByPhone(phone: string) {
          return await this.userRepository.findOne({ phone }); //we are not passing any entity here while making a database call.
        }
    }
    

    EntityManager 处理所有实体。这就是为什么在使用 find() 和 findAll() 调用时,我们需要提及我们想要访问的实体。

    例子:

    return await orm.em.findOne(User, { phone: phoneNumber }); //we have to pass User entity here while making a database call.
    

    【讨论】:

    • 明白了,感谢您的最佳解释:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-20
    • 2019-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-20
    相关资源
    最近更新 更多