@onehat/data 1.7.9 → 1.7.14

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.9",
3
+ "version": "1.7.14",
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
@@ -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
  */
@@ -266,6 +266,11 @@ export default class Repository extends EventEmitter {
266
266
  }
267
267
 
268
268
  this._createMethods();
269
+ this._createStatics();
270
+
271
+ if (this.schema.repository.init) {
272
+ await this.schema.repository.init.call(this);
273
+ }
269
274
 
270
275
  this.isInitialized = true;
271
276
  this.emit('initialize');
@@ -287,6 +292,22 @@ export default class Repository extends EventEmitter {
287
292
  }
288
293
  }
289
294
 
295
+ /**
296
+ * Creates the static properties for this Repository, based on Schema.
297
+ * @private
298
+ */
299
+ _createStatics = () => {
300
+ if (this.isDestroyed) {
301
+ throw Error('this._createStatics is no longer valid. Entity has been destroyed.');
302
+ }
303
+ const staticsDefinitions = this.schema.repository.statics;
304
+ if (!_.isEmpty(staticsDefinitions)) {
305
+ _.each(staticsDefinitions, (value, key) => {
306
+ this[key] = value;
307
+ });
308
+ }
309
+ }
310
+
290
311
 
291
312
  // __ __
292
313
  // / / ____ ____ _____/ /
@@ -1251,49 +1272,6 @@ export default class Repository extends EventEmitter {
1251
1272
  return associatedRepository;
1252
1273
  }
1253
1274
 
1254
- /**
1255
- * Gets the Schema object
1256
- * @return {Schema} schema
1257
- */
1258
- getSchema = () => {
1259
- if (this.isDestroyed) {
1260
- throw Error('this.getSchema is no longer valid. Entity has been destroyed.');
1261
- }
1262
- return this.schema;
1263
- }
1264
-
1265
- /**
1266
- * Gets the associated Repository
1267
- * @param {string} repositoryName - Name of the Repository to retrieve
1268
- * @return {boolean} hasProperty
1269
- */
1270
- getAssociatedRepository = (repositoryName) => {
1271
- if (this.isDestroyed) {
1272
- throw Error('this.getAssociatedRepository is no longer valid. Entity has been destroyed.');
1273
- }
1274
-
1275
- const schema = this.getSchema();
1276
- if (!schema.model.associations.hasOne.includes(repositoryName) &&
1277
- !schema.model.associations.hasMany.includes(repositoryName) &&
1278
- !schema.model.associations.belongsTo.includes(repositoryName) &&
1279
- !schema.model.associations.belongsToMany.includes(repositoryName)
1280
- ) {
1281
- throw Error(repositoryName + ' is not associated with this schema');
1282
- }
1283
-
1284
- const oneHatData = this.oneHatData;
1285
- if (!oneHatData) {
1286
- throw Error('No global oneHatData object');
1287
- }
1288
-
1289
- const associatedRepository = oneHatData.getRepository(repositoryName);
1290
- if (!associatedRepository) {
1291
- throw Error('Repository ' + repositoryName + ' cannot be found');
1292
- }
1293
-
1294
- return associatedRepository;
1295
- }
1296
-
1297
1275
  /**
1298
1276
  * Utility function.
1299
1277
  * Detects if entity is in the current page of the storage medium.