@onehat/data 1.8.6 → 1.8.9

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onehat/data",
3
- "version": "1.8.6",
3
+ "version": "1.8.9",
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/OneHatData.js CHANGED
@@ -50,6 +50,12 @@ export class OneHatData extends EventEmitter {
50
50
  * @private
51
51
  */
52
52
  this.repositories = {};
53
+
54
+ /**
55
+ * @member {Object} uniqueRepositoriesMap - Object map of all unique Repositories, with signature of { mapName: id }
56
+ * @private
57
+ */
58
+ this.uniqueRepositoriesMap = {};
53
59
 
54
60
  /**
55
61
  * @member {boolean} isDestroyed - Whether this object has been destroyed
@@ -442,6 +448,34 @@ export class OneHatData extends EventEmitter {
442
448
  return schema.getBoundRepository();
443
449
  }
444
450
 
451
+ /**
452
+ * Gets or creates a unique repository with the supplied schemaName and name
453
+ * @param {string} schemaName - Name of Schema
454
+ * @param {string} mapName - Name of unique repository (will be internally mapped to an id)
455
+ * @return {Repository} repository
456
+ */
457
+ getOrCreateUniqueRepository = async (mapName, schemaName) => {
458
+ if (this.isDestroyed) {
459
+ throw new Error('this.getUniqueRepository is no longer valid. OneHatData has been destroyed.');
460
+ }
461
+
462
+ // Try to get it
463
+ const id = this.uniqueRepositoriesMap[mapName];
464
+ if (id) {
465
+ return this.getRepositoryById(id);
466
+ }
467
+
468
+ // Try to create it
469
+ const schema = this.getSchema(schemaName);
470
+ if (!schema) {
471
+ return null;
472
+ }
473
+
474
+ const repository = await this.createRepository(schemaName);
475
+ this.uniqueRepositoriesMap[mapName] = repository.id;
476
+ return repository;
477
+ }
478
+
445
479
  /**
446
480
  * Checks whether the requested bound Repository exists.
447
481
  * @param {string} name - Name of Schema
@@ -357,9 +357,10 @@ class AjaxRepository extends Repository {
357
357
  * Loads data into the Repository.
358
358
  * This loads only a single page of data.
359
359
  * @param {object} params - Params to send to server
360
+ * @param {function} callback - Function to call after loading is complete
360
361
  * @fires beforeLoad,changeData,load,error
361
362
  */
362
- load = async (params) => {
363
+ load = async (params, callback = null) => {
363
364
  if (this.isDestroyed) {
364
365
  throw Error('this.load is no longer valid. Repository has been destroyed.');
365
366
  }
@@ -417,6 +418,10 @@ class AjaxRepository extends Repository {
417
418
 
418
419
  this.emit('changeData', this.entities);
419
420
  this.emit('load', this);
421
+
422
+ if (callback) {
423
+ callback(this.entities);
424
+ }
420
425
  })
421
426
  .finally(() => {
422
427
  this.isLoading = false;
@@ -426,10 +431,11 @@ class AjaxRepository extends Repository {
426
431
  /**
427
432
  * Reload a single entity from storage.
428
433
  * If the entity is in the internal representation, update it.
434
+ * @param {function} callback - Function to call after loading is complete
429
435
  * @returns {entity} The newly updated entity
430
436
  * @fires reloadEntity,beforeLoad,changeData,load,error
431
437
  */
432
- reloadEntity = async (entity) => {
438
+ reloadEntity = async (entity, callback = null) => {
433
439
  if (this.isDestroyed) {
434
440
  throw Error('this.reloadEntity is no longer valid. Repository has been destroyed.');
435
441
  }
@@ -470,6 +476,10 @@ class AjaxRepository extends Repository {
470
476
  this.emit('changeData', this.entities);
471
477
  this.emit('load', this);
472
478
  this.emit('reloadEntity', entity);
479
+
480
+ if (callback) {
481
+ callback(entity);
482
+ }
473
483
  })
474
484
  .finally(() => {
475
485
  this.isLoading = false;
@@ -163,7 +163,7 @@ class OneBuildRepository extends AjaxRepository {
163
163
  }
164
164
  });
165
165
 
166
- if (this.isLoaded) {
166
+ if (this.isLoaded && this.autoLoad) {
167
167
  return this.reload();
168
168
  }
169
169
  }
@@ -183,7 +183,7 @@ class OneBuildRepository extends AjaxRepository {
183
183
  this.setBaseParam('order', sorterStrings.join(','));
184
184
  }
185
185
 
186
- if (this.isLoaded) {
186
+ if (this.isLoaded && this.autoLoad) {
187
187
  return this.reload();
188
188
  }
189
189
  }