I have a model like this, to be able to store details field as JSON
class Person {
set details(data) { this._details = data ? JSON.stringify(data) : null; }
get details() { return this._details ? JSON.parse(this._details) : null; }
}
Person.schema = {
name: 'Person',
primaryKey: 'id',
properties: {
id: 'string',
name: 'string',
_details: { type: 'string', optional: true }
}
};
Now when I do
realm.write(() => {
// this is a write block for some other objects, but at some point I need to find a person
let person = realm.objectForPrimaryKey('Person', id);
console.log(person);
});
It will log out an instance of RealmObject with getters and setters for _details but nothing for details, so its not an instance of Person, am I missing something here?
I also tried class Person extends Realm.Object { ... } but nothing changed. I read the documentation and assumed it should work this way, and I hope it does otherwise I'll have to change a lot of code.
Temporarily fixed (hacked) it by doing
let _person = realm.objectForPrimaryKey('Person', id);
if (_person) {
let person = new Person();
_.assign(person, _person);
}
Any better suggestions?
Can you share the code where you initialize your Realm? You need to pass your object constructor rather than a string in the schema array in order to get your objects back. So if you are initializing your realm as
var realm = new Realm({schema: [Person]})
then in theory everything should work as you expect.
@alazier yes I'm doing the same...
// models/Person/index.js
class Person { ... }
Person.schema = { ... }
export default Person;
and then
import Person from './models/Person'
const realm = new Realm({ schema: [ Person ], schemaVersion: 1 });
// and the rest
So I just tested this out by adding a custom property to the Todo object in the example:
class Todo extends Realm.Object {
set details(data) { console.log("set"); }
get details() { console.log("get"); return 1; }
}
Todo.schema = {
name: 'Todo',
primaryKey: 'text',
properties: {
done: {type: 'bool', default: false},
text: 'string',
},
};
Both the getter and setter seem to work in both chrome debug mode and normally, but when printing the object in non debug mode the custom property is not printed out (it does show up in the chrome console). So I think this is just an error with console/printing the object and custom properties should work as intended.
Weird, for me there were no class getters / setters, and person.details was always undefined, @alazier how did you query the object? using realm.objectForPrimaryKey( ... ) or realm.objects( ... ).filtered( ... )?
I tried both. If you share your project I can try to take a look. You can send stuff to us privately at [email protected].
thank you @alazier for now I'm just gonna close this issue, I've already used some "dirty" workarounds to get it working, probably will try again in a few months after updating react-native and realm
I made that working by doing something like
export default class RealmModel {
static types = {
bool: true,
string: true,
int: true,
boolean: true,
float: true,
double: true,
date: true
}
toString() {
return JSON.stringify(this.toJSON());
}
toJSON() {
let schema = this.constructor.schema.properties;
let json = {};
for (var varId in schema) {
if (schema.hasOwnProperty(varId)) {
let type = null;
if(schema[varId] instanceof Object){
type = schema[varId]['type'];
} else {
type = schema[varId];
}
if(RealmModel.types[type]) {
json[varId] = this[varId];
} else if(type === 'list') {
let obj = this[varId];
json[varId] = [];
for(let i = 0; i < obj.length; i++){
json[varId].push(obj[i].toJSON());
}
} else {
json[varId] = this[varId].toJSON();
}
}
}
return json;
}
}
And then in the class definition
import RealmModel from './RealmModel';
class EventType extends RealmModel {
}
EventType.schema = {
name: 'EventType',
primaryKey: 'id',
properties: {
id: 'string',
name: 'string',
description: {type: 'string', optional: true},
eventCategories: {type: 'list', objectType: 'EventCategory', default: []}
}
};
export default EventType;
I just use eventType.toJSON() and I get
{ id: '1',
name: 'Visita',
description: 'Visitas a la obra',
eventCategories:
[ { id: '1', name: 'Tecnicas', description: 'Visitas tecnicas' },
{ id: '2', name: 'Filial', description: 'Visitas familiares' } ] }
@alexsotocx how to check the types for int?[ ] or string?[ ] ...
Most helpful comment
I made that working by doing something like
And then in the class definition
I just use
eventType.toJSON()and I get