From 0daec81dcc4359827c64e6d599bcc8168e2a396f Mon Sep 17 00:00:00 2001 From: Dekel Barzilay Date: Sat, 9 May 2020 13:23:30 +0300 Subject: [PATCH] Patch with same field in data & query now returns success result --- src/index.js | 6 ++++++ test/index.test.js | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/src/index.js b/src/index.js index f1c10c0..8afddb0 100644 --- a/src/index.js +++ b/src/index.js @@ -613,6 +613,12 @@ class Service extends AdapterService { const selectParam = filters.$select ? { $select: filters.$select } : undefined; const findParams = Object.assign({}, params, { query: Object.assign({}, params.query, this.getIdsQuery(id, idList), selectParam) }); + for (const key of Object.keys(dataCopy)) { + if (findParams.query[key]) { + findParams.query[key] = dataCopy[key]; + } + } + return q.patch(dataCopy).then(() => { return params.query && params.query.$noSelect ? {} : this._find(findParams).then(page => { const items = page.data || page; diff --git a/test/index.test.js b/test/index.test.js index 768c15b..d0fffcf 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -1785,4 +1785,42 @@ describe('Feathers Objection Service', () => { }); }); }); + + describe('Patch with same field in data & query', () => { + let company; + + beforeEach(async () => { + company = await companies.create({ name: 'Apple' }); + }); + + afterEach(async () => { + await companies.remove(null); + }); + + it('Patch with id', () => { + return companies.patch(company.id, { + name: 'Google' + }, { + query: { + name: 'Apple' + } + }).then(data => { + expect(data).to.be.ok; + expect(data.name).to.be.equal('Google'); + }); + }); + + it('Patch without id', () => { + return companies.patch(null, { + name: 'Google' + }, { + query: { + name: 'Apple' + } + }).then(data => { + expect(data.length).to.be.equal(1); + expect(data[0].name).to.be.equal('Google'); + }); + }); + }); });