@onehat/data 1.7.15 → 1.8.1

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.15",
3
+ "version": "1.8.1",
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
@@ -1022,23 +1022,58 @@ class Entity extends EventEmitter {
1022
1022
  propertyValues[propertyName] = rawValue;
1023
1023
  return this.setValues(propertyValues);
1024
1024
  }
1025
+
1026
+ /**
1027
+ * Sets Property values
1028
+ * @param {object} rawData - Raw data object. These are prior to mapping,
1029
+ * similar to what you'd use to create a brand new Entity. Make sure *all*
1030
+ * values are here, not just a few.
1031
+ * @return {boolean} isChanged - Whether any values were actually changed
1032
+ */
1033
+ setRawValues = (rawData) => {
1034
+ if (this.isDestroyed) {
1035
+ throw Error('this.setRawValues is no longer valid. Entity has been destroyed.');
1036
+ }
1037
+
1038
+ const [dependentProperties, nonDependentProperties] = _.partition(this.properties, (property) => {
1039
+ return property.hasDepends;
1040
+ });
1041
+ const mappedData = {};
1042
+ function setMappedValue(property) {
1043
+ let rawValue;
1044
+ if (property.hasMapping) {
1045
+ rawValue = Entity.getMappedValue(property.mapping, rawData);
1046
+ } else {
1047
+ rawValue = rawData[property.name];
1048
+ }
1049
+ if (_.isNil(rawValue)) {
1050
+ rawValue = property.getDefaultValue();
1051
+ }
1052
+ mappedData[property.name] = rawValue;
1053
+ }
1054
+
1055
+ _.each(nonDependentProperties, setMappedValue);
1056
+ _.each(dependentProperties, setMappedValue);
1057
+
1058
+ return this.setValues(mappedData);
1059
+ }
1025
1060
 
1026
1061
  /**
1027
1062
  * Sets Property values
1028
- * @param {object} rawData - Raw data object. Keys are Property names, Values are Property values.
1063
+ * @param {object} data - Raw data object. Keys are Property names, Values are Property values.
1029
1064
  * @return {boolean} isChanged - Whether any values were actually changed
1030
1065
  * @fires change
1031
1066
  */
1032
- setValues = (rawData) => {
1067
+ setValues = (data) => {
1033
1068
  if (this.isDestroyed) {
1034
1069
  throw Error('this.setValues is no longer valid. Entity has been destroyed.');
1035
1070
  }
1036
- if (_.indexOf(rawData, this.getIdProperty().name) !== -1) {
1071
+ if (_.indexOf(data, this.getIdProperty().name) !== -1) {
1037
1072
  throw new Error('Cannot change id via entity.setValues(). Must use entity.setId() first.');
1038
1073
  }
1039
1074
 
1040
1075
  let isChanged = false;
1041
- _.each(rawData, (value, propertyName) => {
1076
+ _.each(data, (value, propertyName) => {
1042
1077
  const property = this.getProperty(propertyName);
1043
1078
  property.pauseEvents(); // We don't need property_change to fire
1044
1079
  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
@@ -274,8 +274,9 @@ export default class Repository extends EventEmitter {
274
274
  this._createMethods();
275
275
  this._createStatics();
276
276
 
277
- if (this.schema.repository.init) {
278
- await this.schema.repository.init.call(this);
277
+ const init = this.schema.repository.init || this.originalConfig.init; // The latter is mainly for lfr repositories
278
+ if (init) {
279
+ await init.call(this);
279
280
  }
280
281
 
281
282
  this.isInitialized = true;
@@ -290,7 +291,7 @@ export default class Repository extends EventEmitter {
290
291
  if (this.isDestroyed) {
291
292
  throw Error('this._createMethods is no longer valid. Repository has been destroyed.');
292
293
  }
293
- const methodDefinitions = this.schema.repository.methods;
294
+ const methodDefinitions = this.schema.repository.methods || this.originalConfig.methods; // The latter is mainly for lfr repositories
294
295
  if (!_.isEmpty(methodDefinitions)) {
295
296
  _.each(methodDefinitions, (method, name) => {
296
297
  this[name] = method; // NOTE: Methods must be defined in schema as "function() {}", not as "() => {}" so "this" will be assigned correctly
@@ -306,7 +307,7 @@ export default class Repository extends EventEmitter {
306
307
  if (this.isDestroyed) {
307
308
  throw Error('this._createStatics is no longer valid. Entity has been destroyed.');
308
309
  }
309
- const staticsDefinitions = this.schema.repository.statics;
310
+ const staticsDefinitions = this.schema.repository.statics || this.originalConfig.statics; // The latter is mainly for lfr repositories
310
311
  if (!_.isEmpty(staticsDefinitions)) {
311
312
  _.each(staticsDefinitions, (value, key) => {
312
313
  this[key] = value;
@@ -922,6 +923,19 @@ export default class Repository extends EventEmitter {
922
923
  throw Error('this.add is no longer valid. Repository has been destroyed.');
923
924
  }
924
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
+
925
939
  let entity = data;
926
940
  if (!(data instanceof Entity)) {
927
941
  // Create the new entity