@onehat/data 1.4.13 → 1.4.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.
@@ -14,7 +14,7 @@ describe('Entity', function() {
14
14
  properties: [
15
15
  { name: 'foo', type: 'int' },
16
16
  { name: 'bar' },
17
- { name: 'baz', mapping: 'baz.test.val', type: 'bool' },
17
+ { name: 'baz', mapping: 'baz.test.val', type: 'bool', defaultValue: null, },
18
18
  ],
19
19
  },
20
20
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onehat/data",
3
- "version": "1.4.13",
3
+ "version": "1.4.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
@@ -32,8 +32,10 @@ class Entity extends EventEmitter {
32
32
  * @constructor
33
33
  * @param {Schema} schema - Schema object
34
34
  * @param {object} rawData - Raw data object. Keys are Property names, Values are Property values.
35
+ * @param {Repository} repository
36
+ * @param {boolean} - Has rawData already been mapped according to schema?
35
37
  */
36
- constructor(schema, rawData = {}, repository = null) {
38
+ constructor(schema, rawData = {}, repository = null, originalIsMapped = false) {
37
39
  super(...arguments);
38
40
 
39
41
  if (!schema) {
@@ -86,6 +88,12 @@ class Entity extends EventEmitter {
86
88
  */
87
89
  this._originalDataParsed = null;
88
90
 
91
+ /**
92
+ * @member {boolean} originalIsMapped - Has the original data already been mapped according to schema?
93
+ * @private
94
+ */
95
+ this.originalIsMapped = originalIsMapped;
96
+
89
97
  /**
90
98
  * @member {Object} properties - Object of all Properties, keyed by id (for quick access)
91
99
  * These properties are actually created in the initialize() function.
@@ -204,6 +212,11 @@ class Entity extends EventEmitter {
204
212
  throw new Error('PropertyType ' + type + ' does not exist.');
205
213
  }
206
214
 
215
+ if (this.originalIsMapped) {
216
+ // Data has already been mapped according to schema, so alter definition
217
+ definition = _.clone(definition); // Clone it so you don't alter original in schema
218
+ definition.mapping = definition.name;
219
+ }
207
220
  const Property = PropertyTypes[type],
208
221
  property = new Property(definition, this._proxy);
209
222
 
@@ -847,10 +847,11 @@ export default class Repository extends EventEmitter {
847
847
  * Creates a single new Entity in storage medium.
848
848
  * @param {object} data - Either raw data object or Entity. If raw data, keys are Property names, Values are Property values.
849
849
  * @param {boolean} isPersisted - Whether the new entity should be marked as already being persisted in storage medium.
850
+ * @param {boolean} originalIsMapped - Has data already been mapped according to schema?
850
851
  * @return {object} entity - new Entity object
851
852
  * @fires add
852
853
  */
853
- add = async (data, isPersisted) => {
854
+ add = async (data, isPersisted = false, originalIsMapped = false) => {
854
855
  if (this.isDestroyed) {
855
856
  throw Error('this.add is no longer valid. Repository has been destroyed.');
856
857
  }
@@ -858,7 +859,7 @@ export default class Repository extends EventEmitter {
858
859
  let entity = data;
859
860
  if (!(data instanceof Entity)) {
860
861
  // Create the new entity
861
- entity = Repository._createEntity(this.schema, data, this, isPersisted);
862
+ entity = Repository._createEntity(this.schema, data, this, isPersisted, originalIsMapped);
862
863
  }
863
864
  this._relayEntityEvents(entity);
864
865
  this.entities.push(entity);
@@ -877,20 +878,27 @@ export default class Repository extends EventEmitter {
877
878
  return entity;
878
879
  }
879
880
 
881
+ /**
882
+ * Convenience function to add entity with mapped data.
883
+ */
884
+ addMapped = (data, isPersisted = false) => {
885
+ return this.add(data, isPersisted, true);
886
+ }
887
+
880
888
  /**
881
889
  * Convenience function to create multiple new Entities in storage medium.
882
890
  * @param {array} data - Array of data objects or Entities.
883
891
  * @param {boolean} isPersisted - Whether the new entities should be marked as already being persisted in storage medium.
884
892
  * @return {array} entities - new Entity objects
885
893
  */
886
- addMultiple = async (allData, isPersisted) => {
894
+ addMultiple = async (allData, isPersisted = false, originalIsMapped = false) => {
887
895
 
888
896
  let entities = [],
889
897
  i;
890
898
 
891
899
  for (i = 0; i < allData.length; i++) {
892
900
  const data = allData[i],
893
- entity = await this.add(data, isPersisted);
901
+ entity = await this.add(data, isPersisted, originalIsMapped);
894
902
  entities.push(entity);
895
903
  };
896
904
 
@@ -907,8 +915,8 @@ export default class Repository extends EventEmitter {
907
915
  * @return {object} entity - new Entity object
908
916
  * @private
909
917
  */
910
- static _createEntity = (schema, rawData, repository = null, isPersisted = false) => {
911
- const entity = new Entity(schema, rawData, repository);
918
+ static _createEntity = (schema, rawData, repository = null, isPersisted = false, originalIsMapped = false) => {
919
+ const entity = new Entity(schema, rawData, repository, originalIsMapped);
912
920
  entity.initialize();
913
921
  entity.isPersisted = isPersisted;
914
922
  return entity;