| 525 | function _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); } |
| 526 | |
| 527 | function set(newDocument) { |
| 528 | var _this = this; |
| 529 | |
| 530 | var collectionName = this.collectionName; |
| 531 | var docSelectionCriteria = this.docSelectionCriteria; |
| 532 | return new Promise(function (resolve, reject) { |
| 533 | // set document by criteria |
| 534 | _this.setDocumentByCriteria = function () { |
| 535 | var docsToSet = []; |
| 536 | |
| 537 | _this.lf[collectionName].iterate(function (value, key) { |
| 538 | if ((0, _isSubset["default"])(value, docSelectionCriteria)) { |
| 539 | docsToSet.push({ |
| 540 | key: key, |
| 541 | newDocument: newDocument |
| 542 | }); |
| 543 | } |
| 544 | }).then(function () { |
| 545 | if (!docsToSet.length) { |
| 546 | reject(_error["default"].call(_this, "No Documents found in ".concat(collectionName, " Collection with criteria ").concat(JSON.stringify(docSelectionCriteria), "."))); |
| 547 | } |
| 548 | |
| 549 | if (docsToSet.length > 1) { |
| 550 | _logger["default"].warn.call(_this, "Multiple documents (".concat(docsToSet.length, ") with ").concat(JSON.stringify(docSelectionCriteria), " found for setting.")); |
| 551 | } |
| 552 | }).then(function () { |
| 553 | docsToSet.forEach(function (docToSet, index) { |
| 554 | _this.lf[collectionName].setItem(docToSet.key, docToSet.newDocument).then(function (value) { |
| 555 | if (index === docsToSet.length - 1) { |
| 556 | resolve(_success["default"].call(_this, "".concat(docsToSet.length, " Document").concat(docsToSet.length > 1 ? 's' : '', " in \"").concat(collectionName, "\" collection with ").concat(JSON.stringify(docSelectionCriteria), " set to:"), newDocument)); |
| 557 | } |
| 558 | })["catch"](function (err) { |
| 559 | reject(_error["default"].call(_this, "Could not set ".concat(docsToSet.length, " Documents in ").concat(collectionName, " Collection."))); |
| 560 | }); |
| 561 | }); |
| 562 | }); |
| 563 | }; // set document by key |
| 564 | |
| 565 | |
| 566 | _this.setDocumentByKey = function () { |
| 567 | _this.lf[collectionName].setItem(docSelectionCriteria, newDocument).then(function (value) { |
| 568 | resolve(_success["default"].call(_this, "Document in \"".concat(collectionName, "\" collection with key ").concat(JSON.stringify(docSelectionCriteria), " set to:"), newDocument)); |
| 569 | })["catch"](function (err) { |
| 570 | reject(_error["default"].call(_this, "Document in \"".concat(collectionName, "\" collection with key ").concat(JSON.stringify(docSelectionCriteria), " could not be set."))); |
| 571 | }); |
| 572 | }; // check for user errors |
| 573 | |
| 574 | |
| 575 | if (!newDocument) { |
| 576 | _this.userErrors.push('No new Document object provided to set() method. Use an object e.g. { id: 1, name: "Bill", age: 47 }'); |
| 577 | } else if (!(_typeof(newDocument) == 'object' && newDocument instanceof Array == false)) { |
| 578 | _this.userErrors.push('Data passed to .set() must be an object. Not an array, string, number or boolean.'); |
| 579 | } |
| 580 | |
| 581 | if (!_this.userErrors.length) { |
| 582 | if (_typeof(docSelectionCriteria) == 'object') { |
| 583 | return _this.setDocumentByCriteria(); |
| 584 | } else { |