@onehat/data 1.11.6 → 1.13.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onehat/data",
3
- "version": "1.11.6",
3
+ "version": "1.13.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
@@ -34,7 +34,7 @@ class Entity extends EventEmitter {
34
34
  * @param {Repository} repository
35
35
  * @param {boolean} - Has rawData already been mapped according to schema?
36
36
  */
37
- constructor(schema, rawData = {}, repository = null, originalIsMapped = false) {
37
+ constructor(schema, rawData = {}, repository = null, originalIsMapped = false, isDelayedSave = false) {
38
38
  super(...arguments);
39
39
 
40
40
  if (!schema) {
@@ -110,6 +110,7 @@ class Entity extends EventEmitter {
110
110
 
111
111
  /**
112
112
  * @member {Boolean} isInitialized - State: whether or not this entity has been completely initialized
113
+ * @public
113
114
  */
114
115
  this.isInitialized = false;
115
116
 
@@ -127,28 +128,37 @@ class Entity extends EventEmitter {
127
128
 
128
129
  /**
129
130
  * @member {boolean} isDestroyed - Whether this object has been destroyed
130
- * @private
131
+ * @public
131
132
  */
132
133
  this.isDestroyed = false;
133
134
 
134
135
  /**
135
- * @member {string} lastModified - Last time this entity was modified
136
+ * @member {boolean} isFrozen - Prevent the entity from being saved, so an editor can change it before it gets saved to remote storage.
137
+ * @public
138
+ */
139
+ this.isDelayedSave = isDelayedSave;
140
+
141
+ /**
142
+ * @member {boolean} lastModified - Last time this entity was modified
143
+ * @public
136
144
  */
137
145
  this.lastModified = null;
138
146
 
139
147
  /**
140
- * @member {string} isFrozen - Prevent the entity from being destroyed, but don't let it be changed either.
148
+ * @member {boolean} isFrozen - Prevent the entity from being destroyed, but don't let it be changed either.
149
+ * @public
141
150
  */
142
151
  this.isFrozen = false;
143
152
 
144
153
  /**
145
154
  * @member {boolean} isValid - Whether this Entity passes validation
146
- * @private
155
+ * @public
147
156
  */
148
157
  this.isValid = null;
149
158
 
150
159
  /**
151
160
  * @member {object} validationError - Any error in last validation.
161
+ * @public
152
162
  */
153
163
  this.validationError = null;
154
164
 
@@ -479,9 +479,15 @@ export default class Repository extends EventEmitter {
479
479
  throw Error('this.getDefaultSorters is no longer valid. Repository has been destroyed.');
480
480
  }
481
481
  let sorters = [];
482
- if (this.schema && this.schema.model) {
482
+ if (this.schema?.model) {
483
483
  if (_.size(this.schema.model.sorters) > 0) {
484
484
  sorters = this.schema.model.sorters
485
+ } else if (!_.isNil(this.schema.model.sortProperty)) {
486
+ sorters = [{
487
+ name: this.schema.model.sortProperty,
488
+ direction: 'ASC',
489
+ fn: 'default',
490
+ }];
485
491
  } else if (!_.isNil(this.schema.model.displayProperty)) {
486
492
  sorters = [{
487
493
  name: this.schema.model.displayProperty,
@@ -923,10 +929,11 @@ export default class Repository extends EventEmitter {
923
929
  * @param {object} data - Either raw data object or Entity. If raw data, keys are Property names, Values are Property values.
924
930
  * @param {boolean} isPersisted - Whether the new entity should be marked as already being persisted in storage medium.
925
931
  * @param {boolean} originalIsMapped - Has data already been mapped according to schema?
932
+ * @param {boolean} isDelayedSave - Should the repository skip autosave when immediately adding the record?
926
933
  * @return {object} entity - new Entity object
927
934
  * @fires add
928
935
  */
929
- add = async (data, isPersisted = false, originalIsMapped = false) => {
936
+ add = async (data, isPersisted = false, originalIsMapped = false, isDelayedSave = false) => {
930
937
  if (this.isDestroyed) {
931
938
  throw Error('this.add is no longer valid. Repository has been destroyed.');
932
939
  }
@@ -947,7 +954,7 @@ export default class Repository extends EventEmitter {
947
954
  let entity = data;
948
955
  if (!(data instanceof Entity)) {
949
956
  // Create the new entity
950
- entity = Repository._createEntity(this.schema, data, this, isPersisted, originalIsMapped);
957
+ entity = Repository._createEntity(this.schema, data, this, isPersisted, originalIsMapped, isDelayedSave);
951
958
  }
952
959
  this._relayEntityEvents(entity);
953
960
  this.entities.push(entity);
@@ -959,7 +966,7 @@ export default class Repository extends EventEmitter {
959
966
 
960
967
  this.emit('add', entity);
961
968
 
962
- if (this.isAutoSave && !entity.isPersisted) {
969
+ if (this.isAutoSave && !entity.isPersisted && !entity.isDelayedSave) {
963
970
  await this.save(entity);
964
971
  }
965
972
 
@@ -973,12 +980,12 @@ export default class Repository extends EventEmitter {
973
980
  * @param {boolean} originalIsMapped - Has data already been mapped according to schema?
974
981
  * @return {object} entity - new Entity object
975
982
  */
976
- createStandaloneEntity = async (data, isPersisted = false, originalIsMapped = false) => {
983
+ createStandaloneEntity = async (data, isPersisted = false, originalIsMapped = false, isDelayedSave = false) => {
977
984
  if (this.isDestroyed) {
978
985
  throw Error('this.createStandaloneEntity is no longer valid. Repository has been destroyed.');
979
986
  }
980
987
 
981
- const entity = Repository._createEntity(this.schema, data, this, isPersisted, originalIsMapped);
988
+ const entity = Repository._createEntity(this.schema, data, this, isPersisted, originalIsMapped, isDelayedSave);
982
989
 
983
990
  if (entity.isPhantom) {
984
991
  entity.createTempId();
@@ -1024,8 +1031,8 @@ export default class Repository extends EventEmitter {
1024
1031
  * @return {object} entity - new Entity object
1025
1032
  * @private
1026
1033
  */
1027
- static _createEntity = (schema, rawData, repository = null, isPersisted = false, originalIsMapped = false) => {
1028
- const entity = new Entity(schema, rawData, repository, originalIsMapped);
1034
+ static _createEntity = (schema, rawData, repository = null, isPersisted = false, originalIsMapped = false, isDelayedSave = false) => {
1035
+ const entity = new Entity(schema, rawData, repository, originalIsMapped, isDelayedSave);
1029
1036
  entity.initialize();
1030
1037
  entity.isPersisted = isPersisted;
1031
1038
  return entity;
@@ -58,6 +58,12 @@ export default class Schema extends EventEmitter {
58
58
  */
59
59
  displayProperty: null,
60
60
 
61
+ /**
62
+ * @member {string} sortProperty - name of sort Property (e.g. 'users__sort_order')
63
+ * if sortProperty has a value and sorters is left blank, an automatic sorter with this property will be created.
64
+ */
65
+ sortProperty: null,
66
+
61
67
  /**
62
68
  * @member {array} properties - Array of Property definition objects
63
69
  */