midwayjs / midway

🍔 A Node.js Serverless Framework for front-end/full-stack developers. Build the application for next decade. Works on AWS, Alibaba Cloud, Tencent Cloud and traditional VM/Container. Super easy integrate with React and Vue. 🌈
https://www.midwayjs.org/
MIT License
7.4k stars 577 forks source link

@midwayjs/orm@2.14.7 支持树结构吗 #3003

Closed jsbay closed 1 year ago

jsbay commented 1 year ago

如题 @midwayjs/orm@2.14.7 支持树结构吗? 或者 其他版本的 @midwayjs/orm 支持树结构吗? 应该怎么声明 树实体 及使用?

czy88840616 commented 1 year ago

3.x @midwayjs/typeorm 会在声明 TreeRepository 类型的时候调用 getTreeRepository。 老的 orm 可以参考 https://github.com/midwayjs/midway/blob/2.x/packages/orm/test/fixtures/base-fn/src/configuration.ts#LL32C12-L32C25

jsbay commented 1 year ago

版本 @midwayjs/orm@2.14.7;

我尝试在 users.entity.ts 中 定义:

//  src/entity/users.entity.ts
import { EntityModel } from '@midwayjs/orm';
import {
  Column,
  PrimaryGeneratedColumn,
  ManyToOne,
  OneToMany,
} from 'typeorm';

@EntityModel('users')
export class UsersEntity {
  @PrimaryGeneratedColumn('uuid', {
    comment: '自增 ID',
  })
  uid?: string;

  // name
  @Column({
    name: 'name',
    comment: '姓名',
    unique: true,
  })
  name: string;

  @ManyToOne(type => UsersEntity, usersEntity => usersEntity.children)
  @JoinColumn({
    name: 'parent_uid',
  })
  parent: UsersEntity;

  @OneToMany(type => UsersEntity, usersEntity => usersEntity.parent)
  children: UsersEntity[];
}

并在 user.service.ts 中使用

// src/service/user.service.ts
import { Provide, App } from '@midwayjs/decorator';
import { InjectEntityModel } from '@midwayjs/orm';
import { TreeRepository } from 'typeorm';

import { UsersEntity } from '../entity/users.entity';

@Provide()
export class UsersService {
  @App()
  app: Application;

  @InjectEntityModel(UsersEntity)
  usersModel: TreeRepository<UsersEntity>;
  /**
   * 返回 子节点
   */
  async findTrees() {
    // 下面的代码 会报错 this.usersModel.findTrees is not a function
    return await this.usersModel.findTrees();
  }
}

在 2.x 的 @midwayjs/orm 并未找到 任何 getTreeRepository 有效使用, 我可以理解为 您的意思是 2.x 不支持吗?

czy88840616 commented 1 year ago

我给链接了呀,

@Inject('orm: getTreeRepository') getTreeRepository: getTreeRepository;

const newUser = this.getTreeRepository(UserRepository);

jsbay commented 1 year ago

好的, 谢谢

github-actions[bot] commented 1 year ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.