我是 Realm
数据库的新手,这太棒了,我对关系这件事几乎没有怀疑。与其一一提出疑问,我更愿意通过一个例子来理解它。下面给出了从 Realm javascript
文档复制的示例 schema
。
const CarSchema = {
name: 'Car',
properties: {
make: 'string',
model: 'string',
miles: {type: 'int', default: 0},
}
};
const PersonSchema = {
name: 'Person',
properties: {
name: 'string',
birthday: 'date',
cars: 'Car[]'
picture: 'data?', // optional property
}
};
现在问题...
1) 我如何从上面给定的模式中获取拥有特定汽车的人的列表?
2) 我应该给 Car
模式一个 property
,比如 owner: 'Person'
吗?
或者是否有任何反向关系的东西。 (我不知道完美的术语,我是新手 :/)
注意:更多问题可以在评论中提出。
最佳答案
是的,需要定义反比关系。为此,您应该像这样创建架构,
const CarSchema = {
name: 'Car',
properties: {
make: 'string',
model: 'string',
miles: {type: 'int', default: 0},
owner: 'Person'
}
};
const PersonSchema = {
name: 'Person',
properties: {
name: 'string',
birthday: 'date',
cars: {type: 'linkingObjects', objectType: 'Car', property: 'owner'}
picture: 'data?'
}
};
在这里,Person 拥有汽车,其中 Car 对象中的 owner
属性映射到此人。 car.owner
为您提供 Person 对象,person.cars
为您提供此人拥有的所有汽车。
关于javascript - 如何从多个模式中收集数据? - 境界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50628716/