Skip to content

Commit

Permalink
Fix patch method with complex query (#151)
Browse files Browse the repository at this point in the history
* Fix schema use with graph

* Add $startTransaction params

* allow for atomic graph upsert/insert
* allow for atomic multi create

* Fix $startTransaction (atomic)

* Is now $atomic
* Better testing
* README infos

* Fix confilts with upstream

* Fix/Add comments

* Fix patch query with complex query

* Fixed typo

Co-authored-by: Dekel Barzilay <dekelev@gmail.com>
  • Loading branch information
Thomas-git and dekelev committed May 25, 2021
1 parent 4c611dc commit 49b7889
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ class Service extends AdapterService {
const updateId = this.getIdsQuery(id, undefined, false);
Object.keys(updateId).forEach(key => {
if (!Object.prototype.hasOwnProperty.call(newObject, key)) {
newObject[key] = updateId[key]; // id is missing in data, we had it
newObject[key] = updateId[key]; // id is missing in data, we add it
} else if (newObject[key] !== updateId[key]) {
throw new errors.BadRequest(`Id '${key}': values mismatch between data '${newObject[key]}' and request '${updateId[key]}'`);
}
Expand Down Expand Up @@ -602,7 +602,7 @@ class Service extends AdapterService {
_get (id, params = {}) {
// merge user query with the 'id' to get
const findQuery = Object.assign({}, { $and: [] }, params.query);
findQuery.$and.push(this.getIdsQuery(id));
findQuery.$and.push(this.getIdsQuery(id)); // BUG will fail with composite primary key because table name will be missing

return this._find(Object.assign({}, params, { query: findQuery }))
.then(page => {
Expand Down Expand Up @@ -848,11 +848,20 @@ 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 (key in findParams.query) {
findParams.query[key] = dataCopy[key];
// Update find query if needed with patched values
const updateKeys = (obj) => {
for (const key of Object.keys(obj)) {
if (key in dataCopy) {
obj[key] = dataCopy[key];
} else {
if (Array.isArray(obj[key])) {
obj[key].forEach(obj => updateKeys(obj));
}
}
}
}
};
updateKeys(findParams.query);

return q.patch(dataCopy).then(() => {
return params.query && params.query.$noSelect
? dataCopy
Expand Down
7 changes: 7 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,13 @@ describe('Feathers Objection Service', () => {
});
});

it('allows patch multiple records with patched keys in complex query', () => {
return peopleRooms.patch(null, { admin: false }, { query: { $and: [{ admin: true }] } }).then(data => {
expect(data).to.be.instanceof(Array);
expect(data.length).to.equal(3);
});
});

it('patch with partial id throws an error', () => {
return peopleRooms.patch([2], { admin: false }).then(() => {
throw new Error('Should never get here');
Expand Down

0 comments on commit 49b7889

Please sign in to comment.