@onehat/data 1.8.7 → 1.8.10
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
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
|
package/src/Repository/Ajax.js
CHANGED
|
@@ -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;
|
|
@@ -285,7 +285,7 @@ class LocalFromRemoteRepository extends EventEmitter {
|
|
|
285
285
|
/**
|
|
286
286
|
* Syncs local and remote repositories, based on operation mode.
|
|
287
287
|
*/
|
|
288
|
-
sync = async (entity) => {
|
|
288
|
+
sync = async (entity, callback = null) => {
|
|
289
289
|
try {
|
|
290
290
|
if (!this.isOnline) {
|
|
291
291
|
this._doAutoSync(true);
|
|
@@ -405,6 +405,9 @@ class LocalFromRemoteRepository extends EventEmitter {
|
|
|
405
405
|
} finally {
|
|
406
406
|
this.isSyncing = false;
|
|
407
407
|
this.emit('endSync', this);
|
|
408
|
+
if (callback) {
|
|
409
|
+
callback();
|
|
410
|
+
}
|
|
408
411
|
}
|
|
409
412
|
|
|
410
413
|
}
|