electrodb 2.8.1 → 2.8.2

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/entity.js +17 -7
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "electrodb",
3
- "version": "2.8.1",
3
+ "version": "2.8.2",
4
4
  "description": "A library to more easily create and interact with multiple entities and heretical relationships in dynamodb",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/src/entity.js CHANGED
@@ -1886,12 +1886,20 @@ class Entity {
1886
1886
  // should only happen when an attribute is changed.
1887
1887
  const { indexKey, updatedKeys, deletedKeys = [] } = this._getUpdatedKeys(pk, sk, preparedUpdateValues, removed);
1888
1888
  const accessPattern = this.model.translations.indexes.fromIndexToAccessPattern[TableIndex];
1889
-
1890
1889
  for (const path of Object.keys(preparedUpdateValues)) {
1891
1890
  if (modifiedAttributeNames[path] !== undefined && preparedUpdateValues[path] !== undefined) {
1892
1891
  update.updateValue(modifiedAttributeNames[path], preparedUpdateValues[path]);
1893
1892
  } else if (preparedUpdateValues[path] !== undefined) {
1894
- update.set(path, preparedUpdateValues[path]);
1893
+ const attr = this.model.schema.getAttribute(path);
1894
+ if (attr) {
1895
+ // attributes might enter into this flow because they were triggered via a `watch` event and were
1896
+ // not supplied directly by the user. In this case we should set the field name.
1897
+ // TODO: This will only work with root attributes and should be refactored for nested attributes.
1898
+ update.set(attr.field, preparedUpdateValues[path]);
1899
+ } else {
1900
+ // this could be fields added by electro that don't apeear in the schema
1901
+ update.set(path, preparedUpdateValues[path]);
1902
+ }
1895
1903
  }
1896
1904
  }
1897
1905
 
@@ -1901,7 +1909,6 @@ class Entity {
1901
1909
  const wasNotAlreadyModified = modifiedAttributeNames[indexKey] === undefined;
1902
1910
  if (isNotTablePK && isNotTableSK && wasNotAlreadyModified) {
1903
1911
  update.set(indexKey, updatedKeys[indexKey]);
1904
-
1905
1912
  }
1906
1913
  }
1907
1914
 
@@ -1948,7 +1955,6 @@ class Entity {
1948
1955
  _makePutParams({ data } = {}, pk, sk) {
1949
1956
  let { updatedKeys, setAttributes } = this._getPutKeys(pk, sk && sk.facets, data);
1950
1957
  let translatedFields = this.model.schema.translateToFields(setAttributes);
1951
-
1952
1958
  return {
1953
1959
  Item: {
1954
1960
  ...translatedFields,
@@ -2406,8 +2412,10 @@ class Entity {
2406
2412
  indexKey[sk] = keys.sk[0];
2407
2413
  }
2408
2414
  }
2409
- updatedKeys[pk] = keys.pk;
2410
- if (sk) {
2415
+ if (keys.pk !== undefined && keys.pk !== '') {
2416
+ updatedKeys[pk] = keys.pk;
2417
+ }
2418
+ if (sk && keys.sk[0] !== undefined && keys.sk[0] !== '') {
2411
2419
  updatedKeys[sk] = keys.sk[0];
2412
2420
  }
2413
2421
  }
@@ -3298,7 +3306,9 @@ class Entity {
3298
3306
  if (Array.isArray(sk.facets)) {
3299
3307
  let duplicates = pk.facets.filter(facet => sk.facets.includes(facet));
3300
3308
  if (duplicates.length !== 0) {
3301
- throw new e.ElectroError(e.ErrorCodes.DuplicateIndexCompositeAttributes, `The Access Pattern '${accessPattern}' contains duplicate references the composite attribute(s): ${u.commaSeparatedString(duplicates)}. Composite attributes may only be used once within an index. If this leaves the Sort Key (sk) without any composite attributes simply set this to be an empty array.`);
3309
+ if (sk.facets.length > 1) {
3310
+ throw new e.ElectroError(e.ErrorCodes.DuplicateIndexCompositeAttributes, `The Access Pattern '${accessPattern}' contains duplicate references the composite attribute(s): ${u.commaSeparatedString(duplicates)}. Composite attributes can only be used more than once in an index if your sort key is limitted to a single attribute. This is to prevent unexpected runtime errors related to the inability to generate keys.`);
3311
+ }
3302
3312
  }
3303
3313
  }
3304
3314