Skip to content
This repository was archived by the owner on Sep 2, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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