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

Instance method toJSON() missing from TS definitions #2903

Closed
Dids opened this issue May 29, 2020 · 10 comments · Fixed by #2985
Closed

Instance method toJSON() missing from TS definitions #2903

Dids opened this issue May 29, 2020 · 10 comments · Fixed by #2985

Comments

@Dids
Copy link

Dids commented May 29, 2020

Goals

Realm JS v5.0.0 release notes mention the .toJSON() instance method that should be available on all Realm.Object instances (as far as I can tell).

Expected Results

I expect to be able to serialize Realm objects to a JSON string.

Actual Results

Realm.Object.toJSON() does not exist, anywhere. Not in the API documentation, not in the sources.

Steps to Reproduce

Query for Realm objects and attempt to serialize them.

Code Sample

for (const foo of realm.objects('Foo')) {
  console.log(foo.toJSON())
}

Version of Realm and Tooling

  • Realm JS SDK Version: 6.0.1
  • Node or React Native: Node
  • Client OS & Version: macOS 10.15.5
  • Which debugger for React Native: None
@Dids
Copy link
Author

Dids commented May 29, 2020

Apparently foo.keys()and foo.entries() don't work either, even though the TS definitions are there (runtime exceptions for missing functions). Calling Realm.Object.prototype.keys.call(foo) does work however. I've yet to try this for toJSON(), as it was missing from the TS definitions entirely.

@Dids
Copy link
Author

Dids commented May 29, 2020

Eureka! This seems to work:

(Realm.Object.prototype as any).toJSON.call(foo)

So I guess the real issue is just the missing TS definition for toJSON()?

@Dids Dids changed the title Instance method toJSON() does not exist Instance method toJSON() missing from TS definitions May 29, 2020
@Dids
Copy link
Author

Dids commented May 29, 2020

While I'm at it, is toJSON() supposed to be recursive? Or does it only work at the top level?

@kneth
Copy link
Contributor

kneth commented Jun 15, 2020

@Dids It looks like an oversight from our side. No, toJSON() is not recursive. It is possible to construct cycles with Realm, which means that toJSON() will loop forever. The API documentation could be cleaner on that aspect too!

@Dids
Copy link
Author

Dids commented Jun 15, 2020

@kneth Thanks! Luckily I managed to make my own safe recursive serializer with the help of toJSON().

Before I open a separate issue, are the changes made to the Realm JS change handler documented anywhere? Didn't see much in the release notes here and was wondering about several of the changes, including how the Realm objects referenced with each change event don't seem to have an isValid() method (or property) anymore?

@Dids
Copy link
Author

Dids commented Jun 22, 2020

@steffenagger Were keys() and entries() also fixed?

@steffenagger
Copy link
Contributor

@Dids They should already be fixed in the latest patch-releases (no?). Please note that this toJSON declaration fix has not yet been released: CHANGELOG.

@Dids
Copy link
Author

Dids commented Jun 22, 2020

@steffenagger The declarations are indeed there, but calling them throws an exception at runtime, unless called through the constructor, see this previous comment here: #2903 (comment)

@mattmogford
Copy link

I see it at the top of the CHANGELOG, but with no version number.
Is this released yet on a version?

@steffenagger
Copy link
Contributor

steffenagger commented Jul 9, 2020

@Dids sorry for the late reply. Is Foo an interface (with separate schema) or a class model?

Class models does not automatically get extended with Realm.Object, even though the current TS declaration states it. The correct way for class models would be to extend Realm.Object - but before I waste your time with that approach, then I should inform you that Realm.Object is not currently extendable in TS, due to the declarations..
There's currently a PR draft that solves those issues here: #3013

We are focusing on getting v10 out of beta, so I'm fairly sure these changes will land in v10 beta first (#3044)

@mattmogford No, the missing version number indicates it hasn't yet been release. The upcoming v6 release is still awaiting some changes.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 15, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants