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

Expose RealmObjectBase.schema #1449

Closed
nirinchev opened this issue Dec 14, 2023 · 2 comments · Fixed by #1540
Closed

Expose RealmObjectBase.schema #1449

nirinchev opened this issue Dec 14, 2023 · 2 comments · Fixed by #1540
Assignees

Comments

@nirinchev
Copy link
Member

nirinchev commented Dec 14, 2023

No description provided.

@mpobrien
Copy link

mpobrien commented Dec 21, 2023

I started poking at this one to get acquainted with the codebase but I may need a little more context on the motivation and desired changes - the generated code for each model already has a static SchemaObject get schema func which returns the SchemaObject for the generated class, so from any model isn't it already be possible to get the schema?

@nirinchev
Copy link
Member Author

nirinchev commented Jan 1, 2024

The motivation is threefold:

  1. When storing an object in a mixed field, having the schema on the object makes it much easier to write code that inspects the schema. Using the generated schema, you would need to do something like:
    final myObj = obj.mixedValue;
    late Schema myObjSchema;
    if (myObj is Foo) {
       myObjSchema = Foo.schema;
    } else if (myObj is Bar) {
       myObjSchema = Bar.schema
    } ...
  2. For generated classes, the generated schema will always reflect the schema that was known at compile time, even if the server made changes to it, making it difficult to evaluate new properties using the dynamic API. For example, if a user has a CustomMetadata embedded class that has Date created, updated fields initially and then the user adds more fields at runtime, the generated schema would not know of the existence of these new fields and one would never be able to introspect the schema and access them using the dynamic API. Note that this may not work immediately, as I don't believe the flutter SDK registers a schema update callback to update its cached Realm schema, but that'd be a separate project.
  3. Finally, for fully dynamic Realm usage, where the schema is read from disk, there'd be no generated schema, meaning the object schema will need to be read through Realm.schema, which is keyed on the class name, but with dynamic classes, we don't have a way to introspect the name to look up the schema. This means the developers need to always pass both the object and its schema as a tuple to be able to access any data in it.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
2 participants