@onehat/data 1.23.3 → 1.23.5

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.
@@ -942,6 +942,7 @@ describe('Repository Base', function() {
942
942
 
943
943
  describe('deleting', function() {
944
944
 
945
+
945
946
  it('clear', function() {
946
947
  this.repository.clear();
947
948
  expect(this.repository.entities.length).to.be.eq(0);
@@ -967,6 +968,21 @@ describe('Repository Base', function() {
967
968
  expect(this.repository.entities.length).to.be.eq(5);
968
969
  });
969
970
 
971
+ it('removeById', function() {
972
+ const entity = this.repository.entities[0];
973
+ const id = entity.id;
974
+ this.repository.removeById(id);
975
+ expect(this.repository.entities.includes(entity)).to.be.false;
976
+ });
977
+
978
+ it('removeDestroyedEntities', function() {
979
+ const entity = this.repository.entities[0];
980
+ expect(this.repository.entities.includes(entity)).to.be.true;
981
+ entity.destroy();
982
+ this.repository.removeDestroyedEntities();
983
+ expect(this.repository.entities.includes(entity)).to.be.false;
984
+ });
985
+
970
986
 
971
987
  // I'm going to skip these, as they're just combinations of other, tested functions
972
988
  // deleteByIx
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onehat/data",
3
- "version": "1.23.3",
3
+ "version": "1.23.5",
4
4
  "description": "JS data modeling package with adapters for many storage mediums.",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
@@ -430,33 +430,69 @@ class OneBuildRepository extends AjaxRepository {
430
430
  this.throwError('this.getSingleEntityFromServer is no longer valid. Repository has been destroyed.');
431
431
  return;
432
432
  }
433
+ return this.getMultipleEntitiesFromServer([id])
434
+ .then(entities => {
435
+ if (!entities || entities.length === 0) {
436
+ return null;
437
+ }
438
+ return entities[0];
439
+ });
440
+ }
441
+
442
+ async getMultipleEntitiesFromServer(ids) {
443
+ if (this.isDestroyed) {
444
+ this.throwError('this.getMultipleEntitiesFromServer is no longer valid. Repository has been destroyed.');
445
+ return;
446
+ }
433
447
  if (!this.api.get) {
434
448
  this.throwError('No "get" api endpoint defined.');
435
449
  return;
436
450
  }
437
451
 
438
- if (!id) {
452
+ if (!ids || !Array.isArray(ids) || ids.length === 0) {
439
453
  return null;
440
454
  }
441
455
 
456
+ const entitiesById = new Map();
457
+ _.each(this.entities, (entity) => {
458
+ entitiesById.set(entity.id, entity);
459
+ });
460
+
461
+ const idsToFetch = [];
462
+ const idsToFetchSet = new Set();
463
+ _.each(ids, (id) => {
464
+ if (entitiesById.has(id)) {
465
+ return;
466
+ }
467
+ if (idsToFetchSet.has(id)) {
468
+ return;
469
+ }
470
+ idsToFetchSet.add(id);
471
+ idsToFetch.push(id);
472
+ });
473
+
474
+ if (!idsToFetch.length) {
475
+ return [];
476
+ }
477
+
442
478
  this.markLoading();
443
479
 
444
480
  const idPropertyName = this.getSchema().model.idProperty;
445
481
  const params = {};
446
- params['conditions[' + idPropertyName + ']'] = id;
482
+ params['conditions[' + idPropertyName + ' IN]'] = idsToFetch;
447
483
 
448
484
  const
449
485
  url = this.getModel() + '/' + this.api.get,
450
486
  data = _.merge(params, this._baseParams);
451
487
 
452
488
  if (this.debugMode) {
453
- console.log('getSingleEntityFromServer', data);
489
+ console.log('getMultipleEntitiesFromServer', data);
454
490
  }
455
491
 
456
492
  return this._send(this.methods.get, url, data)
457
493
  .then(result => {
458
494
  if (this.debugMode) {
459
- console.log('Response for getSingleEntityFromServer for ' + this.name, result);
495
+ console.log('Response for getMultipleEntitiesFromServer for ' + this.name, result);
460
496
  }
461
497
 
462
498
  if (this.isDestroyed) {
@@ -473,18 +509,48 @@ class OneBuildRepository extends AjaxRepository {
473
509
  } = this._processServerResponse(result);
474
510
 
475
511
  if (!root[0]) {
476
- return null;
512
+ return [];
477
513
  }
478
514
 
479
- const entity = this.createStandaloneEntity(root[0]);
480
- entity.isPersisted = true;
481
- entity.isRemotePhantom = false;
482
- return entity;
515
+ const newEntities = [];
516
+ _.each(root, (item) => {
517
+ const entity = this.createStandaloneEntity(item);
518
+
519
+ if (entitiesById.has(entity.id)) {
520
+ // If an entity with the same ID already exists, we don't want to add a duplicate.
521
+ entity.destroy();
522
+ return;
523
+ }
524
+
525
+ entity.isPersisted = true;
526
+ entity.isRemotePhantom = false;
527
+ this._relayEntityEvents(entity);
528
+ entitiesById.set(entity.id, entity);
529
+ newEntities.push(entity);
530
+ });
531
+
532
+ // Add any new entities to the repository's main entities array.
533
+ if (newEntities.length) {
534
+ this.entities = this.entities.concat(newEntities);
535
+ }
536
+ if (this.isTree) {
537
+ this.assembleTreeNodes();
538
+ // pagination vars shouldn't be affected by the loading of additional entities
539
+ }
540
+
541
+ // I don't want to do the following because there might be event handler
542
+ // side-effects of loading these!
543
+ // this.markLoaded();
544
+ // this.rehash();
545
+ // this.emit('load', this);
546
+
547
+ return newEntities;
483
548
  })
484
549
  .finally(() => {
485
550
  this.markLoading(false);
486
551
  });
487
552
 
553
+
488
554
  }
489
555
 
490
556
  async getLastModifiedDate() {
@@ -2074,6 +2074,24 @@ export default class Repository extends EventEmitter {
2074
2074
  }
2075
2075
  }
2076
2076
 
2077
+ /**
2078
+ * Removes an Entity from the current page by ID
2079
+ */
2080
+ removeById(id) {
2081
+ const entity = this.getById(id);
2082
+ if (entity) {
2083
+ this.removeEntity(entity);
2084
+ }
2085
+ }
2086
+
2087
+ /**
2088
+ * Removes all entities that have been destroyed from the current page.
2089
+ * This is an optimized version of removeById, specifically targeting destroyed entities.
2090
+ */
2091
+ removeDestroyedEntities() {
2092
+ this.entities = _.filter(this.entities, (entity) => !entity.isDestroyed);
2093
+ }
2094
+
2077
2095
  /**
2078
2096
  * Deletes a single Entity by its index (zero-indexed) on the current page
2079
2097
  * @param {integer} ix - Index