What I’m trying to do is save a table and his following fields , but these table’s are defined by an API response and the fields are predeterminated, but I cannot figure out how to make it works properlly since the table is variable, I will show you some code:
Here is my model where I call the realm to open my table , as you can see there is a variable called table
and this is where I send a string which is the name of the table that I want to open.
export const getRealm = async(table) => {
const tabela = {
name: table,
properties: {
field: "string",
fieldtype: "string",
}
}
try {
return await Realm.open(
{
schema: [tabela],
deleteRealmIfMigrationNeeded: true,
}
);
} catch (error) {
console.log('ERRRRROOOOOOOR REALM',error.message);
}
};
When I want to store some data into my realm, with the controller realmAction.js
, my dados
is a JSON
and my objeto
is the name of the table which is a string
export const FieldsSchema = async (dados,objeto) => {
try {
dados.map(
async (item) => {
await getRealm(objeto).then((realm) => {
const data = {
field: item.field,
fieldtype: item.fieldtype,
};
realm.write(() => {
realm.create(objeto, data, 'modified');
});
// realm.close(); Closing results me in an error which is `Cannot access realm that has been closed` cause its a map function and send more than once the same table string
});
}
)
} catch (error) {
console.log('ERROOOR', error.message);
}
};
What seems to be wrong is that realm cannot handle oppening two different tables in the same thread , but as I read in the mongo/realm documentation I need to seet a model schema , but my models are a variable which the users create , someone knows how can I handle this ?