diff --git a/CHANGELOG.md b/CHANGELOG.md index 1e35c19..3753252 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/package.json b/package.json index 3e1098e..61fbd56 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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" } diff --git a/src/index.js b/src/index.js index bb53d47..e71a98a 100644 --- a/src/index.js +++ b/src/index.js @@ -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 = { @@ -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)) @@ -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) @@ -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)) @@ -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)) @@ -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) diff --git a/test/afterCreate.test.js b/test/afterCreate.test.js index cd55cf2..fd94bee 100644 --- a/test/afterCreate.test.js +++ b/test/afterCreate.test.js @@ -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 @@ -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 @@ -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 @@ -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 @@ -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() }) }) } diff --git a/test/afterUpdate.test.js b/test/afterUpdate.test.js index 7d8c6e6..303faf5 100644 --- a/test/afterUpdate.test.js +++ b/test/afterUpdate.test.js @@ -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 @@ -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 @@ -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 @@ -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 @@ -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() }) }) } diff --git a/test/beforeCreate.test.js b/test/beforeCreate.test.js index e9cf4aa..88e919a 100644 --- a/test/beforeCreate.test.js +++ b/test/beforeCreate.test.js @@ -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 @@ -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 @@ -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 @@ -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() }) }) } diff --git a/test/beforeUpdate.test.js b/test/beforeUpdate.test.js index d74eebe..abc5812 100644 --- a/test/beforeUpdate.test.js +++ b/test/beforeUpdate.test.js @@ -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 @@ -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 @@ -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 @@ -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() }) }) } diff --git a/test/update.test.js b/test/update.test.js index 114cbf1..32b5d0c 100644 --- a/test/update.test.js +++ b/test/update.test.js @@ -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() + }) }) }