I have data that looks like this
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"]
}
},
...
}}
How do I write the schema in realm
for react native
?
const personSchema = {
name: "Person",
properties: {
name: string,
age: int,
// favorites: I don't know what to write here??
}
}
I tried using the type dictionary ("{}") but it's giving me an error:
[Error: A mixed property cannot contain an array of values.]
and when I used the type "mixed" I get this error:
[Error: Only realm instances are supported.]
Do I need to create an object type for that? If so, how to do it when I don't know for sure what the keys in favorites are?
Here is my code to create and write the instance.
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
}
}
or should I change how I write into the database instead? Can anyone help me with this? Thanks in advance.