@onehat/data 1.8.11 → 1.8.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.
- package/cypress/integration/Entity.spec.js +16 -0
- package/package.json +1 -1
- package/src/Entity.js +49 -0
- package/src/Repository/Repository.js +4 -2
- package/src/Util/Parsers.js +11 -11
|
@@ -631,6 +631,22 @@ describe('Entity', function() {
|
|
|
631
631
|
lateLastModified = this.entity.lastModified;
|
|
632
632
|
expect(earlyLastModified < lateLastModified).to.be.true;
|
|
633
633
|
});
|
|
634
|
+
|
|
635
|
+
it('freeze', function() {
|
|
636
|
+
this.entity.freeze();
|
|
637
|
+
expect(this.entity.isFrozen).to.be.true;
|
|
638
|
+
|
|
639
|
+
this.entity.destroy();
|
|
640
|
+
expect(this.entity.isDestroyed).to.be.false;
|
|
641
|
+
|
|
642
|
+
let isError = false;
|
|
643
|
+
try {
|
|
644
|
+
this.entity.foo = 2;
|
|
645
|
+
} catch(e) {
|
|
646
|
+
isError = true;
|
|
647
|
+
}
|
|
648
|
+
expect(isError).to.be.true;
|
|
649
|
+
});
|
|
634
650
|
});
|
|
635
651
|
|
|
636
652
|
describe('events', function() {
|
package/package.json
CHANGED
package/src/Entity.js
CHANGED
|
@@ -135,6 +135,12 @@ class Entity extends EventEmitter {
|
|
|
135
135
|
*/
|
|
136
136
|
this.lastModified = null;
|
|
137
137
|
|
|
138
|
+
/**
|
|
139
|
+
* @member {string} isFrozen - Prevent the entity from being destroyed, but don't let it be changed either.
|
|
140
|
+
*/
|
|
141
|
+
this.isFrozen = false;
|
|
142
|
+
|
|
143
|
+
|
|
138
144
|
// This ES6 Proxy allows us to create magic getters and setters for all property values.
|
|
139
145
|
// However, these getters and setters are *not* available within the Entity itself.
|
|
140
146
|
this._proxy = new Proxy(this, {
|
|
@@ -151,6 +157,9 @@ class Entity extends EventEmitter {
|
|
|
151
157
|
return Reflect.get(target, name, receiver);
|
|
152
158
|
},
|
|
153
159
|
set (target, name, value, receiver) {
|
|
160
|
+
if (this.isFrozen) {
|
|
161
|
+
throw Error('Entity is frozen.');
|
|
162
|
+
}
|
|
154
163
|
if (!Reflect.has(target, name)) {
|
|
155
164
|
target.setValue(name, value);
|
|
156
165
|
} else {
|
|
@@ -1011,6 +1020,9 @@ class Entity extends EventEmitter {
|
|
|
1011
1020
|
* @return {boolean} isChanged - Whether id was actually changed
|
|
1012
1021
|
*/
|
|
1013
1022
|
setId = (id, force = false) => {
|
|
1023
|
+
if (this.isFrozen) {
|
|
1024
|
+
throw Error('Entity is frozen.');
|
|
1025
|
+
}
|
|
1014
1026
|
let isChanged = false;
|
|
1015
1027
|
const idProperty = this.getIdProperty();
|
|
1016
1028
|
|
|
@@ -1045,6 +1057,9 @@ class Entity extends EventEmitter {
|
|
|
1045
1057
|
* @return {boolean} isChanged - Whether any values were actually changed
|
|
1046
1058
|
*/
|
|
1047
1059
|
setValue = (propertyName, rawValue) => {
|
|
1060
|
+
if (this.isFrozen) {
|
|
1061
|
+
throw Error('Entity is frozen.');
|
|
1062
|
+
}
|
|
1048
1063
|
if (this.isDestroyed) {
|
|
1049
1064
|
throw Error('this.setValue is no longer valid. Entity has been destroyed.');
|
|
1050
1065
|
}
|
|
@@ -1061,6 +1076,9 @@ class Entity extends EventEmitter {
|
|
|
1061
1076
|
* @return {boolean} isChanged - Whether any values were actually changed
|
|
1062
1077
|
*/
|
|
1063
1078
|
setRawValues = (rawData) => {
|
|
1079
|
+
if (this.isFrozen) {
|
|
1080
|
+
throw Error('Entity is frozen.');
|
|
1081
|
+
}
|
|
1064
1082
|
if (this.isDestroyed) {
|
|
1065
1083
|
throw Error('this.setRawValues is no longer valid. Entity has been destroyed.');
|
|
1066
1084
|
}
|
|
@@ -1095,6 +1113,9 @@ class Entity extends EventEmitter {
|
|
|
1095
1113
|
* @fires change
|
|
1096
1114
|
*/
|
|
1097
1115
|
setValues = (data) => {
|
|
1116
|
+
if (this.isFrozen) {
|
|
1117
|
+
throw Error('Entity is frozen.');
|
|
1118
|
+
}
|
|
1098
1119
|
if (this.isDestroyed) {
|
|
1099
1120
|
throw Error('this.setValues is no longer valid. Entity has been destroyed.');
|
|
1100
1121
|
}
|
|
@@ -1156,6 +1177,9 @@ class Entity extends EventEmitter {
|
|
|
1156
1177
|
* @fires save
|
|
1157
1178
|
*/
|
|
1158
1179
|
save = () => {
|
|
1180
|
+
if (this.isFrozen) {
|
|
1181
|
+
throw Error('Entity is frozen.');
|
|
1182
|
+
}
|
|
1159
1183
|
if (this.isDestroyed) {
|
|
1160
1184
|
throw Error('this.save is no longer valid. Entity has been destroyed.');
|
|
1161
1185
|
}
|
|
@@ -1169,6 +1193,9 @@ class Entity extends EventEmitter {
|
|
|
1169
1193
|
* Marks an entity as having been saved to storage medium.
|
|
1170
1194
|
*/
|
|
1171
1195
|
markSaved = () => {
|
|
1196
|
+
if (this.isFrozen) {
|
|
1197
|
+
throw Error('Entity is frozen.');
|
|
1198
|
+
}
|
|
1172
1199
|
if (this.isDestroyed) {
|
|
1173
1200
|
throw Error('this.markSaved is no longer valid. Entity has been destroyed.');
|
|
1174
1201
|
}
|
|
@@ -1184,6 +1211,9 @@ class Entity extends EventEmitter {
|
|
|
1184
1211
|
* @param {boolean} bool - How it should be marked. Defaults to true.
|
|
1185
1212
|
*/
|
|
1186
1213
|
markDeleted = (bool = true) => {
|
|
1214
|
+
if (this.isFrozen) {
|
|
1215
|
+
throw Error('Entity is frozen.');
|
|
1216
|
+
}
|
|
1187
1217
|
if (this.isDestroyed) {
|
|
1188
1218
|
throw Error('this.markDeleted is no longer valid. Entity has been destroyed.');
|
|
1189
1219
|
}
|
|
@@ -1195,6 +1225,9 @@ class Entity extends EventEmitter {
|
|
|
1195
1225
|
* @fires delete
|
|
1196
1226
|
*/
|
|
1197
1227
|
delete = () => {
|
|
1228
|
+
if (this.isFrozen) {
|
|
1229
|
+
throw Error('Entity is frozen.');
|
|
1230
|
+
}
|
|
1198
1231
|
if (this.isDestroyed) {
|
|
1199
1232
|
throw Error('this.delete is no longer valid. Entity has been destroyed.');
|
|
1200
1233
|
}
|
|
@@ -1211,6 +1244,9 @@ class Entity extends EventEmitter {
|
|
|
1211
1244
|
* @fires delete
|
|
1212
1245
|
*/
|
|
1213
1246
|
undelete = () => {
|
|
1247
|
+
if (this.isFrozen) {
|
|
1248
|
+
throw Error('Entity is frozen.');
|
|
1249
|
+
}
|
|
1214
1250
|
if (this.isDestroyed) {
|
|
1215
1251
|
throw Error('this.undelete is no longer valid. Entity has been destroyed.');
|
|
1216
1252
|
}
|
|
@@ -1227,6 +1263,9 @@ class Entity extends EventEmitter {
|
|
|
1227
1263
|
* @param {boolean} bool - How it should be marked. Defaults to true.
|
|
1228
1264
|
*/
|
|
1229
1265
|
markStaged = (bool = true) => {
|
|
1266
|
+
if (this.isFrozen) {
|
|
1267
|
+
throw Error('Entity is frozen.');
|
|
1268
|
+
}
|
|
1230
1269
|
if (this.isDestroyed) {
|
|
1231
1270
|
throw Error('this.markStaged is no longer valid. Entity has been destroyed.');
|
|
1232
1271
|
}
|
|
@@ -1240,6 +1279,13 @@ class Entity extends EventEmitter {
|
|
|
1240
1279
|
this.markStaged(true);
|
|
1241
1280
|
}
|
|
1242
1281
|
|
|
1282
|
+
/**
|
|
1283
|
+
* Prevent the entity from being destroyed, but don't let it be changed either.
|
|
1284
|
+
*/
|
|
1285
|
+
freeze = () => {
|
|
1286
|
+
this.isFrozen = true;
|
|
1287
|
+
}
|
|
1288
|
+
|
|
1243
1289
|
/**
|
|
1244
1290
|
* Destroy this object.
|
|
1245
1291
|
* - Removes all circular references to parent objects
|
|
@@ -1248,6 +1294,9 @@ class Entity extends EventEmitter {
|
|
|
1248
1294
|
* @fires destroy
|
|
1249
1295
|
*/
|
|
1250
1296
|
destroy = () => {
|
|
1297
|
+
if (this.isFrozen) {
|
|
1298
|
+
return;
|
|
1299
|
+
}
|
|
1251
1300
|
this._id = this.id; // save id, so we can query it later--even on a destroyed entity
|
|
1252
1301
|
|
|
1253
1302
|
// parent objects
|
|
@@ -716,10 +716,12 @@ export default class Repository extends EventEmitter {
|
|
|
716
716
|
isChanged = true;
|
|
717
717
|
this.filters = filters;
|
|
718
718
|
this.resetPagination();
|
|
719
|
-
|
|
719
|
+
let ret;
|
|
720
720
|
if (this._onChangeFilters) {
|
|
721
|
-
|
|
721
|
+
ret = this._onChangeFilters();
|
|
722
722
|
}
|
|
723
|
+
this.emit('changeFilters');
|
|
724
|
+
return ret;
|
|
723
725
|
}
|
|
724
726
|
return isChanged;
|
|
725
727
|
}
|
package/src/Util/Parsers.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import moment from 'moment';
|
|
2
2
|
import momentAlt from 'relative-time-parser'; // Notice this version of moment is imported from 'relative-time-parser', and may be out of sync with our general 'moment' package
|
|
3
3
|
import accounting from 'accounting-js';
|
|
4
|
-
import * as chrono from 'chrono-node';
|
|
4
|
+
// import * as chrono from 'chrono-node';
|
|
5
5
|
import _ from 'lodash';
|
|
6
6
|
|
|
7
7
|
class Parsers {
|
|
@@ -90,16 +90,16 @@ class Parsers {
|
|
|
90
90
|
result = moment(value, format);
|
|
91
91
|
} catch(err) {}
|
|
92
92
|
|
|
93
|
-
if (!result || !result.isValid()) {
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
}
|
|
93
|
+
// if ((!result || !result.isValid() && chrono)) {
|
|
94
|
+
// // try using chrono
|
|
95
|
+
// const parsed = chrono.parse(value);
|
|
96
|
+
// if (parsed && parsed[0] && parsed[0].date) {
|
|
97
|
+
// const dateString = parsed[0].date();
|
|
98
|
+
// try {
|
|
99
|
+
// result = moment(dateString);
|
|
100
|
+
// } catch(err) {}
|
|
101
|
+
// }
|
|
102
|
+
// }
|
|
103
103
|
return result;
|
|
104
104
|
}
|
|
105
105
|
|