@onehat/data 1.6.8 → 1.6.12

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.
@@ -137,6 +137,12 @@ describe('Entity', function() {
137
137
  expect(this.entity.bar).to.be.eq('one');
138
138
  expect(this.entity.isDirty).to.be.false;
139
139
  expect(_.isEqual(this.entity.getParsedValues(), originalData)).to.be.true;
140
+
141
+ this.entity.delete();
142
+ expect(this.entity.isDeleted).to.be.true;
143
+
144
+ this.entity.reset();
145
+ expect(this.entity.isDeleted).to.be.false;
140
146
  });
141
147
 
142
148
  it('getMappedValue', function() {
@@ -533,6 +539,9 @@ describe('Entity', function() {
533
539
  it('markDeleted', function() {
534
540
  this.entity.markDeleted();
535
541
  expect(this.entity.isDeleted).to.be.true;
542
+
543
+ this.entity.markDeleted(false);
544
+ expect(this.entity.isDeleted).to.be.false;
536
545
  });
537
546
 
538
547
  it('delete', function() {
@@ -540,6 +549,11 @@ describe('Entity', function() {
540
549
  expect(this.entity.isDeleted).to.be.true;
541
550
  });
542
551
 
552
+ it('undelete', function() {
553
+ this.entity.delete();
554
+ this.entity.undelete();
555
+ expect(this.entity.isDeleted).to.be.false;
556
+ });
543
557
  });
544
558
 
545
559
  describe('events', function() {
@@ -594,6 +608,20 @@ describe('Entity', function() {
594
608
  expect(didFire).to.be.true;
595
609
  });
596
610
 
611
+ it('undelete', function() {
612
+ this.entity.delete();
613
+ expect(this.entity.isDeleted).to.be.true;
614
+
615
+ let didFire = false;
616
+ this.entity.on('undelete', () => {
617
+ didFire = true;
618
+ });
619
+ this.entity.undelete();
620
+
621
+ expect(didFire).to.be.true;
622
+ expect(this.entity.isDeleted).to.be.false;
623
+ });
624
+
597
625
  it('destroy', function() {
598
626
  let didFire = false;
599
627
  this.entity.on('destroy', () => {
@@ -630,6 +630,15 @@ describe('Repository Base', function() {
630
630
  expect(_.isEqual(entity, dirty[0])).to.be.true;
631
631
  });
632
632
 
633
+ it('isDirty', function() {
634
+ expect(this.repository.isDirty).to.be.false;
635
+
636
+ this.repository.setAutoSave(false);
637
+ const entity = this.repository.getByIx(0);
638
+ entity.value = 'test';
639
+ expect(this.repository.isDirty).to.be.true;
640
+ });
641
+
633
642
  it('getDeleted', function() {
634
643
  this.repository.setAutoSave(false);
635
644
  const entity = this.repository.getByIx(0);
@@ -718,6 +727,11 @@ describe('Repository Base', function() {
718
727
  // deleteById
719
728
  // deleteDirty
720
729
  // deletePhantom
730
+ // undeleteByIx
731
+ // undeleteByRange
732
+ // undeleteBy
733
+ // undeleteById
734
+ // undeleteDeleted
721
735
 
722
736
  });
723
737
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onehat/data",
3
- "version": "1.6.8",
3
+ "version": "1.6.12",
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
@@ -52,6 +52,7 @@ class Entity extends EventEmitter {
52
52
  'reload',
53
53
  'save',
54
54
  'delete',
55
+ 'undelete',
55
56
  'destroy',
56
57
  ]);
57
58
 
@@ -286,7 +287,7 @@ class Entity extends EventEmitter {
286
287
 
287
288
  /**
288
289
  * Resets the Entity to a state as if it had just been created,
289
- * all based on _originalData.
290
+ * Gets data to restore from _originalData.
290
291
  */
291
292
  reset = () => {
292
293
  if (this.isDestroyed) {
@@ -297,6 +298,10 @@ class Entity extends EventEmitter {
297
298
  this._resetPropertyValues();
298
299
  this._originalDataParsed = this.getParsedValues();
299
300
 
301
+ if (this.isDeleted) {
302
+ this.undelete();
303
+ }
304
+
300
305
  this.emit('reset', this._proxy);
301
306
  }
302
307
 
@@ -1047,18 +1052,17 @@ class Entity extends EventEmitter {
1047
1052
 
1048
1053
  /**
1049
1054
  * Marks an entity as having been saved to storage medium.
1055
+ * @param {boolean} bool - How it should be marked. Defaults to true.
1050
1056
  */
1051
- markDeleted = () => {
1057
+ markDeleted = (bool = true) => {
1052
1058
  if (this.isDestroyed) {
1053
1059
  throw Error('this.markDeleted is no longer valid. Entity has been destroyed.');
1054
1060
  }
1055
- this.isDeleted = true;
1061
+ this.isDeleted = bool;
1056
1062
  }
1057
1063
 
1058
1064
  /**
1059
1065
  * Tells the Repository to delete this entity from the storage medium.
1060
- * @param {function} onSuccess - Optional function to execute when save operation successfully completes.
1061
- * @param {function} onFailure - Optional function to execute when save operation successfully completes.
1062
1066
  * @fires delete
1063
1067
  */
1064
1068
  delete = () => {
@@ -1072,6 +1076,23 @@ class Entity extends EventEmitter {
1072
1076
  }
1073
1077
  }
1074
1078
 
1079
+ /**
1080
+ * Marks a deleted entity as undeleted.
1081
+ * Only works when autoSave is off for the containing repository
1082
+ * @fires delete
1083
+ */
1084
+ undelete = () => {
1085
+ if (this.isDestroyed) {
1086
+ throw Error('this.undelete is no longer valid. Entity has been destroyed.');
1087
+ }
1088
+ const repository = this.getRepository();
1089
+ if (repository && repository.autoSave) {
1090
+ throw Error('Cannot undelete entities on an autoSave repository.');
1091
+ }
1092
+ this.markDeleted(false);
1093
+ this.emit('undelete', this._proxy);
1094
+ }
1095
+
1075
1096
  /**
1076
1097
  * Destroy this object.
1077
1098
  * - Removes all circular references to parent objects
@@ -1181,6 +1181,18 @@ export default class Repository extends EventEmitter {
1181
1181
  return !_.isNil(this.getById(idOrEntity));
1182
1182
  }
1183
1183
 
1184
+ /**
1185
+ * Getter of isDirty for this Repository.
1186
+ * Returns true if any Entities within it are dirty
1187
+ * @return {boolean} isDirty
1188
+ */
1189
+ get isDirty() {
1190
+ if (this.isDestroyed) {
1191
+ throw Error('this.isDirty is no longer valid. Repository has been destroyed.');
1192
+ }
1193
+ return !!this.getDirty().length;
1194
+ }
1195
+
1184
1196
  /**
1185
1197
  * Convenience function
1186
1198
  * Alias for isInRepository
@@ -1478,6 +1490,52 @@ export default class Repository extends EventEmitter {
1478
1490
  await this.delete(this.getPhantom());
1479
1491
  }
1480
1492
 
1493
+ /**
1494
+ * Undelete a single Entity by its index (zero-indexed) on the current page
1495
+ * @param {integer} ix - Index
1496
+ * @return {object} entity - Entity
1497
+ */
1498
+ undeleteByIx = async (ix) => {
1499
+ await this.undelete(this.getByIx(ix));
1500
+ }
1501
+
1502
+ /**
1503
+ * Undelete multiple Entities by their range of indices
1504
+ * (zero-indexed) on the current page
1505
+ * @param {integer} startIx - Index
1506
+ * @param {integer} endIx - Index (inclusive)
1507
+ * @return {array} entities - Array of Entities
1508
+ */
1509
+ undeleteByRange = async (startIx, endIx) => {
1510
+ await this.undelete(this.getByRange(startIx, endIx));
1511
+ }
1512
+
1513
+ /**
1514
+ * Undelete multiple Entities by supplied filter function
1515
+ * @param {function} fn - Filter function to apply to all entities
1516
+ * @return {Entity[]} Entities that passed through filter
1517
+ */
1518
+ undeleteBy = async (filter) => {
1519
+ await this.undelete(this.getBy(filter));
1520
+ }
1521
+
1522
+ /**
1523
+ * Undelete a single Entity by its id
1524
+ * @param {integer} id - id of record to retrieve
1525
+ * @return {Entity} The Entity with matching id
1526
+ */
1527
+ undeleteById = async (id) => {
1528
+ await this.undelete(this.getById(id));
1529
+ }
1530
+
1531
+ /**
1532
+ * Undelete all deleted Entities
1533
+ * @return {Entity[]} Entities that passed through filter
1534
+ */
1535
+ undeleteDeleted = async () => {
1536
+ await this.undelete(this.getDeleted());
1537
+ }
1538
+
1481
1539
 
1482
1540
 
1483
1541