Mobile Bytes #9 : Realm React for React Native

Thanks to you @Andrew_Meyer my realm setup is almost done. But there are few remainings things that I would like to ask / understand :

  • I read that I can use an arrow function as default value when defining the schema, is there a way in this function to access a value defined in another field ? Because since I am keeping an history of changes I need 2 IDs fields in my objets, they must be the same at creation (and then after updates only one is changing).
    So I would like to make something like :
rowId: { type: 'uuid', default: () => new BSON.UUID() }
branchId: { type: 'uuid', default: () => this.rowId }, // error because _this_ is not the realm object...`
  • I also have a user Id in my objects, this id is retrieved from an async function, but that does not seems to be supported, any workaround for those “async” situations ?

authorId: { type: 'uuid', default: () => async () => await User.userId() ?? new UUID('00000000-0000-4000-8000-000000000000') }

  • What would be the equivalent of willSave / didSave methods of Core Data framework ? Previously using Core Data I was able, thanks to these methods to performs things like : updating last modification time / author, duplicate the row being saved (in order to keep my changes history). perform encryption (at least with Realm I won’t have to do that part :))

  • The support of object classes by Realm is also unclear for me. If I have a class “A” with a non static method “method”, why do I get an “undefined” error when performing this kind of code :

const aObjects = realm.objects<A>(A.schema.name)
aObjects[0].method()

By the way, I am wondering if it is a good architecture to have helper methods for some queries inside those model classes files because they needs the Realm Instance to perform them but in my case those files are also included by the schema.ts, which is included by the RealmInstance.ts, so I end up having a require cycle, right ?

  • And the last question: what is the difference between these 2 way to define Realm object classes ?

export abstract class MyRealmClass extends Realm.Object
VS
export abstract class MyRealmClass extends Realm.Object<MyRealmClass>

Thanks for your answers :slight_smile:

Hello, that would really help me to have an answer on those questions :slight_smile:

Thanks.

1 Like

Hi @Julien_Curro, sorry for the late response. There is a lot here to unpack and I must have got distracted :sweat_smile:

Unfortunately no, if you need to do this, then I would not use default values for this:

const id = new BSON.UUID();
realm.create('Item', { _id: id, rowId: id });

No, once again, if you are trying to do something more complex for default values, then this would best be done outside of the model.

One could register a listener to update this on change, but this could result in an infinite loop if not careful.
But in the end, we have no equivalent for these methods. You will have to do this manually.

That should work, but you should make the following changes:

const aObjects = realm.objects(A);
aObjects[0].method();

Seems fine to me, but you will most likely have to provide realm as an argument to these methods. The architecture seems logical if you want to reuse a complex query somewhere else.

We have experimental support for creating realm objects using the constructor of the class rather than realm.create.

realm.write(() => {
const newObject = new Object(realm, { _id: "123", name: "charles" });
})

The second definition will ensure the return type from new Object is correctly typed. If you omit this, then it will be typed as Realm.Object<unknown> instead of Realm.Object<MyRealmClass>.
You can ignore this and use realm.create if you so choose.

Thanks for your time and answers.