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

Support Map data types. #685

Closed
salithashamood1 opened this issue Jun 27, 2022 · 25 comments · Fixed by #1406
Closed

Support Map data types. #685

salithashamood1 opened this issue Jun 27, 2022 · 25 comments · Fixed by #1406
Assignees

Comments

@salithashamood1
Copy link

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

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

@nielsenko
Copy link
Contributor

nielsenko commented Jun 27, 2022

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 nielsenko changed the title Problem in realm data types. Support Map data types. Jun 27, 2022
@nielsenko nielsenko self-assigned this Jun 27, 2022
@toshiossada
Copy link

whats is GA?

@toshiossada
Copy link

Is this already works?

@nirinchev
Copy link
Member

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.

@toshiossada
Copy link

do they have a deadline ?

@nirinchev
Copy link
Member

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.

@d95sld95
Copy link

Any update on Maps being supported?

@blagoev
Copy link
Contributor

blagoev commented Jun 26, 2023

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

@plotic
Copy link

plotic commented Aug 10, 2023

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

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

@blagoev
Copy link
Contributor

blagoev commented Aug 10, 2023

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

@MICKEY88661
Copy link

MICKEY88661 commented Sep 7, 2023

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

@nielsenko
Copy link
Contributor

nielsenko commented Sep 7, 2023

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

@MICKEY88661
Copy link

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

@sync-by-unito sync-by-unito bot assigned nielsenko and unassigned blagoev Sep 19, 2023
@florinleu
Copy link

Hey guys. Do we have any updates on this?

@nirinchev
Copy link
Member

Not yet, unfortunately.

@Shreedhar73
Copy link

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

@nielsenko
Copy link
Contributor

nielsenko commented Jan 26, 2024

This will land with #1469 (soon)

@nirinchev
Copy link
Member

This is not supported today, but we're working on it. You can follow this PR: #1469, which adds the ability to store collections inside RealmValue.

@Shreedhar73
Copy link

@nirinchev thanks . Is there any ETA ?

@nirinchev
Copy link
Member

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.

@Shreedhar73
Copy link

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?

@nirinchev
Copy link
Member

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.

@Shreedhar73
Copy link

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
Copy link

Shreedhar73 commented Mar 21, 2024

@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'
   }
} 

@nirinchev
Copy link
Member

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 20, 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.

10 participants