@onehat/data 1.7.7 → 1.7.11

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.
@@ -347,6 +347,18 @@ describe('Entity', function() {
347
347
  expect(_.isEqual(result, expected)).to.be.true;
348
348
  });
349
349
 
350
+ it('getChangedValues', function() {
351
+ this.entity.foo = 2;
352
+ const result = this.entity.getChangedValues(),
353
+ expected = {
354
+ foo: {
355
+ original: 1,
356
+ current: 2,
357
+ },
358
+ };
359
+ expect(_.isEqual(result, expected)).to.be.true;
360
+ });
361
+
350
362
  it('data', function() {
351
363
  const result = this.entity.data,
352
364
  expected = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onehat/data",
3
- "version": "1.7.7",
3
+ "version": "1.7.11",
4
4
  "description": "JS data modeling package with adapters for many storage mediums.",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
package/src/Entity.js CHANGED
@@ -187,7 +187,7 @@ class Entity extends EventEmitter {
187
187
  if (this.isDestroyed) {
188
188
  throw Error('this._createStatics is no longer valid. Entity has been destroyed.');
189
189
  }
190
- const staticsDefinitions = this.schema.entity.methods;
190
+ const staticsDefinitions = this.schema.entity.statics;
191
191
  if (!_.isEmpty(staticsDefinitions)) {
192
192
  _.each(staticsDefinitions, (value, key) => {
193
193
  this[key] = value;
@@ -711,21 +711,39 @@ class Entity extends EventEmitter {
711
711
  * @return {array|boolean} diff - Array of property names that have changed, or false
712
712
  */
713
713
  getChanged = () => {
714
- const obj1 = this._originalDataParsed,
715
- obj2 = this.getRawValues(),
716
- diff = Object.keys(obj1).reduce((result, key) => { // from https://stackoverflow.com/a/40610459/9163076
717
- if (obj2 && !obj2.hasOwnProperty(key)) {
714
+ const original = this._originalDataParsed,
715
+ current = this.getRawValues(),
716
+ diff = Object.keys(original).reduce((result, key) => { // from https://stackoverflow.com/a/40610459/9163076
717
+ if (current && !current.hasOwnProperty(key)) {
718
718
  result.push(key);
719
- } else if (_.isEqual(obj1[key], obj2[key])) {
719
+ } else if (_.isEqual(original[key], current[key])) {
720
720
  const resultKeyIndex = result.indexOf(key);
721
721
  result.splice(resultKeyIndex, 1);
722
722
  }
723
723
  return result;
724
- }, Object.keys(obj2));
724
+ }, Object.keys(current));
725
725
 
726
726
  return !_.isEmpty(diff) ? diff : false;
727
727
  }
728
728
 
729
+ /**
730
+ * Gets a comprehensive analysis of what has changed since the last save
731
+ * @return {object} changedPropertyValues - Object representing each changed field and both its original and current value
732
+ */
733
+ getChangedValues = () => {
734
+ const original = this._originalDataParsed,
735
+ current = this.getRawValues(),
736
+ names = this.getChanged();
737
+ const changedPropertyValues = {};
738
+ _.each(names, (name) => {
739
+ changedPropertyValues[name] = {
740
+ original: original[name],
741
+ current: current[name],
742
+ };
743
+ });
744
+ return changedPropertyValues;
745
+ }
746
+
729
747
  /**
730
748
  * Alias for this.submitValues
731
749
  */
@@ -834,6 +834,9 @@ class AjaxRepository extends Repository {
834
834
  * @private
835
835
  */
836
836
  _finalizeSave = (promises) => {
837
+ if (!promises.length) {
838
+ return;
839
+ }
837
840
  return this.axios.all(promises)
838
841
  .then(this.axios.spread((...batchOperationResults) => {
839
842
  // All requests are now complete
@@ -267,6 +267,10 @@ export default class Repository extends EventEmitter {
267
267
 
268
268
  this._createMethods();
269
269
 
270
+ if (this.schema.repository.initialize) {
271
+ this.schema.repository.initialize();
272
+ }
273
+
270
274
  this.isInitialized = true;
271
275
  this.emit('initialize');
272
276
  }
@@ -1398,7 +1402,9 @@ export default class Repository extends EventEmitter {
1398
1402
  if (this.combineBatch) {
1399
1403
 
1400
1404
  result = this.batchAsSynchronous ? await this._doBatchAdd(entities) : this._doBatchAdd(entities);
1401
- results.push(result);
1405
+ if (result) {
1406
+ results.push(result);
1407
+ }
1402
1408
 
1403
1409
  } else {
1404
1410
  for (i = 0; i < entities.length; i++) {
@@ -1411,7 +1417,9 @@ export default class Repository extends EventEmitter {
1411
1417
  }
1412
1418
 
1413
1419
  result = this.batchAsSynchronous ? await this._doAdd(entity) : this._doAdd(entity);
1414
- results.push(result);
1420
+ if (result) {
1421
+ results.push(result);
1422
+ }
1415
1423
  }
1416
1424
  }
1417
1425
  }
@@ -1425,7 +1433,9 @@ export default class Repository extends EventEmitter {
1425
1433
  if (this.combineBatch) {
1426
1434
 
1427
1435
  result = this.batchAsSynchronous ? await this._doBatchEdit(entities) : this._doBatchEdit(entities);
1428
- results.push(result);
1436
+ if (result) {
1437
+ results.push(result);
1438
+ }
1429
1439
 
1430
1440
  } else {
1431
1441
  for (i = 0; i < entities.length; i++) {
@@ -1438,7 +1448,9 @@ export default class Repository extends EventEmitter {
1438
1448
  }
1439
1449
 
1440
1450
  result = this.batchAsSynchronous ? await this._doEdit(entity) : this._doEdit(entity);
1441
- results.push(result);
1451
+ if (result) {
1452
+ results.push(result);
1453
+ }
1442
1454
  }
1443
1455
  }
1444
1456
  }
@@ -1452,7 +1464,9 @@ export default class Repository extends EventEmitter {
1452
1464
  if (this.combineBatch) {
1453
1465
 
1454
1466
  result = this.batchAsSynchronous ? await this._doBatchDelete(entities) : this._doBatchDelete(entities);
1455
- results.push(result);
1467
+ if (result) {
1468
+ results.push(result);
1469
+ }
1456
1470
 
1457
1471
  } else {
1458
1472
  for (i = 0; i < entities.length; i++) {
@@ -1463,7 +1477,9 @@ export default class Repository extends EventEmitter {
1463
1477
  } else {
1464
1478
  result = this.batchAsSynchronous ? await this._doDelete(entity) : this._doDelete(entity);
1465
1479
  }
1466
- results.push(result);
1480
+ if (result) {
1481
+ results.push(result);
1482
+ }
1467
1483
  }
1468
1484
  }
1469
1485
  }