UNPKG

ottoman

Version:
48 lines 2.26 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.updateCallback = exports.updateMany = void 0; const utils_1 = require("./utils"); const types_1 = require("./types"); /** * Async Function: Update all of the documents that match conditions from the collection. * Allows use of filters and options. * * @param documents List of documents to update * @param doc Fields to update. * * @return (ManyQueryResponse)[(/classes/queryresponse.html)] */ const updateMany = (metadata) => (documents, doc, options) => __awaiter(void 0, void 0, void 0, function* () { function cb(document, metadata, extra) { return __awaiter(this, void 0, void 0, function* () { return (0, exports.updateCallback)(document, metadata, extra, options); }); } return yield (0, utils_1.batchProcessQueue)(metadata)(documents, cb, doc, options, 100); }); exports.updateMany = updateMany; /** * @ignore */ const updateCallback = (document, metadata, extra, options) => { const model = metadata.ottoman.getModel(metadata.modelName); return model .updateById(document[metadata.ID_KEY], Object.assign(Object.assign({}, document), extra), options) .then((updated) => { return Promise.resolve(new types_1.StatusExecution(updated, 'SUCCESS')); }) .catch((error) => { return Promise.reject(new types_1.StatusExecution(document[metadata.ID_KEY], 'FAILURE', error.constructor.name, error.message)); }); }; exports.updateCallback = updateCallback; //# sourceMappingURL=update_many.js.map