正确使用IdentifiedReference和{wrapedReference:true}

时间:2019-12-11 18:33:17

标签: mikro-orm

对于this,我并没有完全理解它,也不想污染原始问题。我需要始终将{ wrappedReference: true }IdentifiedReference一起使用吗?

因为此失败:

@Entity({ collection: "account" })
export class AccountEntity implements IdEntity<AccountEntity> {
    @PrimaryKey()
    public id!: number;

    @OneToOne(() => ArtistEntity, "account", { wrappedReference: true })
    public artist!: IdentifiedReference<ArtistEntity>;
}

@Entity({ collection: "artist" })
export class ArtistEntity implements IdEntity<ArtistEntity> {
    @PrimaryKey()
    public id!: number;

    @OneToOne(() => AccountEntity, { wrappedReference: true })
    public account!: IdentifiedReference<AccountEntity>;
}

具有:

  

src / entities / artistEntity.ts(15,38):错误TS2345:类型为“ {   包装的参考:布尔值; }'不能分配给类型的参数   '“ id” | “名称” | “密码” | “电子邮件” | “图片” | “艺术家” |   “收藏” | “工作室” | (((e:AccountEntity)=>任何)|未定义”。   对象文字只能指定已知的属性,并且   类型“(e:AccountEntity)=>任何”中不存在“ wrappedReference”。

那么,这正确吗?

@Entity({ collection: "account" })
export class AccountEntity implements IdEntity<AccountEntity> {
    @PrimaryKey()
    public id!: number;

    @OneToOne(() => ArtistEntity, "account", { wrappedReference: true })
    public artist!: IdentifiedReference<ArtistEntity>;
}

@Entity({ collection: "artist" })
export class ArtistEntity implements IdEntity<ArtistEntity> {
    @PrimaryKey()
    public id!: number;

    @OneToOne(() => AccountEntity)
    public account!: IdentifiedReference<AccountEntity>;
}

1 个答案:

答案 0 :(得分:1)

问题是您正在尝试将@OneToOne装饰器的第二个参数用于options对象,但是当前仅支持第一个或第三个参数

// works if you use the default `TsMorphMetadataProvider`
@OneToOne()
public account!: IdentifiedReference<AccountEntity>;

// use first param as options
@OneToOne({ entity: () => AccountEntity, wrappedReference: true })
public account!: IdentifiedReference<AccountEntity>;

// use third param as options
@OneToOne(() => AccountEntity, undefined, { wrappedReference: true })
public account!: IdentifiedReference<AccountEntity>;

// you can also do this if you like
@OneToOne(() => AccountEntity, 'artist', { owner: true, wrappedReference: true })
public account!: IdentifiedReference<AccountEntity>;

但这会在最终的v3版本之前更改,因此,如果您使用TsMorphMetadataProvider(默认值),即使没有显式的wrappedReference: true,它也将始终有效。

这是您可以订阅的有关此即将到来的更改的问题:https://github.com/mikro-orm/mikro-orm/issues/265

(关于可空性,但这是此更改的另一个副作用)