diff --git a/README.md b/README.md index b6c27d81..992f6287 100644 --- a/README.md +++ b/README.md @@ -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 `.hooks.js`. +Start working with transaction enabled by adding the following lines in `app.hooks.js` or `.hooks.js`. ```js const TransactionManager = require('feathers-mongoose').TransactionManager; diff --git a/lib/service.js b/lib/service.js index cc29dd50..94c0251b 100755 --- a/lib/service.js +++ b/lib/service.js @@ -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') { @@ -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 @@ -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);