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

Support Map data types.

Open salithashamood1 opened this issue 2 years ago • 1 comments

Can I use Map data type for realm model in flutter?

Like this one, final Map<String, dynamic> properties;

salithashamood1 avatar Jun 27 '22 08:06 salithashamood1

We do not currently support Map (https://www.mongodb.com/docs/atlas/app-services/schemas/types/#std-label-schema-type-dictionary), but we will before GA. There will be some restrictions, such as keys must be String and the values will have the same restrictions as Mixed:

... 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.

In particular realm maps are not directly suitable for storing json.

nielsenko avatar Jun 27 '22 09:06 nielsenko

whats is GA?

toshiossada avatar Feb 14 '23 17:02 toshiossada

Is this already works?

toshiossada avatar Feb 14 '23 17:02 toshiossada

No, this didn't make it into the 1.0 release, but the team is looking to add support for maps in the near future.

nirinchev avatar Feb 15 '23 00:02 nirinchev

do they have a deadline ?

toshiossada avatar Feb 15 '23 14:02 toshiossada

We have a policy not to communicate timelines publicly. If you're using sync/Atlas App Services and this feature is critical for your application, please reach out to your account executive or support and they should be able to give you more concrete answers.

nirinchev avatar Feb 15 '23 21:02 nirinchev

Any update on Maps being supported?

d95sld95 avatar Jun 26 '23 12:06 d95sld95

Hi, this is high priority on our list for the upcoming features.

blagoev avatar Jun 26 '23 13:06 blagoev

Hi, this is high priority on our list for the upcoming features.

Hey @blagoev thanks for the update, is there an ETA ?

plotic avatar Aug 10 '23 10:08 plotic

@plotic As @nirinchev said we have a policy not to discuss timelines, but I can assure you this is on top of our priorities.

blagoev avatar Aug 10 '23 10:08 blagoev

What is the suggested workaround? Should I store keys and values in two different lists?

MICKEY88661 avatar Sep 07 '23 02:09 MICKEY88661

@MICKEY88661 You could do something like this:

import 'package:realm_dart/realm.dart';

part 'fake_map.g.dart';

@RealmModel()
class _MapEntry {
  @PrimaryKey()
  @MapTo('_id')
  late int id; // cannot use key directly
  late String key;
  late RealmValue value;
}

@RealmModel()
class _Stuff {
  @PrimaryKey()
  @MapTo('_id')
  late ObjectId id;

  late Set<_MapEntry> fakeMap;
}

extension on RealmSet<MapEntry> {
  T? mapLookup<T>(String key) {
    return query('key == \$0', [key]).singleOrNull?.value.as<T>();
  }

  void mapInsert<T>(Object parentId, String key, T value) {
    final entry = MapEntry(
      Object.hash(parentId, key),
      key,
      value: RealmValue.from(value),
    );
    realm.add(entry, update: true);
    add(entry);
  }
}

void main(List<String> arguments) {
  final realm = Realm(Configuration.inMemory([Stuff.schema, MapEntry.schema]));
  final stuff = realm.write(() {
    final stuff = Stuff(ObjectId());
    realm.add(stuff);
    stuff.fakeMap
      ..mapInsert(stuff.id, 'a', 'value')
      ..mapInsert(stuff.id, 'b', 1);

    final anotherStuff = Stuff(ObjectId());
    realm.add(anotherStuff);
    anotherStuff.fakeMap
      ..mapInsert(anotherStuff.id, 'a', 'not what you want')
      ..mapInsert(anotherStuff.id, 'b', 666);

    return stuff;
  });

  assert(stuff.fakeMap.mapLookup<String>('a') == 'value');
  assert(stuff.fakeMap.mapLookup<int>('b') == 1);

  realm.all<MapEntry>().forEach((entry) {
    print('${entry.id} ${entry.key} => ${entry.value}');
  });

  Realm.shutdown();
}

If you really need it, but it obviously impacts your schema.

nielsenko avatar Sep 07 '23 09:09 nielsenko

Hi @nirinchev . Thanks for the quick response , I will take it :)

MICKEY88661 avatar Sep 08 '23 02:09 MICKEY88661

Hey guys. Do we have any updates on this?

florinleu avatar Nov 27 '23 16:11 florinleu

Not yet, unfortunately.

nirinchev avatar Nov 28 '23 03:11 nirinchev

Has the support for Nested Maps been provided ?

For eg :

I wanted to store the following data as RealmMap:

final mapData = {
  "title" : 'xyz',
  "age",: 12,
  "dob" : DateTime.now(),
  'metaData' : {
     "a": 'a',
     "b" : 'b'
   }
}

My parentRealm Objects is as ::

final realmObjec = ParentModel{
_id : ObjectId(),
...

mapData : <String,RealmValue>{...},
}

I couldn't figure out how to store , a Map Datatype with in a value of a key in the map . ( I think i can tackle this issue embedded object inside a map instead of nested map , but that is not my preference )

So is it possible to implement nested maps ? (If not , are there any plans to implement that ).

[!NOTE] On the RealmValue class , I saw RealmValue.fromString() ..fromDateTime() and all . Can ..fromMap() be implemented ?

@nirinchev

Shreedhar73 avatar Jan 26 '24 11:01 Shreedhar73

This will land with https://github.com/realm/realm-dart/pull/1469 (soon)

nielsenko avatar Jan 26 '24 11:01 nielsenko

This is not supported today, but we're working on it. You can follow this PR: https://github.com/realm/realm-dart/pull/1469, which adds the ability to store collections inside RealmValue.

nirinchev avatar Jan 26 '24 11:01 nirinchev

@nirinchev thanks . Is there any ETA ?

Shreedhar73 avatar Jan 26 '24 11:01 Shreedhar73

Not yet - it's fairly close to completion for the local database, but if you're using Sync, you'll need to wait a bit longer for server side support.

nirinchev avatar Jan 26 '24 11:01 nirinchev

Not yet - it's fairly close to completion for the local database, but if you're using Sync, you'll need to wait a bit longer for server side support.

@nirinchev Hii, any updates on this?

Shreedhar73 avatar Feb 27 '24 15:02 Shreedhar73

This has been implemented for the local database and we're close to being done for sync. We expect to release it in the next couple of weeks.

nirinchev avatar Feb 27 '24 15:02 nirinchev

This has been implemented for the local database and we're close to being done for sync. We expect to release it in the next couple of weeks.

thanks

Shreedhar73 avatar Feb 27 '24 15:02 Shreedhar73

@nirinchev Seems like, sync for maps has arrived on the stable 2.0.

Still Couldnt figure out how to handle sync for nested Maps .

For Something like :

final mapData = {
  "title" : 'xyz',
  "age",: 12,
  "dob" : DateTime.now(),
  'metaData' : {
     "a": 'a',
     "b" : 'b'
   }
} 

Shreedhar73 avatar Mar 21 '24 11:03 Shreedhar73

We're rolling out sync support, so we haven't enabled it for all customers/apps yet. If you want to test it out asap, you can open a support ticket and ask the team to enable it for your app.

nirinchev avatar Mar 21 '24 16:03 nirinchev