* Executes a batch of queries represented in the items. * @param {Array } items * @param {Object|String} [executionOptions] An object containing the options to be used for the requests * execution or a string representing the name of the execution profile. * @param {String
(items, executionOptions)
| 137 | * @returns {Promise<Result>} A Promise that resolves to a [Result]{@link module:mapping~Result}. |
| 138 | */ |
| 139 | batch(items, executionOptions) { |
| 140 | if (!Array.isArray(items) || !(items.length > 0)) { |
| 141 | return Promise.reject( |
| 142 | new errors.ArgumentError('First parameter items should be an Array with 1 or more ModelBatchItem instances')); |
| 143 | } |
| 144 | |
| 145 | const queries = []; |
| 146 | let isIdempotent = true; |
| 147 | let isCounter; |
| 148 | |
| 149 | return Promise |
| 150 | .all(items |
| 151 | .map(item => { |
| 152 | if (!(item instanceof ModelBatchItem)) { |
| 153 | return Promise.reject(new Error( |
| 154 | 'Batch items must be instances of ModelBatchItem, use modelMapper.batching object to create each item')); |
| 155 | } |
| 156 | |
| 157 | return item.pushQueries(queries) |
| 158 | .then(options => { |
| 159 | // The batch is idempotent when all the queries contained are idempotent |
| 160 | isIdempotent = isIdempotent && options.isIdempotent; |
| 161 | |
| 162 | // Let it fail at server level when there is a mix of counter and normal mutations |
| 163 | isCounter = options.isCounter; |
| 164 | }); |
| 165 | })) |
| 166 | .then(() => |
| 167 | this.client.batch(queries, DocInfoAdapter.adaptBatchOptions(executionOptions, isIdempotent, isCounter))) |
| 168 | .then(rs => { |
| 169 | // Results should only be adapted when the batch contains LWT (single table) |
| 170 | const info = items[0].getMappingInfo(); |
| 171 | return new Result(rs, info, ResultMapper.getMutationAdapter(rs)); |
| 172 | }); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | /** |
no test coverage detected