Mikro-orm:嵌套对象(不是实体)中的关系

时间:2020-08-22 18:11:55

标签: mongodb mikro-orm

我想知道嵌套对象内部是否可能存在(由Orm管理的)关系。
在以下情况下:

abstract class BaseEntity {
    @PrimaryKey()
    _id!: ObjectID;
    @SerializedPrimaryKey()
    id!: string;
}

@Entity({ tableName: 'users' })
class User extends BaseEntity {
    @Property()
    name!: string;
    @OneToMany(() => Post, 'author')
    posts = new Collection<Post>(this);
}

@Entity({ tableName: 'posts' })
class Post extends BaseEntity {
    // bidirectional relation with users collection:
    @ManyToOne(() => User)
    author!: IdentifiedReference<User>;
    @Property()
    comments?: Comment[];
}

class Comment {
    // unidirectional relation with users collection:
    @ManyToOne(() => User)
    author!: IdentifiedReference<User>;
    @Property()
    content!: string;
}

数据库单篇文章中的内容如下:

enter image description here

但是对于mikro-orm Comment来说,作者只是ObjectID而不是对用户实体的引用:

Post {
    _id: ObjectId('5f41575784cc27344cafecbe'),
        user: Ref<User> { _id: ObjectId('5f41575784cc27344cafecbd') }, // ref to user entity
        title: 'Sample post',
        comments: [
        {
            author: ObjectId('5f41575784cc27344cafecbd'), // just a id :<
            content: 'Sample comment'
        }
    ]
}

我做错什么了吗?或者根本就不可能吗?

我同时尝试了mikro-orm v3.6.15和@ mikro-orm / core,@ mikro-orm / mongodb v4.0.0-rc.2。

0 个答案:

没有答案