@onehat/data 1.6.1 → 1.6.3

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.
@@ -685,6 +685,11 @@ describe('Repository Base', function() {
685
685
 
686
686
  describe('deleting', function() {
687
687
 
688
+ it('clear', function() {
689
+ this.repository.clear();
690
+ expect(this.repository.entities.length).to.be.eq(0);
691
+ });
692
+
688
693
  it('delete', function() {
689
694
  let didFire = false;
690
695
  this.repository.on('delete', () => {
@@ -698,6 +703,13 @@ describe('Repository Base', function() {
698
703
  expect(didFire).to.be.true;
699
704
  });
700
705
 
706
+ it('delete() / removeEntity', async function() {
707
+ this.repository.setAutoSave(false);
708
+ const entity = await this.repository.add({ value: 'six' });
709
+ this.repository.delete(entity);
710
+ expect(this.repository.entities.length).to.be.eq(5);
711
+ });
712
+
701
713
 
702
714
  // I'm going to skip these, as they're just combinations of other, tested functions
703
715
  // deleteByIx
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onehat/data",
3
- "version": "1.6.1",
3
+ "version": "1.6.3",
4
4
  "description": "JS data modeling package with adapters for many storage mediums.",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
@@ -678,15 +678,6 @@ class AjaxRepository extends Repository {
678
678
  }));
679
679
  }
680
680
 
681
- /**
682
- * Deletes all locally cached entities in repository,
683
- * usually, the current "page".
684
- * Does not actually affect anything on the server.
685
- */
686
- clear = async () => {
687
- this._destroyEntities();
688
- }
689
-
690
681
  }
691
682
 
692
683
  AjaxRepository.className = 'Ajax';
@@ -972,6 +972,15 @@ export default class Repository extends EventEmitter {
972
972
  this.entities = [];
973
973
  }
974
974
 
975
+ /**
976
+ * Deletes all locally cached entities in repository,
977
+ * usually, the current "page".
978
+ * Does not actually affect anything on the server.
979
+ */
980
+ clear = async () => {
981
+ this._destroyEntities();
982
+ }
983
+
975
984
  /**
976
985
  * Gets an array of "submit" values objects for the entities
977
986
  * @return {array} map -
@@ -1365,7 +1374,7 @@ export default class Repository extends EventEmitter {
1365
1374
 
1366
1375
  /**
1367
1376
  * Marks entities for deletion from storage medium.
1368
- * Actual deletion takes place in save()
1377
+ * Actual deletion takes place in save(), unless isPhantom
1369
1378
  * @param {object|array} entities - one or more entities to delete
1370
1379
  * @fires delete
1371
1380
  */
@@ -1381,7 +1390,12 @@ export default class Repository extends EventEmitter {
1381
1390
  return;
1382
1391
  }
1383
1392
  _.each(entities, (entity) => {
1384
- entity.markDeleted(); // Entity is still there, it's just marked for deletion
1393
+ if (entity.isPhantom) {
1394
+ // Just auto-remove it. Don't bother saving to storage medium.
1395
+ this.removeEntity(entity);
1396
+ } else {
1397
+ entity.markDeleted(); // Entity is still there, it's just marked for deletion
1398
+ }
1385
1399
  });
1386
1400
 
1387
1401
  this.emit('delete', entities);
@@ -1391,6 +1405,16 @@ export default class Repository extends EventEmitter {
1391
1405
  }
1392
1406
  }
1393
1407
 
1408
+ /**
1409
+ * Removes an Entity from the current page
1410
+ * Mainly used for phantom Entities
1411
+ * Helper for delete()
1412
+ */
1413
+ removeEntity = async (entity) => {
1414
+ this.entities = _.filter(this.entities, e => e !== entity);
1415
+ entity.destroy();
1416
+ }
1417
+
1394
1418
  /**
1395
1419
  * Deletes a single Entity by its index (zero-indexed) on the current page
1396
1420
  * @param {integer} ix - Index