Skip to content

Commit a2fea62

Browse files
authored
(DOCSP-27544): Flutter MapTo for realm models (#2530)
## Pull Request Info Flutter MapTo for realm models > **Note** > > Intentionally not going into much detail in the description b/c i think each of these topics would take up a lot of space. Whereas here I just am going for a minimalist API description of this utility annotations so that users can decide what they do with it. ### Jira - https://jira.mongodb.org/browse/DOCSP-27544 ### Staged Changes - [Define a Realm Object Schema (Flutter)](https://docs-mongodbcom-staging.corp.mongodb.com/realm/docsworker-xlarge/DOCSP-27544/sdk/flutter/realm-database/model-data/define-realm-object-schema/) ### Reminder Checklist If your PR modifies the docs, you might need to also update some corresponding pages. Check if completed or N/A. - [x] Create Jira ticket for corresponding docs-app-services update(s), if any - [x] Checked/updated Admin API - [x] Checked/updated CLI reference ### Review Guidelines [REVIEWING.md](https://github.com/mongodb/docs-realm/blob/master/REVIEWING.md)
1 parent da17966 commit a2fea62

File tree

4 files changed

+100
-0
lines changed

4 files changed

+100
-0
lines changed

examples/dart/test/define_realm_model_test.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,19 @@ class _Person {
3737
}
3838
// :snippet-end:
3939

40+
// :snippet-start: map-to
41+
@RealmModel()
42+
@MapTo('naval_ship')
43+
class _Boat {
44+
@PrimaryKey()
45+
late ObjectId id;
46+
47+
late String name;
48+
late int? maxKnots;
49+
late int? nauticalMiles;
50+
}
51+
// :snippet-end:
52+
4053
main() {
4154
test('Create a new schema version', () {
4255
// :snippet-start: schema-version

examples/dart/test/define_realm_model_test.g.dart

Lines changed: 57 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
@RealmModel()
2+
@MapTo('naval_ship')
3+
class _Boat {
4+
@PrimaryKey()
5+
late ObjectId id;
6+
7+
late String name;
8+
late int? maxKnots;
9+
late int? nauticalMiles;
10+
}

source/sdk/flutter/realm-database/model-data/define-realm-object-schema.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,26 @@ inverse relationships, and embedding objects within other objects.
187187
To learn more about how to define relationships in your Realm object schema,
188188
refer to :ref:`Relationships <flutter-client-relationships>`.
189189

190+
.. _flutter-map-model-name:
191+
192+
Map Realm Model to Different Name
193+
---------------------------------
194+
195+
You can use the `MapTo <https://pub.dev/documentation/realm_common/latest/realm_common/MapTo-class.html>`__
196+
annotation to map a Realm object model to a different model name in Realm Database.
197+
This can be useful in the following scenarios:
198+
199+
- Working with one realm from different Realm SDKs
200+
- Migrating Realm object models
201+
- Adhering to certain code style conventions
202+
203+
If you're using Atlas Device Sync, the name that you map to corresponds
204+
with the :ref:`App Services Schema <schemas>` name.
205+
206+
.. literalinclude:: /examples/generated/flutter/define_realm_model_test.snippet.map-to.dart
207+
:language: dart
208+
:emphasize-lines: 2
209+
190210
.. _flutter-generate-realm-object:
191211

192212
Generate the RealmObject

0 commit comments

Comments
 (0)