@onehat/data 1.8.7 → 1.8.8

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/OneHatData.js +34 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onehat/data",
3
- "version": "1.8.7",
3
+ "version": "1.8.8",
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