@onehat/data 1.11.5 → 1.12.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 +1 -1
- package/src/Entity.js +15 -5
- package/src/Repository/Offline.js +30 -30
- package/src/Repository/Repository.js +8 -7
package/package.json
CHANGED
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
|
-
* @
|
|
131
|
+
* @public
|
|
131
132
|
*/
|
|
132
133
|
this.isDestroyed = false;
|
|
133
134
|
|
|
134
135
|
/**
|
|
135
|
-
* @member {
|
|
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 {
|
|
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
|
-
* @
|
|
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
|
|
|
@@ -169,17 +169,16 @@ class OfflineRepository extends MemoryRepository {
|
|
|
169
169
|
// Attempt to add
|
|
170
170
|
super._doAdd(entity);
|
|
171
171
|
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
.
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
});
|
|
172
|
+
let storageResult;
|
|
173
|
+
try {
|
|
174
|
+
storageResult = await this._storageSetValue(entity.id, entity.getOriginalData());
|
|
175
|
+
this._addToIndex(entity.id);
|
|
176
|
+
} catch (e) {
|
|
177
|
+
// Revert to clone
|
|
178
|
+
delete this._keyedEntities[entity.id];
|
|
179
|
+
entity.destroy();
|
|
180
|
+
this._keyedEntities[clone.id] = clone;
|
|
181
|
+
}
|
|
183
182
|
|
|
184
183
|
return storageResult;
|
|
185
184
|
}
|
|
@@ -206,15 +205,16 @@ class OfflineRepository extends MemoryRepository {
|
|
|
206
205
|
|
|
207
206
|
// Attempt to edit
|
|
208
207
|
super._doEdit(entity);
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
.
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
208
|
+
|
|
209
|
+
let storageResult;
|
|
210
|
+
try {
|
|
211
|
+
storageResult = await this._storageSetValue(entity.id, entity.getOriginalData());
|
|
212
|
+
} catch (e) {
|
|
213
|
+
// Revert to clone
|
|
214
|
+
entity.isPersisted = clone.isPersisted;
|
|
215
|
+
entity._originalData = clone._originalData;
|
|
216
|
+
entity._originalDataParsed = clone._originalDataParsed;
|
|
217
|
+
}
|
|
218
218
|
|
|
219
219
|
return storageResult;
|
|
220
220
|
}
|
|
@@ -228,16 +228,16 @@ class OfflineRepository extends MemoryRepository {
|
|
|
228
228
|
|
|
229
229
|
// Attempt to delete
|
|
230
230
|
super._doDelete(entity);
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
storageResult
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
let storageResult;
|
|
234
|
+
try {
|
|
235
|
+
storageResult = await this._storageDeleteValue(entity.id);
|
|
236
|
+
this._deleteFromIndex(entity.id);
|
|
237
|
+
} catch (e) {
|
|
238
|
+
// Revert to clone
|
|
239
|
+
this._keyedEntities[clone.id] = clone;
|
|
240
|
+
}
|
|
241
241
|
|
|
242
242
|
return storageResult;
|
|
243
243
|
}
|
|
@@ -923,10 +923,11 @@ export default class Repository extends EventEmitter {
|
|
|
923
923
|
* @param {object} data - Either raw data object or Entity. If raw data, keys are Property names, Values are Property values.
|
|
924
924
|
* @param {boolean} isPersisted - Whether the new entity should be marked as already being persisted in storage medium.
|
|
925
925
|
* @param {boolean} originalIsMapped - Has data already been mapped according to schema?
|
|
926
|
+
* @param {boolean} isDelayedSave - Should the repository skip autosave when immediately adding the record?
|
|
926
927
|
* @return {object} entity - new Entity object
|
|
927
928
|
* @fires add
|
|
928
929
|
*/
|
|
929
|
-
add = async (data, isPersisted = false, originalIsMapped = false) => {
|
|
930
|
+
add = async (data, isPersisted = false, originalIsMapped = false, isDelayedSave = false) => {
|
|
930
931
|
if (this.isDestroyed) {
|
|
931
932
|
throw Error('this.add is no longer valid. Repository has been destroyed.');
|
|
932
933
|
}
|
|
@@ -947,7 +948,7 @@ export default class Repository extends EventEmitter {
|
|
|
947
948
|
let entity = data;
|
|
948
949
|
if (!(data instanceof Entity)) {
|
|
949
950
|
// Create the new entity
|
|
950
|
-
entity = Repository._createEntity(this.schema, data, this, isPersisted, originalIsMapped);
|
|
951
|
+
entity = Repository._createEntity(this.schema, data, this, isPersisted, originalIsMapped, isDelayedSave);
|
|
951
952
|
}
|
|
952
953
|
this._relayEntityEvents(entity);
|
|
953
954
|
this.entities.push(entity);
|
|
@@ -959,7 +960,7 @@ export default class Repository extends EventEmitter {
|
|
|
959
960
|
|
|
960
961
|
this.emit('add', entity);
|
|
961
962
|
|
|
962
|
-
if (this.isAutoSave && !entity.isPersisted) {
|
|
963
|
+
if (this.isAutoSave && !entity.isPersisted && !entity.isDelayedSave) {
|
|
963
964
|
await this.save(entity);
|
|
964
965
|
}
|
|
965
966
|
|
|
@@ -973,12 +974,12 @@ export default class Repository extends EventEmitter {
|
|
|
973
974
|
* @param {boolean} originalIsMapped - Has data already been mapped according to schema?
|
|
974
975
|
* @return {object} entity - new Entity object
|
|
975
976
|
*/
|
|
976
|
-
createStandaloneEntity = async (data, isPersisted = false, originalIsMapped = false) => {
|
|
977
|
+
createStandaloneEntity = async (data, isPersisted = false, originalIsMapped = false, isDelayedSave = false) => {
|
|
977
978
|
if (this.isDestroyed) {
|
|
978
979
|
throw Error('this.createStandaloneEntity is no longer valid. Repository has been destroyed.');
|
|
979
980
|
}
|
|
980
981
|
|
|
981
|
-
const entity = Repository._createEntity(this.schema, data, this, isPersisted, originalIsMapped);
|
|
982
|
+
const entity = Repository._createEntity(this.schema, data, this, isPersisted, originalIsMapped, isDelayedSave);
|
|
982
983
|
|
|
983
984
|
if (entity.isPhantom) {
|
|
984
985
|
entity.createTempId();
|
|
@@ -1024,8 +1025,8 @@ export default class Repository extends EventEmitter {
|
|
|
1024
1025
|
* @return {object} entity - new Entity object
|
|
1025
1026
|
* @private
|
|
1026
1027
|
*/
|
|
1027
|
-
static _createEntity = (schema, rawData, repository = null, isPersisted = false, originalIsMapped = false) => {
|
|
1028
|
-
const entity = new Entity(schema, rawData, repository, originalIsMapped);
|
|
1028
|
+
static _createEntity = (schema, rawData, repository = null, isPersisted = false, originalIsMapped = false, isDelayedSave = false) => {
|
|
1029
|
+
const entity = new Entity(schema, rawData, repository, originalIsMapped, isDelayedSave);
|
|
1029
1030
|
entity.initialize();
|
|
1030
1031
|
entity.isPersisted = isPersisted;
|
|
1031
1032
|
return entity;
|