如何为对象(或字典)的属性定义以数组为值的领域模式



我的数据看起来像这个

PersonJSONData = {
"key1": {
"name": "odo",
"age": 10,
"favorites": {
"food": ["rice", "chocolate", "sugar"],
"game": [],
"color": ["red"]
},
"key2": {
"name": "yana",
"age": 50,
"favorites": {
"band": [],
"food": ["eggs"],
"book": ["ABC", "how to cook"]
}
},
...
}}

如何在realm中为react native编写架构?

const personSchema = {
name: "Person",
properties: {
name: string,
age: int,
// favorites: I don't know what to write here??
}
}

我试着使用类型字典("{}"(,但它给了我一个错误:

[错误:混合属性不能包含值数组。]

并且当我使用类型";混合的";我得到这个错误:

[错误:只支持领域实例。]

我需要为此创建一个对象类型吗?如果是这样,当我不确定收藏夹中的关键点是什么时,该怎么做?

这是我创建和编写实例的代码。

const PersonInstance = new Realm(schema: [personSchema] })
function writePerson(){
const personKeys = Object.keys(PersonJSONData)
try { 
personKeys.forEach((key) => {
const { name, age, favorites } = PersonJSONData[key]

PersonInstance.write(() => {
PersonInstance.create('Person', {
name, age, favorites
})}

})
} catch(err) {
// error handling
}
}

还是应该改为更改写入数据库的方式?有人能帮我吗?提前谢谢。

const PERSON_SCHEMA = {
name: 'person_schema',
properties: {
id: 'int?',
data: {type: 'list', objectType: 'person_schema_data'},
},
primaryKey: 'id',
};
const PERSON_SCHEMA_DATA = {
name: 'person_schema_data',
properties: {
name: 'string?',
age: 'int?',
favorites: {type: 'object', objectType: 'person_favorites'},
},
};
const PERSON_SCHEMA_FAVORITES = {
name: 'person_favorites',
properties: {
food: {type: 'list', objectType: 'string'},
game: {type: 'list', objectType: 'string'},
color: {type: 'list', objectType: 'string'},
},
};
const realm = await Realm.open({
path:`person_schema_path`,
schema:[PERSON_SCHEMA,PERSON_SCHEMA_DATA,PERSON_SCHEMA_FAVORITES]
})

相关内容

  • 没有找到相关文章

最新更新