Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(v10) toJSON special cases #3356

Merged
merged 12 commits into from
Oct 29, 2020
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
x.x.x Release notes (yyyy-MM-dd)
=============================================================
### Enhancements
* None.

### Fixed
* Fixed an issue in `toJSON()`, in combination with primaryKeys, where data from another table could be returned. ([#3331](https://github.com/realm/realm-js/issues/3331), since v10.0.0)
* Fixed an issue in `toJSON()` where `data` would output as `{}`, it now returns the data base64 encoded. ([#3356](https://github.com/realm/realm-js/pull/3356), since v10.0.0)

### Compatibility
* MongoDB Realm Cloud.
* APIs are backwards compatible with all previous releases of Realm JavaScript in the 10.x.y series.
* File format: generates Realms with format v20 (reads and upgrades file format v5 or later for non-synced Realm, upgrades file format v10 for synced Realms).

### Internal
* None.

10.0.1 Release notes (2020-10-16)
==============================================================
NOTE: Support for syncing with realm.cloud.io and/or Realm Object Server has been replaced with support for syncing with MongoDB Realm Cloud.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,47 +1,52 @@
[
{
"$refId": "Person#5f086d00ddf69c48082eb63b",
"_id": "5f086d00ddf69c48082eb63b",
"age": 42,
"name": "John Doe",
"friends": [
{
"$ref": "5f086d00ddf69c48082eb63b"
"$ref": "Person#5f086d00ddf69c48082eb63b"
},
{
"$refId": "Person#5f086d00ddf69c48082eb63d",
"_id": "5f086d00ddf69c48082eb63d",
"age": 40,
"name": "Jane Doe",
"friends": [
{
"$refId": "Person#5f086d00ddf69c48082eb63f",
"_id": "5f086d00ddf69c48082eb63f",
"age": 35,
"name": "Tony Doe",
"friends": [
{
"$ref": "5f086d00ddf69c48082eb63d"
"$ref": "Person#5f086d00ddf69c48082eb63d"
}
]
},
{
"$ref": "5f086d00ddf69c48082eb63b"
"$ref": "Person#5f086d00ddf69c48082eb63b"
}
]
},
{
"$refId": "Person#5f086d00ddf69c48082eb63f",
"_id": "5f086d00ddf69c48082eb63f",
"age": 35,
"name": "Tony Doe",
"friends": [
{
"$refId": "Person#5f086d00ddf69c48082eb63d",
"_id": "5f086d00ddf69c48082eb63d",
"age": 40,
"name": "Jane Doe",
"friends": [
{
"$ref": "5f086d00ddf69c48082eb63f"
"$ref": "Person#5f086d00ddf69c48082eb63f"
},
{
"$ref": "5f086d00ddf69c48082eb63b"
"$ref": "Person#5f086d00ddf69c48082eb63b"
}
]
}
Expand All @@ -50,38 +55,42 @@
]
},
{
"$refId": "Person#5f086d00ddf69c48082eb63d",
"_id": "5f086d00ddf69c48082eb63d",
"age": 40,
"name": "Jane Doe",
"friends": [
{
"$refId": "Person#5f086d00ddf69c48082eb63f",
"_id": "5f086d00ddf69c48082eb63f",
"age": 35,
"name": "Tony Doe",
"friends": [
{
"$ref": "5f086d00ddf69c48082eb63d"
"$ref": "Person#5f086d00ddf69c48082eb63d"
}
]
},
{
"$refId": "Person#5f086d00ddf69c48082eb63b",
"_id": "5f086d00ddf69c48082eb63b",
"age": 42,
"name": "John Doe",
"friends": [
{
"$ref": "5f086d00ddf69c48082eb63b"
"$ref": "Person#5f086d00ddf69c48082eb63b"
},
{
"$ref": "5f086d00ddf69c48082eb63d"
"$ref": "Person#5f086d00ddf69c48082eb63d"
},
{
"$refId": "Person#5f086d00ddf69c48082eb63f",
"_id": "5f086d00ddf69c48082eb63f",
"age": 35,
"name": "Tony Doe",
"friends": [
{
"$ref": "5f086d00ddf69c48082eb63d"
"$ref": "Person#5f086d00ddf69c48082eb63d"
}
]
}
Expand All @@ -90,31 +99,34 @@
]
},
{
"$refId": "Person#5f086d00ddf69c48082eb63f",
"_id": "5f086d00ddf69c48082eb63f",
"age": 35,
"name": "Tony Doe",
"friends": [
{
"$refId": "Person#5f086d00ddf69c48082eb63d",
"_id": "5f086d00ddf69c48082eb63d",
"age": 40,
"name": "Jane Doe",
"friends": [
{
"$ref": "5f086d00ddf69c48082eb63f"
"$ref": "Person#5f086d00ddf69c48082eb63f"
},
{
"$refId": "Person#5f086d00ddf69c48082eb63b",
"_id": "5f086d00ddf69c48082eb63b",
"age": 42,
"name": "John Doe",
"friends": [
{
"$ref": "5f086d00ddf69c48082eb63b"
"$ref": "Person#5f086d00ddf69c48082eb63b"
},
{
"$ref": "5f086d00ddf69c48082eb63d"
"$ref": "Person#5f086d00ddf69c48082eb63d"
},
{
"$ref": "5f086d00ddf69c48082eb63f"
"$ref": "Person#5f086d00ddf69c48082eb63f"
}
]
}
Expand Down
38 changes: 21 additions & 17 deletions lib/extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,8 @@ module.exports = function(realmConstructor, context) {
value: function (_, cache = new Map()) {
// Construct a reference-id of table-name & primaryKey if it exists, or fall back to objectId.
const { name: schemaName, primaryKey } = this.objectSchema();
const id = primaryKey
? serializedPrimaryKeyValue(this[primaryKey])
: schemaName + this._objectId();
const id = schemaName + "#" +
(primaryKey ? serializedPrimaryKeyValue(this[primaryKey]) : this._objectId());

// Check if current objectId has already processed, to keep object references the same.
const existing = cache.get(id);
Expand All @@ -119,22 +118,30 @@ module.exports = function(realmConstructor, context) {
const result = {};
cache.set(id, result);

if (primaryKey) {
// Expose the key holding the primaryKey, as a non-enumerable prop, checkable at serialization.
Object.defineProperty(result, "_primaryKeyProp", { value: primaryKey });
} else {
// Add the generated reference-id, as a non-enumerable prop '$refId', for later exposure though e.g. Realm.JsonSerializationReplacer.
Object.defineProperty(result, "$refId", { value: id, configurable: true });
}
// Add the generated reference-id, as a non-enumerable prop '$refId', for later exposure though e.g. Realm.JsonSerializationReplacer.
Object.defineProperty(result, "$refId", { value: id, configurable: true });

// Move all enumerable keys to result, triggering any specific toJSON implementation in the process.
Object.keys(this)
.concat(Object.keys(Object.getPrototypeOf(this)))
.forEach(key => {
const value = this[key];
result[key] = value instanceof realmConstructor.Object || value instanceof realmConstructor.Collection
? value.toJSON(key, cache)
: value;

// skip any functions & constructors (in case of class models).
if (typeof existing === "function") {
return; // continue
}

if (value instanceof realmConstructor.Object || value instanceof realmConstructor.Collection) {
// recursively trigger `toJSON` for Realm instances with the same cache.
result[key] = value.toJSON(key, cache);
} else if (value instanceof ArrayBuffer) {
// convert "data" to base64 strings.
result[key] = Buffer.from(value).toString("base64");
} else {
// default to original value.
result[key] = value;
}
});

return result;
Expand Down Expand Up @@ -568,11 +575,8 @@ module.exports = function(realmConstructor, context) {

if (seen.includes(value)) {
// If we have seen the current value before, return a reference-structure if possible.
if (value._primaryKeyProp) {
return { $ref: value[value._primaryKeyProp] };
}
if (value.$refId) {
return { $ref: value.$refId };
return { $ref: value.$refId };
}
return "[Circular reference]";
}
Expand Down