diff --git a/lib/index.js b/lib/index.js index 9b818e2..7537015 100644 --- a/lib/index.js +++ b/lib/index.js @@ -212,15 +212,29 @@ class Service extends AdapterService { query = Object.assign(query, { collation: params.collation }); } - const findParams = Object.assign({}, params, { - paginate: false - }); const remapModifier = this._remapModifiers(this._normalizeId(id, data)); - return this.Model.updateMany(query, remapModifier, options) - .then(() => this._findOrGet(id, findParams)) - .then(select(params, this.id)) - .catch(errorHandler); + const idParams = Object.assign({}, params, { + paginate: false, + }); + + const ids = this._findOrGet(id, idParams) + .then(result => { + const items = Array.isArray(result) ? result : [result]; + return items.map(item => item[this.id]); + }); + + return ids.then(idList => { + const findParams = Object.assign({}, params, { + paginate: false, + query: { [this.id]: { $in: idList } } + }); + + return this.Model.updateMany(query, remapModifier, options) + .then(() => this._findOrGet(id, findParams)) + .then(select(params, this.id)) + .catch(errorHandler); + }); } _update (id, data, params = {}) { diff --git a/test/index.test.js b/test/index.test.js index 15cd6b6..37d9762 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -33,6 +33,7 @@ const testSuite = adapterTests([ '.patch + id + query', '.patch multiple', '.patch multi query', + '.patch multi query changed', '.patch + NotFound', '.create', '.create + $select', @@ -188,6 +189,41 @@ describe('Feathers MongoDB Service', () => { }); }); + // describe('.patch multi query changed', () => { + // let peopleService, people; + + // beforeEach(async () => { + // peopleService = app.service('/people'); + // peopleService.options.multi = true; + // people = await Promise.all([ + // peopleService.create({ name: 'AAA' }), + // peopleService.create({ name: 'aaa' }), + // peopleService.create({ name: 'ccc' }) + // ]); + // }); + + // afterEach(async () => { + // peopleService.options.multi = false; + // await Promise.all([ + // peopleService.remove(people[0]._id), + // peopleService.remove(people[1]._id), + // peopleService.remove(people[2]._id) + // ]).catch(() => {}); + // }); + + // it('Returns correct result when queried props are patched',async () => { + // const result = await peopleService.patch(null, { name: 'patched' }, { + // query: { name: { $gt: 'AAA' } } + // }); + + // expect(result).to.be.an('array'); + // expect(result).to.have.lengthOf(2); + // result.forEach(person => { + // expect(person.name).to.equal('patched'); + // }); + // }); + // }); + describe('Special collation param', () => { let peopleService, people;