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

Automatic conversion of keys to snake_case #703

Closed
dotjon0 opened this issue Jun 28, 2022 · 3 comments
Closed

Automatic conversion of keys to snake_case #703

dotjon0 opened this issue Jun 28, 2022 · 3 comments

Comments

@dotjon0
Copy link

dotjon0 commented Jun 28, 2022

Within MongoDB Atlas support documentation (not Realm), example model schema usually features snake_case for model properties, for example:

{
    title: `Apples`,
    bsonType: `object`,
    properties: {
      _id: {
        title: `Document ID`,
        bsonType: `objectId`
      },
      apple_type: {
        title: `Apple Type`,
        bsonType: `string`
      },
}

Within flutter, model/class properties are usually camelCase, for example:

@RealmModel()
class _Apples {

  @PrimaryKey()
  @MapTo("_id")
  ObjectId id;
  String appleType;
}

When using Realm Sync for Flutter this of course causes issues as _id does not map to id and appleType does not map to apple_type... Obviously the 'id' scenario would need custom mapping i.e. @MapTo("_id"), but there should be an automatic way to resolve apple_type.

Would the Realm team consider implementing a similar approach to Google's json_serializable package https://pub.dev/packages/json_serializable to get around this? Essentially Google's json_serializable package has a configurable setting 'field_rename' (build.yaml file), which when populated ('snake' for the above example) converts the Flutter class/model to the desired case (in the above example snake_case). Please see an example build.yaml file below:

targets:
  $default:
    builders:
      json_serializable:
        options:
          field_rename: snake

We look forward to your feedback. Best Wishes

@nielsenko
Copy link
Contributor

That sounds like a good idea to me.

@dotjon0
Copy link
Author

dotjon0 commented Jun 28, 2022

Thanks @nielsenko - I wish it was my idea but must give credit to Google! It may be worth looking at the other Build Options Google's package offers (see https://pub.dev/packages/json_serializable#build-configuration) as there may be some other good use cases - although beyond the scope of this issue suggestion as we just need the above...

@blagoev
Copy link
Contributor

blagoev commented Aug 9, 2022

Hi, we have discussed this internally and we think the best way to handle this is through our Dart model generation on the server. When you have an existing snake case named schema on Atlas it will output the correct idiomatic Dart names for the models using the MapTo attributes where needed.

Thanks for the suggestion.

@blagoev blagoev closed this as completed Aug 9, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 14, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants