Repository: js-data/js-data
Pull request #480 (2017-09-15 18:05:00)

fix: dont throw when an inverse relation doesn't exist
Just a small bug fix for an issue I ran into when upgrading to v3

- [x] - `npm test` succeeds
- [x] - Code coverage does not decrease (if any source code was changed)
- [ ] - If applicable, appropriate JSDoc comments were updated in source code (if applicable)
- [ ] - If applicable, approprate changes to js-data.io docs have been suggested ("Suggest Edits" button)


codecov[bot]: # [Codecov](https://codecov.io/gh/js-data/js-data/pull/480?src=pr&el=h1) Report
> Merging [#480](https://codecov.io/gh/js-data/js-data/pull/480?src=pr&el=desc) into [master](https://codecov.io/gh/js-data/js-data/commit/c3d102a5b255578cd91684ec26f43ffee5044e10?src=pr&el=desc) will **not change** coverage.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/js-data/js-data/pull/480/graphs/tree.svg?width=650&src=pr&height=150&token=Vb67ZBEgrc)](https://codecov.io/gh/js-data/js-data/pull/480?src=pr&el=tree)

```diff
@@           Coverage Diff           @@
##           master     #480   +/-   ##
=======================================
  Coverage   95.83%   95.83%           
=======================================
  Files          21       21           
  Lines        2523     2523           
=======================================
  Hits         2418     2418           
  Misses        105      105
```


| [Impacted Files](https://codecov.io/gh/js-data/js-data/pull/480?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [src/DataStore.js](https://codecov.io/gh/js-data/js-data/pull/480?src=pr&el=tree#diff-c3JjL0RhdGFTdG9yZS5qcw==) | `96.83% <100%> (ø)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/js-data/js-data/pull/480?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/js-data/js-data/pull/480?src=pr&el=footer). Last update [c3d102a...bc3fb73](https://codecov.io/gh/js-data/js-data/pull/480?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/src/DataStore.js b/src/DataStore.js
index c567e02..48bc33b 100644
--- a/src/DataStore.js
+++ b/src/DataStore.js
@@ -178,7 +178,7 @@ const props = {
           const inverseDef = def.getInverse(mapper)
           const currentParentId = currentParent ? utils.get(currentParent, def.getRelation().idAttribute) : undefined
 
-          if (currentParent && currentParentId !== undefined && currentParentId !== value) {
+          if (inverseDef && currentParent && currentParentId !== undefined && currentParentId !== value) {
             if (inverseDef.type === hasOneType) {
               safeSetLink(currentParent, inverseDef.localField, undefined)
             } else if (inverseDef.type === hasManyType) {


Test patch:
diff --git a/test/unit/collection/add.test.js b/test/unit/collection/add.test.js
index 777334d..b532a49 100644
--- a/test/unit/collection/add.test.js
+++ b/test/unit/collection/add.test.js
@@ -298,4 +298,27 @@ describe('Collection#add', function () {
     // console.log('\tinject 10,000 users time taken: ', new Date().getTime() - start, 'ms')
     // console.log('\tusers age 40-44', User.between(40, 45, { index: 'age' }).length)
   })
+  it('should update a records relation when the inverse relation does not exist', function () {
+    const store = new JSData.DataStore()
+    store.defineMapper('user')
+    store.defineMapper('post', {
+      relations: {
+        belongsTo: {
+          user: {
+            localKey: 'user_id',
+            localField: 'user'
+          }
+        }
+      }
+    })
+    const [user1, user2] = store.add('user', [{
+      id: 1, name: 'John'
+    }, {
+      id: 2, name: 'Jane'
+    }])
+    const post = store.add('post', { id: 2, title: 'foo', user_id: 1 })
+    assert.strictEqual(post.user, user1)
+    store.add('post', { id: 2, title: 'foo', user_id: 2 })
+    assert.strictEqual(post.user, user2)
+  })
 })