8000 check that inverseDef exists before attempting to setup Inverse for belongsTo by pik · Pull Request #382 · js-data/js-data · GitHub
[go: up one dir, main page]

Skip to content

check that inverseDef exists before attempting to setup Inverse for belongsTo #382

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

Merged
merged 3 commits into from
Aug 3, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 6 additions & 42 deletions src/DataStore.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import utils from './utils'
import utils, { safeSetLink, safeSetProp } from './utils'

import {
belongsToType,
hasManyType,
Expand Down Expand Up @@ -261,22 +262,6 @@ const ownMethodsForScoping = [
'hashQuery'
]

const safeSetProp = function (record, field, value) {
if (record && record._set) {
record._set(`props.${field}`, value)
} else {
utils.set(record, field, value)
}
}

const safeSetLink = function (record, field, value) {
if (record && record._set) {
record._set(`links.${field}`, value)
} else {
utils.set(record, field, value)
}
}

const cachedFn = function (name, hashOrId, opts) {
const cached = this._completedQueries[name][hashOrId]
if (utils.isFunction(cached)) {
Expand Down Expand Up @@ -1046,19 +1031,8 @@ const props = {

// e.g. profile.user !== someUser
// or comment.post !== somePost
if (currentParent) {
// e.g. otherUser.profile = undefined
if (inverseDef.type === hasOneType) {
safeSetLink(currentParent, inverseDef.localField, undefined)
} else if (inverseDef.type === hasManyType) {
// e.g. remove comment from otherPost.comments
const children = utils.get(currentParent, inverseDef.localField)
if (id === undefined) {
utils.remove(children, (child) => child === this)
} else {
utils.remove(children, (child) => child === this || id === utils.get(child, idAttribute))
}
}
if (currentParent && inverseDef) {
this.removeInverseRelation(currentParent, id, inverseDef, idAttribute)
}
if (record) {
// e.g. profile.user = someUser
Expand All @@ -1077,18 +1051,8 @@ const props = {
safeSetProp(this, foreignKey, relatedId)
collection.updateIndex(this, updateOpts)

// Update (set) inverse relation
if (inverseDef.type === hasOneType) {
// e.g. someUser.profile = profile
safeSetLink(record, inverseDef.localField, this)
} else if (inverseDef.type === hasManyType) {
// e.g. add comment to somePost.comments
const children = utils.get(record, inverseDef.localField)
if (id === undefined) {
utils.noDupeAdd(children, this, (child) => child === this)
} else {
utils.noDupeAdd(children, this, (child) => child === this || id === utils.get(child, idAttribute))
}
if (inverseDef) {
this.setupInverseRelation(record, id, inverseDef, idAttribute)
}
} else {
// Unset in-memory link only
Expand Down
37 changes: 36 additions & 1 deletion src/Record.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import utils from './utils'
import utils, { safeSetLink } from './utils'
import Component from './Component'
import Settable from './Settable'
import {
belongsToType,
hasManyType,
hasOneType
} from './decorators'

const DOMAIN = 'Record'

Expand Down Expand Up @@ -386,6 +391,36 @@ export default Component.extend({
return !this._mapper().validate(this, opts)
},

removeInverseRelation(currentParent, id, inverseDef, idAttribute) {
if (inverseDef.type === hasOneType) {
safeSetLink(currentParent, inverseDef.localField, undefined)
} else if (inverseDef.type === hasManyType) {
// e.g. remove comment from otherPost.comments
const children = utils.get(currentParent, inverseDef.localField)
if (id === undefined) {
utils.remove(children, (child) => child === this)
} else {
utils.remove(children, (child) => child === this || id === utils.get(child, idAttribute))
}
}
},

setupInverseRelation(record, id, inverseDef, idAttribute) {
// Update (set) inverse relation
if (inverseDef.type === hasOneType) {
// e.g. someUser.profile = profile
safeSetLink(record, inverseDef.localField, this)
} else if (inverseDef.type === hasManyType) {
// e.g. add comment to somePost.comments
const children = utils.get(record, inverseDef.localField)
if (id === undefined) {
utils.noDupeAdd(children, this, (child) => child === this)
} else {
utils.noDupeAdd(children, this, (child) => child === this || id === utils.get(child, idAttribute))
}
}
},

/**
* Lazy load relations of this record, to be attached to the record once their
* loaded.
Expand Down
17 changes: 17 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -1576,6 +1576,23 @@ http://www.js-data.io/v3.0/docs/errors#${code}`
}

object[last] = undefined
},

}

export const safeSetProp = function (record, field, value) {
if (record && record._set) {
record._set(`props.${field}`, value)
} else {
utils.set(record, field, value)
}
}

export const safeSetLink = function (record, field, value) {
if (record && record._set) {
record._set(`links.${field}`, value)
} else {
utils.set(record, field, value)
}
}

Expand Down
20 changes: 20 additions & 0 deletions test/unit/decorators/belongsTo.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,26 @@ describe('JSData.belongsTo', function () {
assert.strictEqual(bar2.foo, foo)
assert.strictEqual(bar3.foo, foo)
})

it ('should not create an inverseLink if no inverseRelationship is defined', function() {
const store = new JSData.DataStore()
store.defineMapper('foo', {
})
67AC store.defineMapper('bar', {
relations: {
belongsTo: {
foo: {
localField: '_foo',
foreignKey: 'foo_id'
}
}
}
})
const foo = store.add('foo', { id: 1 })
const bar = store.add('bar', { id: 1, foo_id: 1 })
assert.strictEqual(bar._foo, foo)
})

it('should add property accessors to prototype of target and allow relation re-assignment using customizations', function () {
const store = new JSData.DataStore()
store.defineMapper('foo', {
Expand Down
0