MCPcopy
hub / github.com/alyssaxuu/motionity / update

Function update

src/js/libraries/localbase.js:619–690  ·  view source on GitHub ↗
(docUpdates)

Source from the content-addressed store, hash-verified

617function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
618
619function update(docUpdates) {
620 var _this = this;
621
622 var collectionName = this.collectionName;
623 var docSelectionCriteria = this.docSelectionCriteria;
624 return new Promise(function (resolve, reject) {
625 // update document by criteria
626 _this.updateDocumentByCriteria = function () {
627 var docsToUpdate = [];
628
629 _this.lf[collectionName].iterate(function (value, key) {
630 if ((0, _isSubset["default"])(value, docSelectionCriteria)) {
631 var newDocument = (0, _updateObject["default"])(value, docUpdates);
632 docsToUpdate.push({
633 key: key,
634 newDocument: newDocument
635 });
636 }
637 }).then(function () {
638 if (!docsToUpdate.length) {
639 reject(_error["default"].call(_this, "No Documents found in ".concat(collectionName, " Collection with criteria ").concat(JSON.stringify(docSelectionCriteria), ".")));
640 }
641
642 if (docsToUpdate.length > 1) {
643 _logger["default"].warn.call(_this, "Multiple documents (".concat(docsToUpdate.length, ") with ").concat(JSON.stringify(docSelectionCriteria), " found for updating."));
644 }
645 }).then(function () {
646 docsToUpdate.forEach(function (docToUpdate, index) {
647 _this.lf[collectionName].setItem(docToUpdate.key, docToUpdate.newDocument).then(function (value) {
648 if (index === docsToUpdate.length - 1) {
649 resolve(_success["default"].call(_this, "".concat(docsToUpdate.length, " Document").concat(docsToUpdate.length > 1 ? 's' : '', " in \"").concat(collectionName, "\" collection with ").concat(JSON.stringify(docSelectionCriteria), " updated with:"), docUpdates));
650 }
651 })["catch"](function (err) {
652 reject(_error["default"].call(_this, "Could not update ".concat(docsToUpdate.length, " Documents in ").concat(collectionName, " Collection.")));
653 });
654 });
655 });
656 }; // update document by key
657
658
659 _this.updateDocumentByKey = function () {
660 var newDocument = {};
661
662 _this.lf[collectionName].getItem(docSelectionCriteria).then(function (value) {
663 newDocument = (0, _updateObject["default"])(value, docUpdates);
664
665 _this.lf[collectionName].setItem(docSelectionCriteria, newDocument);
666
667 resolve(_success["default"].call(_this, "Document in \"".concat(collectionName, "\" collection with key ").concat(JSON.stringify(docSelectionCriteria), " updated to:"), newDocument));
668 })["catch"](function (err) {
669 reject(_error["default"].call(_this, "No Document found in \"".concat(collectionName, "\" collection with key ").concat(JSON.stringify(docSelectionCriteria))));
670 });
671 }; // check for user errors
672
673
674 if (!docUpdates) {
675 _this.userErrors.push('No update object provided to update() method. Use an object e.g. { name: "William" }');
676 } else if (!(_typeof(docUpdates) == 'object' && docUpdates instanceof Array == false)) {

Callers

nothing calls this directly

Calls 3

rejectFunction · 0.85
resolveFunction · 0.85
_typeofFunction · 0.85

Tested by

no test coverage detected