@onehat/data 1.23.3 → 1.23.6

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.6",
4
4
  "description": "JS data modeling package with adapters for many storage mediums.",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
@@ -417,10 +417,6 @@ class OneBuildRepository extends AjaxRepository {
417
417
  return null;
418
418
  }
419
419
 
420
- this._relayEntityEvents(entity);
421
- this.entities.push(entity);
422
- this.total++;
423
- this._setPaginationVars();
424
420
  this.emit('changeData', this.entities);
425
421
  return entity;
426
422
  }
@@ -430,33 +426,69 @@ class OneBuildRepository extends AjaxRepository {
430
426
  this.throwError('this.getSingleEntityFromServer is no longer valid. Repository has been destroyed.');
431
427
  return;
432
428
  }
429
+ return this.getMultipleEntitiesFromServer([id])
430
+ .then(entities => {
431
+ if (!entities || entities.length === 0) {
432
+ return null;
433
+ }
434
+ return entities[0];
435
+ });
436
+ }
437
+
438
+ async getMultipleEntitiesFromServer(ids) {
439
+ if (this.isDestroyed) {
440
+ this.throwError('this.getMultipleEntitiesFromServer is no longer valid. Repository has been destroyed.');
441
+ return;
442
+ }
433
443
  if (!this.api.get) {
434
444
  this.throwError('No "get" api endpoint defined.');
435
445
  return;
436
446
  }
437
447
 
438
- if (!id) {
448
+ if (!ids || !Array.isArray(ids) || ids.length === 0) {
439
449
  return null;
440
450
  }
441
451
 
452
+ const entitiesById = new Map();
453
+ _.each(this.entities, (entity) => {
454
+ entitiesById.set(entity.id, entity);
455
+ });
456
+
457
+ const idsToFetch = [];
458
+ const idsToFetchSet = new Set();
459
+ _.each(ids, (id) => {
460
+ if (entitiesById.has(id)) {
461
+ return;
462
+ }
463
+ if (idsToFetchSet.has(id)) {
464
+ return;
465
+ }
466
+ idsToFetchSet.add(id);
467
+ idsToFetch.push(id);
468
+ });
469
+
470
+ if (!idsToFetch.length) {
471
+ return [];
472
+ }
473
+
442
474
  this.markLoading();
443
475
 
444
476
  const idPropertyName = this.getSchema().model.idProperty;
445
477
  const params = {};
446
- params['conditions[' + idPropertyName + ']'] = id;
478
+ params['conditions[' + idPropertyName + ' IN]'] = idsToFetch;
447
479
 
448
480
  const
449
481
  url = this.getModel() + '/' + this.api.get,
450
482
  data = _.merge(params, this._baseParams);
451
483
 
452
484
  if (this.debugMode) {
453
- console.log('getSingleEntityFromServer', data);
485
+ console.log('getMultipleEntitiesFromServer', data);
454
486
  }
455
487
 
456
488
  return this._send(this.methods.get, url, data)
457
489
  .then(result => {
458
490
  if (this.debugMode) {
459
- console.log('Response for getSingleEntityFromServer for ' + this.name, result);
491
+ console.log('Response for getMultipleEntitiesFromServer for ' + this.name, result);
460
492
  }
461
493
 
462
494
  if (this.isDestroyed) {
@@ -473,18 +505,48 @@ class OneBuildRepository extends AjaxRepository {
473
505
  } = this._processServerResponse(result);
474
506
 
475
507
  if (!root[0]) {
476
- return null;
508
+ return [];
477
509
  }
478
510
 
479
- const entity = this.createStandaloneEntity(root[0]);
480
- entity.isPersisted = true;
481
- entity.isRemotePhantom = false;
482
- return entity;
511
+ const newEntities = [];
512
+ _.each(root, (item) => {
513
+ const entity = this.createStandaloneEntity(item);
514
+
515
+ if (entitiesById.has(entity.id)) {
516
+ // If an entity with the same ID already exists, we don't want to add a duplicate.
517
+ entity.destroy();
518
+ return;
519
+ }
520
+
521
+ entity.isPersisted = true;
522
+ entity.isRemotePhantom = false;
523
+ this._relayEntityEvents(entity);
524
+ entitiesById.set(entity.id, entity);
525
+ newEntities.push(entity);
526
+ });
527
+
528
+ // Add any new entities to the repository's main entities array.
529
+ if (newEntities.length) {
530
+ this.entities = this.entities.concat(newEntities);
531
+ }
532
+ if (this.isTree) {
533
+ this.assembleTreeNodes();
534
+ // pagination vars shouldn't be affected by the loading of additional entities
535
+ }
536
+
537
+ // I don't want to do the following because there might be event handler
538
+ // side-effects of loading these!
539
+ // this.markLoaded();
540
+ // this.rehash();
541
+ // this.emit('load', this);
542
+
543
+ return newEntities;
483
544
  })
484
545
  .finally(() => {
485
546
  this.markLoading(false);
486
547
  });
487
548
 
549
+
488
550
  }
489
551
 
490
552
  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