realm-dart icon indicating copy to clipboard operation
realm-dart copied to clipboard

Support AnyRealmValue

Open dotjon0 opened this issue 2 years ago • 4 comments

Within MongoDB Atlas you can have schema validation that supports more then 1 x type for a field - i.e.:

multi_type_field: {
    title: `Multi Type Field`,
    bsonType: [ `date`, `bool`, `decimal`, `int`, `string` ],
  },

Within Dart/Flutter you can have the same effect by using the 'dynamic' type i.e.:

final dynamic dynamicVariable = '123';

We have also noticed Realm's Swift SDK supports the type AnyRealmValue which appears to support any type (Realm supports) (https://www.mongodb.com/docs/realm/sdk/swift/data-types/anyrealmvalue/#overview).

Of course in a high quality application (in an ideal world) every class property should have a specific type (i.e. not 'dynamic' type), however there will always be edge cases. Would the Realm team consider supporting 'dynamic' types for the classes/models that realm-dart auto generates with perhaps one of the approaches below (no 2 is our preference as it strictly declares what data types can be stored):

1. Declare the 'dynamic' type

@RealmModel()
class _Car {

    late dynamic make;

    late String model;

    int? kilometers = 500;
  }

OR

2. Declare the 'dynamic' type & specify the multiple types accepted

Use json_annotation package https://pub.dev/packages/json_annotation via '@JsonKey(' or similar to pass in the bsonTypes accepted - if the incorrect type is handed into Realm local DB, throwing an exception would be amazing, but this may reach too far for Realm local DB remit:

@RealmModel()
class _Car {

    @JsonKey( bsonType: [ `date`, `bool`, `decimal`, `int`, `string` ] )
    late dynamic make;

    late String model;

    int? kilometers = 500;
 }

The use case for us is that we have some models that are flexible on the data type we are storing so the user can customise their instance of the application.

Looking forward to any thoughts!

dotjon0 avatar Jun 23 '22 18:06 dotjon0

We will eventually support AnyRealmValue (name may differ) in Dart as well, ie Mixed from Atlas App Services (https://www.mongodb.com/docs/atlas/app-services/schemas/types/#std-label-schema-type-mixed).

There are restrictions on what can be stored:

Mixed A mixed field may contain any schema type except for arrays, embedded objects, sets, and dictionaries. App Services does not enforce a consistent type across documents, so two different documents may have values of different types.

We are unlikely to allow models to define dynamic fields though, as we prefer to have static type safety. How would that work for you?

nielsenko avatar Jun 24 '22 13:06 nielsenko

@nielsenko this sounds perfect, thank you so much - the restrictions are completely understandable and we completely agree static type safety is best. Really looking forward to AnyRealmValue and Mixed. Completely get lead times are impossible to predict with development, but can you give us an idea of how far up the road map this is?

dotjon0 avatar Jun 24 '22 19:06 dotjon0

@dotjon0 Our policy prevents me from talking about timelines. Obviously we are heading into summer vacation territory, so I would be surprised if it happens this side of summer.

nielsenko avatar Jun 27 '22 10:06 nielsenko

@nielsenko completely get that, we are the same - thanks for the heads up re summer its appreciated!

dotjon0 avatar Jun 27 '22 12:06 dotjon0

Just wondering if there have been any advances on this?

dotjon0 avatar Oct 31 '22 16:10 dotjon0

@dotjon0 I know that the kotlin team was working through some issues in the realm-core C-API on this as late as today. I assume it will be one of the next thing on our agenda.

nielsenko avatar Oct 31 '22 17:10 nielsenko

Fantastic news @nielsenko, looking forward to it!

dotjon0 avatar Oct 31 '22 17:10 dotjon0