@onehat/data 1.4.14 → 1.5.0

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.
@@ -198,6 +198,17 @@ describe('Entity', function() {
198
198
  expect(_.isEqual(this.entity.submitValues, expected)).to.be.true;
199
199
  });
200
200
 
201
+ it('getSubmitValuesMapped & submitValuesMapped', function() {
202
+ const result = this.entity.getSubmitValuesMapped(),
203
+ expected = {
204
+ foo: 1,
205
+ bar: 'one',
206
+ 'baz.test.val': true,
207
+ };
208
+ expect(_.isEqual(result, expected)).to.be.true;
209
+ expect(_.isEqual(this.entity.submitValuesMapped, expected)).to.be.true;
210
+ });
211
+
201
212
  it('getDisplayValues & displayValues', function() {
202
213
  const result = this.entity.getDisplayValues(),
203
214
  expected = {
@@ -258,20 +269,48 @@ describe('Entity', function() {
258
269
  });
259
270
 
260
271
  it('getReverseMappedRawValue', function() {
261
- const definition = {
262
- name: 'foo',
263
- mapping: 'a.b.c',
264
- type: 'int',
265
- },
266
- Property = PropertyTypes[definition.type];
267
- const property = new Property(definition, 'fakeEntity');
272
+ const definition1 = {
273
+ name: 'foo',
274
+ mapping: 'a.b.c',
275
+ type: 'int',
276
+ },
277
+ Property1 = PropertyTypes[definition1.type];
278
+ const property1 = new Property1(definition1, 'fakeEntity');
279
+
280
+ property1.setValue('47');
281
+
282
+ const reverseMapped1 = Entity.getReverseMappedRawValue(property1),
283
+ expected1 = { a: { b: { c: '47' }, }, };
284
+
285
+ expect(_.isEqual(reverseMapped1, expected1)).to.be.true;
286
+
287
+
288
+ // Now, with no mapping
289
+ const definition2 = {
290
+ name: 'foo',
291
+ type: 'int',
292
+ },
293
+ Property2 = PropertyTypes[definition2.type];
294
+ const property2 = new Property2(definition2, 'fakeEntity');
268
295
 
269
- property.setValue('47');
296
+ property2.setValue('47');
270
297
 
271
- const reverseMapped = Entity.getReverseMappedRawValue(property),
272
- expected = { a: { b: { c: '47' }, }, };
298
+ const reverseMapped2 = Entity.getReverseMappedRawValue(property2),
299
+ expected2 = { foo: '47' };
273
300
 
274
- expect(_.isEqual(reverseMapped, expected)).to.be.true;
301
+ expect(_.isEqual(reverseMapped2, expected2)).to.be.true;
302
+ });
303
+
304
+ it('getReverseMappedRawValues', function() {
305
+ const result = this.entity.getReverseMappedRawValues();
306
+ expect(_.isEqual(result, this.data)).to.be.true;
307
+ });
308
+
309
+ it('build a new entity from an existing one using getDataForNewEntity', function() {
310
+ const entity = new Entity(this.schema, this.entity.getDataForNewEntity());
311
+ entity.initialize();
312
+
313
+ expect(_.isEqual(entity.submitValues, this.entity.submitValues)).to.be.true;
275
314
  });
276
315
 
277
316
  it('getChanged', function() {
@@ -108,6 +108,10 @@ describe('Property', function() {
108
108
  const property = entity.getProperty('bar');
109
109
  expect(property.isDisplayProperty).to.be.true;
110
110
  });
111
+
112
+ it('getMapping', function() {
113
+ expect(this.property.getMapping()).to.be.eq('id');
114
+ });
111
115
 
112
116
  });
113
117
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onehat/data",
3
- "version": "1.4.14",
3
+ "version": "1.5.0",
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
@@ -457,6 +457,36 @@ class Entity extends EventEmitter {
457
457
  return this.getSubmitValues();
458
458
  }
459
459
 
460
+ /**
461
+ * Gets an object of properties/values for this Entity,
462
+ * and returns them with the mapped names
463
+ * Values are the "submit" values, not the "raw" or "parsed" or "display" values.
464
+ * @return {object} propertyValues
465
+ */
466
+ getSubmitValuesMapped = () => {
467
+ if (this.isDestroyed) {
468
+ throw Error('this.getSubmitValuesMapped is no longer valid. Entity has been destroyed.');
469
+ }
470
+
471
+ let propertyValues = {};
472
+ _.forOwn(this.properties, (property) => {
473
+ const name = property.hasMapping ? property.getMapping() : property.name;
474
+ propertyValues[name] = property.getSubmitValue();
475
+ });
476
+ return propertyValues;
477
+ }
478
+
479
+ /**
480
+ * Gets "submit" values for this Entity, and returns them with the mapped names
481
+ * @return {object} values
482
+ */
483
+ get submitValuesMapped() {
484
+ if (this.isDestroyed) {
485
+ throw Error('this.submitValuesMapped is no longer valid. Entity has been destroyed.');
486
+ }
487
+ return this.getSubmitValuesMapped();
488
+ }
489
+
460
490
  /**
461
491
  * Gets an object of values for this Entity,
462
492
  * Values are the "display" values, not the "raw" or "parsed" or "submit" values.
@@ -569,6 +599,11 @@ class Entity extends EventEmitter {
569
599
  * @static
570
600
  */
571
601
  static getReverseMappedRawValue(property) {
602
+ if (!property.hasMapping) {
603
+ const obj = {};
604
+ obj[property.name] = property.rawValue;
605
+ return obj;
606
+ }
572
607
  const mapStack = property.mapping.split('.'),
573
608
  rawValue = property.getRawValue();
574
609
 
@@ -591,6 +626,37 @@ class Entity extends EventEmitter {
591
626
  return value;
592
627
  }
593
628
 
629
+ /**
630
+ * Builds up an object of original values for this entity, from which another entity could be easily created
631
+ * @return {object} value - An object representing the 'path' to the raw value.
632
+ * e.g. With a mapping of 'a.b.c' and a property rawValue of '47', the
633
+ * following object will be returned:{ a: { b: { c: '47' }, }, }
634
+ */
635
+ getReverseMappedRawValues = () => {
636
+ if (this.isDestroyed) {
637
+ throw Error('this.getReverseMappedRawValues is no longer valid. Entity has been destroyed.');
638
+ }
639
+
640
+ let propertyValues = {};
641
+ _.forOwn(this.properties, (property) => {
642
+ const reverseMapped = Entity.getReverseMappedRawValue(property);
643
+ _.assign(propertyValues, reverseMapped);
644
+ });
645
+ return propertyValues;
646
+ }
647
+
648
+ /**
649
+ * Convenience function
650
+ * Build a new entity with this data
651
+ */
652
+ getDataForNewEntity = () => {
653
+ if (this.isDestroyed) {
654
+ throw Error('this.getDataForNewEntity is no longer valid. Entity has been destroyed.');
655
+ }
656
+
657
+ return this.getReverseMappedRawValues();
658
+ }
659
+
594
660
  /**
595
661
  * Gets the values that have changed since last time saved
596
662
  * @return {array|boolean} diff - Array of property names that have changed, or false
@@ -405,6 +405,17 @@ export default class Property extends EventEmitter {
405
405
  return this.__proto__.constructor.className;
406
406
  }
407
407
 
408
+ /**
409
+ * Gets the mapped name of this Property.
410
+ * @return {string} name
411
+ */
412
+ getMapping = () => {
413
+ if (this.isDestroyed) {
414
+ throw Error('this.getMappedName is no longer valid. Property has been destroyed.');
415
+ }
416
+ return this.mapping;
417
+ }
418
+
408
419
  /**
409
420
  * Destroy this object.
410
421
  * - Removes all circular references to parent objects