@aws-amplify/datastore 3.7.4-custom-pk.79 → 3.7.4-custom-pk.80

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -82412,7 +82412,6 @@ var createModelClass = function createModelClass(modelDefinition) {
82412
82412
  var model = Object(immer__WEBPACK_IMPORTED_MODULE_4__["produce"])(source, function (draft) {
82413
82413
  fn(draft);
82414
82414
  var keyNames = Object(_util__WEBPACK_IMPORTED_MODULE_12__["extractPrimaryKeyFieldNames"])(modelDefinition); // Keys are immutable
82415
- // CPK TODO: Previous comment from Ivan:
82416
82415
  // @ts-ignore TODO: fix type
82417
82416
 
82418
82417
  keyNames.forEach(function (key) {
@@ -87463,9 +87462,10 @@ function () {
87463
87462
  };
87464
87463
 
87465
87464
  IndexedDBAdapter.prototype.getIndexKeyPath = function (namespaceName, modelName) {
87466
- var _a, _b;
87465
+ var _a;
87467
87466
 
87468
- var keyPath = (_b = (_a = this.schema.namespaces[namespaceName]) === null || _a === void 0 ? void 0 : _a.keys[modelName]) === null || _b === void 0 ? void 0 : _b.primaryKey;
87467
+ var namespace = this.schema.namespaces[namespaceName];
87468
+ var keyPath = (_a = namespace === null || namespace === void 0 ? void 0 : namespace.keys[modelName]) === null || _a === void 0 ? void 0 : _a.primaryKey;
87469
87469
 
87470
87470
  if (keyPath) {
87471
87471
  return keyPath;
@@ -89108,7 +89108,7 @@ function () {
89108
89108
  var e_10, _a, e_11, _b, e_12, _c;
89109
89109
 
89110
89110
  return __awaiter(this, void 0, void 0, function () {
89111
- var rel, relationType, fieldName, modelName, targetName, targetNames, storeName, index, _d, model, hasOneIndex, hasConnectedModelFields, keyValues, values, recordToDelete, hasOneIndex_1, hasOneCustomField, keyValues, value, recordToDelete, e_11_1, model, keyValues, hasManyIndex, childrenArray, e_12_1, e_10_1;
89111
+ var rel, relationType, fieldName, modelName, targetName, targetNames, storeName, index, _d, model, hasOneIndex, hasConnectedModelFields, keyValues, values, recordToDelete, hasOneIndex_1, hasOneCustomField, keyValues, value, recordToDelete, e_11_1, model, keyValues, childrenArray, e_12_1, e_10_1;
89112
89112
 
89113
89113
  var _this = this;
89114
89114
 
@@ -89219,13 +89219,7 @@ function () {
89219
89219
  hasOneIndex_1 = index || 'byPk';
89220
89220
  hasOneCustomField = targetName in model;
89221
89221
  keyValues = this.getIndexKeyValues(model);
89222
- value = hasOneCustomField ? model[targetName] : keyValues[0];
89223
-
89224
- if (hasOneIndex_1 === 'byPk') {
89225
- // byPk requires an array keyValue
89226
- value = [value];
89227
- }
89228
-
89222
+ value = hasOneCustomField ? [model[targetName]] : keyValues;
89229
89223
  if (!value) return [3
89230
89224
  /*break*/
89231
89225
  , 12];
@@ -89316,20 +89310,17 @@ function () {
89316
89310
  , 26];
89317
89311
  model = models_2_1.value;
89318
89312
  keyValues = this.getIndexKeyValues(model);
89319
- hasManyIndex = index || 'byPk';
89320
89313
  return [4
89321
89314
  /*yield*/
89322
- , this.db.transaction(storeName, 'readwrite').objectStore(storeName).index(hasManyIndex).getAll(keyValues[0])];
89315
+ , this.db.transaction(storeName, 'readwrite').objectStore(storeName).index(index).getAll(keyValues)];
89323
89316
 
89324
89317
  case 23:
89325
- childrenArray = _e.sent(); // .getAll(keyValues); // TODO
89326
-
89318
+ childrenArray = _e.sent();
89327
89319
  return [4
89328
89320
  /*yield*/
89329
89321
  , this.deleteTraverse(this.schema.namespaces[nameSpace].relationships[modelName].relationTypes, childrenArray, modelName, nameSpace, deleteQueue)];
89330
89322
 
89331
89323
  case 24:
89332
- // .getAll(keyValues); // TODO
89333
89324
  _e.sent();
89334
89325
 
89335
89326
  _e.label = 25;
@@ -89660,18 +89651,19 @@ function () {
89660
89651
 
89661
89652
  IndexedDBAdapter.prototype.createObjectStoreForModel = function (db, namespaceName, storeName, modelName) {
89662
89653
  return __awaiter(this, void 0, void 0, function () {
89663
- var store, indexes, keyPath;
89654
+ var store, indexes;
89664
89655
  return __generator(this, function (_a) {
89665
89656
  store = db.createObjectStore(storeName, {
89666
89657
  autoIncrement: true
89667
89658
  });
89668
89659
  indexes = this.schema.namespaces[namespaceName].relationships[modelName].indexes;
89669
- indexes.forEach(function (index) {
89670
- return store.createIndex(index, index);
89671
- });
89672
- keyPath = this.getIndexKeyPath(namespaceName, modelName);
89673
- store.createIndex('byPk', keyPath, {
89674
- unique: true
89660
+ indexes.forEach(function (_a) {
89661
+ var _b = __read(_a, 3),
89662
+ idxName = _b[0],
89663
+ keyPath = _b[1],
89664
+ options = _b[2];
89665
+
89666
+ store.createIndex(idxName, keyPath, options);
89675
89667
  });
89676
89668
  return [2
89677
89669
  /*return*/
@@ -96161,11 +96153,17 @@ function getConnectionFields(modelDefinition, namespace) {
96161
96153
  var _b = __read(Object(_util__WEBPACK_IMPORTED_MODULE_4__["establishRelationAndKeys"])(namespace), 1),
96162
96154
  relations = _b[0];
96163
96155
 
96164
- var connectedModelName = modelDefinition.fields[name].type['model']; // keyFields are fields of the connected model
96156
+ var connectedModelName = modelDefinition.fields[name].type['model'];
96157
+ var byPkIndex = relations[connectedModelName].indexes.find(function (_a) {
96158
+ var _b = __read(_a, 1),
96159
+ name = _b[0];
96165
96160
 
96166
- var keyFields = relations[connectedModelName].indexes; // We rely on `_deleted` when we process the sync query (e.g. in batchSave in the adapters)
96161
+ return name === 'byPk';
96162
+ });
96163
+ var keyFields = byPkIndex && byPkIndex[1];
96164
+ var keyFieldSelectionSet = keyFields === null || keyFields === void 0 ? void 0 : keyFields.join(' '); // We rely on `_deleted` when we process the sync query (e.g. in batchSave in the adapters)
96167
96165
 
96168
- result.push(name + " { " + keyFields + " _deleted }");
96166
+ result.push(name + " { " + keyFieldSelectionSet + " _deleted }");
96169
96167
  } else {
96170
96168
  // backwards-compatability for schema generated prior to custom primary key support
96171
96169
  result.push(name + " { id _deleted }");
@@ -97094,7 +97092,7 @@ var LimitTimerRaceResolvedValues;
97094
97092
  /*!*************************!*\
97095
97093
  !*** ./lib-esm/util.js ***!
97096
97094
  \*************************/
97097
- /*! exports provided: extractKeyIfExists, extractPrimaryKeyFieldNames, extractPrimaryKeyValues, isIdManaged, isIdOptionallyManaged, NAMESPACES, USER, SYNC, STORAGE, DATASTORE, USER_AGENT_SUFFIX_DATASTORE, exhaustiveCheck, isNullOrUndefined, validatePredicate, validatePredicateField, isModelConstructor, registerNonModelClass, isNonModelConstructor, processCompositeKeys, establishRelationAndKeys, traverseModel, getIndex, getIndexFromAssociation, isPrivateMode, monotonicUlidFactory, getNow, sortCompareFunction, valuesEqual, isAWSDate, isAWSTime, isAWSDateTime, isAWSTimestamp, isAWSEmail, isAWSJSON, isAWSURL, isAWSPhone, isAWSIPAddress, DeferredPromise, DeferredCallbackResolver, mergePatches, extractTargetNamesFromSrc */
97095
+ /*! exports provided: extractKeyIfExists, extractPrimaryKeyFieldNames, extractPrimaryKeyValues, isIdManaged, isIdOptionallyManaged, NAMESPACES, USER, SYNC, STORAGE, DATASTORE, USER_AGENT_SUFFIX_DATASTORE, exhaustiveCheck, isNullOrUndefined, validatePredicate, validatePredicateField, isModelConstructor, registerNonModelClass, isNonModelConstructor, processCompositeKeys, establishRelationAndKeys, traverseModel, getIndex, getIndexFromAssociation, isPrivateMode, monotonicUlidFactory, getNow, sortCompareFunction, valuesEqual, isAWSDate, isAWSTime, isAWSDateTime, isAWSTimestamp, isAWSEmail, isAWSJSON, isAWSURL, isAWSPhone, isAWSIPAddress, DeferredPromise, DeferredCallbackResolver, mergePatches, extractTargetNamesFromSrc, indexNameFromKeys */
97098
97096
  /***/ (function(module, __webpack_exports__, __webpack_require__) {
97099
97097
 
97100
97098
  "use strict";
@@ -97140,6 +97138,7 @@ __webpack_require__.r(__webpack_exports__);
97140
97138
  /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "DeferredCallbackResolver", function() { return DeferredCallbackResolver; });
97141
97139
  /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "mergePatches", function() { return mergePatches; });
97142
97140
  /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "extractTargetNamesFromSrc", function() { return extractTargetNamesFromSrc; });
97141
+ /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "indexNameFromKeys", function() { return indexNameFromKeys; });
97143
97142
  /* harmony import */ var buffer__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! buffer */ "../../node_modules/buffer/index.js");
97144
97143
  /* harmony import */ var buffer__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(buffer__WEBPACK_IMPORTED_MODULE_0__);
97145
97144
  /* harmony import */ var ulid__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ulid */ "../../node_modules/ulid/dist/index.esm.js");
@@ -97616,7 +97615,7 @@ var establishRelationAndKeys = function establishRelationAndKeys(namespace) {
97616
97615
  var relationship = {};
97617
97616
  var keys = {};
97618
97617
  Object.keys(namespace.models).forEach(function (mKey) {
97619
- var e_1, _c, e_2, _d;
97618
+ var e_1, _c;
97620
97619
 
97621
97620
  relationship[mKey] = {
97622
97621
  indexes: [],
@@ -97639,20 +97638,11 @@ var establishRelationAndKeys = function establishRelationAndKeys(namespace) {
97639
97638
  });
97640
97639
 
97641
97640
  if (connectionType === 'BELONGS_TO') {
97642
- // const targetNames: string[] = extractTargetNamesFromSrc(
97643
- // fieldAttribute.association
97644
- // );
97645
- // targetNames.forEach((targetName: string) => {
97646
- // relationship[mKey].indexes.push(targetName);
97647
- // });
97648
- if (fieldAttribute.association.targetName) {
97649
- // backwards-compatability for schema generated prior to custom primary key support
97650
- relationship[mKey].indexes.push(fieldAttribute.association['targetName']);
97651
- } else {
97652
- // iterate through fieldAttribute.association.targetNames, and push to relationship[mKey].indexes
97653
- fieldAttribute.association.targetNames.forEach(function (targetName) {
97654
- relationship[mKey].indexes.push(targetName);
97655
- });
97641
+ var targetNames = extractTargetNamesFromSrc(fieldAttribute.association);
97642
+
97643
+ if (targetNames) {
97644
+ var idxName = indexNameFromKeys(targetNames);
97645
+ relationship[mKey].indexes.push([idxName, targetNames]);
97656
97646
  }
97657
97647
  }
97658
97648
  }
@@ -97661,41 +97651,37 @@ var establishRelationAndKeys = function establishRelationAndKeys(namespace) {
97661
97651
  if (model.attributes) {
97662
97652
  keys[mKey].compositeKeys = processCompositeKeys(model.attributes);
97663
97653
 
97664
- try {
97665
- for (var _e = __values(model.attributes), _f = _e.next(); !_f.done; _f = _e.next()) {
97666
- var attribute = _f.value;
97654
+ var _loop_1 = function _loop_1(attribute) {
97655
+ if (!Object(_types__WEBPACK_IMPORTED_MODULE_4__["isModelAttributeKey"])(attribute)) {
97656
+ return "continue";
97657
+ }
97667
97658
 
97668
- if (!Object(_types__WEBPACK_IMPORTED_MODULE_4__["isModelAttributeKey"])(attribute)) {
97669
- continue;
97670
- }
97659
+ var fields = attribute.properties.fields;
97671
97660
 
97672
- if (Object(_types__WEBPACK_IMPORTED_MODULE_4__["isModelAttributePrimaryKey"])(attribute)) {
97673
- keys[mKey].primaryKey = attribute.properties.fields;
97674
- }
97661
+ if (Object(_types__WEBPACK_IMPORTED_MODULE_4__["isModelAttributePrimaryKey"])(attribute)) {
97662
+ keys[mKey].primaryKey = fields;
97663
+ return "continue";
97664
+ } // create indexes for all other keys
97675
97665
 
97676
- var fields = attribute.properties.fields;
97677
97666
 
97678
- try {
97679
- for (var fields_1 = (e_2 = void 0, __values(fields)), fields_1_1 = fields_1.next(); !fields_1_1.done; fields_1_1 = fields_1.next()) {
97680
- var field = fields_1_1.value; // only add index if it hasn't already been added
97667
+ var idxName = indexNameFromKeys(fields);
97668
+ var idxExists = relationship[mKey].indexes.find(function (_c) {
97669
+ var _d = __read(_c, 1),
97670
+ index = _d[0];
97681
97671
 
97682
- var exists = relationship[mKey].indexes.includes(field);
97672
+ return index === idxName;
97673
+ });
97683
97674
 
97684
- if (!exists) {
97685
- relationship[mKey].indexes.push(field);
97686
- }
97687
- }
97688
- } catch (e_2_1) {
97689
- e_2 = {
97690
- error: e_2_1
97691
- };
97692
- } finally {
97693
- try {
97694
- if (fields_1_1 && !fields_1_1.done && (_d = fields_1["return"])) _d.call(fields_1);
97695
- } finally {
97696
- if (e_2) throw e_2.error;
97697
- }
97698
- }
97675
+ if (!idxExists) {
97676
+ relationship[mKey].indexes.push([idxName, fields]);
97677
+ }
97678
+ };
97679
+
97680
+ try {
97681
+ for (var _d = __values(model.attributes), _e = _d.next(); !_e.done; _e = _d.next()) {
97682
+ var attribute = _e.value;
97683
+
97684
+ _loop_1(attribute);
97699
97685
  }
97700
97686
  } catch (e_1_1) {
97701
97687
  e_1 = {
@@ -97703,16 +97689,22 @@ var establishRelationAndKeys = function establishRelationAndKeys(namespace) {
97703
97689
  };
97704
97690
  } finally {
97705
97691
  try {
97706
- if (_f && !_f.done && (_c = _e["return"])) _c.call(_e);
97692
+ if (_e && !_e.done && (_c = _d["return"])) _c.call(_d);
97707
97693
  } finally {
97708
97694
  if (e_1) throw e_1.error;
97709
97695
  }
97710
97696
  }
97697
+ } // set 'id' as the PK for models without a custom PK explicitly defined
97711
97698
 
97712
- if (!keys[mKey].primaryKey) {
97713
- keys[mKey].primaryKey = [ID];
97714
- }
97715
- }
97699
+
97700
+ if (!keys[mKey].primaryKey) {
97701
+ keys[mKey].primaryKey = [ID];
97702
+ } // create primary index
97703
+
97704
+
97705
+ relationship[mKey].indexes.push(['byPk', keys[mKey].primaryKey, {
97706
+ unique: true
97707
+ }]);
97716
97708
  });
97717
97709
  return [relationship, keys];
97718
97710
  };
@@ -97852,10 +97844,21 @@ var getIndex = function getIndex(rel, src) {
97852
97844
  return index;
97853
97845
  };
97854
97846
  var getIndexFromAssociation = function getIndexFromAssociation(indexes, src) {
97855
- var index = indexes.find(function (idx) {
97856
- return idx === src;
97847
+ var indexName;
97848
+
97849
+ if (Array.isArray(src)) {
97850
+ indexName = indexNameFromKeys(src);
97851
+ } else {
97852
+ indexName = src;
97853
+ }
97854
+
97855
+ var associationIndex = indexes.find(function (_c) {
97856
+ var _d = __read(_c, 1),
97857
+ idxName = _d[0];
97858
+
97859
+ return idxName === indexName;
97857
97860
  });
97858
- return index;
97861
+ return associationIndex && associationIndex[0];
97859
97862
  };
97860
97863
  var privateModeCheckResult;
97861
97864
  var isPrivateMode = function isPrivateMode() {
@@ -97953,7 +97956,7 @@ function getNow() {
97953
97956
  }
97954
97957
  function sortCompareFunction(sortPredicates) {
97955
97958
  return function compareFunction(a, b) {
97956
- var e_3, _c;
97959
+ var e_2, _c;
97957
97960
 
97958
97961
  try {
97959
97962
  // enable multi-field sort by iterating over predicates until
@@ -97973,15 +97976,15 @@ function sortCompareFunction(sortPredicates) {
97973
97976
  return 1 * sortMultiplier;
97974
97977
  }
97975
97978
  }
97976
- } catch (e_3_1) {
97977
- e_3 = {
97978
- error: e_3_1
97979
+ } catch (e_2_1) {
97980
+ e_2 = {
97981
+ error: e_2_1
97979
97982
  };
97980
97983
  } finally {
97981
97984
  try {
97982
97985
  if (sortPredicates_1_1 && !sortPredicates_1_1.done && (_c = sortPredicates_1["return"])) _c.call(sortPredicates_1);
97983
97986
  } finally {
97984
- if (e_3) throw e_3.error;
97987
+ if (e_2) throw e_2.error;
97985
97988
  }
97986
97989
  }
97987
97990
 
@@ -97994,7 +97997,7 @@ function sortCompareFunction(sortPredicates) {
97994
97997
  // to normalize for GQL response values for undefined fields
97995
97998
 
97996
97999
  function valuesEqual(valA, valB, nullish) {
97997
- var e_4, _c;
98000
+ var e_3, _c;
97998
98001
 
97999
98002
  if (nullish === void 0) {
98000
98003
  nullish = false;
@@ -98058,15 +98061,15 @@ function valuesEqual(valA, valB, nullish) {
98058
98061
  return false;
98059
98062
  }
98060
98063
  }
98061
- } catch (e_4_1) {
98062
- e_4 = {
98063
- error: e_4_1
98064
+ } catch (e_3_1) {
98065
+ e_3 = {
98066
+ error: e_3_1
98064
98067
  };
98065
98068
  } finally {
98066
98069
  try {
98067
98070
  if (keys_1_1 && !keys_1_1.done && (_c = keys_1["return"])) _c.call(keys_1);
98068
98071
  } finally {
98069
- if (e_4) throw e_4.error;
98072
+ if (e_3) throw e_3.error;
98070
98073
  }
98071
98074
  }
98072
98075
 
@@ -98262,6 +98265,17 @@ function extractTargetNamesFromSrc(src) {
98262
98265
  } else {
98263
98266
  return undefined;
98264
98267
  }
98268
+ } // Generates snake-cased index name from an aray of key field names
98269
+ // E.g. for keys `[id, title]` => 'id-title'
98270
+
98271
+ function indexNameFromKeys(keys) {
98272
+ return keys.reduce(function (prev, cur, idx) {
98273
+ if (idx === 0) {
98274
+ return cur;
98275
+ }
98276
+
98277
+ return prev + "-" + cur;
98278
+ }, '');
98265
98279
  }
98266
98280
 
98267
98281
  /***/ }),