Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

支持 TypeORM #56

Closed
minjs1cn opened this issue Dec 18, 2024 · 2 comments
Closed

支持 TypeORM #56

minjs1cn opened this issue Dec 18, 2024 · 2 comments

Comments

@minjs1cn
Copy link

minjs1cn commented Dec 18, 2024

背景

使用TypeORM来操作Mysql数据库,使用装饰器来定义Entity

/**
 * 用户
 */
@Entity('sys_user')
export class UserEntity extends BaseEntity {
  @ManyToOne(() => DepartmentEntity, (department) => department)
  @JoinColumn()
  department: DepartmentEntity;

  @ManyToMany(() => RoleEntity)
  @JoinTable()
  roles: RoleEntity[];

  @Column({ comment: '邮箱' })
  email: string;

  @Column({ comment: '密码' })
  password: string;

  @Column({ comment: '昵称' })
  nickname: string;

  @Column({
    comment: '状态 0-禁用 1-启用',
    type: 'enum',
    enum: UserStatus,
    default: UserStatus.Enabled,
  })
  status: UserStatus;
}

将Entit作为类型使用

export type ResGetData= UserEntity;

启动报错,信息如下:

⨯ Cannot find reference target '../entities/sys/user.entity/UserEntity'
at ResGetData
at PtlGetData
⨯ ServiceProto 生成失败,请检查 TypeScript 代码是否编译报错: src/shared/protocols/serviceProto.ts

看起来装饰器的Entity在当前环境无法正确编译

@minjs1cn
Copy link
Author

minjs1cn commented Dec 18, 2024

已解决部分问题

  1. protocols放弃引用entity的定义
  2. tsconfig增加配置
{
  "compilerOptions": {
    "experimentalDecorators": true,
    "strictPropertyInitialization": false,
    "emitDecoratorMetadata": true
  }
}

可以正常运行,但是如何去复用entity定义的类型呢?看起来就是复杂的entity定义无法顺利编译出proto文件

@k8w
Copy link
Owner

k8w commented Dec 18, 2024

是的,目前还没有支持 class 类型,支持 TypeORM 在 Todo List 中...

当然,如果使用 MongoDB 的话,可以无需 ORM...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants