Skip to content

Commit 49b7889

Browse files
Thomas-gitdekelev
andauthored
Fix patch method with complex query (#151)
* 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>
1 parent 4c611dc commit 49b7889

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

src/index.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ class Service extends AdapterService {
419419
const updateId = this.getIdsQuery(id, undefined, false);
420420
Object.keys(updateId).forEach(key => {
421421
if (!Object.prototype.hasOwnProperty.call(newObject, key)) {
422-
newObject[key] = updateId[key]; // id is missing in data, we had it
422+
newObject[key] = updateId[key]; // id is missing in data, we add it
423423
} else if (newObject[key] !== updateId[key]) {
424424
throw new errors.BadRequest(`Id '${key}': values mismatch between data '${newObject[key]}' and request '${updateId[key]}'`);
425425
}
@@ -602,7 +602,7 @@ class Service extends AdapterService {
602602
_get (id, params = {}) {
603603
// merge user query with the 'id' to get
604604
const findQuery = Object.assign({}, { $and: [] }, params.query);
605-
findQuery.$and.push(this.getIdsQuery(id));
605+
findQuery.$and.push(this.getIdsQuery(id)); // BUG will fail with composite primary key because table name will be missing
606606

607607
return this._find(Object.assign({}, params, { query: findQuery }))
608608
.then(page => {
@@ -848,11 +848,20 @@ class Service extends AdapterService {
848848
const selectParam = filters.$select ? { $select: filters.$select } : undefined;
849849
const findParams = Object.assign({}, params, { query: Object.assign({}, params.query, this.getIdsQuery(id, idList), selectParam) });
850850

851-
for (const key of Object.keys(dataCopy)) {
852-
if (key in findParams.query) {
853-
findParams.query[key] = dataCopy[key];
851+
// Update find query if needed with patched values
852+
const updateKeys = (obj) => {
853+
for (const key of Object.keys(obj)) {
854+
if (key in dataCopy) {
855+
obj[key] = dataCopy[key];
856+
} else {
857+
if (Array.isArray(obj[key])) {
858+
obj[key].forEach(obj => updateKeys(obj));
859+
}
860+
}
854861
}
855-
}
862+
};
863+
updateKeys(findParams.query);
864+
856865
return q.patch(dataCopy).then(() => {
857866
return params.query && params.query.$noSelect
858867
? dataCopy

test/index.test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,13 @@ describe('Feathers Objection Service', () => {
601601
});
602602
});
603603

604+
it('allows patch multiple records with patched keys in complex query', () => {
605+
return peopleRooms.patch(null, { admin: false }, { query: { $and: [{ admin: true }] } }).then(data => {
606+
expect(data).to.be.instanceof(Array);
607+
expect(data.length).to.equal(3);
608+
});
609+
});
610+
604611
it('patch with partial id throws an error', () => {
605612
return peopleRooms.patch([2], { admin: false }).then(() => {
606613
throw new Error('Should never get here');

0 commit comments

Comments
 (0)