realm / realm-js

Realm is a mobile database: an alternative to SQLite & key-value stores
https://realm.io
Apache License 2.0
5.72k stars 564 forks source link

toJSON() serialization of BSON.ObjectId to String #6783

Open bgever opened 1 month ago

bgever commented 1 month ago

Problem

I'm using toJSON() on my RealmObject's to get serializable state which I can use in Redux.

BSON.ObjectId's appear to be serialized to an Object which when called with toString() returns the hex string of the ID.

However I was expecting to get a String instead, since I would be able to use a string as key in a Map, to allow fast lookups, whereas the Object comparisons don't work for equality when retrieved from different documents.

Solution

I'm not sure if there's an intent behind serializing the BSON.ObjectId as an Object, but it would be great if they could be serialized as strings instead, similar to BSON.ObjectId.toHexString().

Alternatives

Currently I'm overriding toJSON() on my RealmObjects's and manually serializing to string:

  toJSON() {
    const o = super.toJSON() as MyObject;
    o._id = o._id.toString();
    o.category._id = o.category._id.toString();
    return o;
  }

How important is this improvement for you?

I would like to have it but have a workaround

Feature would mainly be used with

Atlas Device Sync

sync-by-unito[bot] commented 1 month ago

➤ PM Bot commented:

Jira ticket: RJS-2860