Uploaded image for project: 'Realm JavaScript SDK'
  1. Realm JavaScript SDK
  2. RJS-245

[Feature Request] Convert a plain object with nested lists to something that can be passed to Realm.

      Goals

      The goal is to pass plain objects (Realm Object converted to JSON and back) to realm, but the nested collections are parsed as objects instead of arrays, see the example below.

      simple example schema

      exports.Settings = {  
          name: 'Settings',  
          primaryKey: 'Id',  
          properties: {  
              Id: 'string',  
              User: 'User',  
              FavoriteChannels: 'Channel[]',  
          }  
      };
      
      exports.User = {  
          name: 'User',  
          primaryKey: 'Id',  
          properties: {  
              Id: 'string',  
              FirstName: 'string?',  
              LastName: 'string?',  
              PhoneNumbers: 'PhoneNumber[]'  
          }  
      };
      
      exports.PhoneNumber = {  
          name: 'PhoneNumber',  
          primaryKey: 'Id',  
          properties: {  
              Id: 'string',  
              Number: 'string',  
          }  
      };
      
      exports.Channel = {  
          name: 'Channel',  
          primaryKey: 'Id',  
          properties: {  
              Id: 'string',  
              ChannelName: 'string?',  
              Users: 'User[]',  
          }  
      };
      

      "settings" - the received plain object

      realm.create('Settings', {  
          Id: settings.Id,  
          User: settings.User,  
          FavoriteChannels: settings.FavoriteChannels,  
      }, true)
      

      If I try to pass "settings" directly in "realm.create", I get the following error:
      User.PhoneNumbers must be of type 'PhoneNumber[]', got 'object' ([object Object])

      Obviously, it expects an array, but the plain object's collections are deserialized to objects (ex. {{PhoneNumbers:

      { '0': [Object] }

      }}). There's no way for the deserializer to know how to parse Realm Object's "List {}" (ex. {{[PhoneNumbers]: List

      { [0]: [Object] }

      }}) to arrays.

      Here's a related stackoverflow post:
      https://stackoverflow.com/questions/51488133/convert-a-complex-plain-object-to-realm-object

      Expected Results

      A function that can convert the plain object's nested "object collections" to arrays, so they can be passed to Realm or some other workaround.

            Assignee:
            Unassigned Unassigned
            Reporter:
            unitosyncbot Unito Sync Bot
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: