Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 fix 404 on patch call #362

Merged
merged 4 commits into from
Jan 4, 2020
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ For more information on MongoDB's collation feature, visit the [collation refere

This adapter includes support to enable database transaction to rollback the persisted records for any error occured for a api call. This requires [Mongo-DB v4.x](https://docs.mongodb.com/manual/) installed and [replica-set](https://linode.com/docs/databases/mongodb/create-a-mongodb-replica-set/#start-replication-and-add-members) enabled.

Start working with transactin enabled by adding the following lines in `app.hooks.js` or `<any-service>.hooks.js`.
Start working with transaction enabled by adding the following lines in `app.hooks.js` or `<any-service>.hooks.js`.

```js
const TransactionManager = require('feathers-mongoose').TransactionManager;
Expand Down
11 changes: 7 additions & 4 deletions lib/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,14 +224,14 @@ class Service extends AdapterService {

_patch (id, data, params = {}) {
const { query } = this.filterQuery(params);
const mapIds = data => data.map(current => current[this.id]);
const mapIds = data => Array.isArray(data) ? data.map(current => current[this.id]) : [data[this.id]];

// By default we will just query for the one id. For multi patch
// we create a list of the ids of all items that will be changed
// to re-query them after the update
const ids = id === null ? this._find(Object.assign({}, params, {
const ids = this._getOrFind(id, Object.assign({}, params, {
paginate: false
})).then(mapIds) : Promise.resolve([id]);
})).then(mapIds);

// Handle case where data might be a mongoose model
if (typeof data.toObject === 'function') {
Expand Down Expand Up @@ -268,7 +268,7 @@ class Service extends AdapterService {
const { query: { $populate } = {} } = params;
// Create a new query that re-queries all ids that
// were originally changed
const updatedQuery = (idList.length && id === null) ? { [this.id]: { $in: idList } } : params.query;
const updatedQuery = { [this.id]: { $in: idList } };
const findParams = Object.assign({}, params, {
paginate: false,
query: $populate ? Object.assign(updatedQuery, { $populate }) : updatedQuery
Expand All @@ -286,6 +286,9 @@ class Service extends AdapterService {
if (options.writeResult) {
return writeResult;
}
if ('upserted' in writeResult) {
return this._getOrFind(id, Object.assign({}, params, { query: { [this.id]: { $in: writeResult.upserted.map(doc => doc._id) } } }, { paginate: false }));
}
return this._getOrFind(id, findParams);
});
}).then(select(params, this.id)).catch(errorHandler);
Expand Down