@onehat/data 1.7.16 → 1.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.
@@ -476,15 +476,40 @@ describe('Entity', function() {
476
476
  it('setValues', function() {
477
477
  expect(this.entity.foo).to.be.eq(1);
478
478
  expect(this.entity.bar).to.be.eq('one');
479
+ expect(this.entity.baz).to.be.eq(true);
479
480
  expect(this.entity.isDirty).to.be.false;
480
481
 
481
482
  this.entity.setValues({
482
483
  foo: 2,
483
484
  bar: 'two',
485
+ baz: false,
484
486
  });
485
487
 
486
488
  expect(this.entity.foo).to.be.eq(2);
487
489
  expect(this.entity.bar).to.be.eq('two');
490
+ expect(this.entity.baz).to.be.eq(false);
491
+ expect(this.entity.isDirty).to.be.true;
492
+ });
493
+
494
+ it('setRawValues', function() {
495
+ expect(this.entity.foo).to.be.eq(1);
496
+ expect(this.entity.bar).to.be.eq('one');
497
+ expect(this.entity.baz).to.be.eq(true);
498
+ expect(this.entity.isDirty).to.be.false;
499
+
500
+ this.entity.setRawValues({
501
+ foo: 2,
502
+ bar: 'two',
503
+ baz: {
504
+ test: {
505
+ val: false,
506
+ },
507
+ },
508
+ });
509
+
510
+ expect(this.entity.foo).to.be.eq(2);
511
+ expect(this.entity.bar).to.be.eq('two');
512
+ expect(this.entity.baz).to.be.eq(false);
488
513
  expect(this.entity.isDirty).to.be.true;
489
514
  });
490
515
 
@@ -553,6 +553,30 @@ describe('Repository Base', function() {
553
553
  expect(didFireAdd).to.be.true;
554
554
  });
555
555
 
556
+ it('add already existing', async function() {
557
+ const value = 'another one';
558
+ await this.repository.add({ key: 6, value: 'six' });
559
+ await this.repository.add({ key: 6, value });
560
+ expect(_.size(this.repository.entities)).to.be.eq(6);
561
+
562
+ const entity = this.repository.getById(6);
563
+ expect(entity.value).to.be.eq(value);
564
+ })
565
+
566
+ it('add with an existing id', async function() {
567
+ this.repository.autoSave = false;
568
+
569
+ // ID suppied; should not be temp ID or phantom
570
+ const entity = await this.repository.add({ key: 6, value: 'six' });
571
+ expect(entity.isTempId).to.be.false;
572
+ expect(entity.isPhantom).to.be.false;
573
+
574
+ // No ID suppled. Make it phantom and temp ID
575
+ const entity2 = await this.repository.add({ value: 'seven' });
576
+ expect(entity2.isTempId).to.be.true;
577
+ expect(entity2.isPhantom).to.be.true;
578
+ });
579
+
556
580
  it('createStandaloneEntity', async function() {
557
581
  const entity = await this.repository.createStandaloneEntity({ key: 6, value: 'six' });
558
582
  expect(entity.id).to.be.eq(6);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onehat/data",
3
- "version": "1.7.16",
3
+ "version": "1.8.2",
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
@@ -331,6 +331,7 @@ class Entity extends EventEmitter {
331
331
  if (this.isDeleted) {
332
332
  this.undelete();
333
333
  }
334
+ this.markStaged(false);
334
335
  this.setLastModified();
335
336
 
336
337
  this.emit('reset', this._proxy);
@@ -1022,23 +1023,58 @@ class Entity extends EventEmitter {
1022
1023
  propertyValues[propertyName] = rawValue;
1023
1024
  return this.setValues(propertyValues);
1024
1025
  }
1026
+
1027
+ /**
1028
+ * Sets Property values
1029
+ * @param {object} rawData - Raw data object. These are prior to mapping,
1030
+ * similar to what you'd use to create a brand new Entity. Make sure *all*
1031
+ * values are here, not just a few.
1032
+ * @return {boolean} isChanged - Whether any values were actually changed
1033
+ */
1034
+ setRawValues = (rawData) => {
1035
+ if (this.isDestroyed) {
1036
+ throw Error('this.setRawValues is no longer valid. Entity has been destroyed.');
1037
+ }
1038
+
1039
+ const [dependentProperties, nonDependentProperties] = _.partition(this.properties, (property) => {
1040
+ return property.hasDepends;
1041
+ });
1042
+ const mappedData = {};
1043
+ function setMappedValue(property) {
1044
+ let rawValue;
1045
+ if (property.hasMapping) {
1046
+ rawValue = Entity.getMappedValue(property.mapping, rawData);
1047
+ } else {
1048
+ rawValue = rawData[property.name];
1049
+ }
1050
+ if (_.isNil(rawValue)) {
1051
+ rawValue = property.getDefaultValue();
1052
+ }
1053
+ mappedData[property.name] = rawValue;
1054
+ }
1055
+
1056
+ _.each(nonDependentProperties, setMappedValue);
1057
+ _.each(dependentProperties, setMappedValue);
1058
+
1059
+ return this.setValues(mappedData);
1060
+ }
1025
1061
 
1026
1062
  /**
1027
1063
  * Sets Property values
1028
- * @param {object} rawData - Raw data object. Keys are Property names, Values are Property values.
1064
+ * @param {object} data - Raw data object. Keys are Property names, Values are Property values.
1029
1065
  * @return {boolean} isChanged - Whether any values were actually changed
1030
1066
  * @fires change
1031
1067
  */
1032
- setValues = (rawData) => {
1068
+ setValues = (data) => {
1033
1069
  if (this.isDestroyed) {
1034
1070
  throw Error('this.setValues is no longer valid. Entity has been destroyed.');
1035
1071
  }
1036
- if (_.indexOf(rawData, this.getIdProperty().name) !== -1) {
1072
+ if (_.indexOf(data, this.getIdProperty().name) !== -1) {
1037
1073
  throw new Error('Cannot change id via entity.setValues(). Must use entity.setId() first.');
1038
1074
  }
1039
1075
 
1040
1076
  let isChanged = false;
1041
- _.each(rawData, (value, propertyName) => {
1077
+ _.each(data, (value, propertyName) => {
1042
1078
  const property = this.getProperty(propertyName);
1043
1079
  property.pauseEvents(); // We don't need property_change to fire
1044
1080
  if (property.setValue(value)) {
@@ -102,6 +102,11 @@ class AjaxRepository extends Repository {
102
102
  */
103
103
  this.allowsMultiSort = false;
104
104
 
105
+ /**
106
+ * @member {object} lastSendOptions - Last options sent to server
107
+ */
108
+ this.lastSendOptions = null;
109
+
105
110
  /**
106
111
  * @member {Object} _params - Object of query params to submit to server
107
112
  * @private
@@ -786,6 +791,8 @@ class AjaxRepository extends Repository {
786
791
  console.log(url, options);
787
792
  }
788
793
 
794
+ this.lastSendOptions = options;
795
+
789
796
  return this.axios(options)
790
797
  .catch(error => {
791
798
  if (this.debugMode) {
@@ -113,6 +113,8 @@ class OneBuildRepository extends AjaxRepository {
113
113
  console.log(url, options);
114
114
  }
115
115
 
116
+ this.lastSendOptions = options;
117
+
116
118
  return this.axios(options)
117
119
  .catch(error => {
118
120
  // BEGIN MOD
@@ -923,6 +923,19 @@ export default class Repository extends EventEmitter {
923
923
  throw Error('this.add is no longer valid. Repository has been destroyed.');
924
924
  }
925
925
 
926
+ // Does it already exist? If so, edit the existing
927
+ const idProperty = this.getSchema().model.idProperty;
928
+ if (data.hasOwnProperty(idProperty)) {
929
+ if (this.isInRepository(data[idProperty])) {
930
+ const existing = this.getById(data[idProperty]);
931
+ existing.setRawValues(data);
932
+ if (this.autoSave && !existing.isPersisted) {
933
+ await this.save(existing);
934
+ }
935
+ return existing;
936
+ }
937
+ }
938
+
926
939
  let entity = data;
927
940
  if (!(data instanceof Entity)) {
928
941
  // Create the new entity