Skip to content

Commit

Permalink
Merge branch 'master' into release
Browse files Browse the repository at this point in the history
  • Loading branch information
jmdobry committed Aug 4, 2016
2 parents ea26bb5 + fe9d4cb commit 0c96838
Show file tree
Hide file tree
Showing 13 changed files with 172 additions and 95 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Expand Up @@ -5,6 +5,7 @@
#
# The email address is not required for organizations.
#
Alex <alexander.maznev@gmail.com>
antoinebrault <antoinebrault@gmail.com>
David Madner <david.madner@gmx.net>
InternalFX <bryan@internalfx.com>
Expand Down
8 changes: 8 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,11 @@
##### 3.0.0-rc.4 - 03 August 2016

###### Bug fixes
- #376 - Fix unlinking of records
- #379 fixes #380 - fix(hasOne): do not create links for undefined or null keys by @nickescallon
- #382 fixes #381 - check that inverseDef exists before attempting to setup Inverse for belongsTo by @pik
- #384 - DataStore does not correctly call super

##### 3.0.0-rc.3 - 25 July 2016

###### Bug fixes
Expand Down
10 changes: 5 additions & 5 deletions package.json
@@ -1,7 +1,7 @@
{
"name": "js-data",
"description": "Robust, framework-agnostic in-memory data store.",
"version": "3.0.0-rc.3",
"version": "3.0.0-rc.4",
"homepage": "http://www.js-data.io",
"repository": {
"type": "git",
Expand Down Expand Up @@ -55,7 +55,7 @@
"doc": "jsdoc -c conf.json src && node scripts/cleanup.js",
"lint": "repo-tools lint src/**/*.js test/**/*.js scripts/*.js lib/**/*.js *.config.js",
"bundle:es5": "rollup src/index.js -c -o dist/js-data.js -m dist/js-data.js.map -f umd",
"bundle:next": "rollup src/index.js -c -o dist/js-data.es2015.js -m dist/js-data.es2015.js.map -f es6",
"bundle:next": "rollup src/index.js -c -o dist/js-data.es2015.js -m dist/js-data.es2015.js.map -f es",
"bundle": "npm run bundle:es5 && npm run bundle:next && repo-tools write-version dist/js-data.js dist/js-data.es2015.js",
"min": "uglifyjs dist/js-data.js -o dist/js-data.min.js --source-map dist/js-data.min.map --source-map-url js-data.min.map -v -m -c --keep-fnames --screw-ie8",
"banner": "node scripts/banner.js",
Expand All @@ -69,18 +69,18 @@
},
"devDependencies": {
"babel-plugin-syntax-async-functions": "6.8.0",
"babel-plugin-transform-es2015-modules-umd": "6.8.0",
"babel-plugin-transform-es2015-modules-umd": "6.12.0",
"babel-plugin-transform-regenerator": "6.11.4",
"js-data-repo-tools": "0.5.5",
"karma": "1.1.1",
"karma": "1.1.2",
"karma-babel-preprocessor": "6.0.1",
"karma-chai": "0.1.0",
"karma-chrome-launcher": "1.0.1",
"karma-mocha": "1.1.1",
"karma-phantomjs-launcher": "1.0.1",
"karma-sauce-launcher": "1.0.0",
"karma-sinon": "1.0.5",
"phantomjs-prebuilt": "2.1.7",
"phantomjs-prebuilt": "2.1.10",
"uglify-js": "2.7.0"
}
}
82 changes: 21 additions & 61 deletions src/DataStore.js
@@ -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 @@ -394,10 +379,6 @@ function DataStore (opts) {
const props = {
constructor: DataStore,

_callSuper (method, ...args) {
return this.constructor.__super__.prototype[method].apply(this, args)
},

/**
* Internal method used to handle Mapper responses.
*
Expand Down Expand Up @@ -872,7 +853,7 @@ const props = {
*/
create (name, record, opts) {
opts || (opts = {})
return this._callSuper('create', name, record, opts)
return Container.prototype.create.call(this, name, record, opts)
.then((result) => this._end(name, result, opts))
},

Expand Down Expand Up @@ -969,7 +950,7 @@ const props = {
*/
createMany (name, records, opts) {
opts || (opts = {})
return this._callSuper('createMany', name, records, opts)
return Container.prototype.createMany.call(this, name, records, opts)
.then((result) => this._end(name, result, opts))
},

Expand Down Expand Up @@ -1046,19 +1027,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 +1047,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 Expand Up @@ -1316,13 +1276,13 @@ const props = {
return current
}
const inverseLocalField = def.getInverse(mapper).localField
// Update (unset) inverse relation
if (current) {
safeSetProp(current, foreignKey, undefined)
self.getCollection(relation).updateIndex(current, updateOpts)
safeSetLink(current, inverseLocalField, undefined)
}
if (record) {
// Update (unset) inverse relation
if (current) {
safeSetProp(current, foreignKey, undefined)
self.getCollection(relation).updateIndex(current, updateOpts)
safeSetLink(current, inverseLocalField, undefined)
}
const relatedId = utils.get(record, def.getRelation().idAttribute)
// Prefer store record
if (relatedId !== undefined) {
Expand Down Expand Up @@ -1459,7 +1419,7 @@ const props = {
*/
destroy (name, id, opts) {
opts || (opts = {})
return this._callSuper('destroy', name, id, opts).then((result) => {
return Container.prototype.destroy.call(this, name, id, opts).then((result) => {
const record = this.getCollection(name).remove(id, opts)

if (record && this.unlinkOnDestroy) {
Expand Down Expand Up @@ -1572,7 +1532,7 @@ const props = {
*/
destroyAll (name, query, opts) {
opts || (opts = {})
return this._callSuper('destroyAll', name, query, opts).then((result) => {
return Container.prototype.destroyAll.call(this, name, query, opts).then((result) => {
const records = this.getCollection(name).removeAll(query, opts)

if (records && records.length && this.unlinkOnDestroy) {
Expand Down Expand Up @@ -1703,7 +1663,7 @@ const props = {
let promise

if (opts.force || !item) {
promise = this._pendingQueries[name][id] = this._callSuper('find', name, id, opts).then((result) => {
promise = this._pendingQueries[name][id] = Container.prototype.find.call(this, name, id, opts).then((result) => {
delete this._pendingQueries[name][id]
result = this._end(name, result, opts)
this.cacheFind(name, result, id, opts)
Expand Down Expand Up @@ -1816,7 +1776,7 @@ const props = {
let promise

if (opts.force || !items) {
promise = this._pendingQueries[name][hash] = this._callSuper('findAll', name, query, opts).then((result) => {
promise = this._pendingQueries[name][hash] = Container.prototype.findAll.call(this, name, query, opts).then((result) => {
delete this._pendingQueries[name][hash]
result = this._end(name, result, opts)
this.cacheFindAll(name, result, hash, opts)
Expand Down Expand Up @@ -2101,7 +2061,7 @@ const props = {
*/
update (name, id, record, opts) {
opts || (opts = {})
return this._callSuper('update', name, id, record, opts)
return Container.prototype.update.call(this, name, id, record, opts)
.then((result) => this._end(name, result, opts))
},

Expand Down Expand Up @@ -2193,7 +2153,7 @@ const props = {
*/
updateAll (name, props, query, opts) {
opts || (opts = {})
return this._callSuper('updateAll', name, query, props, opts)
return Container.prototype.updateAll.call(this, name, query, props, opts)
.then((result) => this._end(name, result, opts))
},

Expand Down Expand Up @@ -2285,7 +2245,7 @@ const props = {
*/
updateMany (name, records, opts) {
opts || (opts = {})
return this._callSuper('updateMany', name, records, opts)
return Container.prototype.updateMany.call(this, name, records, opts)
.then((result) => this._end(name, result, opts))
}
}
Expand Down
4 changes: 0 additions & 4 deletions src/LinkedCollection.js
Expand Up @@ -100,8 +100,6 @@ export default Collection.extend({
}

if (mapper.relationList.length && record) {
// Check the currently visited record for relations that need to be
// inserted into their respective collections.
mapper.relationList.forEach(function (def) {
def.removeLinkedRecords(mapper, [record])
})
Expand All @@ -116,8 +114,6 @@ export default Collection.extend({
records.forEach(this._clearMeta, this)

if (mapper.relationList.length && records.length) {
// Check the currently visited record for relations that need to be
// inserted into their respective collections.
mapper.relationList.forEach(function (def) {
def.removeLinkedRecords(mapper, records)
})
Expand Down
37 changes: 36 additions & 1 deletion src/Record.js
@@ -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
12 changes: 3 additions & 9 deletions src/Relation.js
Expand Up @@ -155,19 +155,10 @@ utils.addHiddenPropsToTarget(Relation.prototype, {
removeLinkedRecords (relatedMapper, records) {
const localField = this.localField
records.forEach((record) => {
const relatedData = utils.get(record, localField)
this.unlinkInverseRecords(relatedData)
utils.set(record, localField, undefined)
})
},

unlinkInverseRecords (record) {
if (!record) {
return
}
utils.set(record, this.getInverse(this.mapper).localField, undefined)
},

linkRecord (record, relatedRecord) {
const relatedId = utils.get(relatedRecord, this.mapper.idAttribute)

Expand All @@ -193,6 +184,9 @@ utils.addHiddenPropsToTarget(Relation.prototype, {

// e.g. user hasMany post via "foreignKey", so find all posts of user
findExistingLinksByForeignKey (id) {
if (id === undefined || id === null) {
return
}
return this.relatedCollection.filter({
[this.foreignKey]: id
})
Expand Down
10 changes: 0 additions & 10 deletions src/Relation/HasMany.js
Expand Up @@ -17,16 +17,6 @@ export const HasManyRelation = Relation.extend({
return !!(hasForeignKeys || (this.localKeys && utils.get(record, this.localKeys)))
},

unlinkInverseRecords (records) {
if (!records) {
return
}
const localField = this.getInverse(this.mapper).localField
records.forEach(function (record) {
utils.set(record, localField, undefined)
})
},

linkRecord (record, relatedRecords) {
const relatedCollection = this.relatedCollection
const canAutoAddLinks = this.canAutoAddLinks
Expand Down
2 changes: 1 addition & 1 deletion src/Relation/HasOne.js
Expand Up @@ -6,7 +6,7 @@ export const HasOneRelation = Relation.extend({
const recordId = utils.get(record, relatedMapper.idAttribute)
const records = this.findExistingLinksByForeignKey(recordId)

if (records.length) {
if (records && records.length) {
return records[0]
}
}
Expand Down
17 changes: 17 additions & 0 deletions src/utils.js
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

0 comments on commit 0c96838

Please sign in to comment.