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

Values are replaces by default value when updating

      How frequently does the bug occur?

      Always

      Description

      When you update a record and the schema includes default values, any properties that are not explicitly provided in the update will be replaced with their default values.

      Note: This only happens after upgrading to ```v12```

      Schema

      {
              name: 'MySchema',
              primaryKey: 'id',
              properties: {
                  id: { type: 'int' },
                  fieldOne: { type: 'string' },
                  fieldTwo:  { type: 'string', default: 'DEFAULT_VALUE' },
              },
      }
      

      Creating the record

      realm.create("MySchema", {id: 1337, fieldOne: "SOME_VALUE", fieldTwo: "NOT_DEFAULT_VALUE" })
      

      Updating the record

      realm.create("MySchema", {id: 1337, fieldOne: "SOME_OTHER_VALUE" },  Realm.UpdateMode.All)
      

      This will cause the value of the ``fieldTwo`{{ field to be updated to }}`DEFAULT_VALUE``, even though we didn't explicitly specify this property when performing the update.

      from docs:

      Unable to find source-code formatter for language: diff. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      declare enum UpdateMode {
          /**
           * Objects are only created. If an existing object exists, an exception is thrown.
           */
          Never = "never",
          /**
           * If an existing object exists, only properties where the value has actually
           * changed will be updated. This improves notifications and server side
           * performance but also have implications for how changes across devices are
           * merged. For most use cases, the behavior will match the intuitive behavior
           * of how changes should be merged, but if updating an entire object is
           * considered an atomic operation, this mode should not be used.
           */
          Modified = "modified",
          /**
      +     * If an existing object is found, all properties provided will be updated,
      +     * any other properties will remain unchanged.     <==========
           */
          All = "all"
      }
      

      Expected behaviour

      Default values should be used only when the property is missing, and this should happen exclusively when you're creating a new record that doesn't already exist.

      Version

      12.1.0

      What services are you using?

      Local Database only

      Are you using encryption?

      Yes

      Platform OS and version(s)

      iOS 16.4

            Assignee:
            kenneth.geisshirt@mongodb.com Kenneth Geisshirt (Inactive)
            Reporter:
            unitosyncbot Unito Sync Bot
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: