Skip to content
This repository has been archived by the owner on Nov 21, 2024. It is now read-only.

Commit

Permalink
Prepare for 0.7.3
Browse files Browse the repository at this point in the history
  • Loading branch information
jmdobry committed May 26, 2016
1 parent 8ad5171 commit 6c7f5e6
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 14 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
##### 0.7.3 - 25 May 2016

###### Bug fixes
- Relations specified by `opts.with` weren't being passed through

##### 0.7.2 - 17 May 2016

###### Bug fixes
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "js-data-adapter",
"description": "Base adapter class that all other js-data adapters extend.",
"version": "0.7.2",
"version": "0.7.3",
"homepage": "https://github.com/js-data/js-data-adapter",
"repository": {
"type": "git",
Expand Down Expand Up @@ -68,12 +68,12 @@
},
"dependencies": {
"chai": "^3.5.0",
"mocha": "^2.4.5",
"mocha": "^2.5.3",
"sinon": "^1.17.4"
},
"devDependencies": {
"babel-plugin-syntax-async-functions": "6.8.0",
"babel-plugin-transform-regenerator": "6.8.0",
"babel-plugin-transform-regenerator": "6.9.0",
"babel-preset-stage-0": "6.5.0",
"js-data-repo-tools": "0.5.2"
}
Expand Down
20 changes: 13 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,14 @@ export const unique = function (array) {
return final
}

export const withoutRelations = function (mapper, props) {
return utils.omit(props, mapper.relationFields || [])
export const withoutRelations = function (mapper, props, opts) {
opts || (opts = {})
opts.with || (opts.with = [])
const relationFields = mapper.relationFields || []
const toStrip = relationFields.filter(function (value) {
return opts.with.indexOf(value) === -1
})
return utils.omit(props, toStrip)
}

const DEFAULTS = {
Expand Down Expand Up @@ -667,7 +673,7 @@ utils.addHiddenPropsToTarget(Adapter.prototype, {
return utils.resolve(self[op](mapper, props, opts)).then(function (_props) {
// Allow for re-assignment from lifecycle hook
props = utils.isUndefined(_props) ? props : _props
props = withoutRelations(mapper, props)
props = withoutRelations(mapper, props, opts)
op = opts.op = 'create'
self.dbg(op, mapper, props, opts)
return utils.resolve(self._create(mapper, props, opts))
Expand Down Expand Up @@ -711,7 +717,7 @@ utils.addHiddenPropsToTarget(Adapter.prototype, {
// Allow for re-assignment from lifecycle hook
props = utils.isUndefined(_props) ? props : _props
props = props.map(function (record) {
return withoutRelations(mapper, record)
return withoutRelations(mapper, record, opts)
})
op = opts.op = 'createMany'
self.dbg(op, mapper, props, opts)
Expand Down Expand Up @@ -1363,7 +1369,7 @@ utils.addHiddenPropsToTarget(Adapter.prototype, {
return utils.resolve(self[op](mapper, id, props, opts)).then(function (_props) {
// Allow for re-assignment from lifecycle hook
props = utils.isUndefined(_props) ? props : _props
props = withoutRelations(mapper, props)
props = withoutRelations(mapper, props, opts)
op = opts.op = 'update'
self.dbg(op, mapper, id, props, opts)
return utils.resolve(self._update(mapper, id, props, opts))
Expand Down Expand Up @@ -1415,7 +1421,7 @@ utils.addHiddenPropsToTarget(Adapter.prototype, {
return utils.resolve(self[op](mapper, props, query, opts)).then(function (_props) {
// Allow for re-assignment from lifecycle hook
props = utils.isUndefined(_props) ? props : _props
props = withoutRelations(mapper, props)
props = withoutRelations(mapper, props, opts)
op = opts.op = 'updateAll'
self.dbg(op, mapper, props, query, opts)
return utils.resolve(self._updateAll(mapper, props, query, opts))
Expand Down Expand Up @@ -1465,7 +1471,7 @@ utils.addHiddenPropsToTarget(Adapter.prototype, {
// Allow for re-assignment from lifecycle hook
records = utils.isUndefined(_records) ? records : _records
records = records.map(function (record) {
return withoutRelations(mapper, record)
return withoutRelations(mapper, record, opts)
})
op = opts.op = 'updateMany'
self.dbg(op, mapper, records, opts)
Expand Down
5 changes: 5 additions & 0 deletions test/afterCreate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export default function (options) {
assert.objectsEqual(args[1], { name: 'John' }, 'afterCreate should have received create props')
assert.isObject(args[2], 'afterCreate should have received options')
assert.isObject(args[3], 'afterCreate should have received record')
adapter.afterCreate.restore()
})
it('should allow re-assignment', async function () {
const adapter = this.$$adapter
Expand All @@ -55,6 +56,7 @@ export default function (options) {
assert.objectsEqual(args[1], { name: 'John' }, 'afterCreate should have received create props')
assert.isObject(args[2], 'afterCreate should have received options')
assert.isObject(args[3], 'afterCreate should have received record')
adapter.afterCreate.restore()
})
it('should allow returning a promise', async function () {
const adapter = this.$$adapter
Expand Down Expand Up @@ -82,6 +84,7 @@ export default function (options) {
assert.objectsEqual(args[1], { name: 'John' }, 'afterCreate should have received create props')
assert.isDefined(args[2], 'afterCreate should have received options')
assert.isObject(args[3], 'afterCreate should have received record')
adapter.afterCreate.restore()
})
it('should allow returning a promise and re-assignment', async function () {
const adapter = this.$$adapter
Expand All @@ -108,6 +111,7 @@ export default function (options) {
assert.objectsEqual(args[1], { name: 'John' }, 'afterCreate should have received create props')
assert.isObject(args[2], 'afterCreate should have received options')
assert.isObject(args[3], 'afterCreate should have received record')
adapter.afterCreate.restore()
})
it('should receive raw', async function () {
const adapter = this.$$adapter
Expand Down Expand Up @@ -137,6 +141,7 @@ export default function (options) {
assert.isObject(args[3], 'afterCreate should have received result')
assert.equal(args[3].created, 1, 'result.created')
assert.isObject(args[3].data, 'result.data')
adapter.afterCreate.restore()
})
})
}
5 changes: 5 additions & 0 deletions test/afterUpdate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export default function (options) {
assert.isDefined(args[4], 'afterUpdate should have received updated record')
assert.equal(args[4][User.idAttribute], userId, `args[4].${User.idAttribute}`)
assert.equal(args[4].name, 'Johnny', 'args[4].name')
adapter.afterUpdate.restore()
})
it('should receive raw', async function () {
const adapter = this.$$adapter
Expand Down Expand Up @@ -80,6 +81,7 @@ export default function (options) {
assert.isDefined(args[4].data, 'args[4].data')
assert.equal(args[4].data[User.idAttribute], userId, `args[4].data.${User.idAttribute}`)
assert.equal(args[4].data.name, 'Johnny', 'args[4].data.name')
adapter.afterUpdate.restore()
})
it('should allow re-assignment', async function () {
const adapter = this.$$adapter
Expand Down Expand Up @@ -117,6 +119,7 @@ export default function (options) {
assert.isDefined(args[4], 'afterUpdate should have received updated record')
assert.equal(args[4][User.idAttribute], userId, `args[4].${User.idAttribute}`)
assert.equal(args[4].name, 'Johnny', 'args[4].name')
adapter.afterUpdate.restore()
})
it('should allow returning a promise', async function () {
const adapter = this.$$adapter
Expand Down Expand Up @@ -155,6 +158,7 @@ export default function (options) {
assert.isDefined(args[4], 'afterUpdate should have received updated record')
assert.equal(args[4][User.idAttribute], userId, `args[4].${User.idAttribute}`)
assert.equal(args[4].name, 'Johnny', 'args[4].name')
adapter.afterUpdate.restore()
})
it('should allow returning a promise and re-assignment', async function () {
const adapter = this.$$adapter
Expand Down Expand Up @@ -192,6 +196,7 @@ export default function (options) {
assert.isDefined(args[4], 'afterUpdate should have received updated record')
assert.equal(args[4][User.idAttribute], userId, `args[4].${User.idAttribute}`)
assert.equal(args[4].name, 'Johnny', 'args[4].name')
adapter.afterUpdate.restore()
})
})
}
4 changes: 4 additions & 0 deletions test/beforeCreate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export default function (options) {
assert.isTrue(args[0] === User, 'beforeCreate should have received User mapper')
assert.objectsEqual(args[1], { name: 'John' }, 'beforeCreate should have received create props')
assert.isObject(args[2], 'beforeCreate should have received options')
adapter.beforeCreate.restore()
})
it('should allow re-assignment', async function () {
const adapter = this.$$adapter
Expand All @@ -54,6 +55,7 @@ export default function (options) {
assert.isTrue(args[0] === User, 'beforeCreate should have received User mapper')
assert.objectsEqual(args[1], { name: 'John' }, 'beforeCreate should have received create props')
assert.isObject(args[2], 'beforeCreate should have received options')
adapter.beforeCreate.restore()
})
it('should allow returning a promise', async function () {
const adapter = this.$$adapter
Expand All @@ -80,6 +82,7 @@ export default function (options) {
assert.isTrue(args[0] === User, 'beforeCreate should have received User mapper')
assert.objectsEqual(args[1], { name: 'John' }, 'beforeCreate should have received create props')
assert.isDefined(args[2], 'beforeCreate should have received options')
adapter.beforeCreate.restore()
})
it('should allow returning a promise and re-assignment', async function () {
const adapter = this.$$adapter
Expand All @@ -106,6 +109,7 @@ export default function (options) {
assert.isTrue(args[0] === User, 'beforeCreate should have received User mapper')
assert.objectsEqual(args[1], { name: 'John' }, 'beforeCreate should have received create props')
assert.isObject(args[2], 'beforeCreate should have received options')
adapter.beforeCreate.restore()
})
})
}
12 changes: 8 additions & 4 deletions test/beforeUpdate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ export default function (options) {
assert.isTrue(args[0] === User, 'beforeUpdate should have received User mapper')
assert.isTrue(args[1] === userId, 'beforeUpdate should have received user id')
assert.objectsEqual(args[2], { name: 'Johnny' }, 'beforeUpdate should have received update props')
assert.isObject(args[3], 'beforeCreate should have received options')
assert.isObject(args[3], 'beforeUpdate should have received options')
adapter.beforeUpdate.restore()
})
it('should allow re-assignment', async function () {
const adapter = this.$$adapter
Expand Down Expand Up @@ -69,7 +70,8 @@ export default function (options) {
assert.isTrue(args[0] === User, 'beforeUpdate should have received User mapper')
assert.isTrue(args[1] === userId, 'beforeUpdate should have received user id')
assert.objectsEqual(args[2], { name: 'Johnny' }, 'beforeUpdate should have received update props')
assert.isObject(args[3], 'beforeCreate should have received options')
assert.isObject(args[3], 'beforeUpdate should have received options')
adapter.beforeUpdate.restore()
})
it('should allow returning a promise', async function () {
const adapter = this.$$adapter
Expand Down Expand Up @@ -103,7 +105,8 @@ export default function (options) {
assert.isTrue(args[0] === User, 'beforeUpdate should have received User mapper')
assert.isTrue(args[1] === userId, 'beforeUpdate should have received user id')
assert.objectsEqual(args[2], { name: 'Johnny' }, 'beforeUpdate should have received update props')
assert.isObject(args[3], 'beforeCreate should have received options')
assert.isObject(args[3], 'beforeUpdate should have received options')
adapter.beforeUpdate.restore()
})
it('should allow returning a promise and re-assignment', async function () {
const adapter = this.$$adapter
Expand Down Expand Up @@ -137,7 +140,8 @@ export default function (options) {
assert.isTrue(args[0] === User, 'beforeUpdate should have received User mapper')
assert.isTrue(args[1] === userId, 'beforeUpdate should have received user id')
assert.objectsEqual(args[2], { name: 'Johnny' }, 'beforeUpdate should have received update props')
assert.isObject(args[3], 'beforeCreate should have received options')
assert.isObject(args[3], 'beforeUpdate should have received options')
adapter.beforeUpdate.restore()
})
})
}
45 changes: 45 additions & 0 deletions test/update.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,50 @@ export default function (options) {
assert.equal(err.message, 'Not Found', 'err.message should be "Not Found"')
}
})
it('should keep relations specified by "with"', async function () {
const adapter = this.$$adapter
const store = this.$$container

sinon.stub(adapter, '_update', function (mapper, id, props, opts) {
assert.deepEqual(props.posts, [
{
id: 1234,
userId: 1
}
])
assert.deepEqual(props.profile, {
id: 238,
userId: 1
})
assert.equal(props.address, undefined)
assert.equal(props.organization, undefined)
return [props, {}]
})

assert.debug('update', 1, { id: 1 })
const result = await store.update('user', 1, {
id: 1,
posts: [
{
id: 1234,
userId: 1
}
],
address: {
id: 412,
userId: 1
},
profile: {
id: 238,
userId: 1
},
organizationId: 333,
organization: {
id: 333
}
}, { with: ['posts', 'profile'] })
assert.debug('updated', 1, result)
adapter._update.restore()
})
})
}

0 comments on commit 6c7f5e6

Please sign in to comment.