正确使用 IdentifiedReference 和 { wrappedReference: true }
Correct use of IdentifiedReference and { wrappedReference: true }
对于 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): error TS2345: Argument of type '{
wrappedReference: boolean; }' is not assignable to parameter of type
'"id" | "name" | "password" | "email" | "image" | "artist" |
"collections" | "studios" | ((e: AccountEntity) => any) | undefined'.
Object literal may only specify known properties, and
'wrappedReference' does not exist in type '(e: AccountEntity) => any'.
那么,这是正确的吗?
@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>;
}
问题是您正在尝试将 @OneToOne
装饰器的第二个参数用于选项对象,但目前仅支持第一个或第三个参数
// 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
(这是关于可空性,但这是此更改的另一个副作用)
对于 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): error TS2345: Argument of type '{ wrappedReference: boolean; }' is not assignable to parameter of type '"id" | "name" | "password" | "email" | "image" | "artist" | "collections" | "studios" | ((e: AccountEntity) => any) | undefined'. Object literal may only specify known properties, and 'wrappedReference' does not exist in type '(e: AccountEntity) => any'.
那么,这是正确的吗?
@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>;
}
问题是您正在尝试将 @OneToOne
装饰器的第二个参数用于选项对象,但目前仅支持第一个或第三个参数
// 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
(这是关于可空性,但这是此更改的另一个副作用)